From 921a993330a96130b12331f29944d93578a8bd2c Mon Sep 17 00:00:00 2001 From: William Rubel Date: Sun, 17 Feb 2019 14:30:43 -0600 Subject: [PATCH] cleaning up files --- file.tmp | 9 ---- moto/iam/models.py | 13 +++-- moto/iam/responses.py | 79 +++++++++++++++++++++++++++-- moto/packages/httpretty/core.py | 62 +--------------------- moto/packages/httpretty/http.py | 2 - tests/test_awslambda/test_lambda.py | 2 - tests/test_kms/test_kms.py | 1 - 7 files changed, 85 insertions(+), 83 deletions(-) delete mode 100644 file.tmp diff --git a/file.tmp b/file.tmp deleted file mode 100644 index 80053c647..000000000 --- a/file.tmp +++ /dev/null @@ -1,9 +0,0 @@ - - AWSTemplateFormatVersion: '2010-09-09' - Description: Simple CloudFormation Test Template - Resources: - S3Bucket: - Type: AWS::S3::Bucket - Properties: - AccessControl: PublicRead - BucketName: cf-test-bucket-1 diff --git a/moto/iam/models.py b/moto/iam/models.py index 86a5d7a32..a5f40b996 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -51,8 +51,8 @@ class Policy(BaseModel): self.default_version_id = default_version_id or 'v1' self.versions = [PolicyVersion(self.arn, document, True)] - self.create_datetime = datetime.now(pytz.utc) - self.update_datetime = datetime.now(pytz.utc) + self.create_datetime = datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%SZ") + self.update_datetime = datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%SZ") class SAMLProvider(BaseModel): @@ -76,7 +76,7 @@ class PolicyVersion(object): self.is_default = is_default self.version_id = 'v1' - self.create_datetime = datetime.now(pytz.utc) + self.create_datetime = datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%SZ") class ManagedPolicy(Policy): @@ -132,8 +132,9 @@ class Role(BaseModel): self.path = path or '/' self.policies = {} self.managed_policies = {} - self.create_date = datetime.now(pytz.utc) + self.create_date = datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%SZ") self.tags = {} + self.description = "" @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name): @@ -473,6 +474,10 @@ class IAMBackend(BaseBackend): policy = arns[policy_arn] policy.attach_to(self.get_role(role_name)) + def update_role_description(self, role_name, role_description): + role = self.get_role(role_name) + role.description = role_description + def detach_role_policy(self, policy_arn, role_name): arns = dict((p.arn, p) for p in self.managed_policies.values()) try: diff --git a/moto/iam/responses.py b/moto/iam/responses.py index e3cc4b90b..e624c18c3 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -107,6 +107,10 @@ class IamResponse(BaseResponse): template = self.response_template(LIST_POLICIES_TEMPLATE) return template.render(policies=policies, marker=marker) + def list_entities_for_policy(self): + template = self.response_template(LIST_ENTITIES_FOR_POLICY_TEMPLATE) + return template.render() + def create_role(self): role_name = self._get_param('RoleName') path = self._get_param('Path') @@ -169,6 +173,20 @@ class IamResponse(BaseResponse): template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name="UpdateAssumeRolePolicyResponse") + def update_role_description(self): + role_name = self._get_param('RoleName') + description = self._get_param('Description') + role = iam_backend.update_role_description(role_name,description) + template = self.response_template(UPDATE_ROLE_DESCRIPTION_TEMPLATE) + return template.render(role=role) + + def update_role(self): + role_name = self._get_param('RoleName') + description = self._get_param('Description') + role = iam_backend.update_role_description(role_name,description) + template = self.response_template(UPDATE_ROLE_DESCRIPTION_TEMPLATE) + return template.render(role=role) + def create_policy_version(self): policy_arn = self._get_param('PolicyArn') policy_document = self._get_param('PolicyDocument') @@ -654,6 +672,33 @@ class IamResponse(BaseResponse): template = self.response_template(UNTAG_ROLE_TEMPLATE) return template.render() +LIST_ENTITIES_FOR_POLICY_TEMPLATE = """ + + + + DevRole + + + + + Dev + + + false + + + Alice + + + Bob + + + + + eb358e22-9d1f-11e4-93eb-190ecEXAMPLE + +""" + ATTACH_ROLE_POLICY_TEMPLATE = """ @@ -696,12 +741,12 @@ CREATE_POLICY_TEMPLATE = """ {{ policy.arn }} {{ policy.attachment_count }} - {{ policy.create_datetime.isoformat() }} + {{ policy.create_datetime }} {{ policy.default_version_id }} {{ policy.path }} {{ policy.id }} {{ policy.name }} - {{ policy.update_datetime.isoformat() }} + {{ policy.update_datetime }} @@ -719,8 +764,8 @@ GET_POLICY_TEMPLATE = """ {{ policy.path }} {{ policy.arn }} {{ policy.attachment_count }} - {{ policy.create_datetime.isoformat() }} - {{ policy.update_datetime.isoformat() }} + {{ policy.create_datetime }} + {{ policy.update_datetime }} @@ -898,6 +943,32 @@ GET_ROLE_POLICY_TEMPLATE = """ + + + {{ role.path }} + {{ role.arn }} + {{ role.name }} + {{ role.assume_role_policy_document }} + {{ role.create_date }} + {{ role.id }} + {% if role.tags %} + + {% for tag in role.get_tags() %} + + {{ tag['Key'] }} + {{ tag['Value'] }} + + {% endfor %} + + {% endif %} + + + + df37e965-9967-11e1-a4c3-270EXAMPLE04 + +""" + GET_ROLE_TEMPLATE = """ diff --git a/moto/packages/httpretty/core.py b/moto/packages/httpretty/core.py index 168f18431..4eb92108f 100644 --- a/moto/packages/httpretty/core.py +++ b/moto/packages/httpretty/core.py @@ -72,19 +72,6 @@ from datetime import datetime from datetime import timedelta from errno import EAGAIN -import logging -from inspect import currentframe -import inspect - -logging.basicConfig(filename='/tmp/models.log',level=logging.DEBUG) - -DEBUG=0 - -def get_linenumber(): - cf = currentframe() - return " - "+str(cf.f_back.f_lineno) - - # Some versions of python internally shadowed the # SocketType variable incorrectly https://bugs.python.org/issue20386 BAD_SOCKET_SHADOW = socket.socket != socket.SocketType @@ -168,34 +155,15 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): """ def __init__(self, headers, body=''): - - if DEBUG: - logging.debug('__init__ - ' - ' -caller: ' + str( - inspect.stack()[1][3]) + "-" + get_linenumber()) - logging.debug('headers: '+str(headers)) - # first of all, lets make sure that if headers or body are # unicode strings, it must be converted into a utf-8 encoded # byte string self.raw_headers = utf8(headers.strip()) - - if DEBUG: - logging.debug('raw_headers: '+str(self.raw_headers)) - self.body = utf8(body) - if DEBUG: - logging.debug('body: '+str(self.body)) - # Now let's concatenate the headers with the body, and create # `rfile` based on it self.rfile = StringIO(b'\r\n\r\n'.join([self.raw_headers, self.body])) - - if DEBUG: - logging.debug('rfile: '+str(self.rfile)) - - self.wfile = StringIO() # Creating `wfile` as an empty # StringIO, just to avoid any real # I/O calls @@ -203,10 +171,6 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): # parsing the request line preemptively self.raw_requestline = self.rfile.readline() - if DEBUG: - logging.debug('raw_requestline: '+str(self.raw_requestline)) - - # initiating the error attributes with None self.error_code = None self.error_message = None @@ -218,9 +182,6 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): # making the HTTP method string available as the command self.method = self.command - if DEBUG: - logging.debug('method: '+str(self.method)) - # Now 2 convenient attributes for the HTTPretty API: # `querystring` holds a dictionary with the parsed query string @@ -246,23 +207,8 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): ) def parse_querystring(self, qs): - - if DEBUG: - logging.debug('parse_querystring - ' - ' -caller: ' + str( - inspect.stack()[1][3]) + "-" + get_linenumber()) - logging.debug('qs: '+str(qs)) - expanded = unquote_utf8(qs) - - if DEBUG: - logging.debug('expanded: '+str(expanded)) - parsed = parse_qs(expanded) - - if DEBUG: - logging.debug('parsed: '+str(parsed)) - result = {} for k in parsed: result[k] = list(map(decode_utf8, parsed[k])) @@ -272,12 +218,6 @@ class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): def parse_request_body(self, body): """ Attempt to parse the post based on the content-type passed. Return the regular body if not """ - if DEBUG: - logging.debug('parse_request_body - ' - ' -caller: ' + str( - inspect.stack()[1][3]) + "-" + get_linenumber()) - logging.debug('body: '+str(body)) - PARSING_FUNCTIONS = { 'application/json': json.loads, 'text/json': json.loads, @@ -1173,4 +1113,4 @@ def httprettified(test): if isinstance(test, ClassTypes): return decorate_class(test) - return decorate_callable(test) + return decorate_callable(test) \ No newline at end of file diff --git a/moto/packages/httpretty/http.py b/moto/packages/httpretty/http.py index 17d580f5f..ee1625905 100644 --- a/moto/packages/httpretty/http.py +++ b/moto/packages/httpretty/http.py @@ -133,7 +133,6 @@ def parse_requestline(s): ... ValueError: Not a Request-Line """ - methods = '|'.join(HttpBaseClass.METHODS) m = re.match(r'(' + methods + ')\s+(.*)\s+HTTP/(1.[0|1])', s, re.I) if m: @@ -146,7 +145,6 @@ def last_requestline(sent_data): """ Find the last line in sent_data that can be parsed with parse_requestline """ - for line in reversed(sent_data): try: parse_requestline(decode_utf8(line)) diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index c05f9f0ac..71f6746a9 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -821,7 +821,6 @@ def get_function_policy(): assert res['Statement'][0]['Action'] == 'lambda:InvokeFunction' - @mock_lambda @mock_s3 def test_list_versions_by_function(): @@ -854,7 +853,6 @@ def test_list_versions_by_function(): assert versions['Versions'][0]['FunctionArn'] == 'arn:aws:lambda:us-west-2:123456789012:function:testFunction:$LATEST' - @mock_lambda @mock_s3 def test_create_function_with_already_exists(): diff --git a/tests/test_kms/test_kms.py b/tests/test_kms/test_kms.py index 520c7262c..0f7bab4cd 100644 --- a/tests/test_kms/test_kms.py +++ b/tests/test_kms/test_kms.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals import os, re - import boto3 import boto.kms from boto.exception import JSONResponseError