From ef97e0f28f9f2bd33749a7e171b887b4745fbe7d Mon Sep 17 00:00:00 2001 From: acsbendi Date: Thu, 4 Jul 2019 20:04:27 +0200 Subject: [PATCH 1/4] Extended create_access_key test. --- tests/test_iam/test_iam.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index 0d96fd1b1..df084c9e2 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -577,13 +577,16 @@ def test_delete_login_profile(): conn.delete_login_profile('my-user') -@mock_iam_deprecated() +@mock_iam() def test_create_access_key(): - conn = boto.connect_iam() - with assert_raises(BotoServerError): - conn.create_access_key('my-user') - conn.create_user('my-user') - conn.create_access_key('my-user') + conn = boto3.client('iam', region_name='us-east-1') + with assert_raises(ClientError): + conn.create_access_key(UserName='my-user') + conn.create_user(UserName='my-user') + access_key = conn.create_access_key(UserName='my-user') + (datetime.utcnow() - access_key["CreateDate"]).seconds.should.be.within(0, 2) + access_key["AccessKeyId"].should.have.length_of(20) + assert access_key["AccessKeyId"].startswith("AKIA") @mock_iam_deprecated() From 0bd2c5adb401a84e0d7afc2cbb3eaa1d9d273793 Mon Sep 17 00:00:00 2001 From: acsbendi Date: Thu, 4 Jul 2019 20:12:53 +0200 Subject: [PATCH 2/4] Fixed minor errors in test. --- tests/test_iam/test_iam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index df084c9e2..d1326712e 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -583,8 +583,8 @@ def test_create_access_key(): with assert_raises(ClientError): conn.create_access_key(UserName='my-user') conn.create_user(UserName='my-user') - access_key = conn.create_access_key(UserName='my-user') - (datetime.utcnow() - access_key["CreateDate"]).seconds.should.be.within(0, 2) + access_key = conn.create_access_key(UserName='my-user')["AccessKey"] + (datetime.utcnow() - access_key["CreateDate"].replace(tzinfo=None)).seconds.should.be.within(0, 2) access_key["AccessKeyId"].should.have.length_of(20) assert access_key["AccessKeyId"].startswith("AKIA") From 9382c40c37fe2b7074feb85a39972aaf2aacbfc4 Mon Sep 17 00:00:00 2001 From: acsbendi Date: Thu, 4 Jul 2019 20:13:38 +0200 Subject: [PATCH 3/4] Return CreateDate in CreateAccessKey response. --- moto/iam/responses.py | 1 + 1 file changed, 1 insertion(+) diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 05624101a..3ee3377c5 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -1493,6 +1493,7 @@ CREATE_ACCESS_KEY_TEMPLATE = """ {{ key.access_key_id }} {{ key.status }} {{ key.secret_access_key }} + {{ key.created_iso_8601 }} From 48f0c6f194175f515738768bd0c996082e5188ef Mon Sep 17 00:00:00 2001 From: acsbendi Date: Thu, 4 Jul 2019 20:20:08 +0200 Subject: [PATCH 4/4] Fixed format of access key ID and secret access key. --- moto/iam/models.py | 4 ++-- moto/iam/utils.py | 2 +- tests/test_iam/test_iam.py | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/moto/iam/models.py b/moto/iam/models.py index 4bc7a5447..98abc538d 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -292,8 +292,8 @@ class AccessKey(BaseModel): def __init__(self, user_name): self.user_name = user_name - self.access_key_id = random_access_key() - self.secret_access_key = random_alphanumeric(32) + self.access_key_id = "AKIA" + random_access_key() + self.secret_access_key = random_alphanumeric(40) self.status = 'Active' self.create_date = datetime.utcnow() self.last_used = datetime.utcnow() diff --git a/moto/iam/utils.py b/moto/iam/utils.py index f59bdfffe..2bd6448f9 100644 --- a/moto/iam/utils.py +++ b/moto/iam/utils.py @@ -7,7 +7,7 @@ import six def random_alphanumeric(length): return ''.join(six.text_type( random.choice( - string.ascii_letters + string.digits + string.ascii_letters + string.digits + "+" + "/" )) for _ in range(length) ) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index d1326712e..679d04d9c 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -584,8 +584,9 @@ def test_create_access_key(): conn.create_access_key(UserName='my-user') conn.create_user(UserName='my-user') access_key = conn.create_access_key(UserName='my-user')["AccessKey"] - (datetime.utcnow() - access_key["CreateDate"].replace(tzinfo=None)).seconds.should.be.within(0, 2) + (datetime.utcnow() - access_key["CreateDate"].replace(tzinfo=None)).seconds.should.be.within(0, 10) access_key["AccessKeyId"].should.have.length_of(20) + access_key["SecretAccessKey"].should.have.length_of(40) assert access_key["AccessKeyId"].startswith("AKIA")