2023-11-30 15:55:51 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2021-09-22 19:42:42 +00:00
|
|
|
import boto3
|
|
|
|
import pytest
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-09-22 19:42:42 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-22 19:42:42 +00:00
|
|
|
def test_get_all_server_certs():
|
|
|
|
conn = boto3.client("iam", region_name="us-east-1")
|
|
|
|
|
|
|
|
conn.upload_server_certificate(
|
|
|
|
ServerCertificateName="certname",
|
|
|
|
CertificateBody="certbody",
|
|
|
|
PrivateKey="privatekey",
|
|
|
|
)
|
|
|
|
certs = conn.list_server_certificates()["ServerCertificateMetadataList"]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert len(certs) == 1
|
2021-09-22 19:42:42 +00:00
|
|
|
cert1 = certs[0]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert cert1["ServerCertificateName"] == "certname"
|
|
|
|
assert cert1["Arn"] == f"arn:aws:iam::{ACCOUNT_ID}:server-certificate/certname"
|
2021-09-22 19:42:42 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-22 19:42:42 +00:00
|
|
|
def test_get_server_cert_doesnt_exist():
|
|
|
|
conn = boto3.client("iam", region_name="us-east-1")
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
conn.get_server_certificate(ServerCertificateName="NonExistant")
|
|
|
|
err = ex.value.response["Error"]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert err["Code"] == "NoSuchEntity"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "The Server Certificate with name NonExistant cannot be found."
|
2021-09-22 19:42:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-22 19:42:42 +00:00
|
|
|
def test_get_server_cert():
|
|
|
|
conn = boto3.client("iam", region_name="us-east-1")
|
|
|
|
|
|
|
|
conn.upload_server_certificate(
|
|
|
|
ServerCertificateName="certname",
|
|
|
|
CertificateBody="certbody",
|
|
|
|
PrivateKey="privatekey",
|
|
|
|
)
|
|
|
|
cert = conn.get_server_certificate(ServerCertificateName="certname")[
|
|
|
|
"ServerCertificate"
|
|
|
|
]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert cert["CertificateBody"] == "certbody"
|
|
|
|
assert "CertificateChain" not in cert
|
|
|
|
assert "Tags" not in cert
|
2021-09-22 19:42:42 +00:00
|
|
|
metadata = cert["ServerCertificateMetadata"]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert metadata["Path"] == "/"
|
|
|
|
assert metadata["ServerCertificateName"] == "certname"
|
|
|
|
assert metadata["Arn"] == f"arn:aws:iam::{ACCOUNT_ID}:server-certificate/certname"
|
|
|
|
assert "ServerCertificateId" in metadata
|
|
|
|
assert isinstance(metadata["UploadDate"], datetime)
|
|
|
|
assert isinstance(metadata["Expiration"], datetime)
|
2021-09-22 19:42:42 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-22 19:42:42 +00:00
|
|
|
def test_delete_server_cert():
|
|
|
|
conn = boto3.client("iam", region_name="us-east-1")
|
|
|
|
|
|
|
|
conn.upload_server_certificate(
|
|
|
|
ServerCertificateName="certname",
|
|
|
|
CertificateBody="certbody",
|
|
|
|
PrivateKey="privatekey",
|
|
|
|
)
|
|
|
|
conn.get_server_certificate(ServerCertificateName="certname")
|
|
|
|
conn.delete_server_certificate(ServerCertificateName="certname")
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
conn.get_server_certificate(ServerCertificateName="certname")
|
|
|
|
err = ex.value.response["Error"]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert err["Code"] == "NoSuchEntity"
|
|
|
|
assert (
|
|
|
|
err["Message"] == "The Server Certificate with name certname cannot be found."
|
2021-09-22 19:42:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-22 19:42:42 +00:00
|
|
|
def test_delete_unknown_server_cert():
|
|
|
|
conn = boto3.client("iam", region_name="us-east-1")
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
conn.delete_server_certificate(ServerCertificateName="certname")
|
|
|
|
err = ex.value.response["Error"]
|
2023-07-30 19:37:08 +00:00
|
|
|
assert err["Code"] == "NoSuchEntity"
|
|
|
|
assert (
|
|
|
|
err["Message"] == "The Server Certificate with name certname cannot be found."
|
2021-09-22 19:42:42 +00:00
|
|
|
)
|