from __future__ import unicode_literals from moto.core.responses import BaseResponse from .models import iam_backend class IamResponse(BaseResponse): def create_role(self): role_name = self._get_param('RoleName') path = self._get_param('Path') assume_role_policy_document = self._get_param('AssumeRolePolicyDocument') role = iam_backend.create_role(role_name, assume_role_policy_document, path) template = self.response_template(CREATE_ROLE_TEMPLATE) return template.render(role=role) def get_role(self): role_name = self._get_param('RoleName') role = iam_backend.get_role(role_name) template = self.response_template(GET_ROLE_TEMPLATE) return template.render(role=role) def list_role_policies(self): role_name = self._get_param('RoleName') role_policies_names = iam_backend.list_role_policies(role_name) template = self.response_template(LIST_ROLE_POLICIES) return template.render(role_policies=role_policies_names) def put_role_policy(self): role_name = self._get_param('RoleName') policy_name = self._get_param('PolicyName') policy_document = self._get_param('PolicyDocument') iam_backend.put_role_policy(role_name, policy_name, policy_document) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name="PutRolePolicyResponse") def get_role_policy(self): role_name = self._get_param('RoleName') policy_name = self._get_param('PolicyName') policy_name, policy_document = iam_backend.get_role_policy(role_name, policy_name) template = self.response_template(GET_ROLE_POLICY_TEMPLATE) return template.render(role_name=role_name, policy_name=policy_name, policy_document=policy_document) def update_assume_role_policy(self): role_name = self._get_param('RoleName') role = iam_backend.get_role(role_name) role.assume_role_policy_document = self._get_param('PolicyDocument') template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name="UpdateAssumeRolePolicyResponse") def create_instance_profile(self): profile_name = self._get_param('InstanceProfileName') path = self._get_param('Path') profile = iam_backend.create_instance_profile(profile_name, path, role_ids=[]) template = self.response_template(CREATE_INSTANCE_PROFILE_TEMPLATE) return template.render(profile=profile) def get_instance_profile(self): profile_name = self._get_param('InstanceProfileName') profile = iam_backend.get_instance_profile(profile_name) template = self.response_template(GET_INSTANCE_PROFILE_TEMPLATE) return template.render(profile=profile) def add_role_to_instance_profile(self): profile_name = self._get_param('InstanceProfileName') role_name = self._get_param('RoleName') iam_backend.add_role_to_instance_profile(profile_name, role_name) template = self.response_template(ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE) return template.render() def list_roles(self): roles = iam_backend.get_roles() template = self.response_template(LIST_ROLES_TEMPLATE) return template.render(roles=roles) def list_instance_profiles(self): profiles = iam_backend.get_instance_profiles() template = self.response_template(LIST_INSTANCE_PROFILES_TEMPLATE) return template.render(instance_profiles=profiles) def list_instance_profiles_for_role(self): role_name = self._get_param('RoleName') profiles = iam_backend.get_instance_profiles_for_role(role_name=role_name) template = self.response_template(LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE) return template.render(instance_profiles=profiles) def upload_server_certificate(self): cert_name = self._get_param('ServerCertificateName') cert_body = self._get_param('CertificateBody') path = self._get_param('Path') private_key = self._get_param('PrivateKey') cert_chain = self._get_param('CertificateName') cert = iam_backend.upload_server_cert(cert_name, cert_body, private_key, cert_chain=cert_chain, path=path) template = self.response_template(UPLOAD_CERT_TEMPLATE) return template.render(certificate=cert) def list_server_certificates(self, marker=None): certs = iam_backend.get_all_server_certs(marker=marker) template = self.response_template(LIST_SERVER_CERTIFICATES_TEMPLATE) return template.render(server_certificates=certs) def get_server_certificate(self): cert_name = self._get_param('ServerCertificateName') cert = iam_backend.get_server_certificate(cert_name) template = self.response_template(GET_SERVER_CERTIFICATE_TEMPLATE) return template.render(certificate=cert) def create_group(self): group_name = self._get_param('GroupName') path = self._get_param('Path') group = iam_backend.create_group(group_name, path) template = self.response_template(CREATE_GROUP_TEMPLATE) return template.render(group=group) def get_group(self): group_name = self._get_param('GroupName') group = iam_backend.get_group(group_name) template = self.response_template(GET_GROUP_TEMPLATE) return template.render(group=group) def create_user(self): user_name = self._get_param('UserName') path = self._get_param('Path') user = iam_backend.create_user(user_name, path) template = self.response_template(USER_TEMPLATE) return template.render(action='Create', user=user) def get_user(self): user_name = self._get_param('UserName') user = iam_backend.get_user(user_name) template = self.response_template(USER_TEMPLATE) return template.render(action='Get', user=user) def create_login_profile(self): user_name = self._get_param('UserName') password = self._get_param('Password') iam_backend.create_login_profile(user_name, password) template = self.response_template(CREATE_LOGIN_PROFILE_TEMPLATE) return template.render(user_name=user_name) def add_user_to_group(self): group_name = self._get_param('GroupName') user_name = self._get_param('UserName') iam_backend.add_user_to_group(group_name, user_name) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='AddUserToGroup') def remove_user_from_group(self): group_name = self._get_param('GroupName') user_name = self._get_param('UserName') iam_backend.remove_user_from_group(group_name, user_name) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='RemoveUserFromGroup') def get_user_policy(self): user_name = self._get_param('UserName') policy_name = self._get_param('PolicyName') policy_document = iam_backend.get_user_policy(user_name, policy_name) template = self.response_template(GET_USER_POLICY_TEMPLATE) return template.render( user_name=user_name, policy_name=policy_name, policy_document=policy_document ) def put_user_policy(self): user_name = self._get_param('UserName') policy_name = self._get_param('PolicyName') policy_document = self._get_param('PolicyDocument') iam_backend.put_user_policy(user_name, policy_name, policy_document) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='PutUserPolicy') def delete_user_policy(self): user_name = self._get_param('UserName') policy_name = self._get_param('PolicyName') iam_backend.delete_user_policy(user_name, policy_name) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='DeleteUserPolicy') def create_access_key(self): user_name = self._get_param('UserName') key = iam_backend.create_access_key(user_name) template = self.response_template(CREATE_ACCESS_KEY_TEMPLATE) return template.render(key=key) def list_access_keys(self): user_name = self._get_param('UserName') keys = iam_backend.get_all_access_keys(user_name) template = self.response_template(LIST_ACCESS_KEYS_TEMPLATE) return template.render(user_name=user_name, keys=keys) def delete_access_key(self): user_name = self._get_param('UserName') access_key_id = self._get_param('AccessKeyId') iam_backend.delete_access_key(access_key_id, user_name) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='DeleteAccessKey') def delete_user(self): user_name = self._get_param('UserName') iam_backend.delete_user(user_name) template = self.response_template(GENERIC_EMPTY_TEMPLATE) return template.render(name='DeleteUser') def generate_credential_report(self): if iam_backend.report_generated(): template = self.response_template(CREDENTIAL_REPORT_GENERATED) else: template = self.response_template(CREDENTIAL_REPORT_GENERATING) iam_backend.generate_report() return template.render() def get_credential_report(self): report = iam_backend.get_credential_report() template = self.response_template(CREDENTIAL_REPORT) return template.render(report=report) GENERIC_EMPTY_TEMPLATE = """<{{ name }}Response> 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ CREATE_INSTANCE_PROFILE_TEMPLATE = """ {{ profile.id }} {{ profile.name }} {{ profile.path }} arn:aws:iam::123456789012:instance-profile/application_abc/component_xyz/Webserver 2012-05-09T16:11:10.222Z 974142ee-99f1-11e1-a4c3-27EXAMPLE804 """ GET_INSTANCE_PROFILE_TEMPLATE = """ {{ profile.id }} {% for role in profile.roles %} {{ role.path }} arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access {{ role.name }} {{ role.assume_role_policy_document }} 2012-05-09T15:45:35Z {{ role.id }} {% endfor %} {{ profile.name }} {{ profile.path }} arn:aws:iam::123456789012:instance-profile/application_abc/component_xyz/Webserver 2012-05-09T16:11:10Z 37289fda-99f2-11e1-a4c3-27EXAMPLE804 """ CREATE_ROLE_TEMPLATE = """ {{ role.path }} arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access {{ role.name }} {{ role.assume_role_policy_document }} 2012-05-08T23:34:01.495Z {{ role.id }} 4a93ceee-9966-11e1-b624-b1aEXAMPLE7c """ GET_ROLE_POLICY_TEMPLATE = """ {{ policy_name }} {{ role_name }} {{ policy_document }} 7e7cd8bc-99ef-11e1-a4c3-27EXAMPLE804 """ GET_ROLE_TEMPLATE = """ {{ role.path }} arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access {{ role.name }} {{ role.assume_role_policy_document }} 2012-05-08T23:34:01Z {{ role.id }} df37e965-9967-11e1-a4c3-270EXAMPLE04 """ ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE = """ 12657608-99f2-11e1-a4c3-27EXAMPLE804 """ LIST_ROLES_TEMPLATE = """ false {% for role in roles %} {{ role.path }} arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access {{ role.name }} {{ role.assume_role_policy_document }} 2012-05-09T15:45:35Z {{ role.id }} {% endfor %} 20f7279f-99ee-11e1-a4c3-27EXAMPLE804 """ LIST_ROLE_POLICIES = """ {% for policy_name in role_policies %} {{ policy_name }} {% endfor %} false 8c7e1816-99f0-11e1-a4c3-27EXAMPLE804 """ LIST_INSTANCE_PROFILES_TEMPLATE = """ false {% for instance in instance_profiles %} {{ instance.id }} {{ instance.name }} {{ instance.path }} arn:aws:iam::123456789012:instance-profile/application_abc/component_xyz/Database 2012-05-09T16:27:03Z {% endfor %} fd74fa8d-99f3-11e1-a4c3-27EXAMPLE804 """ UPLOAD_CERT_TEMPLATE = """ {{ certificate.cert_name }} {% if certificate.path %} {{ certificate.path }} {% endif %} arn:aws:iam::123456789012:server-certificate/{{ certificate.path }}/{{ certificate.cert_name }} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ LIST_SERVER_CERTIFICATES_TEMPLATE = """ false {% for certificate in server_certificates %} {{ 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 }} {% endif %} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z {% endfor %} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ 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 }} {% endif %} 2010-05-08T01:02:03.004Z ASCACKCEVSQ6C2EXAMPLE 2012-05-08T01:02:03.004Z {{ certificate.cert_body }} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ CREATE_GROUP_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} arn:aws:iam::123456789012:group/{{ group.path }} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ GET_GROUP_TEMPLATE = """ {{ group.path }} {{ group.name }} {{ group.id }} arn:aws:iam::123456789012:group/{{ group.path }} {% for user in group.users %} {{ user.path }} {{ user.name }} {{ user.id }} arn:aws:iam::123456789012:user/{{ user.path }}/{{ user.name}} {% endfor %} false 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ USER_TEMPLATE = """<{{ action }}UserResponse> <{{ action }}UserResult> {{ user.path }} {{ user.name }} {{ user.id }} arn:aws:iam::123456789012:user/{{ user.path }}/{{ user.name }} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ CREATE_LOGIN_PROFILE_TEMPLATE = """ {{ user_name }} 2011-09-19T23:00:56Z 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ GET_USER_POLICY_TEMPLATE = """ {{ user_name }} {{ policy_name }} {{ policy_document }} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ CREATE_ACCESS_KEY_TEMPLATE = """ {{ key.user_name }} {{ key.access_key_id }} {{ key.status }} {{ key.secret_access_key }} 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ LIST_ACCESS_KEYS_TEMPLATE = """ {{ user_name }} {% for key in keys %} {{ user_name }} {{ key.access_key_id }} {{ key.status }} {% endfor %} false 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE """ CREDENTIAL_REPORT_GENERATING = """ STARTED No report exists. Starting a new report generation task fa788a82-aa8a-11e4-a278-1786c418872b" """ CREDENTIAL_REPORT_GENERATED = """ COMPLETE fa788a82-aa8a-11e4-a278-1786c418872b" """ CREDENTIAL_REPORT = """ {{ report }} 2015-02-02T20:02:02Z text/csv fa788a82-aa8a-11e4-a278-1786c418872b" """ LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE = """ false {% for profile in instance_profiles %} {{ profile.id }} {% for role in profile.roles %} {{ role.path }} arn:aws:iam::123456789012:role{{ role.path }}S3Access {{ role.name }} {{ role.assume_policy_document }} 2012-05-09T15:45:35Z {{ role.id }} {% endfor %} {{ profile.name }} {{ profile.path }} arn:aws:iam::123456789012:instance-profile{{ profile.path }}Webserver 2012-05-09T16:27:11Z {% endfor %} 6a8c3992-99f4-11e1-a4c3-27EXAMPLE804 """