mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cognito): check the custom domain certificate in ACM and mark it in use (#3035)
* feat(cognito): require an issued ACM certificate for a custom domain and track it as in use CreateUserPoolDomain and UpdateUserPoolDomain stored any CertificateArn as given. The certificate must now exist in ACM, in us-east-1, with status ISSUED, as AWS requires; anything else is rejected with AWS's InvalidParameterException message. While a domain uses a certificate, ACM lists the domain's CloudFront distribution under InUseBy and DeleteCertificate refuses with ResourceInUseException. Deleting the domain, or moving it to another certificate, releases the old one. AcmService gains addInUseBy and removeInUseBy for this; the refusal on delete already existed. DeleteStack deleted resources in reverse insertion order, so a certificate added by a later update was deleted before the domain that still used it and the stack ended in DELETE_FAILED. Resources are now deleted in reverse dependency order from the stack's template, as on AWS, with insertion order as the fallback when the template cannot be read. * feat(cognito): register the certificate before storing the domain and require a certificate ARN Review follow-ups: - The domain was stored before it was registered on its certificate, so a certificate deleted between the check and the registration left a stored domain that ACM did not protect. Registration now comes first on create, and a changed certificate is checked and registered before an update touches the domain; the old certificate is released after the store. A certificate that disappears in that window fails with the same InvalidParameterException as a missing one. - The ARN check accepted any ACM resource. It now also requires the resource to be certificate/<id>, before ACM is asked.
This commit is contained in:
1 parent
587d0b5758
commit
d771783afb
11 files changed
+552
-24
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package io.github.hectorvent.floci.services.acm;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.github.hectorvent.floci.services.acm.model.ValidationMethod;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.Security;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The {@code InUseBy} bookkeeping other services drive: a registered consumer blocks
|
||||
* {@code DeleteCertificate} with {@code ResourceInUseException}, as on AWS, until it is released.
|
||||
*/
|
||||
class AcmInUseByTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String CONSUMER = "arn:aws:cloudfront::000000000000:distribution/E1234567890ABC";
|
||||
private static final String MISSING = "arn:aws:acm:us-east-1:000000000000:certificate/00000000-0000-0000-0000-000000000000";
|
||||
|
||||
private static CertificateGenerator generator;
|
||||
private AcmService service;
|
||||
|
||||
@BeforeAll
|
||||
static void registerProvider() {
|
||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
generator = new CertificateGenerator();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
service = new AcmService(new InMemoryStorage<>(), generator, regionResolver, 0);
|
||||
}
|
||||
|
||||
private String requestCertificate() {
|
||||
return service.requestCertificate("auth.example.com", List.of(), ValidationMethod.DNS, null,
|
||||
KeyAlgorithm.RSA_2048, null, null, Map.of(), REGION).getArn();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInUseByRecordsTheConsumerOnce() {
|
||||
String arn = requestCertificate();
|
||||
|
||||
service.addInUseBy(arn, CONSUMER, REGION);
|
||||
service.addInUseBy(arn, CONSUMER, REGION);
|
||||
|
||||
assertEquals(List.of(CONSUMER), service.describeCertificate(arn, REGION).getInUseBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCertificateInUseCannotBeDeleted() {
|
||||
String arn = requestCertificate();
|
||||
service.addInUseBy(arn, CONSUMER, REGION);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.deleteCertificate(arn, REGION));
|
||||
|
||||
assertEquals("ResourceInUseException", failure.getErrorCode());
|
||||
assertEquals(List.of(CONSUMER), service.describeCertificate(arn, REGION).getInUseBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeInUseByReleasesTheCertificateForDeletion() {
|
||||
String arn = requestCertificate();
|
||||
service.addInUseBy(arn, CONSUMER, REGION);
|
||||
|
||||
service.removeInUseBy(arn, CONSUMER, REGION);
|
||||
|
||||
assertEquals(List.of(), service.describeCertificate(arn, REGION).getInUseBy());
|
||||
assertDoesNotThrow(() -> service.deleteCertificate(arn, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeInUseByOnAMissingCertificateIsANoOp() {
|
||||
assertDoesNotThrow(() -> service.removeInUseBy(MISSING, CONSUMER, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInUseByOnAMissingCertificateIsNotFound() {
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.addInUseBy(MISSING, CONSUMER, REGION));
|
||||
|
||||
assertEquals("ResourceNotFoundException", failure.getErrorCode());
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -28,7 +28,8 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||
* {@code DescribeUserPoolDomain} reports rather than the literal {@code Domain.CloudFrontDistribution}
|
||||
* the stub arm would leave in the record, that rotating to a new certificate resource updates the
|
||||
* domain in place so the alias target survives, that a changed domain name replaces the domain, and
|
||||
* that deleting the stack removes the domain before the pool that owns it. A prefix domain, which
|
||||
* that deleting the stack removes the domain before the pool that owns it and the certificate after
|
||||
* the domain that uses it. A prefix domain, which
|
||||
* has no distribution of its own, resolves the attribute to an empty string rather than the literal.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@@ -139,6 +140,7 @@ class CognitoCfnIntegrationTest {
|
||||
assertTrue(certificateArn.startsWith("arn:aws:acm:us-east-1:"),
|
||||
"the certificate must be provisioned rather than stubbed: " + certificateArn);
|
||||
assertCertificateStatus(certificateArn, "ISSUED");
|
||||
assertCertificateConsumers(certificateArn, 1);
|
||||
|
||||
JsonNode description = describeDomain(DOMAIN);
|
||||
assertEquals(poolId, description.path("UserPoolId").asText());
|
||||
@@ -159,6 +161,7 @@ class CognitoCfnIntegrationTest {
|
||||
assertEquals(aliasTarget, description.path("CloudFrontDistribution").asText(),
|
||||
"a certificate change must keep the CloudFront distribution the alias record points at");
|
||||
assertCertificateIsGone(certificateArn);
|
||||
assertCertificateConsumers(renewedCertificateArn, 1);
|
||||
|
||||
// Domain is createOnly: a new name is a replacement, created before the old domain goes.
|
||||
cloudFormation(CUSTOM_STACK, "UpdateStack", customDomainTemplate(REPLACEMENT_DOMAIN, "RenewedCert"));
|
||||
@@ -265,6 +268,13 @@ class CognitoCfnIntegrationTest {
|
||||
.body("Certificate.Status", equalTo(status));
|
||||
}
|
||||
|
||||
private static void assertCertificateConsumers(String certificateArn, int count) {
|
||||
awsAction("CertificateManager", "DescribeCertificate", "{\"CertificateArn\": \"" + certificateArn + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Certificate.InUseBy.size()", equalTo(count));
|
||||
}
|
||||
|
||||
private static void assertCertificateIsGone(String certificateArn) {
|
||||
awsAction("CertificateManager", "DescribeCertificate", "{\"CertificateArn\": \"" + certificateArn + "\"}")
|
||||
.then()
|
||||
|
||||
+4
-1
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ReservedTags;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -19,6 +20,7 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class CognitoJsonHandlerTest {
|
||||
|
||||
@@ -37,7 +39,8 @@ class CognitoJsonHandlerTest {
|
||||
new InMemoryStorage<>(), // revokedTokenStore
|
||||
"http://localhost:4566",
|
||||
regionResolver,
|
||||
null
|
||||
null,
|
||||
mock(AcmService.class)
|
||||
);
|
||||
handler = new CognitoJsonHandler(service, mapper);
|
||||
}
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPool;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
|
||||
@@ -57,7 +58,7 @@ class CognitoLambdaTriggersTest {
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), // revokedTokenStore
|
||||
"http://localhost:4566", regionResolver, lambdaService);
|
||||
"http://localhost:4566", regionResolver, lambdaService, mock(AcmService.class));
|
||||
}
|
||||
|
||||
private UserPool createPoolWithLambdaConfig(Map<String, Object> lambdaConfig) {
|
||||
|
||||
@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ReservedTags;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import io.github.hectorvent.floci.services.acm.model.Certificate;
|
||||
import io.github.hectorvent.floci.services.acm.model.CertificateStatus;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
|
||||
import io.github.hectorvent.floci.services.cognito.model.IdentityProvider;
|
||||
@@ -27,11 +30,13 @@ import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -43,12 +48,17 @@ class CognitoServiceTest {
|
||||
private InMemoryStorage<String, CognitoUser> userStore;
|
||||
private InMemoryStorage<String, CognitoGroup> groupStore;
|
||||
private RegionResolver regionResolver;
|
||||
private AcmService acmService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
userStore = new InMemoryStorage<>();
|
||||
groupStore = new InMemoryStorage<>();
|
||||
regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
acmService = mock(AcmService.class);
|
||||
// Every certificate exists and is issued unless a test says otherwise.
|
||||
when(acmService.describeCertificate(anyString(), eq("us-east-1")))
|
||||
.thenAnswer(inv -> issuedCertificate(inv.getArgument(0)));
|
||||
service = new CognitoService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
@@ -58,7 +68,8 @@ class CognitoServiceTest {
|
||||
new InMemoryStorage<>(), // revokedTokenStore
|
||||
"http://localhost:4566",
|
||||
regionResolver,
|
||||
null
|
||||
null,
|
||||
acmService
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1391,6 +1402,7 @@ class CognitoServiceTest {
|
||||
"http://localhost:4566",
|
||||
regionResolver,
|
||||
null,
|
||||
acmService,
|
||||
verificationCodeService,
|
||||
messageDispatcher
|
||||
);
|
||||
@@ -1430,6 +1442,7 @@ class CognitoServiceTest {
|
||||
"http://localhost:4566",
|
||||
regionResolver,
|
||||
null,
|
||||
acmService,
|
||||
verificationCodeService,
|
||||
messageDispatcher
|
||||
);
|
||||
@@ -2900,6 +2913,7 @@ class CognitoServiceTest {
|
||||
"http://localhost:4566",
|
||||
regionResolver,
|
||||
null,
|
||||
acmService,
|
||||
verificationCodeService,
|
||||
messageDispatcher
|
||||
);
|
||||
@@ -3269,6 +3283,173 @@ class CognitoServiceTest {
|
||||
return pool;
|
||||
}
|
||||
|
||||
private static final String AWS_CERTIFICATE_MESSAGE = "The specified SSL certificate doesn't exist, "
|
||||
+ "isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain.";
|
||||
|
||||
private static Certificate issuedCertificate(String arn) {
|
||||
Certificate certificate = new Certificate();
|
||||
certificate.setArn(arn);
|
||||
certificate.setStatus(CertificateStatus.ISSUED);
|
||||
return certificate;
|
||||
}
|
||||
|
||||
/** The ARN a domain registers on its certificate: the CloudFront distribution serving it. */
|
||||
private String consumerArn(String domain) {
|
||||
String distribution = service.describeUserPoolDomain(domain).getCloudFrontDistribution();
|
||||
return "arn:aws:cloudfront::000000000000:distribution/"
|
||||
+ distribution.substring(0, distribution.indexOf('.')).toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRejectsACertificateAcmDoesNotKnow() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
when(acmService.describeCertificate(CERTIFICATE_ARN, "us-east-1")).thenThrow(
|
||||
new AwsException("ResourceNotFoundException", "The certificate " + CERTIFICATE_ARN + " does not exist.", 404));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", CERTIFICATE_ARN), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(AWS_CERTIFICATE_MESSAGE, failure.getMessage());
|
||||
assertThrows(AwsException.class, () -> service.describeUserPoolDomain("auth.example.com"));
|
||||
verify(acmService, never()).addInUseBy(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRejectsACertificateOutsideUsEast1() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
String elsewhere = "arn:aws:acm:eu-west-1:000000000000:certificate/11111111-2222-3333-4444-555555555555";
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", elsewhere), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(AWS_CERTIFICATE_MESSAGE, failure.getMessage());
|
||||
verify(acmService, never()).describeCertificate(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRejectsACertificateThatIsNotIssued() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
Certificate pending = issuedCertificate(CERTIFICATE_ARN);
|
||||
pending.setStatus(CertificateStatus.PENDING_VALIDATION);
|
||||
when(acmService.describeCertificate(CERTIFICATE_ARN, "us-east-1")).thenReturn(pending);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", CERTIFICATE_ARN), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(AWS_CERTIFICATE_MESSAGE, failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRejectsAnAcmArnThatIsNotACertificate() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
String notACertificate = "arn:aws:acm:us-east-1:000000000000:certificate-authority/11111111-2222-3333-4444-555555555555";
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", notACertificate), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(AWS_CERTIFICATE_MESSAGE, failure.getMessage());
|
||||
verify(acmService, never()).describeCertificate(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRejectsAMalformedCertificateArn() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", "not-an-arn"), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
verify(acmService, never()).describeCertificate(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainStoresNothingWhenTheCertificateVanishesBeforeRegistration() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "DomainPool"), "us-east-1");
|
||||
doThrow(new AwsException("ResourceNotFoundException", "gone", 404))
|
||||
.when(acmService).addInUseBy(eq(CERTIFICATE_ARN), any(), eq("us-east-1"));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.createUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", CERTIFICATE_ARN), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(AWS_CERTIFICATE_MESSAGE, failure.getMessage());
|
||||
assertThrows(AwsException.class, () -> service.describeUserPoolDomain("auth.example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserPoolDomainKeepsTheCurrentCertificateWhenTheNewOneVanishesBeforeRegistration() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
doThrow(new AwsException("ResourceNotFoundException", "gone", 404))
|
||||
.when(acmService).addInUseBy(eq(RENEWED_CERTIFICATE_ARN), any(), eq("us-east-1"));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.updateUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", RENEWED_CERTIFICATE_ARN), 2));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
UserPoolDomain stored = service.describeUserPoolDomain("auth.example.com");
|
||||
assertEquals(CERTIFICATE_ARN, stored.getCertificateArn());
|
||||
assertEquals(1, stored.getManagedLoginVersion());
|
||||
verify(acmService, never()).removeInUseBy(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDomainRegistersTheDomainAsACertificateConsumer() {
|
||||
createPoolWithCustomDomain("auth.example.com");
|
||||
|
||||
verify(acmService).addInUseBy(CERTIFICATE_ARN, consumerArn("auth.example.com"), "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserPoolDomainMovesTheRegistrationToTheNewCertificate() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
String consumer = consumerArn("auth.example.com");
|
||||
|
||||
service.updateUserPoolDomain("auth.example.com", pool.getId(),
|
||||
Map.of("CertificateArn", RENEWED_CERTIFICATE_ARN), null);
|
||||
|
||||
verify(acmService).removeInUseBy(CERTIFICATE_ARN, consumer, "us-east-1");
|
||||
verify(acmService).addInUseBy(RENEWED_CERTIFICATE_ARN, consumer, "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserPoolDomainWithTheSameCertificateKeepsTheRegistration() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
|
||||
service.updateUserPoolDomain("auth.example.com", pool.getId(),
|
||||
Map.of("CertificateArn", CERTIFICATE_ARN), 2);
|
||||
|
||||
verify(acmService, never()).removeInUseBy(any(), any(), any());
|
||||
verify(acmService, times(1)).addInUseBy(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserPoolDomainRejectsAnUnknownCertificateAndKeepsTheCurrentOne() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
when(acmService.describeCertificate(RENEWED_CERTIFICATE_ARN, "us-east-1")).thenThrow(
|
||||
new AwsException("ResourceNotFoundException", "does not exist", 404));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.updateUserPoolDomain(
|
||||
"auth.example.com", pool.getId(), Map.of("CertificateArn", RENEWED_CERTIFICATE_ARN), null));
|
||||
|
||||
assertEquals("InvalidParameterException", failure.getErrorCode());
|
||||
assertEquals(CERTIFICATE_ARN, service.describeUserPoolDomain("auth.example.com").getCertificateArn());
|
||||
verify(acmService, never()).removeInUseBy(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUserPoolDomainReleasesTheCertificate() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
String consumer = consumerArn("auth.example.com");
|
||||
|
||||
service.deleteUserPoolDomain("auth.example.com", pool.getId());
|
||||
|
||||
verify(acmService).removeInUseBy(CERTIFICATE_ARN, consumer, "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUserPoolDomainReplacesTheCertificateAndKeepsTheCloudFrontDistribution() {
|
||||
UserPool pool = createPoolWithCustomDomain("auth.example.com");
|
||||
|
||||
+90
-10
@@ -14,12 +14,14 @@ import java.util.UUID;
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Covers CreateUserPoolDomain/DescribeUserPoolDomain/UpdateUserPoolDomain/DeleteUserPoolDomain (lex00/floci#63)
|
||||
* for both an Amazon Cognito prefix domain and a custom domain fronted by an ACM certificate.
|
||||
* for both an Amazon Cognito prefix domain and a custom domain fronted by an ACM certificate, including
|
||||
* the certificate's InUseBy bookkeeping in ACM.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -28,10 +30,10 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
private static String poolId;
|
||||
private static String prefixDomain;
|
||||
private static String customDomain;
|
||||
private static final String CERTIFICATE_ARN =
|
||||
"arn:aws:acm:us-east-1:000000000000:certificate/" + UUID.randomUUID();
|
||||
private static final String RENEWED_CERTIFICATE_ARN =
|
||||
"arn:aws:acm:us-east-1:000000000000:certificate/" + UUID.randomUUID();
|
||||
private static String CERTIFICATE_ARN;
|
||||
private static String RENEWED_CERTIFICATE_ARN;
|
||||
private static final String AWS_CERTIFICATE_MESSAGE = "The specified SSL certificate doesn't exist, "
|
||||
+ "isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain.";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
@@ -49,6 +51,25 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
poolId = poolResponse.path("UserPool").path("Id").asText();
|
||||
prefixDomain = "floci-test-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
customDomain = "auth-" + UUID.randomUUID().toString().substring(0, 8) + ".example.com";
|
||||
CERTIFICATE_ARN = requestCertificate(customDomain);
|
||||
RENEWED_CERTIFICATE_ARN = requestCertificate(customDomain);
|
||||
}
|
||||
|
||||
private static String requestCertificate(String domainName) throws Exception {
|
||||
return RestAssuredJsonUtils.awsActionJson("CertificateManager", "RequestCertificate", """
|
||||
{
|
||||
"DomainName": "%s",
|
||||
"ValidationMethod": "DNS"
|
||||
}
|
||||
""".formatted(domainName)).path("CertificateArn").asText();
|
||||
}
|
||||
|
||||
private static io.restassured.response.Response acm(String action, String certificateArn) {
|
||||
return RestAssuredJsonUtils.awsAction("CertificateManager", action, """
|
||||
{
|
||||
"CertificateArn": "%s"
|
||||
}
|
||||
""".formatted(certificateArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,6 +165,21 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void certificateInUseByTheDomainCannotBeDeleted() {
|
||||
acm("DescribeCertificate", CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Certificate.InUseBy.size()", equalTo(1))
|
||||
.body("Certificate.InUseBy[0]", startsWith("arn:aws:cloudfront::"));
|
||||
|
||||
acm("DeleteCertificate", CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(409)
|
||||
.body("__type", equalTo("ResourceInUseException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void updateCustomDomainReplacesTheCertificateAndKeepsTheCloudFrontDistribution() throws Exception {
|
||||
String cloudFront = cognitoJson("DescribeUserPoolDomain", """
|
||||
{
|
||||
@@ -177,7 +213,20 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@Order(10)
|
||||
void updateMovesTheRegistrationToTheRenewedCertificate() {
|
||||
acm("DescribeCertificate", CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Certificate.InUseBy.size()", equalTo(0));
|
||||
acm("DescribeCertificate", RENEWED_CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Certificate.InUseBy.size()", equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void updateDomainOfAnotherPoolIsNotFound() {
|
||||
cognitoAction("UpdateUserPoolDomain", """
|
||||
{
|
||||
@@ -193,7 +242,7 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
@Order(12)
|
||||
void createCustomDomainWithoutCertificateArnFails() {
|
||||
cognitoAction("CreateUserPoolDomain", """
|
||||
{
|
||||
@@ -208,7 +257,25 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
@Order(13)
|
||||
void createCustomDomainWithUnknownCertificateFails() {
|
||||
cognitoAction("CreateUserPoolDomain", """
|
||||
{
|
||||
"Domain": "%s",
|
||||
"UserPoolId": "%s",
|
||||
"CustomDomainConfig": {
|
||||
"CertificateArn": "arn:aws:acm:us-east-1:000000000000:certificate/%s"
|
||||
}
|
||||
}
|
||||
""".formatted("unknown-" + UUID.randomUUID().toString().substring(0, 8) + ".example.com", poolId, UUID.randomUUID()))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(AWS_CERTIFICATE_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void deleteCustomDomainThenDescribeIsNotFound() {
|
||||
cognitoAction("DeleteUserPoolDomain", """
|
||||
{
|
||||
@@ -230,7 +297,20 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
@Order(15)
|
||||
void deletingTheDomainReleasesTheCertificate() {
|
||||
acm("DescribeCertificate", RENEWED_CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Certificate.InUseBy.size()", equalTo(0));
|
||||
|
||||
acm("DeleteCertificate", RENEWED_CERTIFICATE_ARN)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void deleteUserPoolWithDomainIsRejected() throws Exception {
|
||||
// The DeleteUserPool API reference's own example documents this refusal verbatim.
|
||||
String blockingDomain = "floci-block-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
@@ -263,7 +343,7 @@ class CognitoUserPoolDomainIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
@Order(17)
|
||||
void deleteUserPoolSucceedsOnceDomainIsGone() {
|
||||
cognitoAction("DeleteUserPool", """
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user