mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
feat(iam): model server certificates (#4975)
Replace the ListServerCertificates stub with the server-certificate lifecycle: UploadServerCertificate, GetServerCertificate, UpdateServerCertificate, DeleteServerCertificate, ListServerCertificates and the tag operations. The uploaded material is really parsed, because two of the upload's modeled errors cannot be answered otherwise. CertificateBody, and every certificate in CertificateChain rather than only its first block, must be readable PEM or the upload is MalformedCertificate; the private key must match the certificate's public key or it is KeyPairMismatch. The match is a sign-then-verify check, so it holds for RSA and EC alike. All of it goes through core.common.Pem rather than a second copy of the parsing. Expiration is read from the certificate's own notAfter rather than stored separately, so it cannot drift from what it describes. ServerCertificateId uses AWS's ASCA prefix. The private key is stored and never returned: AWS marks privateKeyType sensitive and models it only on the upload, so neither reader echoes it back, and ListServerCertificates returns metadata only as AWS documents. Request shapes are enforced from the wire model: the 16384-character caps on the body and key, the much larger 2097152 cap on the chain, and the per-account quota of 20 that the User Guide's table publishes, counted under the same lock as the write. A rename that only changes case is allowed, excluding the certificate from its own duplicate check by id the way updateUser does. GetAccountSummary's ServerCertificates count is now backed by the store instead of a hardcoded zero. ResourceArnBuilder gains an iam case naming the certificate an operation targets, so a policy statement scoped to one certificate constrains it under enforcement. Every other IAM action still resolves to *, which is long-standing behaviour rather than something this adds; documented in docs/services/iam.md and tracked in #4979. DeleteServerCertificate does not raise the in-use DeleteConflict AWS returns when a load balancer still references the certificate. Floci cannot determine that yet, because ELB Classic and CloudFront store certificate identifiers without resolving them against IAM. Documented, and tracked in #4875.
This commit is contained in:
1 parent
543f01618f
commit
d0c7c23f6b
16 files changed
+2090
-22
No files matched your search
+232
@@ -0,0 +1,232 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.GetServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.GetServerCertificateResponse;
|
||||
import software.amazon.awssdk.services.iam.model.ListServerCertificateTagsRequest;
|
||||
import software.amazon.awssdk.services.iam.model.ListServerCertificateTagsResponse;
|
||||
import software.amazon.awssdk.services.iam.model.ListServerCertificatesRequest;
|
||||
import software.amazon.awssdk.services.iam.model.ListServerCertificatesResponse;
|
||||
import software.amazon.awssdk.services.iam.model.NoSuchEntityException;
|
||||
import software.amazon.awssdk.services.iam.model.ServerCertificateMetadata;
|
||||
import software.amazon.awssdk.services.iam.model.Tag;
|
||||
import software.amazon.awssdk.services.iam.model.TagServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.UntagServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.UpdateServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.UploadServerCertificateRequest;
|
||||
import software.amazon.awssdk.services.iam.model.UploadServerCertificateResponse;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Server certificates driven through the AWS SDK rather than hand-written requests.
|
||||
*
|
||||
* <p>This is the check the handcrafted XML assertions cannot make: the SDK parses the response
|
||||
* against its own model, so a missing required member, a mis-named element or a timestamp in the
|
||||
* wrong format fails here even though the raw XML looked right. {@code UploadDate} and
|
||||
* {@code Expiration} are the interesting ones, being the only timestamps in the shape.
|
||||
*/
|
||||
@DisplayName("IAM Server Certificates")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class IamServerCertificateTest {
|
||||
|
||||
private static IamClient iam;
|
||||
|
||||
private static final String CERT_NAME = "sdk-test-server-cert";
|
||||
private static final String RENAMED = "sdk-test-server-cert-renamed";
|
||||
|
||||
/**
|
||||
* A real self-signed certificate and its matching key, fixed rather than generated so this
|
||||
* suite needs no crypto of its own. Floci parses both and verifies the key signs what the
|
||||
* certificate verifies, so fabricated PEM would be rejected outright. Valid until 2036, which
|
||||
* is also why the expiry assertion below can be a simple "after the epoch" check.
|
||||
*/
|
||||
private static final String CERT_PEM = """
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDDTCCAfWgAwIBAgIUQKw8vNoVuYpX79WK4/CF2csyLBYwDQYJKoZIhvcNAQEL
|
||||
BQAwFjEUMBIGA1UEAwwLc2RrLnRlc3QuaW8wHhcNMjYxMDAzMDAxOTQ2WhcNMzYw
|
||||
OTMwMDAxOTQ2WjAWMRQwEgYDVQQDDAtzZGsudGVzdC5pbzCCASIwDQYJKoZIhvcN
|
||||
AQEBBQADggEPADCCAQoCggEBANya41O8oCtxTORSFpJGXD+8pfQDIa4zs1zyaKvf
|
||||
0OWLIRBBcOAbTRCo8vI8nziu4HOq/7RVK2lbRkd86hgvCmBW3TaTkg38AK6ynTtj
|
||||
lgxwr9DyxGHewMANtZcf/uj0ypSrgqISp7VwiM3KRjTBEEGBUK0vxqsizS4i8cUs
|
||||
5IPkwtT0teubfFBv+/Dy5PKQpYCSKGpSfioqbPO3BG2MYhxTum52xwXNpq08slCn
|
||||
kKBcboSJDkmAiy0bbjuBYd0X/zbFNiJL/e929mRucGDrCOb/xwzO3+sQQGMHOJ82
|
||||
gkM8qd2YnfaKQWjv6Ys2D2ScjhsoAIE/gk7acFEX+tZNje0CAwEAAaNTMFEwHQYD
|
||||
VR0OBBYEFNCvcqlMKI/qgcPfKhsIfpyUq/YpMB8GA1UdIwQYMBaAFNCvcqlMKI/q
|
||||
gcPfKhsIfpyUq/YpMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEB
|
||||
AJykw94trHhwwaua0JI0mHv6YbBmUreuc1UDTzNW+yuoEGpWef3HkkNCn9XlLn2h
|
||||
EZFQRtrLvvxqFwo9P97NX/ehSsqjnuUV5zPXPaQuydd4gOWUt8Ysq0eqIqzy28Pa
|
||||
vQTrGPkPFcL3gll2lecvXZgLTJtQKSoM6VDkUO3cbSNWHeDFxr82NXGPEsRqlVTE
|
||||
bQI1BNOypYjrtkmav2jlgWD0hIT2w54SxCoHldVcACrGeU9DHdVmQueXF+9uppAN
|
||||
inCkDT+tT4RN7hcm72EKke95XQwh0IW7ibFklkapGzVPYLS1oDJg17aaI9Ob/xem
|
||||
t2joILbhDvtwYcpNPaTAIdY=
|
||||
-----END CERTIFICATE-----
|
||||
""";
|
||||
|
||||
private static final String KEY_PEM = """
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDcmuNTvKArcUzk
|
||||
UhaSRlw/vKX0AyGuM7Nc8mir39DliyEQQXDgG00QqPLyPJ84ruBzqv+0VStpW0ZH
|
||||
fOoYLwpgVt02k5IN/ACusp07Y5YMcK/Q8sRh3sDADbWXH/7o9MqUq4KiEqe1cIjN
|
||||
ykY0wRBBgVCtL8arIs0uIvHFLOSD5MLU9LXrm3xQb/vw8uTykKWAkihqUn4qKmzz
|
||||
twRtjGIcU7pudscFzaatPLJQp5CgXG6EiQ5JgIstG247gWHdF/82xTYiS/3vdvZk
|
||||
bnBg6wjm/8cMzt/rEEBjBzifNoJDPKndmJ32ikFo7+mLNg9knI4bKACBP4JO2nBR
|
||||
F/rWTY3tAgMBAAECggEALm2PndozKGlwOhY3p2HU/NoGYDA/iKLbYxqmYlPYqlKp
|
||||
avsm1xeP1MuF0+rjxKFzXgyVQ73wBLyfsiBtQRk3WRa2+Es/AB/zAdFYC312QRh/
|
||||
PB23rHRdwx2vg8aJVrxcHUVqWRNNhf9Q32SseWJ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fIPqRfUDvWqSxRKJTIlT1jKopEgSvB04lBzVjQKuV5B4MmbHb1SNAmWcccLsz2g
|
||||
B9HFJ5PLRjDOwlSZnOPlmLPP
|
||||
-----END PRIVATE KEY-----
|
||||
""";
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
iam = TestFixtures.iamClient();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (iam != null) {
|
||||
for (String name : new String[] {CERT_NAME, RENAMED}) {
|
||||
try {
|
||||
iam.deleteServerCertificate(DeleteServerCertificateRequest.builder()
|
||||
.serverCertificateName(name).build());
|
||||
} catch (Exception ignored) {
|
||||
// Already gone, or never created because an earlier test failed.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@DisplayName("UploadServerCertificate returns metadata the SDK can model")
|
||||
void upload() {
|
||||
UploadServerCertificateResponse response = iam.uploadServerCertificate(
|
||||
UploadServerCertificateRequest.builder()
|
||||
.serverCertificateName(CERT_NAME)
|
||||
.certificateBody(CERT_PEM)
|
||||
.privateKey(KEY_PEM)
|
||||
.tags(Tag.builder().key("env").value("sdk-test").build())
|
||||
.build());
|
||||
|
||||
ServerCertificateMetadata metadata = response.serverCertificateMetadata();
|
||||
assertEquals(CERT_NAME, metadata.serverCertificateName());
|
||||
assertEquals("/", metadata.path());
|
||||
assertTrue(metadata.serverCertificateId().startsWith("ASCA"), metadata.serverCertificateId());
|
||||
assertTrue(metadata.arn().endsWith(":server-certificate/" + CERT_NAME), metadata.arn());
|
||||
// Both timestamps have to parse: the SDK rejects a format it does not recognise, which a
|
||||
// raw-XML assertion would have accepted.
|
||||
assertNotNull(metadata.uploadDate());
|
||||
assertNotNull(metadata.expiration());
|
||||
assertTrue(metadata.expiration().isAfter(Instant.EPOCH));
|
||||
assertEquals("env", response.tags().get(0).key());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
@DisplayName("GetServerCertificate returns the body and no private key")
|
||||
void get() {
|
||||
GetServerCertificateResponse response = iam.getServerCertificate(
|
||||
GetServerCertificateRequest.builder().serverCertificateName(CERT_NAME).build());
|
||||
|
||||
assertTrue(response.serverCertificate().certificateBody().contains("BEGIN CERTIFICATE"));
|
||||
assertEquals(CERT_NAME,
|
||||
response.serverCertificate().serverCertificateMetadata().serverCertificateName());
|
||||
// The shape has no private-key member at all, so the best check is that what came back
|
||||
// carries none of it.
|
||||
assertTrue(!response.toString().contains("PRIVATE KEY"), "the private key must not be returned");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
@DisplayName("ListServerCertificates returns metadata only")
|
||||
void list() {
|
||||
ListServerCertificatesResponse response = iam.listServerCertificates(
|
||||
ListServerCertificatesRequest.builder().build());
|
||||
|
||||
assertTrue(response.serverCertificateMetadataList().stream()
|
||||
.anyMatch(m -> CERT_NAME.equals(m.serverCertificateName())));
|
||||
assertNotNull(response.isTruncated());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@DisplayName("Tags round trip through the SDK")
|
||||
void tags() {
|
||||
iam.tagServerCertificate(TagServerCertificateRequest.builder()
|
||||
.serverCertificateName(CERT_NAME)
|
||||
.tags(Tag.builder().key("owner").value("platform").build())
|
||||
.build());
|
||||
|
||||
ListServerCertificateTagsResponse tags = iam.listServerCertificateTags(
|
||||
ListServerCertificateTagsRequest.builder().serverCertificateName(CERT_NAME).build());
|
||||
assertTrue(tags.tags().stream().anyMatch(t -> "owner".equals(t.key())
|
||||
&& "platform".equals(t.value())));
|
||||
|
||||
iam.untagServerCertificate(UntagServerCertificateRequest.builder()
|
||||
.serverCertificateName(CERT_NAME).tagKeys("owner").build());
|
||||
ListServerCertificateTagsResponse after = iam.listServerCertificateTags(
|
||||
ListServerCertificateTagsRequest.builder().serverCertificateName(CERT_NAME).build());
|
||||
assertTrue(after.tags().stream().noneMatch(t -> "owner".equals(t.key())));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@DisplayName("UpdateServerCertificate renames and moves it")
|
||||
void update() {
|
||||
iam.updateServerCertificate(UpdateServerCertificateRequest.builder()
|
||||
.serverCertificateName(CERT_NAME)
|
||||
.newServerCertificateName(RENAMED)
|
||||
.newPath("/sdk/")
|
||||
.build());
|
||||
|
||||
GetServerCertificateResponse response = iam.getServerCertificate(
|
||||
GetServerCertificateRequest.builder().serverCertificateName(RENAMED).build());
|
||||
assertEquals("/sdk/", response.serverCertificate().serverCertificateMetadata().path());
|
||||
assertTrue(response.serverCertificate().serverCertificateMetadata().arn()
|
||||
.endsWith(":server-certificate/sdk/" + RENAMED));
|
||||
|
||||
assertThrows(NoSuchEntityException.class, () -> iam.getServerCertificate(
|
||||
GetServerCertificateRequest.builder().serverCertificateName(CERT_NAME).build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@DisplayName("DeleteServerCertificate removes it")
|
||||
void delete() {
|
||||
iam.deleteServerCertificate(DeleteServerCertificateRequest.builder()
|
||||
.serverCertificateName(RENAMED).build());
|
||||
|
||||
assertThrows(NoSuchEntityException.class, () -> iam.getServerCertificate(
|
||||
GetServerCertificateRequest.builder().serverCertificateName(RENAMED).build()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user