Implemented get_access_key_last_used

Written test that still does not work due to:
    ParamValidationError: Parameter validation failed:
    Unknown parameter in input: "UserName", must be one of: AccessKeyId
Refactored update_access_key and delete_access_key functions
This commit is contained in:
martynaspaulikas 2018-11-27 11:28:09 +00:00
parent ed861ecae1
commit 7189d019df
4 changed files with 43 additions and 11 deletions

View File

@ -2257,7 +2257,7 @@
- [X] detach_user_policy
- [X] enable_mfa_device
- [ ] generate_credential_report
- [ ] get_access_key_last_used
- [X] get_access_key_last_used
- [X] get_account_authorization_details
- [ ] get_account_password_policy
- [ ] get_account_summary

View File

@ -224,6 +224,10 @@ class AccessKey(BaseModel):
datetime.utcnow(),
"%Y-%m-%dT%H:%M:%SZ"
)
self.last_used = datetime.strftime(
datetime.utcnow(),
"%Y-%m-%dT%H:%M:%SZ"
)
def get_cfn_attribute(self, attribute_name):
from moto.cloudformation.exceptions import UnformattedGetAttTemplateException
@ -351,22 +355,25 @@ class User(BaseModel):
def get_all_access_keys(self):
return self.access_keys
def get_access_key_last_used(self, access_key_id):
key = self.get_access_key_by_id(access_key_id)
return key.last_used
def delete_access_key(self, access_key_id):
for key in self.access_keys:
if key.access_key_id == access_key_id:
self.access_keys.remove(key)
break
else:
raise IAMNotFoundException(
"Key {0} not found".format(access_key_id))
key = self.get_access_key_by_id(access_key_id)
self.access_keys.remove(key)
def update_access_key(self, access_key_id, status):
key = self.get_access_key_by_id(access_key_id)
key.status = status
def get_access_key_by_id(self, access_key_id):
for key in self.access_keys:
if key.access_key_id == access_key_id:
key.status = status
break
return key
else:
raise IAMNotFoundException("The Access Key with id {0} cannot be found".format(access_key_id))
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
@ -838,6 +845,11 @@ class IAMBackend(BaseBackend):
user = self.get_user(user_name)
user.update_access_key(access_key_id, status)
def get_access_key_last_used(self, user_name, access_key_id):
user = self.get_user(user_name)
last_used = user.get_access_key_last_used(access_key_id)
return last_used
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()

View File

@ -454,6 +454,13 @@ class IamResponse(BaseResponse):
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='UpdateAccessKey')
def get_access_key_last_used(self):
user_name = self._get_param('UserName')
access_key_id = self._get_param('AccessKeyId')
iam_backend.get_access_key_last_used(user_name, access_key_id)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='GetAccessKeyLastUsed')
def list_access_keys(self):
user_name = self._get_param('UserName')

View File

@ -695,6 +695,19 @@ def test_update_access_key():
resp['AccessKeyMetadata'][0]['Status'].should.equal('Inactive')
@mock_iam
def test_get_access_key_last_used():
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.get_access_key_last_used(UserName=username, AccessKeyId='non-existent-key')
key = client.create_access_key(UserName=username)['AccessKey']
resp = client.get_access_key_last_used(UserName=username, AccessKeyId=key['AccessKeyId'])
resp.should.equal(key.last_used)
@mock_iam
def test_get_account_authorization_details():
import json