forked from getmoto/moto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iam_signing_certificates.py
More file actions
211 lines (175 loc) · 7.82 KB
/
test_iam_signing_certificates.py
File metadata and controls
211 lines (175 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from datetime import timedelta
import boto3
import cryptography
import pytest
from botocore.exceptions import ClientError
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509 import Name, NameAttribute
from cryptography.x509.oid import NameOID
from moto.core.utils import utcnow
from tests.test_iam import iam_aws_verified
@iam_aws_verified(create_user=True)
@pytest.mark.aws_verified
def test_signing_certs(user_name=None):
client = boto3.client("iam", region_name="us-east-1")
certificate = create_certificate()
# Upload the cert:
resp = client.upload_signing_certificate(
UserName=user_name, CertificateBody=certificate
)["Certificate"]
cert_id = resp["CertificateId"]
assert resp["UserName"] == user_name
assert resp["Status"] == "Active"
assert resp["CertificateBody"] == certificate
assert resp["CertificateId"]
# Update:
client.update_signing_certificate(
UserName=user_name, CertificateId=cert_id, Status="Inactive"
)
# List the certs:
resp = client.list_signing_certificates(UserName=user_name)["Certificates"]
assert len(resp) == 1
assert resp[0]["CertificateBody"] == certificate
assert resp[0]["Status"] == "Inactive" # Changed with the update call above.
# Delete:
client.delete_signing_certificate(UserName=user_name, CertificateId=cert_id)
@iam_aws_verified(create_user=True)
@pytest.mark.aws_verified
def test_create_too_many_certificates(user_name=None):
client = boto3.client("iam", region_name="us-east-1")
certificate1 = create_certificate()
certificate2 = create_certificate()
certificate3 = create_certificate()
# Upload two certs
cert_id1 = client.upload_signing_certificate(
UserName=user_name, CertificateBody=certificate1
)["Certificate"]["CertificateId"]
cert_id2 = client.upload_signing_certificate(
UserName=user_name, CertificateBody=certificate2
)["Certificate"]["CertificateId"]
assert cert_id1 != cert_id2
# Verify that a third certificate exceeds the limit
with pytest.raises(ClientError) as exc:
client.upload_signing_certificate(
UserName=user_name, CertificateBody=certificate3
)
err = exc.value.response["Error"]
assert err["Code"] == "LimitExceeded"
assert err["Message"] == "Cannot exceed quota for CertificatesPerUser: 2"
@iam_aws_verified(create_user=True)
def test_retrieve_cert_details_using_credentials_report(user_name=None):
"""
AWS caches the Credentials Report for 4 hours
Once you generate the report, you can request the report over and over again, but you'll always get that same report back in the next 4 hours
# That makes it slightly impossible to verify this against AWS - that's why there is no `aws_verified`-marker
"""
client = boto3.client("iam", region_name="us-east-1")
certificate1 = create_certificate()
certificate2 = create_certificate()
# Upload the cert:
cert_id1 = client.upload_signing_certificate(
UserName=user_name, CertificateBody=certificate1
)["Certificate"]["CertificateId"]
result = client.generate_credential_report()
while result["State"] != "COMPLETE":
result = client.generate_credential_report()
report = client.get_credential_report()["Content"].decode("utf-8")
our_line = next(line for line in report.split("\n") if line.startswith(user_name))
cert1_active = our_line.split(",")[-4]
assert cert1_active == "true"
cert2_active = our_line.split(",")[-2]
assert cert2_active == "false"
client.upload_signing_certificate(UserName=user_name, CertificateBody=certificate2)
result = client.generate_credential_report()
while result["State"] != "COMPLETE":
result = client.generate_credential_report()
report = client.get_credential_report()["Content"].decode("utf-8")
our_line = next(line for line in report.split("\n") if line.startswith(user_name))
cert1_active = our_line.split(",")[-4]
assert cert1_active == "true"
cert2_active = our_line.split(",")[-2]
assert cert2_active == "true"
# Set Certificate to Inactive
client.update_signing_certificate(
UserName=user_name, CertificateId=cert_id1, Status="Inactive"
)
# Verify the credential report is updated
result = client.generate_credential_report()
while result["State"] != "COMPLETE":
result = client.generate_credential_report()
report = client.get_credential_report()["Content"].decode("utf-8")
our_line = next(line for line in report.split("\n") if line.startswith(user_name))
cert1_active = our_line.split(",")[-4]
assert cert1_active == "false"
cert2_active = our_line.split(",")[-2]
assert cert2_active == "true"
@iam_aws_verified()
@pytest.mark.aws_verified
def test_upload_cert_for_unknown_user(user_name=None):
client = boto3.client("iam", region_name="us-east-1")
with pytest.raises(ClientError) as exc:
client.upload_signing_certificate(
UserName="notauser", CertificateBody=create_certificate()
)
err = exc.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert err["Message"] == "The user with name notauser cannot be found."
with pytest.raises(ClientError) as exc:
client.update_signing_certificate(
UserName="notauser",
CertificateId="asdfasdfasdfasdfasdfasdfasdasdf",
Status="Inactive",
)
err = exc.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert err["Message"] == "The user with name notauser cannot be found."
with pytest.raises(ClientError) as exc:
client.list_signing_certificates(UserName="notauser")
err = exc.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert err["Message"] == "The user with name notauser cannot be found."
with pytest.raises(ClientError):
client.delete_signing_certificate(UserName="notauser", CertificateId="x" * 32)
err = exc.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert err["Message"] == "The user with name notauser cannot be found."
@iam_aws_verified(create_user=True)
@pytest.mark.aws_verified
def test_upload_invalid_certificate(user_name=None):
client = boto3.client("iam", region_name="us-east-1")
with pytest.raises(ClientError) as ce:
client.upload_signing_certificate(
UserName=user_name, CertificateBody="notacert"
)
assert ce.value.response["Error"]["Code"] == "MalformedCertificate"
@iam_aws_verified(create_user=True)
@pytest.mark.aws_verified
def test_update_unknown_certificate(user_name=None):
client = boto3.client("iam", region_name="us-east-1")
fake_id_name = "x" * 32
with pytest.raises(ClientError) as ce:
client.update_signing_certificate(
UserName=user_name, CertificateId=fake_id_name, Status="Inactive"
)
err = ce.value.response["Error"]
assert err["Message"] == f"The Certificate with id {fake_id_name} cannot be found."
def create_certificate():
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
cert_subject = [NameAttribute(NameOID.COMMON_NAME, "iam.amazonaws.com")]
issuer = [
NameAttribute(NameOID.COUNTRY_NAME, "US"),
NameAttribute(NameOID.ORGANIZATION_NAME, "Amazon"),
NameAttribute(NameOID.COMMON_NAME, "Amazon RSA 2048 M01"),
]
cert = (
cryptography.x509.CertificateBuilder()
.subject_name(Name(cert_subject))
.issuer_name(Name(issuer))
.public_key(key.public_key())
.serial_number(cryptography.x509.random_serial_number())
.not_valid_before(utcnow())
.not_valid_after(utcnow() + timedelta(days=365))
.sign(key, hashes.SHA256())
)
return cert.public_bytes(serialization.Encoding.PEM).decode("utf-8")