mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
feat(cloudfront): enforce signed private content (#1831)
* feat(cloudfront): serve distribution requests from S3 and custom origins * feat(cloudfront): enforce signed private content * chore: re-run CI The Build and Test failure on the previous head was RuntimeApiServerTest's extension/stop race, a pre-existing flake unrelated to this change — it also failed on #1820, #1823 and on #2131's own branch (the PR that tried to de-flake it). Reproduced locally under `--cpus=1` and fixed separately. * fix(cloudfront): wildcard aliases match exactly one subdomain level wildcardAliasMatches did a plain suffix check, so "*.example.com" also matched a.b.example.com. AWS is explicit that a wildcard alternate domain name replaces the wildcard at that level only: "you can't add alternate domain names that are at levels higher or lower than the wildcard. For example, you can't add the alternate domain names example.com or marketing.product.example.com." https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html Practical effect: a distribution owning "*.example.com" would answer for a.b.example.com, which on AWS belongs to whoever owns "*.b.example.com" — or to nobody. Require the label standing in for the wildcard to contain no dot. wildcardAliasesMatchSubdomainsAndPreferMoreSpecificNames asserted the old behaviour (a.b.example.test resolving to the *.example.test distribution); it now asserts no match, plus the same for a deeper wildcard. * test(dynamodb): retain Quarkus port for cleanup * fix(cloudfront): isolate mocked storage backends * fix(cloudfront): match wildcards in signed policy queries
This commit is contained in:
1 parent
2ab795c6dc
commit
5657e35007
17 files changed
+3134
-28
No files matched your search
+193
@@ -0,0 +1,193 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudfront.CloudFrontClient;
|
||||
import software.amazon.awssdk.services.cloudfront.model.CreateDistributionResponse;
|
||||
import software.amazon.awssdk.services.cloudfront.model.CreateKeyGroupResponse;
|
||||
import software.amazon.awssdk.services.cloudfront.model.CreatePublicKeyResponse;
|
||||
import software.amazon.awssdk.services.cloudfront.model.DistributionConfig;
|
||||
import software.amazon.awssdk.services.cloudfront.model.GetDistributionConfigResponse;
|
||||
import software.amazon.awssdk.services.cloudfront.model.GetDistributionResponse;
|
||||
import software.amazon.awssdk.services.cloudfront.model.S3OriginConfig;
|
||||
import software.amazon.awssdk.services.cloudfront.model.ViewerProtocolPolicy;
|
||||
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
||||
|
||||
class CloudFrontTrustedKeyGroupsTest {
|
||||
|
||||
@Test
|
||||
void roundTripsTrustedKeyGroupsWithTheAwsSdk() throws Exception {
|
||||
assumeFalse(
|
||||
TestFixtures.isRealAws(),
|
||||
"The immediate distribution lifecycle in this test is emulator-specific");
|
||||
|
||||
String suffix = TestFixtures.uniqueName("cloudfront-trust");
|
||||
String publicKeyId = null;
|
||||
String publicKeyEtag = null;
|
||||
String keyGroupId = null;
|
||||
String keyGroupEtag = null;
|
||||
String distributionId = null;
|
||||
String encodedPublicKey = rsaPublicKeyPem();
|
||||
|
||||
try (CloudFrontClient cloudFront = TestFixtures.cloudFrontClient()) {
|
||||
CreatePublicKeyResponse publicKey = cloudFront.createPublicKey(
|
||||
request -> request.publicKeyConfig(config -> config
|
||||
.callerReference("key-reference-" + suffix)
|
||||
.name("key-" + suffix)
|
||||
.encodedKey(encodedPublicKey)));
|
||||
String createdPublicKeyId = publicKey.publicKey().id();
|
||||
publicKeyId = createdPublicKeyId;
|
||||
publicKeyEtag = publicKey.eTag();
|
||||
|
||||
CreateKeyGroupResponse keyGroup = cloudFront.createKeyGroup(
|
||||
request -> request.keyGroupConfig(config -> config
|
||||
.name("group-" + suffix)
|
||||
.items(createdPublicKeyId)));
|
||||
String createdKeyGroupId = keyGroup.keyGroup().id();
|
||||
keyGroupId = createdKeyGroupId;
|
||||
keyGroupEtag = keyGroup.eTag();
|
||||
assertEquals(
|
||||
List.of(createdPublicKeyId),
|
||||
keyGroup.keyGroup().keyGroupConfig().items());
|
||||
assertEquals(
|
||||
List.of(createdPublicKeyId),
|
||||
cloudFront.getKeyGroupConfig(
|
||||
request -> request.id(createdKeyGroupId))
|
||||
.keyGroupConfig()
|
||||
.items());
|
||||
var listedKeyGroup =
|
||||
cloudFront.listKeyGroups(request -> request.maxItems("100"))
|
||||
.keyGroupList()
|
||||
.items()
|
||||
.stream()
|
||||
.map(summary -> summary.keyGroup())
|
||||
.filter(group -> createdKeyGroupId.equals(group.id()))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertEquals(
|
||||
List.of(createdPublicKeyId),
|
||||
listedKeyGroup.keyGroupConfig().items());
|
||||
|
||||
DistributionConfig configuration = DistributionConfig.builder()
|
||||
.callerReference("distribution-reference-" + suffix)
|
||||
.enabled(true)
|
||||
.origins(origins -> origins
|
||||
.quantity(1)
|
||||
.items(origin -> origin
|
||||
.id("origin")
|
||||
.domainName(
|
||||
"signed-content.s3.amazonaws.com")
|
||||
.s3OriginConfig(
|
||||
S3OriginConfig.builder()
|
||||
.originAccessIdentity("")
|
||||
.build())))
|
||||
.defaultCacheBehavior(behavior -> behavior
|
||||
.targetOriginId("origin")
|
||||
.viewerProtocolPolicy(
|
||||
ViewerProtocolPolicy.ALLOW_ALL)
|
||||
.trustedKeyGroups(groups -> groups
|
||||
.enabled(true)
|
||||
.quantity(1)
|
||||
.items(createdKeyGroupId)))
|
||||
.build();
|
||||
|
||||
CreateDistributionResponse created =
|
||||
cloudFront.createDistribution(
|
||||
request -> request.distributionConfig(configuration));
|
||||
String createdDistributionId = created.distribution().id();
|
||||
distributionId = createdDistributionId;
|
||||
|
||||
GetDistributionResponse fetched =
|
||||
cloudFront.getDistribution(
|
||||
request -> request.id(createdDistributionId));
|
||||
assertTrue(fetched.distribution()
|
||||
.activeTrustedKeyGroups().enabled());
|
||||
assertEquals(
|
||||
keyGroupId,
|
||||
fetched.distribution()
|
||||
.activeTrustedKeyGroups()
|
||||
.items()
|
||||
.get(0)
|
||||
.keyGroupId());
|
||||
assertEquals(
|
||||
publicKeyId,
|
||||
fetched.distribution()
|
||||
.activeTrustedKeyGroups()
|
||||
.items()
|
||||
.get(0)
|
||||
.keyPairIds()
|
||||
.items()
|
||||
.get(0));
|
||||
assertFalse(fetched.distribution()
|
||||
.activeTrustedSigners().enabled());
|
||||
|
||||
GetDistributionConfigResponse fetchedConfig =
|
||||
cloudFront.getDistributionConfig(
|
||||
request -> request.id(createdDistributionId));
|
||||
assertEquals(
|
||||
keyGroupId,
|
||||
fetchedConfig.distributionConfig()
|
||||
.defaultCacheBehavior()
|
||||
.trustedKeyGroups()
|
||||
.items()
|
||||
.get(0));
|
||||
} finally {
|
||||
try (CloudFrontClient cloudFront =
|
||||
TestFixtures.cloudFrontClient()) {
|
||||
if (distributionId != null) {
|
||||
String id = distributionId;
|
||||
GetDistributionConfigResponse current =
|
||||
cloudFront.getDistributionConfig(
|
||||
request -> request.id(id));
|
||||
var disabled = current.distributionConfig()
|
||||
.toBuilder()
|
||||
.enabled(false)
|
||||
.build();
|
||||
var updated = cloudFront.updateDistribution(
|
||||
request -> request
|
||||
.id(id)
|
||||
.ifMatch(current.eTag())
|
||||
.distributionConfig(disabled));
|
||||
cloudFront.deleteDistribution(
|
||||
request -> request
|
||||
.id(id)
|
||||
.ifMatch(updated.eTag()));
|
||||
}
|
||||
if (keyGroupId != null) {
|
||||
String id = keyGroupId;
|
||||
String etag = keyGroupEtag;
|
||||
cloudFront.deleteKeyGroup(
|
||||
request -> request
|
||||
.id(id)
|
||||
.ifMatch(etag));
|
||||
}
|
||||
if (publicKeyId != null) {
|
||||
String id = publicKeyId;
|
||||
String etag = publicKeyEtag;
|
||||
cloudFront.deletePublicKey(
|
||||
request -> request
|
||||
.id(id)
|
||||
.ifMatch(etag));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String rsaPublicKeyPem() throws Exception {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(2048);
|
||||
byte[] encoded = generator.generateKeyPair()
|
||||
.getPublic()
|
||||
.getEncoded();
|
||||
return "-----BEGIN PUBLIC KEY-----\n"
|
||||
+ Base64.getMimeEncoder().encodeToString(encoded)
|
||||
+ "\n-----END PUBLIC KEY-----";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user