diff --git a/Makefile b/Makefile
index f224d7091..de08c6f74 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ test: lint
rm -f .coverage
rm -rf cover
@nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE)
+
test_server:
@TEST_SERVER_MODE=true nosetests -sv --with-coverage --cover-html ./tests/
diff --git a/moto/iam/models.py b/moto/iam/models.py
index 32e1ea5ee..f3d640940 100644
--- a/moto/iam/models.py
+++ b/moto/iam/models.py
@@ -355,10 +355,6 @@ 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):
key = self.get_access_key_by_id(access_key_id)
self.access_keys.remove(key)
@@ -845,10 +841,23 @@ 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_access_key_last_used(self, access_key_id):
+ access_keys_list = self.get_all_access_keys_for_all_users()
+ for key in access_keys_list:
+ if key.access_key_id == access_key_id:
+ return {
+ 'user_name': key.user_name,
+ 'last_used': key.last_used
+ }
+ else:
+ raise IAMNotFoundException(
+ "The Access Key with id {0} cannot be found".format(access_key_id))
+
+ def get_all_access_keys_for_all_users(self):
+ access_keys_list = []
+ for user_name in self.users:
+ access_keys_list += self.get_all_access_keys(user_name)
+ return access_keys_list
def get_all_access_keys(self, user_name, marker=None, max_items=None):
user = self.get_user(user_name)
diff --git a/moto/iam/responses.py b/moto/iam/responses.py
index c3c6f8f8a..1674b1b53 100644
--- a/moto/iam/responses.py
+++ b/moto/iam/responses.py
@@ -455,15 +455,13 @@ class IamResponse(BaseResponse):
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')
+ last_used_response = iam_backend.get_access_key_last_used(access_key_id)
+ template = self.response_template(GET_ACCESS_KEY_LAST_USED_TEMPLATE)
+ return template.render(user_name=last_used_response["user_name"], last_used=last_used_response["last_used"])
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)
@@ -1247,6 +1245,18 @@ LIST_ACCESS_KEYS_TEMPLATE = """
"""
+
+GET_ACCESS_KEY_LAST_USED_TEMPLATE = """
+
+
+ {{ user_name }}
+
+ {{ last_used }}
+
+
+
+"""
+
CREDENTIAL_REPORT_GENERATING = """
diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py
index 343d42b3a..b6c836b17 100644
--- a/tests/test_iam/test_iam.py
+++ b/tests/test_iam/test_iam.py
@@ -11,6 +11,7 @@ from moto.iam.models import aws_managed_policies
from nose.tools import assert_raises, assert_equals
from nose.tools import raises
+from datetime import datetime
from tests.helpers import requires_boto_gte
@@ -702,10 +703,15 @@ def test_get_access_key_last_used():
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)
+ client.get_access_key_last_used(AccessKeyId='non-existent-key-id')
+ create_key_response = client.create_access_key(UserName=username)['AccessKey']
+ resp = client.get_access_key_last_used(AccessKeyId=create_key_response['AccessKeyId'])
+
+ datetime.strftime(resp["AccessKeyLastUsed"]["LastUsedDate"], "%Y-%m-%d").should.equal(datetime.strftime(
+ datetime.utcnow(),
+ "%Y-%m-%d"
+ ))
+ resp["UserName"].should.equal(create_key_response["UserName"])
@mock_iam