diff --git a/moto/iam/models.py b/moto/iam/models.py index 57d24826d..32ca144c3 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -349,6 +349,14 @@ class User(BaseModel): raise IAMNotFoundException( "Key {0} not found".format(access_key_id)) + def update_access_key(self, access_key_id, status): + for key in self.access_keys: + if key.access_key_id == access_key_id: + key.status = status + break + else: + raise IAMNotFoundException("The Access Key with id {0} cannot be found".format(access_key_id)) + def get_cfn_attribute(self, attribute_name): from moto.cloudformation.exceptions import UnformattedGetAttTemplateException if attribute_name == 'Arn': @@ -817,6 +825,10 @@ class IAMBackend(BaseBackend): key = user.create_access_key() return key + def update_access_key(self, user_name, access_key_id, status): + user = self.get_user(user_name) + user.update_access_key(access_key_id, status) + def get_all_access_keys(self, user_name, marker=None, max_items=None): user = self.get_user(user_name) keys = user.get_all_access_keys() diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 0e11c09d5..9931cb8d0 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -440,6 +440,14 @@ class IamResponse(BaseResponse): template = self.response_template(CREATE_ACCESS_KEY_TEMPLATE) return template.render(key=key) + def update_access_key(self): + user_name = self._get_param('UserName') + access_key_id = self._get_param('AccessKeyId') + status = self._get_param('Status') + iam_backend.update_access_key(user_name, access_key_id, status) + template = self.response_template(GENERIC_EMPTY_TEMPLATE) + return template.render(name='UpdateAccessKey') + def list_access_keys(self): user_name = self._get_param('UserName') diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index d50f6999e..b4dfe532d 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -651,3 +651,21 @@ def test_attach_detach_user_policy(): resp = client.list_attached_user_policies(UserName=user.name) resp['AttachedPolicies'].should.have.length_of(0) + + +@mock_iam +def test_update_access_key(): + iam = boto3.resource('iam', region_name='us-east-1') + client = iam.meta.client + username = 'test-user' + iam.create_user(UserName=username) + with assert_raises(ClientError): + client.update_access_key(UserName=username, + AccessKeyId='non-existent-key', + Status='Inactive') + key = client.create_access_key(UserName=username)['AccessKey'] + client.update_access_key(UserName=username, + AccessKeyId=key['AccessKeyId'], + Status='Inactive') + resp = client.list_access_keys(UserName=username) + resp['AccessKeyMetadata'][0]['Status'].should.equal('Inactive')