diff --git a/moto/iam/models.py b/moto/iam/models.py index ba6985895..c150d1c99 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -8,6 +8,8 @@ from moto.core import BaseBackend, BaseModel from .exceptions import IAMNotFoundException, IAMConflictException, IAMReportNotPresentException from .utils import random_access_key, random_alphanumeric, random_resource_id, random_policy_id +ACCOUNT_ID = 123456789012 + class Policy(BaseModel): @@ -82,6 +84,10 @@ class Role(BaseModel): return role + @property + def arn(self): + return "arn:aws:iam::{0}:role{1}{2}".format(ACCOUNT_ID, self.path, self.name) + def put_policy(self, policy_name, policy_json): self.policies[policy_name] = policy_json @@ -115,6 +121,10 @@ class InstanceProfile(BaseModel): role_ids=role_ids, ) + @property + def arn(self): + return "arn:aws:iam::{0}:instance-profile{1}{2}".format(ACCOUNT_ID, self.path, self.name) + @property def physical_resource_id(self): return self.name @@ -132,13 +142,17 @@ class Certificate(BaseModel): self.cert_name = cert_name self.cert_body = cert_body self.private_key = private_key - self.path = path + self.path = path if path else "/" self.cert_chain = cert_chain @property def physical_resource_id(self): return self.name + @property + def arn(self): + return "arn:aws:iam::{0}:server-certificate{1}{2}".format(ACCOUNT_ID, self.path, self.cert_name) + class AccessKey(BaseModel): @@ -179,6 +193,10 @@ class Group(BaseModel): raise NotImplementedError('"Fn::GetAtt" : [ "{0}" , "Arn" ]"') raise UnformattedGetAttTemplateException() + @property + def arn(self): + return "arn:aws:iam::{0}:group/{1}".format(ACCOUNT_ID, self.path) + def get_policy(self, policy_name): try: policy_json = self.policies[policy_name] @@ -208,12 +226,14 @@ class User(BaseModel): datetime.utcnow(), "%Y-%m-%d-%H-%M-%S" ) - self.arn = 'arn:aws:iam::123456789012:user{0}{1}'.format( - self.path, name) self.policies = {} self.access_keys = [] self.password = None + @property + def arn(self): + return "arn:aws:iam::{0}:user{1}{2}".format(ACCOUNT_ID, self.path, self.name) + def get_policy(self, policy_name): policy_json = None try: diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 71e2993f9..27e69537d 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -439,7 +439,7 @@ CREATE_INSTANCE_PROFILE_TEMPLATE = """ {% if certificate.path %} {{ certificate.path }} {% endif %} - arn:aws:iam::123456789012:server-certificate/{{ certificate.path }}/{{ certificate.cert_name }} + {{ certificate.arn }} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z @@ -623,11 +623,9 @@ LIST_SERVER_CERTIFICATES_TEMPLATE = """ {{ certificate.cert_name }} {% if certificate.path %} - {{ certificate.path }} - arn:aws:iam::123456789012:server-certificate/{{ certificate.path }}/{{ certificate.cert_name }} - {% else %} - arn:aws:iam::123456789012:server-certificate/{{ certificate.cert_name }} + {{ certificate.path }} {% endif %} + {{ certificate.arn }} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z @@ -646,11 +644,9 @@ GET_SERVER_CERTIFICATE_TEMPLATE = """ {{ certificate.cert_name }} {% if certificate.path %} - {{ certificate.path }} - arn:aws:iam::123456789012:server-certificate/{{ certificate.path }}/{{ certificate.cert_name }} - {% else %} - arn:aws:iam::123456789012:server-certificate/{{ certificate.cert_name }} + {{ certificate.path }} {% endif %} + {{ certificate.arn }} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z @@ -669,7 +665,7 @@ CREATE_GROUP_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} - arn:aws:iam::123456789012:group/{{ group.path }} + {{ group.arn }} @@ -683,7 +679,7 @@ GET_GROUP_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} - arn:aws:iam::123456789012:group/{{ group.path }} + {{ group.arn }} {% for user in group.users %} @@ -691,9 +687,7 @@ GET_GROUP_TEMPLATE = """ {{ user.path }} {{ user.name }} {{ user.id }} - - arn:aws:iam::123456789012:user/{{ user.path }}/{{ user.name}} - + {{ user.arn }} {% endfor %} @@ -712,7 +706,7 @@ LIST_GROUPS_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} - arn:aws:iam::123456789012:group/{{ group.path }} + {{ group.arn }} {% endfor %} @@ -731,7 +725,7 @@ LIST_GROUPS_FOR_USER_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} - arn:aws:iam::123456789012:group/{{ group.path }} + {{ group.arn }} {% endfor %} @@ -778,7 +772,7 @@ USER_TEMPLATE = """<{{ action }}UserResponse> {{ user.path }} {{ user.name }} {{ user.id }} - arn:aws:iam::123456789012:user/{{ user.path }}/{{ user.name }} + {{ user.arn }} @@ -908,7 +902,7 @@ LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE = """ {{ role.path }} - arn:aws:iam::123456789012:role{{ role.path }}S3Access + {{ role.arn }} {{ role.name }} {{ role.assume_policy_document }} 2012-05-09T15:45:35Z @@ -918,7 +912,7 @@ LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE = """ {{ profile.name }} {{ profile.path }} - arn:aws:iam::123456789012:instance-profile{{ profile.path }}Webserver + {{ profile.arn }} 2012-05-09T16:27:11Z {% endfor %}