feat(iam): read KMS from the generated tables, its permissions from the developer guide (#9221)

KMS is read from iam-request-resources.json now, and ResourceArnBuilder's
KMS branch and the enforcement filter's call around the table are gone,
leaving dynamodb and iam in HAND_BUILT.

What botocore and the Service Reference do not say comes from the KMS
documentation, vendored by fetch_kms_api_permissions.py into
tools/aws/sources/kms-api-permissions.json and read under
PERMISSION_SOURCES: the developer guide's permissions table, the API
reference's sentence that Decrypt and ReEncrypt read their key from the
ciphertext's metadata, and the guide's key identifier formats.

- ReEncrypt is checked as kms:ReEncryptFrom on its source key, or the
  ciphertext's key without one, and kms:ReEncryptTo on its destination
  key, ReplicateKey as kms:ReplicateKey and kms:CreateKey. An operation
  checked as several actions records them (actions, action), which
  RequestResources.checkedAs reads and the filter checks.
- CreateAlias, UpdateAlias and DeleteAlias are checked on the alias and
  the keys the table names, the alias's current key through the alias.
- Decrypt with no KeyId is checked on the ciphertext's key.
- KmsStoredResourceArns finds a key in any state, uses only an enabled
  one for a forwarded request, reaches the key of an alias, a grant or
  a ciphertext, and gives a key's tags.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-22 20:28:28 -04:00
1 parent 81b25c66bc
commit 739a22ab32
19 files changed
+2256 -629

No files matched your search

@@ -189,6 +189,44 @@ class RequestResourceMembersTest {
assertThrows(IllegalStateException.class, () -> parse(clash.toString()));
}
@Test
void anOperationCheckedAsSeveralActionsHasEachResourceUnderItsAction() throws Exception {
// KMS's ReEncrypt is authorized by kms:ReEncryptFrom on its source key and kms:ReEncryptTo on its destination.
ObjectNode document = (ObjectNode) MAPPER.readTree("""
{
"namespaces": {"example": ["example"]},
"arnFormats": {"example": {"key": "arn:${Partition}:example:${Region}:${Account}:key/${KeyId}"}},
"operations": {
"example:ReEncrypt": {
"actions": {"example:ReEncryptFrom": ["key"], "example:ReEncryptTo": ["key"]},
"resources": [
{"type": "key", "fill": {"KeyId": "SourceKeyId"}, "action": "example:ReEncryptFrom"},
{"types": ["key"], "through": "CiphertextBlob", "when": {"SourceKeyId": [null]},
"action": "example:ReEncryptFrom"},
{"type": "key", "fill": {"KeyId": "DestinationKeyId"}, "action": "example:ReEncryptTo"}
]
}
}
}
""");
RequestResourceMembers.Table table = parse(document.toString());
assertEquals(List.of(), table.operations().get("example:ReEncrypt"));
Map<String, List<Resource>> checkedAs = table.checkedAs().get("example:ReEncrypt");
assertEquals(List.of("example:ReEncryptFrom", "example:ReEncryptTo"), List.copyOf(checkedAs.keySet()));
assertEquals(2, checkedAs.get("example:ReEncryptFrom").size());
assertEquals(new Member("CiphertextBlob", false), checkedAs.get("example:ReEncryptFrom").get(1).through());
assertEquals(Map.of("KeyId", new Member("DestinationKeyId", false)),
checkedAs.get("example:ReEncryptTo").getFirst().fill());
ObjectNode unrecorded = document.deepCopy();
((ObjectNode) unrecorded.path("operations").path("example:ReEncrypt").path("resources").path(0))
.put("action", "example:ReEncrypt");
assertThrows(IllegalStateException.class, () -> parse(unrecorded.toString()));
ObjectNode unchecked = document.deepCopy();
((ObjectNode) unchecked.path("operations").path("example:ReEncrypt").path("resources").path(2))
.remove("action");
assertThrows(IllegalStateException.class, () -> parse(unchecked.toString()));
}
@Test
void theTableReadsItsNamespacesAndGivesEachTypeItNamesAnArnFormat() {
RequestResourceMembers.Table table = RequestResourceMembers.table();
@@ -20,6 +20,7 @@ import java.util.UUID;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
/**
@@ -32,11 +33,12 @@ import static org.hamcrest.Matchers.equalTo;
* and a Deny scoped to one key ARN fires for that key: the gap issue #6256 describes, where
* every KMS call was evaluated against {@code key/*}.
*
* <p>The same mirror rule covers the members a handler reads that a plain {@code KeyId} does
* not name: a {@code ReEncrypt} that names no {@code SourceKeyId} is authorized against the key
* that encrypted its {@code CiphertextBlob}, never against a {@code KeyId} it carries, which
* the handler ignores, and a token-only {@code RetireGrant} against the key its grant belongs
* to, which is the key the handler retires the grant under.
* <p>The developer guide's permissions table gives the rest: {@code ReEncrypt} is checked as
* {@code kms:ReEncryptFrom} on the key it decrypts with, its {@code SourceKeyId} or else the key
* that encrypted its {@code CiphertextBlob}, and {@code kms:ReEncryptTo} on the key it encrypts
* with; {@code CreateAlias} on the alias and the key, {@code UpdateAlias} on the alias, the new
* key and the key the alias points at now, {@code DeleteAlias} on the alias and its key; and
* {@code ReplicateKey} as {@code kms:ReplicateKey} on the key and {@code kms:CreateKey}.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
@@ -144,23 +146,28 @@ class KmsKeyArnEnforcementIntegrationTest {
}
@Test
void aDecryptNamingNoKeyTheStoreHoldsFallsBackToEveryKeyAndTheHandlerRefusesIt() {
Session session = assumeRoleWith("""
void aDecryptWhoseCiphertextNamesNoKeyIsCheckedOnStar() {
Session onKeys = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:Decrypt","Resource":"%s*"}]}"""
.formatted(KEY_ARN_PREFIX));
Session onEverything = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:Decrypt","Resource":"*"}]}""");
// A ciphertext no stored key answers to names every key, so a wildcard policy still
// matches it and the handler, not enforcement, refuses it.
keyCall(session, "Decrypt", "{\"CiphertextBlob\": \"AAAA\"}")
// A ciphertext no stored key answers to names no key, so the request is checked on *, which a policy
// granting every resource covers and the handler then refuses.
keyCall(onKeys, "Decrypt", "{\"CiphertextBlob\": \"AAAA\"}")
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(onEverything, "Decrypt", "{\"CiphertextBlob\": \"AAAA\"}")
.then().statusCode(400).body("__type", equalTo("InvalidCiphertextException"));
}
/**
* A caller allowed on one key submits ciphertext another, denied key encrypted and
* carries a KeyId naming the allowed one, which the handler never reads. The source the
* request is authorized against is the key that encrypted the ciphertext, so the denied
* key's data does not come out re-encrypted under the allowed one.
* carries a KeyId naming the allowed one, which the handler never reads. The source
* {@code kms:ReEncryptFrom} is checked on is the key that encrypted the ciphertext, so the
* denied key's data does not come out re-encrypted under the allowed one.
*/
@Test
void aReEncryptIsAuthorizedAgainstTheKeyThatEncryptedTheCiphertextNotAKeyIdItCarries() {
@@ -172,8 +179,8 @@ class KmsKeyArnEnforcementIntegrationTest {
.then().statusCode(200).extract().path("CiphertextBlob");
Session session = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:ReEncrypt","Resource":"%s*"},
{"Effect":"Deny","Action":"kms:ReEncrypt","Resource":"%s"}]}"""
{"Effect":"Allow","Action":"kms:ReEncrypt*","Resource":"%s*"},
{"Effect":"Deny","Action":"kms:ReEncryptFrom","Resource":"%s"}]}"""
.formatted(KEY_ARN_PREFIX, deniedArn));
keyCall(session, "ReEncrypt",
@@ -189,43 +196,114 @@ class KmsKeyArnEnforcementIntegrationTest {
}
/**
* A token-only RetireGrant is authorized against the key the token's grant belongs to, the
* key the handler finds the grant under, so a policy allowing retirement on that key alone
* permits it and one allowing another key does not.
* ReEncrypt is checked as the two actions AWS authorizes it by, each on its own key: a
* caller allowed to re-encrypt from one key and to another may move data only that way, a
* policy naming neither is refused, and no permission named {@code kms:ReEncrypt} exists to
* grant it.
*/
@Test
void aTokenOnlyRetireGrantIsAuthorizedAgainstTheKeyItsGrantBelongsTo() {
String keyArn = createKey();
String otherArn = createKey();
Grant grant = createGrantOn(keyArn);
Session onTheGrantsKey = assumeRoleWith(grantOperationOn("kms:RetireGrant", keyArn));
Session onAnotherKey = assumeRoleWith(grantOperationOn("kms:RetireGrant", otherArn));
void aReEncryptNeedsReEncryptFromOnItsSourceAndReEncryptToOnItsDestination() {
String sourceArn = createKey();
String destinationArn = createKey();
String ciphertext = accountCall("Encrypt", encryptBody(sourceArn))
.then().statusCode(200).extract().path("CiphertextBlob");
String reEncrypt = "{\"CiphertextBlob\": \"" + ciphertext + "\", \"SourceKeyId\": \"" + sourceArn
+ "\", \"DestinationKeyId\": \"" + destinationArn + "\"}";
Session oneWay = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:ReEncryptFrom","Resource":"%s"},
{"Effect":"Allow","Action":"kms:ReEncryptTo","Resource":"%s"}]}"""
.formatted(sourceArn, destinationArn));
Session backwards = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:ReEncryptFrom","Resource":"%s"},
{"Effect":"Allow","Action":"kms:ReEncryptTo","Resource":"%s"}]}"""
.formatted(destinationArn, sourceArn));
Session byItsName = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:ReEncrypt","Resource":"*"}]}""");
keyCall(onAnotherKey, "RetireGrant", "{\"GrantToken\": \"" + grant.token() + "\"}")
keyCall(backwards, "ReEncrypt", reEncrypt)
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(onTheGrantsKey, "RetireGrant", "{\"GrantToken\": \"" + grant.token() + "\"}")
.then().statusCode(200);
accountCall("ListGrants", "{\"KeyId\": \"" + keyArn + "\"}")
.then().statusCode(200).body("Grants.size()", equalTo(0));
keyCall(byItsName, "ReEncrypt", reEncrypt)
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(oneWay, "ReEncrypt", reEncrypt)
.then().statusCode(200).body("KeyId", equalTo(destinationArn)).body("SourceKeyId", equalTo(sourceArn));
}
/** A wildcard allow with a Deny on the grant's key still fires for a token-only RetireGrant. */
/**
* A RetireGrant naming its grant by its token alone names no key the table reads, so it is
* checked on {@code *}: a policy granting retirement on every resource allows it, one scoped
* to the grant's key does not.
*/
@Test
void aDenyOnTheGrantsKeyFiresForATokenOnlyRetireGrant() {
void aTokenOnlyRetireGrantIsCheckedOnStar() {
String keyArn = createKey();
Grant grant = createGrantOn(keyArn);
Session session = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"kms:RetireGrant","Resource":"%s*"},
{"Effect":"Deny","Action":"kms:RetireGrant","Resource":"%s"}]}"""
.formatted(KEY_ARN_PREFIX, keyArn));
Grant refused = createGrantOn(keyArn);
Grant retired = createGrantOn(keyArn);
Session onTheGrantsKey = assumeRoleWith(grantOperationOn("kms:RetireGrant", keyArn));
Session onEverything = assumeRoleWith(grantOperationOn("kms:RetireGrant", "*"));
keyCall(session, "RetireGrant", "{\"GrantToken\": \"" + grant.token() + "\"}")
keyCall(onTheGrantsKey, "RetireGrant", "{\"GrantToken\": \"" + refused.token() + "\"}")
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(onEverything, "RetireGrant", "{\"GrantToken\": \"" + retired.token() + "\"}")
.then().statusCode(200);
accountCall("ListGrants", "{\"KeyId\": \"" + keyArn + "\"}")
.then().statusCode(200).body("Grants.size()", equalTo(1));
}
/**
* The alias calls need their action on the alias and on each key the permissions table
* names: CreateAlias on the key it associates, UpdateAlias on the new key and on the key the
* alias points at now, DeleteAlias on the key the alias points at.
*/
@Test
void theAliasCallsNeedTheirActionOnTheAliasAndItsKeys() {
String currentArn = createKey();
String newArn = createKey();
String aliasName = "alias/" + UUID.randomUUID().toString().substring(0, 8);
String aliasArn = ALIAS_ARN_PREFIX + aliasName.substring("alias/".length());
String create = "{\"AliasName\": \"" + aliasName + "\", \"TargetKeyId\": \"" + currentArn + "\"}";
Session aliasOnly = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["kms:CreateAlias","kms:UpdateAlias","kms:DeleteAlias"],
"Resource":"%s"}]}""".formatted(aliasArn));
Session withKeys = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["kms:CreateAlias","kms:UpdateAlias","kms:DeleteAlias"],
"Resource":["%s","%s"]}]}""".formatted(aliasArn, currentArn));
Session withBothKeys = assumeRoleWith("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["kms:UpdateAlias"],"Resource":["%s","%s","%s"]}]}"""
.formatted(aliasArn, currentArn, newArn));
keyCall(aliasOnly, "CreateAlias", create)
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(withKeys, "CreateAlias", create).then().statusCode(200);
cleanup.register(() -> accountCall("DeleteAlias", "{\"AliasName\": \"" + aliasName + "\"}"));
String update = "{\"AliasName\": \"" + aliasName + "\", \"TargetKeyId\": \"" + newArn + "\"}";
keyCall(withKeys, "UpdateAlias", update)
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(withBothKeys, "UpdateAlias", update).then().statusCode(200);
keyCall(withKeys, "DeleteAlias", "{\"AliasName\": \"" + aliasName + "\"}")
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"));
keyCall(withBothKeys, "UpdateAlias",
"{\"AliasName\": \"" + aliasName + "\", \"TargetKeyId\": \"" + currentArn + "\"}")
.then().statusCode(200);
keyCall(withKeys, "DeleteAlias", "{\"AliasName\": \"" + aliasName + "\"}").then().statusCode(200);
}
/** ReplicateKey also needs kms:CreateKey, which the permissions table lists beside kms:ReplicateKey. */
@Test
void aReplicateKeyNeedsCreateKeyBesideReplicateKey() {
String keyArn = createKey();
Session session = assumeRoleWith(grantOperationOn("kms:ReplicateKey", keyArn));
keyCall(session, "ReplicateKey", "{\"KeyId\": \"" + keyArn + "\", \"ReplicaRegion\": \"us-west-2\"}")
.then().statusCode(400).body("__type", equalTo("AccessDeniedException"))
.body("message", containsString("kms:CreateKey"));
}
/**
* A RevokeGrant naming one key while the grant belongs to another is denied: the request is
* authorized against both, so the pair cannot revoke a grant the named key does not hold.
@@ -24,6 +24,7 @@ import io.floci.aws.services.iam.model.IamUser;
import io.floci.aws.services.iam.model.InstanceProfile;
import io.floci.aws.services.iam.model.ServerCertificate;
import io.floci.aws.services.kms.KmsService;
import io.floci.aws.services.kms.KmsStoredResourceArns;
import io.floci.aws.services.lambda.LambdaStoredResourceArns;
import io.floci.aws.services.lambdamicrovms.LambdaMicrovmsService;
import io.floci.aws.services.s3.S3RequestPathLabels;
@@ -1462,12 +1463,16 @@ class ResourceArnBuilderTest {
private static final String KMS_OTHER_KEY_ARN = "arn:aws:kms:us-east-1:000000000000:key/5678efab-34cd-12ab-90ef-1234567890ab";
private static final String KMS_ALIAS_ARN = "arn:aws:kms:us-east-1:000000000000:alias/app";
/** A builder reading {@code kms}, for a key named by an id, an alias or an ARN. */
/** A builder reading {@code kms} as KMS's stored ARNs answer from it, in the caller's account. */
private static ResourceArnBuilder backedBy(KmsService kms) {
@SuppressWarnings("unchecked")
Instance<KmsService> instance = mock(Instance.class);
when(instance.get()).thenReturn(kms);
return new ResourceArnBuilder(new ObjectMapper(), null, instance);
RegionResolver regions = mock(RegionResolver.class);
when(regions.getAccountId()).thenReturn(ACCOUNT);
return reading(new KmsStoredResourceArns(kms, regions));
}
/** The resources a KMS request is checked on, as the table reads {@code operation}. */
private List<String> kms(ResourceArnBuilder builder, String operation) {
return builder.buildResources("kms", "kms:" + operation, ctx, "us-east-1", ACCOUNT, "aws");
}
/** A builder reading the stored ARNs of a namespace, for every request of that namespace. */
@@ -1475,7 +1480,7 @@ class ResourceArnBuilderTest {
@SuppressWarnings("unchecked")
Instance<StoredResourceArns> instance = mock(Instance.class);
when(instance.iterator()).thenAnswer(invocation -> List.of(stored).iterator());
return new ResourceArnBuilder(new ObjectMapper(), null, null, instance);
return new ResourceArnBuilder(new ObjectMapper(), null, instance);
}
/** The finder of the stored ARNs of a namespace, answering one name of one type with one ARN. */
@@ -1494,308 +1499,162 @@ class ResourceArnBuilderTest {
}
/**
* The gap issue 6256 describes. KMS is a JSON protocol on {@code /} with the key in the body,
* so reading a {@code /keys/<id>} path segment named nothing and every call was evaluated
* against {@code key/*}: an {@code Allow} scoped to one key ARN never matched, and a
* {@code Deny} scoped to one never fired. A key-scoped operation is now evaluated against the
* key its request names.
* The gap issue 6256 describes. KMS is a JSON protocol on {@code /} with the key in the body, so reading a
* {@code /keys/<id>} path segment named nothing and every call was evaluated against {@code key/*}. A key-scoped
* operation is checked on the key its request names, an alias name or ARN resolved to the key it points at, in
* whatever state, as KMS's handler resolves it.
*/
@Test
void kmsNamesTheKeyAKeyScopedOperationActsOn() {
void aKmsKeyScopedOperationIsCheckedOnTheKeyItsRequestNames() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArn(KMS_KEY_ID, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount(KMS_KEY_ID, ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount("alias/app", ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount(KMS_ALIAS_ARN, ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
ResourceArnBuilder backed = backedBy(kms);
for (String action : List.of("kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey",
"kms:GenerateDataKeyWithoutPlaintext", "kms:DescribeKey", "kms:GetPublicKey",
"kms:ScheduleKeyDeletion", "kms:CancelKeyDeletion", "kms:CreateGrant", "kms:ListGrants",
"kms:RevokeGrant", "kms:Sign", "kms:Verify", "kms:GenerateMac", "kms:VerifyMac",
"kms:TagResource", "kms:UntagResource", "kms:ListResourceTags", "kms:GetKeyPolicy",
"kms:PutKeyPolicy", "kms:ListKeyPolicies", "kms:UpdateKeyDescription",
"kms:EnableKey", "kms:DisableKey", "kms:EnableKeyRotation", "kms:DisableKeyRotation",
"kms:GetKeyRotationStatus", "kms:RotateKeyOnDemand", "kms:GetParametersForImport",
"kms:ImportKeyMaterial", "kms:DeleteImportedKeyMaterial", "kms:ReplicateKey",
"kms:RetireGrant")) {
for (String operation : List.of("Encrypt", "Decrypt", "GenerateDataKey", "GenerateDataKeyWithoutPlaintext",
"DescribeKey", "GetPublicKey", "ScheduleKeyDeletion", "CancelKeyDeletion", "CreateGrant", "ListGrants",
"Sign", "Verify", "GenerateMac", "VerifyMac", "TagResource", "UntagResource", "ListResourceTags",
"GetKeyPolicy", "PutKeyPolicy", "ListKeyPolicies", "UpdateKeyDescription", "EnableKey", "DisableKey",
"EnableKeyRotation", "DisableKeyRotation", "GetKeyRotationStatus", "RotateKeyOnDemand",
"GetParametersForImport", "ImportKeyMaterial", "DeleteImportedKeyMaterial", "RevokeGrant")) {
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources(action, ctx, "us-east-1", ACCOUNT, "aws"), action);
assertEquals(List.of(KMS_KEY_ARN), kms(backed, operation), operation);
}
for (String named : List.of("alias/app", KMS_ALIAS_ARN, KMS_KEY_ARN)) {
setJsonBody("{\"KeyId\":\"" + named + "\",\"Plaintext\":\"aGk=\"}");
assertEquals(List.of(KMS_KEY_ARN), kms(backed, "Encrypt"), named);
}
}
private static final String PC_KEY_ARN =
"arn:aws:payment-cryptography:us-east-1:000000000000:key/abc123";
/**
* A key named by an alias is authorized against the key it points at, as the handler resolves
* it. Otherwise an {@code Allow} scoped to {@code key/*} passes while an {@code Deny} scoped
* to the key the alias names is missed, and the caller reads or deletes a key it was denied.
*/
/** A key KMS holds none of is checked on its id in the request's partition, as KMS mints a key there. */
@Test
void paymentCryptographyResolvesAnAliasToTheKeyItPointsAt() {
ResourceArnBuilder reading = reading(stored("payment-cryptography", "key", "alias/team/card-key", PC_KEY_ARN));
// The route its resources are read from is the one the request's path matches, so the
// request names its method and its URI.
when(ctx.getMethod()).thenReturn("POST");
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566/"));
when(uriInfo.getBaseUri()).thenReturn(URI.create("http://localhost:4566/"));
setJsonBody("{\"KeyIdentifier\":\"alias/team/card-key\"}");
assertEquals(List.of(PC_KEY_ARN),
reading.buildResources("payment-cryptography", "payment-cryptography:GetKey", ctx,
"us-east-1", ACCOUNT, "aws"));
}
/** A key named by an alias is authorized against the key it points at, as the handler resolves it. */
@Test
void kmsResolvesAnAliasToTheKeyItPointsAt() {
void aKmsKeyNoneHoldsIsCheckedOnItsIdInTheRequestsPartition() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn("alias/app", "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArn(KMS_ALIAS_ARN, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"KeyId\":\"alias/app\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"KeyId\":\"" + KMS_ALIAS_ARN + "\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
}
/**
* The operations the KMS permissions reference gives no resource, which it also requires a
* policy to name {@code *} for, stay at the wildcard; a key-scoped operation that names no
* key keeps the permissive fallback rather than being refused here, since the handler
* refuses it with its own error.
*/
@Test
void kmsLeavesTheActionsThatNameNoKeyAtTheirOwnResources() {
ResourceArnBuilder backed = backedBy(mock(KmsService.class));
for (String action : List.of("kms:CreateKey", "kms:GenerateRandom", "kms:ListKeys",
"kms:ListAliases", "kms:ListRetirableGrants")) {
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of("*"),
backed.buildKmsResources(action, ctx, "us-east-1", ACCOUNT, "aws"), action);
}
setJsonBody("{}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:RetireGrant", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of("arn:aws-cn:kms:cn-north-1:000000000000:key/*"),
backed.buildKmsResources("kms:RetireGrant", ctx, "cn-north-1", ACCOUNT, "aws-cn"));
}
/**
* A request with no body at all keeps the action's own resources: the operations the reference
* gives no resource stay at {@code *}, and a key-scoped one falls back to every key the
* account holds rather than being refused here.
*/
@Test
void kmsKeepsEachActionsOwnResourcesForARequestWithNoBody() {
ResourceArnBuilder backed = backedBy(mock(KmsService.class));
assertEquals(List.of("*"),
backed.buildKmsResources("kms:CreateKey", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
}
/**
* A key ARN the account holds nothing of is kept as the caller spelled it, a bare key id is
* minted in the request's partition, and an alias that resolves to nothing names every key:
* the operation fails before a policy matters.
*/
@Test
void kmsMintsKeyArnsInTheRequestPartitionAndFallsBackForTheUnresolvable() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ARN + "\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of("arn:aws-cn:kms:cn-north-1:000000000000:key/" + KMS_KEY_ID),
backed.buildKmsResources("kms:Encrypt", ctx, "cn-north-1", ACCOUNT, "aws-cn"));
setJsonBody("{\"KeyId\":\"alias/gone\"}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
backedBy(kms).buildResources("kms", "kms:Encrypt", ctx, "cn-north-1", ACCOUNT, "aws-cn"));
}
/**
* A {@code Decrypt} names its key in an optional {@code KeyId}; without one the key is the
* one that encrypted the {@code CiphertextBlob}, read the same way the handler reads it.
* The calls whose actions the Service Reference gives no resource type, the custom key store ones among them,
* are checked on {@code *}, as AWS checks them.
*/
@Test
void kmsDecryptResolvesTheKeyFromTheCiphertextItNames() {
void theKmsCallsThatNameNoKeyAreCheckedOnStar() {
ResourceArnBuilder backed = backedBy(mock(KmsService.class));
setJsonBody("{\"CustomKeyStoreId\":\"cks-1234567890abcdef0\"}");
for (String operation : List.of("CreateKey", "GenerateRandom", "ListKeys", "ListAliases",
"ListRetirableGrants", "ConnectCustomKeyStore", "DescribeCustomKeyStores", "DeleteCustomKeyStore")) {
assertEquals(List.of("*"), kms(backed, operation), operation);
}
}
/**
* A Decrypt naming no key is checked on the key its ciphertext was encrypted under, which the API reference says
* KMS reads from the blob's metadata; one naming a key on that key; one whose blob names none KMS holds on
* {@code *}.
*/
@Test
void decryptIsCheckedOnTheKeyOfItsCiphertextWhenItNamesNone() {
byte[] blob = "ciphertext".getBytes(StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(blob);
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount("alias/app", ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
when(kms.decryptToKeyArn(blob, "us-east-1")).thenReturn(KMS_KEY_ARN);
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"CiphertextBlob\":\"" + encoded + "\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findKeyArn("alias/app", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
setJsonBody("{\"KeyId\":\"alias/app\",\"CiphertextBlob\":\"" + encoded + "\"}");
assertEquals(List.of(KMS_OTHER_KEY_ARN),
backed.buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
KmsService unresolvable = mock(KmsService.class);
when(unresolvable.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(unresolvable.decryptToKeyArn(any(), any())).thenReturn(null);
setJsonBody("{\"CiphertextBlob\":\"" + encoded + "\"}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backedBy(unresolvable).buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"CiphertextBlob\":629}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"CiphertextBlob\":{\"KeyId\":\"x\"}}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"CiphertextBlob\":[\"aGk=\"]}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:Decrypt", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of(KMS_KEY_ARN), kms(backed, "Decrypt"));
setJsonBody("{\"CiphertextBlob\":\"" + encoded + "\",\"KeyId\":\"alias/app\"}");
assertEquals(List.of(KMS_OTHER_KEY_ARN), kms(backed, "Decrypt"));
setJsonBody("{\"CiphertextBlob\":\"not base64!\"}");
assertEquals(List.of("*"), kms(backed, "Decrypt"));
}
/**
* The alias operations name their resources as the alias permissions page gives them:
* {@code CreateAlias} the alias and the key, {@code UpdateAlias} the alias, the key it points
* at and the key it will point at, and {@code DeleteAlias} the alias and the key it points
* at. A request naming no alias AWS would accept the name of names nothing.
* The alias calls are checked on the alias and the keys the developer guide's permissions table names: CreateAlias
* the key it associates, UpdateAlias the newly and the currently associated key, DeleteAlias the key its alias is
* associated with.
*/
@Test
void kmsAliasOperationsNameTheAliasAndTheKeysTheyTouch() {
void theKmsAliasCallsAreCheckedOnTheAliasAndItsKeys() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArn(KMS_KEY_ID, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount(KMS_KEY_ID, ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArn("alias/app", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"AliasName\":\"alias/app\",\"TargetKeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of(KMS_ALIAS_ARN, KMS_KEY_ARN),
backed.buildKmsResources("kms:CreateAlias", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"AliasName\":\"alias/app\",\"TargetKeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of(KMS_ALIAS_ARN, KMS_OTHER_KEY_ARN, KMS_KEY_ARN),
backed.buildKmsResources("kms:UpdateAlias", ctx, "us-east-1", ACCOUNT, "aws"));
// A retarget onto the key the alias already points at names that key once.
when(kms.findKeyArn("alias/app", "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
setJsonBody("{\"AliasName\":\"alias/app\",\"TargetKeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of(KMS_ALIAS_ARN, KMS_KEY_ARN),
backed.buildKmsResources("kms:UpdateAlias", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findKeyArn("alias/app", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
assertEquals(List.of(KMS_ALIAS_ARN, KMS_KEY_ARN), kms(backed, "CreateAlias"));
assertEquals(List.of(KMS_ALIAS_ARN, KMS_KEY_ARN, KMS_OTHER_KEY_ARN), kms(backed, "UpdateAlias"));
setJsonBody("{\"AliasName\":\"alias/app\"}");
assertEquals(List.of(KMS_ALIAS_ARN, KMS_OTHER_KEY_ARN),
backed.buildKmsResources("kms:DeleteAlias", ctx, "us-east-1", ACCOUNT, "aws"));
// A name AWS would refuse contributes no alias resource, and a request naming no alias
// at all names nothing but the wildcard.
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
setJsonBody("{\"AliasName\":\"app\",\"TargetKeyId\":\"" + KMS_KEY_ID + "\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:CreateAlias", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{}");
assertEquals(List.of("*"),
backed.buildKmsResources("kms:DeleteAlias", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of(KMS_ALIAS_ARN, KMS_OTHER_KEY_ARN), kms(backed, "DeleteAlias"));
}
/**
* A {@code ReEncrypt} acts on two keys, and the filter authorizes a request once per
* resource, so both are named: the source the ciphertext or its optional {@code SourceKeyId}
* resolves to, and the destination its {@code DestinationKeyId} names.
* ReEncrypt is checked as the two actions the permissions table gives it, never as its own name:
* {@code kms:ReEncryptFrom} on the key it decrypts with (its {@code SourceKeyId}, or else the key of its
* ciphertext) and {@code kms:ReEncryptTo} on the key it encrypts with.
*/
@Test
void kmsReEncryptNamesBothKeysItActsOn() {
void reEncryptIsCheckedAsReEncryptFromOnItsSourceAndReEncryptToOnItsDestination() {
byte[] blob = "ciphertext".getBytes(StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(blob);
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.decryptToKeyArn(blob, "us-east-1")).thenReturn(KMS_KEY_ARN);
when(kms.findKeyArn("alias/destination", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount("alias/source", ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
when(kms.findKeyArnOfAccount("alias/destination", ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.decryptToKeyArn(blob, "us-east-1")).thenReturn(KMS_OTHER_KEY_ARN);
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"CiphertextBlob\":\"" + encoded + "\",\"DestinationKeyId\":\"alias/destination\"}");
assertEquals(List.of(KMS_KEY_ARN, KMS_OTHER_KEY_ARN),
backed.buildKmsResources("kms:ReEncrypt", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findKeyArn("alias/source", "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
setJsonBody("{\"SourceKeyId\":\"alias/source\",\"DestinationKeyId\":\"alias/destination\"}");
assertEquals(List.of(KMS_KEY_ARN, KMS_OTHER_KEY_ARN),
backed.buildKmsResources("kms:ReEncrypt", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(Map.of("kms:ReEncryptFrom", List.of(KMS_OTHER_KEY_ARN), "kms:ReEncryptTo", List.of(KMS_KEY_ARN)),
backed.checkedAs("kms", "kms:ReEncrypt", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"CiphertextBlob\":\"" + encoded + "\",\"SourceKeyId\":\"alias/source\","
+ "\"DestinationKeyId\":\"alias/destination\"}");
assertEquals(List.of("kms:ReEncryptFrom", "kms:ReEncryptTo"),
List.copyOf(backed.checkedAs("kms", "kms:ReEncrypt", ctx, "us-east-1", ACCOUNT, "aws").keySet()));
assertEquals(List.of("*"), kms(backed, "ReEncrypt"));
assertEquals(Map.of(), backed.checkedAs("kms", "kms:Encrypt", ctx, "us-east-1", ACCOUNT, "aws"));
}
/**
* A {@code ReEncrypt} never reads a {@code KeyId}: its source is the key that encrypted the
* ciphertext when the request names no {@code SourceKeyId}, so a {@code KeyId} naming
* another key cannot choose the key the request is authorized against. A caller allowed
* on the claimed key only could otherwise submit another key's ciphertext and have it
* re-encrypted under the allowed one.
* ReplicateKey is checked as the two permissions the table gives it: {@code kms:ReplicateKey} on the primary key
* and {@code kms:CreateKey} on no resource.
*/
@Test
void kmsReEncryptNamesTheSourceTheHandlerDecryptsWithNotAKeyId() {
byte[] blob = "ciphertext".getBytes(StandardCharsets.UTF_8);
String encoded = Base64.getEncoder().encodeToString(blob);
void replicateKeyIsCheckedAsReplicateKeyOnItsKeyAndCreateKeyOnStar() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.decryptToKeyArn(blob, "us-east-1")).thenReturn(KMS_KEY_ARN);
when(kms.findKeyArn("alias/claimed", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"CiphertextBlob\":\"" + encoded
+ "\",\"KeyId\":\"alias/claimed\",\"DestinationKeyId\":\"" + KMS_OTHER_KEY_ARN + "\"}");
assertEquals(List.of(KMS_KEY_ARN, KMS_OTHER_KEY_ARN),
backed.buildKmsResources("kms:ReEncrypt", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findKeyArnOfAccount(KMS_KEY_ID, ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\",\"ReplicaRegion\":\"us-west-2\"}");
assertEquals(Map.of("kms:ReplicateKey", List.of(KMS_KEY_ARN), "kms:CreateKey", List.of("*")),
backedBy(kms).checkedAs("kms", "kms:ReplicateKey", ctx, "us-east-1", ACCOUNT, "aws"));
}
/**
* A grant operation names the key the grant it acts on belongs to: a token-only
* {@code RetireGrant} the key the token's grant belongs to, since the handler finds the
* grant by the token alone, and a {@code RetireGrant} or a {@code RevokeGrant} identifying
* its grant by {@code KeyId} and {@code GrantId} the key it names together with that grant's
* key. A token naming no grant the account holds keeps the permissive fallback.
* The grant calls are checked on the key they name and on the key the grant their {@code GrantId} names is on,
* which closes naming one key while revoking another key's grant. A RetireGrant naming its grant by token alone
* names neither, as botocore's record of a grant holds no token: it is checked on {@code *}.
*/
@Test
void kmsGrantOperationsNameTheKeyTheGrantTheyActOnBelongsTo() {
void theKmsGrantCallsAreCheckedOnTheirKeyAndTheKeyOfTheirGrant() {
KmsService kms = mock(KmsService.class);
when(kms.findKeyArn(any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArn(KMS_KEY_ID, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArn("other-key", "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
when(kms.findGrantKeyArn("grant-token", null, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount(any(), any(), any())).thenReturn(Optional.empty());
when(kms.findKeyArnOfAccount(KMS_KEY_ID, ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findKeyArnOfAccount("other-key", ACCOUNT, "us-east-1")).thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
when(kms.findGrantKeyArn(null, "grant-id", "us-east-1")).thenReturn(Optional.of(KMS_KEY_ARN));
when(kms.findGrantKeyArn("unknown-token", null, "us-east-1")).thenReturn(Optional.empty());
ResourceArnBuilder backed = backedBy(kms);
setJsonBody("{\"GrantToken\":\"grant-token\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:RetireGrant", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"GrantToken\":\"unknown-token\"}");
assertEquals(List.of("arn:aws:kms:us-east-1:000000000000:key/*"),
backed.buildKmsResources("kms:RetireGrant", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\",\"GrantId\":\"grant-id\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:RetireGrant", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:RevokeGrant", ctx, "us-east-1", ACCOUNT, "aws"));
setJsonBody("{\"KeyId\":\"other-key\",\"GrantId\":\"grant-id\"}");
assertEquals(List.of(KMS_OTHER_KEY_ARN, KMS_KEY_ARN),
backed.buildKmsResources("kms:RevokeGrant", ctx, "us-east-1", ACCOUNT, "aws"));
when(kms.findGrantKeyArn("grant-token", "grant-id", "us-east-1"))
.thenReturn(Optional.of(KMS_OTHER_KEY_ARN));
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\",\"GrantId\":\"grant-id\",\"GrantToken\":\"grant-token\"}");
assertEquals(List.of(KMS_KEY_ARN),
backed.buildKmsResources("kms:RevokeGrant", ctx, "us-east-1", ACCOUNT, "aws"));
assertEquals(List.of(KMS_OTHER_KEY_ARN, KMS_KEY_ARN), kms(backed, "RevokeGrant"));
assertEquals(List.of(KMS_OTHER_KEY_ARN, KMS_KEY_ARN), kms(backed, "RetireGrant"));
setJsonBody("{\"KeyId\":\"" + KMS_KEY_ID + "\",\"GrantId\":\"grant-id\"}");
assertEquals(List.of(KMS_KEY_ARN), kms(backed, "RevokeGrant"));
setJsonBody("{\"GrantToken\":\"grant-token\"}");
assertEquals(List.of("*"), kms(backed, "RetireGrant"));
}
// ── SSM ──────────────────────────────────────────────────────────────────────
@@ -1971,7 +1830,7 @@ class ResourceArnBuilderTest {
Instance<RequestPathLabels> labels = mock(Instance.class);
when(labels.isUnsatisfied()).thenReturn(false);
when(labels.iterator()).thenAnswer(invocation -> List.of(s3Keys).iterator());
return new ResourceArnBuilder(new ObjectMapper(), null, null, null, labels);
return new ResourceArnBuilder(new ObjectMapper(), null, null, labels);
}
/** A current request whose path is {@code sentPath}, the path the server received. */
@@ -9,7 +9,10 @@ import io.floci.aws.services.kms.model.KmsKey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -96,18 +99,51 @@ class KmsKeyOfAccountTest {
assertFalse(stored.grantsItsAccountThroughServices(key.getArn()));
}
/** A key KMS would refuse a decrypt with, disabled or pending deletion, is none a forwarded request finds. */
/**
* A request naming an alias, a grant or a ciphertext reaches the key the alias points at, the grant is on or the
* ciphertext was encrypted under; a value naming none reaches nothing.
*/
@Test
void theStoredResourceArnsFindNoKeyKmsWouldNotUse() {
void theStoredResourceArnsReachTheKeyOfAnAliasAGrantOrACiphertext() {
KmsStoredResourceArns stored = new KmsStoredResourceArns(owners, new RegionResolver(REGION, OWNER));
String grantId = owners.createGrant(key.getKeyId(), "arn:aws:iam::" + OWNER + ":user/grantee",
List.of("Encrypt"), REGION).getGrantId();
String ciphertext = Base64.getEncoder().encodeToString(
owners.encrypt(key.getKeyId(), "plaintext".getBytes(StandardCharsets.UTF_8), REGION));
assertEquals(List.of(key.getArn()), stored.through("AliasName", "alias/snapshots", REGION));
assertEquals(List.of(key.getArn()), stored.through("GrantId", grantId, REGION));
assertEquals(List.of(key.getArn()), stored.through("CiphertextBlob", ciphertext, REGION));
assertEquals(List.of(), stored.through("AliasName", "alias/none", REGION));
assertEquals(List.of(), stored.through("CiphertextBlob", "not base64!", REGION));
assertEquals(List.of(), stored.through("KeyId", key.getKeyId(), REGION));
}
/** The tags of the key an ARN names, a tag without a value as an empty one; none for an alias. */
@Test
void theStoredResourceArnsGiveTheTagsOfAKey() {
KmsStoredResourceArns stored = new KmsStoredResourceArns(owners, new RegionResolver(REGION, OWNER));
KmsKey tagged = owners.createKey("tagged key", null, Map.of("team", "blue"), REGION);
assertEquals(Map.of("team", "blue"), stored.storedTags(tagged.getArn()));
assertEquals(Map.of(), stored.storedTags("arn:aws:kms:" + REGION + ":" + OWNER + ":alias/snapshots"));
}
/**
* A key disabled or pending deletion is still the key a request naming it acts on, but none KMS uses for a request
* another service forwards, which KMS refuses with it.
*/
@Test
void theStoredResourceArnsFindAKeyInAnyStateAndUseOnlyAnEnabledOne() {
KmsStoredResourceArns stored = new KmsStoredResourceArns(owners, new RegionResolver(REGION, OWNER));
owners.disableKey(key.getKeyId(), REGION);
assertEquals(Optional.empty(), stored.find("key", List.of(key.getKeyId()), REGION));
owners.enableKey(key.getKeyId(), REGION);
assertEquals(Optional.of(key.getArn()), stored.find("key", List.of(key.getKeyId()), REGION));
assertEquals(Optional.of(key.getArn()), stored.find("key", List.of("alias/snapshots"), REGION));
assertFalse(stored.usable(key.getArn()));
owners.enableKey(key.getKeyId(), REGION);
assertTrue(stored.usable(key.getArn()));
owners.scheduleKeyDeletion(key.getKeyId(), 7, REGION);
assertEquals(Optional.empty(), stored.find("key", List.of(key.getKeyId()), REGION));
// Its lookup still finds it: only a request KMS would serve with it is refused.
assertEquals(Optional.of(key.getArn()), owners.findKeyArnOfAccount(key.getKeyId(), OWNER, REGION));
assertEquals(Optional.of(key.getArn()), stored.find("key", List.of(key.getKeyId()), REGION));
assertFalse(stored.usable(key.getArn()));
}
/**