mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(kms): serve the AWS managed keys under alias/aws/* in every region (#4792)
* feat(kms): serve the AWS managed keys under alias/aws/* in every region Every AWS account has a per-region AWS managed key for each service that encrypts data on the account's behalf, published under a reserved alias/aws/<service> alias. Floci had none, so a lookup that succeeds against any real account failed here: DescribeKey on alias/aws/s3 returned NotFoundException and ListAliases returned nothing in that namespace. Nothing declares these keys in a template, which is what made their absence costly to diagnose. A module that leaves a service's encryption at its default resolves one through a data source and fails at plan time. The catalog is data, in kms/aws-managed-keys.yaml, following the IAM managed policy catalog: adding a service is a line there. A region's keys are minted the first time anything reads that region's alias namespace, which is ListAliases or a lookup naming one of the reserved aliases. That mirrors AWS, where a service's key appears once the account first uses the service, and it keeps an unused region free. Minting is idempotent, so the ids survive a restart against persistent storage and ciphertext encrypted under them keeps decrypting. These are AWS's keys, not the account's, so DescribeKey now reports KeyManager per key instead of the constant CUSTOMER it returned before, GetKeyRotationStatus reports true for them, and the administrative operations AWS reserves to a key's owner are refused with AccessDeniedException: ScheduleKeyDeletion, CancelKeyDeletion, PutKeyPolicy, UpdateKeyDescription, EnableKey, DisableKey, EnableKeyRotation, DisableKeyRotation, RotateKeyOnDemand, TagResource and UntagResource. The alias/aws/ namespace is reserved too: CreateAlias under it is rejected with InvalidAliasNameException, and UpdateAlias or DeleteAlias against one of these aliases with AccessDeniedException. Cryptographic operations are unaffected, which is the point of the keys existing. Two deliberate simplifications, both documented: the key policy is the standard account-root policy rather than the service-principal policy with a kms:ViaService condition AWS attaches, and AWS distinguishes some of those refusals as unsupported operations rather than access denials. * fix(kms): seed only AWS managed keys, and rebuild them if the key store is lost Five corrections to the alias/aws/* catalog and the code that mints it. The catalog went from 40 entries to 23. An AWS managed key lives in the customer's account and has an alias/aws/<service> alias; an AWS owned key lives in an AWS-owned account and has no alias there, so DescribeKey on it fails against a real account. Seeding the second kind makes a call succeed here and fail against AWS, which is the worst error this emulator can produce: a module looks compatible when it is not, and nothing reports it. Seventeen entries could not be sourced to a customer-account context and are gone, EKS among them, whose API-data envelope encryption uses an AWS owned key by default. The YAML header now states the managed-versus-owned test so the next line added has a bar to clear. Minting is gated on the alias being present and the key that alias targets being present, not on the alias alone. The two stores are different files and PersistentStorage quarantines an unreadable one and starts that store empty by itself, so a lost kms-keys.json used to leave every alias dangling for the life of the instance: ListAliases advertised alias/aws/s3 while DescribeKey on it reported NotFoundException. The catalog is in the repository, so this is the one part of a damaged store that can rebuild itself, and now it does. ListKeys mints too. Real AWS returns the AWS managed keys from ListKeys unconditionally, and a caller that reads ListKeys, then DescribeKey, then filters on KeyManager saw them or not depending on whether something else had read the alias namespace earlier in the same process. Both stores are written once per region as a batch instead of once per entry. PersistentStorage.put serialises the whole map, so the old path cost two whole-file rewrites per catalog entry on the first read of every region, while holding the mint lock. The aliases are written before the keys deliberately: a crash between the two batches then leaves aliases whose target is missing, which the gate above repairs, rather than keys with no alias, which nothing repairs and which would sit in ListKeys forever. CreateAlias and UpdateAlias no longer accept an AWS managed key as the target. AWS excludes them, so a second alias on one of these keys is the same false-pass shape as seeding a key AWS does not have. The refusal is AccessDeniedException: it is an authorization outcome, the key's policy grants the account nothing, and CreateAlias publishes no error code for this case. Two pre-existing tests asserted counts that ListKeys and ListAliases now answer as AWS does; they select the customer-managed subset instead. * docs(kms): group the AWS managed key catalog by the strength of its evidence * fix(kms): drop alias/aws/elasticache and cite alias/aws/acm in the managed key catalog The ElastiCache at-rest encryption guide describes the default as a service managed key and serverless caches as using an AWS owned key; it never names alias/aws/elasticache, so the entry failed the catalog's own bar. The KMS ListAliases API reference prints alias/aws/acm with a TargetKeyId in its example output, so acm joins the cited group. * refactor(kms): fold requireCustomerManagedTarget into requireCustomerManaged The two helpers had identical bodies. Also narrow the ensureAwsManagedKeys Javadoc: writing aliases before keys helps only for a crash between the two batches; losing kms-aliases.json alone still mints fresh keys and orphans the old ones. * fix(kms): refuse CreateGrant on AWS managed keys An AWS managed key's policy allows kms:CreateGrant only via the owning service (kms:ViaService), never to the account directly, so a direct CreateGrant now gets AccessDeniedException like the other administrative refusals. * test(kms): concurrent cold reads mint each AWS managed key once Sixteen threads read ListAliases on a cold region at once; the key store must hold exactly the catalog's keys and each reserved alias must appear once. Replacing the awsManagedKeyLock monitor with a fresh Object makes this fail (138 keys minted against a 25-entry catalog). * fix(kms): register the AWS managed key catalog for native and re-point squatted reserved aliases - Add kms/*.yaml to quarkus.native.resources.includes. AwsManagedKeys reads kms/aws-managed-keys.yaml through a constant, which native resource analysis cannot follow (NativeImageResourceIncludesTest). - Reconcile a reserved alias as present only when its target is an AWS managed key. A store persisted before alias/aws/ was reserved could hold alias/aws/s3 on a customer key, which DescribeKey then reported as CUSTOMER and the AWS managed key guards let through. - When the alias store is lost but keys survive, re-alias the surviving AWS managed keys by description instead of minting a duplicate set. * docs(kms): cite the sqs, sns, ses, s3 and ebs managed aliases
This commit is contained in:
1 parent
39867e6bcb
commit
5ad97781ef
9 files changed
+1013
-14
No files matched your search
@@ -29,6 +29,7 @@ import java.util.List;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
@@ -36,6 +37,8 @@ import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class KmsIntegrationTest {
|
||||
@@ -3241,6 +3244,145 @@ class KmsIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* The alias/aws/* keys, over the wire. DescribeKey's KeyManager was a constant "CUSTOMER"
|
||||
* before these keys existed, so an AWS managed key has to be asked for through the handler
|
||||
* to prove the field now tracks the key rather than the code path.
|
||||
*/
|
||||
@Test
|
||||
void listAliasesExposesTheAwsManagedAliasesWithTargetKeys() {
|
||||
JsonPath aliases = given()
|
||||
.header("X-Amz-Target", "TrentService.ListAliases")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
|
||||
List<String> names = aliases.getList("Aliases.AliasName");
|
||||
assertTrue(names.contains("alias/aws/s3"), "alias/aws/s3 missing from " + names);
|
||||
assertTrue(names.contains("alias/aws/ebs"), "alias/aws/ebs missing from " + names);
|
||||
|
||||
int index = names.indexOf("alias/aws/s3");
|
||||
String targetKeyId = aliases.getList("Aliases.TargetKeyId", String.class).get(index);
|
||||
assertNotNull(targetKeyId, "alias/aws/s3 resolves to nothing");
|
||||
assertFalse(targetKeyId.isBlank(), "alias/aws/s3 resolves to nothing");
|
||||
assertTrue(aliases.getList("Aliases.AliasArn", String.class).get(index).endsWith(":alias/aws/s3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeKeyReportsKeyManagerPerKey() {
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.DescribeKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"alias/aws/s3\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("KeyMetadata.KeyManager", equalTo("AWS"))
|
||||
.body("KeyMetadata.Enabled", equalTo(true))
|
||||
.body("KeyMetadata.KeyState", equalTo("Enabled"));
|
||||
|
||||
String customerKeyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"Description\": \"customer managed\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.DescribeKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"" + customerKeyId + "\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("KeyMetadata.KeyManager", equalTo("CUSTOMER"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The lookup a module actually performs: encrypt under the service default key and read it
|
||||
* back. A resolvable alias whose key could not encrypt would satisfy DescribeKey and still
|
||||
* fail the caller.
|
||||
*/
|
||||
@Test
|
||||
void encryptsAndDecryptsUnderAnAwsManagedAlias() {
|
||||
String ciphertext = given()
|
||||
.header("X-Amz-Target", "TrentService.Encrypt")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"alias/aws/secretsmanager\", \"Plaintext\": \""
|
||||
+ Base64.getEncoder().encodeToString("default key payload".getBytes(StandardCharsets.UTF_8))
|
||||
+ "\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("CiphertextBlob");
|
||||
|
||||
String plaintext = given()
|
||||
.header("X-Amz-Target", "TrentService.Decrypt")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"CiphertextBlob\": \"" + ciphertext + "\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("Plaintext");
|
||||
|
||||
assertEquals("default key payload",
|
||||
new String(Base64.getDecoder().decode(plaintext), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateAlias must not hand an AWS managed key a second alias. AWS excludes them as a target,
|
||||
* so accepting one here would make a template succeed against Floci and fail against AWS,
|
||||
* which is the one failure mode nothing downstream reports.
|
||||
*/
|
||||
@Test
|
||||
void refusesToPointACustomerAliasAtAnAwsManagedKey() {
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\": \"alias/borrowed-from-aws\", \"TargetKeyId\": \"alias/aws/s3\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.ListAliases")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("Aliases.AliasName", not(hasItem("alias/borrowed-from-aws")));
|
||||
}
|
||||
|
||||
/**
|
||||
* An AWS managed key is AWS's, not the account's. ScheduleKeyDeletion takes a key id rather
|
||||
* than an alias, so the refusal has to be reached through the id the alias resolves to.
|
||||
*/
|
||||
@Test
|
||||
void refusesToScheduleDeletionOfAnAwsManagedKey() {
|
||||
String keyId = given()
|
||||
.header("X-Amz-Target", "TrentService.DescribeKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"alias/aws/ebs\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getString("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.ScheduleKeyDeletion")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"" + keyId + "\", \"PendingWindowInDays\": 7}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.DescribeKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\": \"" + keyId + "\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("KeyMetadata.KeyState", equalTo("Enabled"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps key material exactly as a caller would: the point of the round trip is that the
|
||||
* emulator unwraps what a standard RSAES-OAEP-SHA-256 client produces, not a shape of its own.
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.Mac;
|
||||
@@ -80,6 +81,7 @@ class KmsServiceTest {
|
||||
|
||||
private KmsService kmsService;
|
||||
private InMemoryStorage<String, KmsKey> keyStore;
|
||||
private InMemoryStorage<String, KmsAlias> aliasStore;
|
||||
|
||||
@BeforeAll
|
||||
static void registerBouncyCastle() {
|
||||
@@ -91,9 +93,10 @@ class KmsServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
keyStore = new InMemoryStorage<>();
|
||||
aliasStore = new InMemoryStorage<>();
|
||||
kmsService = new KmsService(
|
||||
keyStore,
|
||||
new InMemoryStorage<>(),
|
||||
aliasStore,
|
||||
new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000")
|
||||
);
|
||||
@@ -331,8 +334,10 @@ class KmsServiceTest {
|
||||
kmsService.createKey("key2", REGION);
|
||||
kmsService.createKey("key3", "eu-west-1");
|
||||
|
||||
List<KmsKey> keys = kmsService.listKeys(REGION);
|
||||
assertEquals(2, keys.size());
|
||||
// ListKeys also returns the region's AWS managed keys, as it does on real AWS, so the
|
||||
// count this test is about is the customer-managed one.
|
||||
assertEquals(2, customerKeys(REGION).size());
|
||||
assertEquals(1, customerKeys("eu-west-1").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -856,12 +861,30 @@ class KmsServiceTest {
|
||||
assertEquals(0, updated.getDeletionDate());
|
||||
}
|
||||
|
||||
/** The keys a test created. ListKeys also returns the region's AWS managed keys. */
|
||||
private List<KmsKey> customerKeys(String region) {
|
||||
return kmsService.listKeys(region).stream()
|
||||
.filter(key -> !"AWS".equals(key.getKeyManager()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The aliases a test created. ListAliases also returns the alias/aws/* aliases of the AWS
|
||||
* managed keys, which every account has and no customer creates, so a test that cares about
|
||||
* its own alias has to say so.
|
||||
*/
|
||||
private List<KmsAlias> customerAliases(String region) {
|
||||
return kmsService.listAliases(region).stream()
|
||||
.filter(alias -> !alias.getAliasName().startsWith("alias/aws/"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAlias() {
|
||||
KmsKey key = kmsService.createKey(null, REGION);
|
||||
kmsService.createAlias("alias/my-key", key.getKeyId(), REGION);
|
||||
|
||||
List<KmsAlias> aliases = kmsService.listAliases(REGION);
|
||||
List<KmsAlias> aliases = customerAliases(REGION);
|
||||
assertEquals(1, aliases.size());
|
||||
assertEquals("alias/my-key", aliases.getFirst().getAliasName());
|
||||
assertEquals(key.getKeyId(), aliases.getFirst().getTargetKeyId());
|
||||
@@ -888,7 +911,7 @@ class KmsServiceTest {
|
||||
|
||||
kmsService.updateAlias("alias/my-key", keyB.getKeyId(), REGION);
|
||||
|
||||
List<KmsAlias> aliases = kmsService.listAliases(REGION);
|
||||
List<KmsAlias> aliases = customerAliases(REGION);
|
||||
assertEquals(1, aliases.size());
|
||||
assertEquals(keyB.getKeyId(), aliases.getFirst().getTargetKeyId());
|
||||
}
|
||||
@@ -911,7 +934,7 @@ class KmsServiceTest {
|
||||
kmsService.updateAlias("alias/my-key", "no-such-key", REGION));
|
||||
|
||||
assertEquals("NotFoundException", ex.getErrorCode());
|
||||
assertEquals(key.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(key.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -925,7 +948,7 @@ class KmsServiceTest {
|
||||
kmsService.updateAlias("alias/my-key", keyB.getKeyId(), REGION));
|
||||
|
||||
assertEquals("KMSInvalidStateException", ex.getErrorCode());
|
||||
assertEquals(keyA.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(keyA.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -938,7 +961,7 @@ class KmsServiceTest {
|
||||
kmsService.updateAlias("alias/my-key", hmacKey.getKeyId(), REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals(symmetricKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(symmetricKey.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -949,7 +972,7 @@ class KmsServiceTest {
|
||||
|
||||
kmsService.updateAlias("alias/my-key", rsa3072.getKeyId(), REGION);
|
||||
|
||||
assertEquals(rsa3072.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(rsa3072.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -960,7 +983,7 @@ class KmsServiceTest {
|
||||
|
||||
kmsService.updateAlias("alias/my-key", eccKey.getKeyId(), REGION);
|
||||
|
||||
assertEquals(eccKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(eccKey.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -973,7 +996,7 @@ class KmsServiceTest {
|
||||
kmsService.updateAlias("alias/my-key", rsaEncryptKey.getKeyId(), REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals(symmetricKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
assertEquals(symmetricKey.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -982,7 +1005,7 @@ class KmsServiceTest {
|
||||
kmsService.createAlias("alias/to-delete", key.getKeyId(), REGION);
|
||||
kmsService.deleteAlias("alias/to-delete", REGION);
|
||||
|
||||
assertTrue(kmsService.listAliases(REGION).isEmpty());
|
||||
assertTrue(customerAliases(REGION).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -3680,4 +3703,340 @@ class KmsServiceTest {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The per-service keys AWS ships under alias/aws/*. No template declares them, so nothing
|
||||
* reports them missing; a module that leaves a service's encryption at its default simply
|
||||
* cannot resolve the key a real account would hand it.
|
||||
*/
|
||||
@Nested
|
||||
class AwsManagedKeyTests {
|
||||
|
||||
@Test
|
||||
void listAliasesMintsEveryReservedAliasWithAResolvableKey() {
|
||||
List<KmsAlias> aliases = kmsService.listAliases(REGION);
|
||||
|
||||
List<String> names = aliases.stream().map(KmsAlias::getAliasName).toList();
|
||||
assertTrue(names.contains("alias/aws/s3"), "alias/aws/s3 missing from " + names);
|
||||
assertTrue(names.contains("alias/aws/ebs"), "alias/aws/ebs missing from " + names);
|
||||
assertTrue(names.contains("alias/aws/rds"), "alias/aws/rds missing from " + names);
|
||||
|
||||
for (KmsAlias alias : aliases) {
|
||||
assertNotNull(alias.getTargetKeyId(), alias.getAliasName() + " has no target key");
|
||||
// The alias is only worth anything if it resolves; a name with a dangling target
|
||||
// fails a data source exactly as a missing name does.
|
||||
KmsKey target = kmsService.describeKey(alias.getAliasName(), REGION);
|
||||
assertEquals(alias.getTargetKeyId(), target.getKeyId());
|
||||
assertEquals("AWS", target.getKeyManager(), alias.getAliasName() + " is not AWS managed");
|
||||
assertTrue(alias.getAliasArn().endsWith(":" + alias.getAliasName()),
|
||||
"alias ARN " + alias.getAliasArn() + " does not name " + alias.getAliasName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeKeyResolvesAReservedAliasWithoutAPriorListAliases() {
|
||||
KmsKey key = kmsService.describeKey("alias/aws/s3", REGION);
|
||||
|
||||
assertEquals("AWS", key.getKeyManager());
|
||||
assertEquals("Enabled", key.getKeyState());
|
||||
assertTrue(key.isEnabled());
|
||||
// AWS rotates its own keys and the customer cannot turn that off.
|
||||
assertTrue(kmsService.getKeyRotationStatus("alias/aws/s3", REGION));
|
||||
assertTrue(key.getArn().contains(REGION), "key ARN " + key.getArn() + " is not in " + REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeKeyResolvesAReservedAliasByItsArn() {
|
||||
KmsAlias alias = kmsService.listAliases(REGION).stream()
|
||||
.filter(a -> "alias/aws/ebs".equals(a.getAliasName()))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
|
||||
assertEquals(alias.getTargetKeyId(),
|
||||
kmsService.describeKey(alias.getAliasArn(), REGION).getKeyId());
|
||||
}
|
||||
|
||||
/**
|
||||
* A seeded key is only real if it encrypts. Asserting that the alias resolves proves the
|
||||
* lookup; asserting that ciphertext produced under it comes back as the original bytes
|
||||
* proves the key behind it has material, which a stub entry in the alias store would not.
|
||||
*/
|
||||
@Test
|
||||
void theAwsManagedKeyEncryptsAndDecrypts() {
|
||||
byte[] plaintext = "default-encrypted object".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] ciphertext = kmsService.encrypt("alias/aws/s3", plaintext, REGION);
|
||||
|
||||
assertFalse(Arrays.equals(plaintext, ciphertext), "ciphertext is the plaintext");
|
||||
assertArrayEquals(plaintext, kmsService.decrypt(ciphertext, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void seedingIsIdempotentAcrossRepeatedLookups() {
|
||||
String first = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
int keysAfterFirst = kmsService.listKeys(REGION).size();
|
||||
int aliasesAfterFirst = kmsService.listAliases(REGION).size();
|
||||
|
||||
kmsService.listAliases(REGION);
|
||||
String second = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
|
||||
assertEquals(first, second, "the AWS managed key was minted twice");
|
||||
assertEquals(keysAfterFirst, kmsService.listKeys(REGION).size());
|
||||
assertEquals(aliasesAfterFirst, kmsService.listAliases(REGION).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachRegionGetsItsOwnAwsManagedKeys() {
|
||||
KmsKey east = kmsService.describeKey("alias/aws/s3", "us-east-1");
|
||||
KmsKey west = kmsService.describeKey("alias/aws/s3", "eu-west-1");
|
||||
|
||||
assertNotEquals(east.getKeyId(), west.getKeyId(),
|
||||
"one key is shared between regions; AWS managed keys are per region");
|
||||
assertTrue(east.getArn().contains("us-east-1"), east.getArn());
|
||||
assertTrue(west.getArn().contains("eu-west-1"), west.getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customerKeysStayCustomerManagedAndAdministrable() {
|
||||
KmsKey key = kmsService.createKey("mine", REGION);
|
||||
|
||||
assertEquals("CUSTOMER", key.getKeyManager());
|
||||
kmsService.tagResource(key.getKeyId(), Map.of("env", "test"), REGION);
|
||||
kmsService.scheduleKeyDeletion(key.getKeyId(), 7, REGION);
|
||||
assertEquals("PendingDeletion", kmsService.describeKey(key.getKeyId(), REGION).getKeyState());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesEveryCustomerAdministrationOfAnAwsManagedKey() {
|
||||
String alias = "alias/aws/rds";
|
||||
Map<String, Executable> denied = new LinkedHashMap<>();
|
||||
denied.put("ScheduleKeyDeletion", () -> kmsService.scheduleKeyDeletion(alias, 30, REGION));
|
||||
denied.put("CancelKeyDeletion", () -> kmsService.cancelKeyDeletion(alias, REGION));
|
||||
denied.put("PutKeyPolicy", () -> kmsService.putKeyPolicy(alias, "{}", REGION));
|
||||
denied.put("UpdateKeyDescription", () -> kmsService.updateKeyDescription(alias, "mine now", REGION));
|
||||
denied.put("EnableKeyRotation", () -> kmsService.enableKeyRotation(alias, REGION));
|
||||
denied.put("DisableKeyRotation", () -> kmsService.disableKeyRotation(alias, REGION));
|
||||
denied.put("EnableKey", () -> kmsService.enableKey(alias, REGION));
|
||||
denied.put("DisableKey", () -> kmsService.disableKey(alias, REGION));
|
||||
denied.put("RotateKeyOnDemand", () -> kmsService.rotateKeyOnDemand(alias, REGION));
|
||||
denied.put("TagResource", () -> kmsService.tagResource(alias, Map.of("a", "b"), REGION));
|
||||
denied.put("UntagResource", () -> kmsService.untagResource(alias, List.of("a"), REGION));
|
||||
// The key policy allows kms:CreateGrant only via the owning service, never directly.
|
||||
denied.put("CreateGrant", () -> kmsService.createGrant(alias, "arn:aws:iam::000000000000:role/r",
|
||||
List.of("Decrypt"), REGION));
|
||||
|
||||
denied.forEach((operation, call) -> {
|
||||
AwsException exception = assertThrows(AwsException.class, call,
|
||||
operation + " was allowed on an AWS managed key");
|
||||
assertEquals("AccessDeniedException", exception.getErrorCode(), operation);
|
||||
});
|
||||
|
||||
// The refusals must not have left the key half-changed.
|
||||
KmsKey key = kmsService.describeKey(alias, REGION);
|
||||
assertEquals("Enabled", key.getKeyState());
|
||||
assertTrue(key.getTags().isEmpty());
|
||||
assertTrue(key.isKeyRotationEnabled());
|
||||
assertEquals(List.of(), kmsService.listGrants(alias, REGION, null, null, null, null).get("Grants"),
|
||||
"CreateGrant was refused but left a grant behind");
|
||||
}
|
||||
|
||||
/**
|
||||
* Minting is check-then-write across two stores, so two cold reads racing each other must
|
||||
* not both mint: the loser's keys would sit in ListKeys unaliased forever.
|
||||
*/
|
||||
@Test
|
||||
void concurrentColdReadsMintEachAwsManagedKeyOnce() throws Exception {
|
||||
int threads = 16;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
try {
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
List<Future<List<KmsAlias>>> reads = new ArrayList<>();
|
||||
for (int i = 0; i < threads; i++) {
|
||||
reads.add(pool.submit(() -> {
|
||||
start.await();
|
||||
return kmsService.listAliases(REGION);
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
for (Future<List<KmsAlias>> read : reads) {
|
||||
read.get(30, TimeUnit.SECONDS);
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
// Read the key store directly, not through listKeys, which would mint again.
|
||||
assertEquals(AwsManagedKeys.KEYS.size(), keyStore.scan(k -> k.startsWith(REGION + "::")).size(),
|
||||
"concurrent cold reads minted more AWS managed keys than the catalog holds");
|
||||
List<String> reserved = kmsService.listAliases(REGION).stream()
|
||||
.map(KmsAlias::getAliasName)
|
||||
.filter(name -> name.startsWith("alias/aws/"))
|
||||
.toList();
|
||||
assertEquals(AwsManagedKeys.KEYS.size(), reserved.size());
|
||||
for (AwsManagedKeys.AwsManagedKeyDef def : AwsManagedKeys.KEYS) {
|
||||
assertEquals(1, reserved.stream().filter(def.aliasName()::equals).count(), def.aliasName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesToClaimAnAliasInTheReservedNamespace() {
|
||||
KmsKey mine = kmsService.createKey("mine", REGION);
|
||||
|
||||
AwsException created = assertThrows(AwsException.class,
|
||||
() -> kmsService.createAlias("alias/aws/s3", mine.getKeyId(), REGION));
|
||||
assertEquals("InvalidAliasNameException", created.getErrorCode());
|
||||
|
||||
AwsException fresh = assertThrows(AwsException.class,
|
||||
() -> kmsService.createAlias("alias/aws/not-a-real-service", mine.getKeyId(), REGION));
|
||||
assertEquals("InvalidAliasNameException", fresh.getErrorCode());
|
||||
|
||||
AwsException updated = assertThrows(AwsException.class,
|
||||
() -> kmsService.updateAlias("alias/aws/s3", mine.getKeyId(), REGION));
|
||||
assertEquals("AccessDeniedException", updated.getErrorCode());
|
||||
|
||||
AwsException deleted = assertThrows(AwsException.class,
|
||||
() -> kmsService.deleteAlias("alias/aws/s3", REGION));
|
||||
assertEquals("AccessDeniedException", deleted.getErrorCode());
|
||||
|
||||
// The alias still points where it did.
|
||||
assertEquals("AWS", kmsService.describeKey("alias/aws/s3", REGION).getKeyManager());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownAliasUnderTheReservedPrefixIsStillNotFound() {
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> kmsService.describeKey("alias/aws/not-a-real-service", REGION));
|
||||
|
||||
assertEquals("NotFoundException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* The aliases and the keys live in different files, and PersistentStorage quarantines an
|
||||
* unreadable one and starts that store empty on its own. Gating minting on the alias alone
|
||||
* left every alias dangling for the life of the instance: ListAliases advertised
|
||||
* alias/aws/s3 while DescribeKey on it reported NotFoundException. The catalog is in the
|
||||
* repository, so this is the one part of a damaged store that can rebuild itself.
|
||||
*/
|
||||
@Test
|
||||
void remintsTheKeyWhenTheKeyStoreLostItAndTheAliasSurvived() {
|
||||
String original = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
keyStore.delete(REGION + "::" + original);
|
||||
|
||||
// Not a shape check: the alias has to resolve to a key that can still do the work.
|
||||
byte[] plaintext = "after the key store was lost".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] ciphertext = kmsService.encrypt("alias/aws/s3", plaintext, REGION);
|
||||
|
||||
assertArrayEquals(plaintext, kmsService.decrypt(ciphertext, REGION));
|
||||
String reminted = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
assertNotEquals(original, reminted, "the alias still points at the key that was lost");
|
||||
assertEquals("AWS", kmsService.describeKey("alias/aws/s3", REGION).getKeyManager());
|
||||
// The repair must not duplicate the alias.
|
||||
assertEquals(1, kmsService.listAliases(REGION).stream()
|
||||
.filter(alias -> "alias/aws/s3".equals(alias.getAliasName())).count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Before alias/aws/ was reserved, a customer could create alias/aws/s3 on their own key and
|
||||
* persist it. Gating on "the target exists" kept that alias pointing at a CUSTOMER key, so
|
||||
* DescribeKey reported CUSTOMER and the AWS managed key guards let ScheduleKeyDeletion
|
||||
* through the reserved alias.
|
||||
*/
|
||||
@Test
|
||||
void repointsAReservedAliasPersistedOnACustomerKey() {
|
||||
KmsKey mine = kmsService.createKey("mine", REGION);
|
||||
aliasStore.put(REGION + "::alias/aws/s3", new KmsAlias("alias/aws/s3",
|
||||
"arn:aws:kms:us-east-1:000000000000:alias/aws/s3", mine.getKeyId()));
|
||||
|
||||
KmsKey resolved = kmsService.describeKey("alias/aws/s3", REGION);
|
||||
|
||||
assertEquals("AWS", resolved.getKeyManager());
|
||||
assertNotEquals(mine.getKeyId(), resolved.getKeyId());
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> kmsService.scheduleKeyDeletion("alias/aws/s3", 7, REGION));
|
||||
assertEquals("AccessDeniedException", refused.getErrorCode());
|
||||
// The customer key itself is untouched.
|
||||
assertEquals("CUSTOMER", kmsService.describeKey(mine.getKeyId(), REGION).getKeyManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* kms-aliases.json lost while kms-keys.json survives: the AWS managed keys are re-aliased
|
||||
* rather than minted again, so ListKeys holds no duplicates and ciphertext bound to the old
|
||||
* key still decrypts and still belongs to the key alias/aws/s3 names.
|
||||
*/
|
||||
@Test
|
||||
void reusesSurvivingAwsManagedKeysWhenTheAliasStoreIsLost() {
|
||||
byte[] plaintext = "before the alias store was lost".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] ciphertext = kmsService.encrypt("alias/aws/s3", plaintext, REGION);
|
||||
String original = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
aliasStore.scan(k -> true).forEach(a -> aliasStore.delete(REGION + "::" + a.getAliasName()));
|
||||
assertTrue(aliasStore.scan(k -> true).isEmpty());
|
||||
|
||||
assertEquals(AwsManagedKeys.KEYS.size(), kmsService.listKeys(REGION).size(),
|
||||
"losing the alias store minted a second set of AWS managed keys");
|
||||
assertEquals(original, kmsService.describeKey("alias/aws/s3", REGION).getKeyId());
|
||||
assertArrayEquals(plaintext, kmsService.decrypt(ciphertext, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAliasesRepairsEveryDanglingReservedAlias() {
|
||||
kmsService.listAliases(REGION).stream()
|
||||
.filter(alias -> alias.getAliasName().startsWith("alias/aws/"))
|
||||
.forEach(alias -> keyStore.delete(REGION + "::" + alias.getTargetKeyId()));
|
||||
|
||||
for (KmsAlias alias : kmsService.listAliases(REGION)) {
|
||||
assertEquals(alias.getTargetKeyId(),
|
||||
kmsService.describeKey(alias.getAliasName(), REGION).getKeyId(),
|
||||
alias.getAliasName() + " did not resolve after the key store was emptied");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Real AWS returns the AWS managed keys from ListKeys unconditionally. Reading ListKeys
|
||||
* first on a cold service used to return nothing, so a caller that filters on KeyManager
|
||||
* saw them or not depending on call order.
|
||||
*/
|
||||
@Test
|
||||
void listKeysMintsTheAwsManagedKeysWithoutAPriorAliasRead() {
|
||||
List<KmsKey> keys = kmsService.listKeys(REGION);
|
||||
|
||||
assertEquals(AwsManagedKeys.KEYS.size(), keys.size());
|
||||
assertTrue(keys.stream().allMatch(key -> "AWS".equals(key.getKeyManager())));
|
||||
// And it did not mint a second set once the alias namespace is read as well.
|
||||
kmsService.listAliases(REGION);
|
||||
assertEquals(keys.size(), kmsService.listKeys(REGION).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesToPointACustomerAliasAtAnAwsManagedKey() {
|
||||
String awsKeyId = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
KmsKey mine = kmsService.createKey("mine", REGION);
|
||||
kmsService.createAlias("alias/mine", mine.getKeyId(), REGION);
|
||||
|
||||
for (String target : List.of("alias/aws/s3", awsKeyId)) {
|
||||
AwsException created = assertThrows(AwsException.class,
|
||||
() -> kmsService.createAlias("alias/borrowed", target, REGION),
|
||||
"CreateAlias accepted an AWS managed key as the target: " + target);
|
||||
assertEquals("AccessDeniedException", created.getErrorCode());
|
||||
|
||||
AwsException updated = assertThrows(AwsException.class,
|
||||
() -> kmsService.updateAlias("alias/mine", target, REGION),
|
||||
"UpdateAlias accepted an AWS managed key as the target: " + target);
|
||||
assertEquals("AccessDeniedException", updated.getErrorCode());
|
||||
}
|
||||
|
||||
assertTrue(customerAliases(REGION).stream()
|
||||
.noneMatch(alias -> "alias/borrowed".equals(alias.getAliasName())));
|
||||
assertEquals(mine.getKeyId(),
|
||||
kmsService.describeKey("alias/mine", REGION).getKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAliasesFilteredByKeyIdReturnsOnlyThatKeysAlias() {
|
||||
String s3KeyId = kmsService.describeKey("alias/aws/s3", REGION).getKeyId();
|
||||
|
||||
List<KmsAlias> aliases = kmsService.listAliases(s3KeyId, REGION);
|
||||
|
||||
assertEquals(List.of("alias/aws/s3"), aliases.stream().map(KmsAlias::getAliasName).toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user