mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -0,0 +1,117 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Catalog of the AWS managed KMS keys every account has, loaded from
|
||||
* {@code kms/aws-managed-keys.yaml}.
|
||||
*
|
||||
* <p>These keys are reference data no customer creates: AWS ships one per service per region
|
||||
* and publishes it under the reserved {@code alias/aws/<service>} alias. Nothing in a Terraform
|
||||
* or CloudFormation template declares them, which is exactly why their absence is invisible to
|
||||
* dependency analysis and fatal at plan time: a module that leaves a service's encryption at its
|
||||
* default reads {@code alias/aws/s3} or {@code alias/aws/ebs} through a data source, and that
|
||||
* lookup succeeds against any real account.
|
||||
*
|
||||
* <p>The catalog is data rather than a list in Java so that adding a service is a line in the
|
||||
* YAML. It is parsed once into an immutable list. The YAML is read through a constant, which
|
||||
* Quarkus's native resource analysis cannot follow, so it is registered under
|
||||
* {@code quarkus.native.resources.includes} in {@code application.yml}.
|
||||
*/
|
||||
final class AwsManagedKeys {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AwsManagedKeys.class);
|
||||
|
||||
/** The alias prefix AWS reserves for its own keys. A customer alias may not use it. */
|
||||
static final String RESERVED_ALIAS_PREFIX = "alias/aws/";
|
||||
|
||||
private static final String CATALOG_RESOURCE_NAME = "kms/aws-managed-keys.yaml";
|
||||
|
||||
/**
|
||||
* One AWS managed key.
|
||||
*
|
||||
* @param aliasName the reserved alias AWS publishes the key under, e.g. {@code alias/aws/s3}
|
||||
* @param description the description DescribeKey reports for it
|
||||
*/
|
||||
record AwsManagedKeyDef(String aliasName, String description) {
|
||||
}
|
||||
|
||||
static final List<AwsManagedKeyDef> KEYS = load();
|
||||
|
||||
private AwsManagedKeys() {
|
||||
}
|
||||
|
||||
/** Whether an alias name falls in the {@code alias/aws/} namespace AWS reserves for itself. */
|
||||
static boolean isReservedAlias(String aliasName) {
|
||||
return aliasName != null && aliasName.startsWith(RESERVED_ALIAS_PREFIX);
|
||||
}
|
||||
|
||||
private static List<AwsManagedKeyDef> load() {
|
||||
try (InputStream in = Thread.currentThread().getContextClassLoader()
|
||||
.getResourceAsStream(CATALOG_RESOURCE_NAME)) {
|
||||
if (in == null) {
|
||||
throw new IllegalStateException(
|
||||
"AWS managed key catalog not found on the classpath: " + CATALOG_RESOURCE_NAME);
|
||||
}
|
||||
Catalog catalog = new ObjectMapper(new YAMLFactory()).readValue(in, Catalog.class);
|
||||
List<AwsManagedKeyDef> defs = new ArrayList<>();
|
||||
Set<String> seen = new LinkedHashSet<>();
|
||||
Set<String> seenDescriptions = new LinkedHashSet<>();
|
||||
for (CatalogEntry entry : catalog.keys == null ? List.<CatalogEntry>of() : catalog.keys) {
|
||||
if (entry.alias == null || entry.alias.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String alias = entry.alias.trim();
|
||||
if (!isReservedAlias(alias)) {
|
||||
// A name outside the reserved namespace would mint a key an ordinary customer
|
||||
// alias could then collide with. Drop it rather than seed it.
|
||||
LOG.warnv("Ignoring AWS managed key {0}: the alias is not under {1}",
|
||||
alias, RESERVED_ALIAS_PREFIX);
|
||||
continue;
|
||||
}
|
||||
if (!seen.add(alias)) {
|
||||
LOG.warnv("Ignoring duplicate AWS managed key entry for {0}", alias);
|
||||
continue;
|
||||
}
|
||||
String description = entry.description == null ? "" : entry.description;
|
||||
if (!seenDescriptions.add(description)) {
|
||||
// Surviving keys are re-aliased by description, so a shared one could hand
|
||||
// a key the other entry's alias.
|
||||
LOG.warnv("Ignoring AWS managed key {0}: its description duplicates another entry", alias);
|
||||
continue;
|
||||
}
|
||||
defs.add(new AwsManagedKeyDef(alias, description));
|
||||
}
|
||||
LOG.debugv("Loaded {0} AWS managed keys from {1}", defs.size(), CATALOG_RESOURCE_NAME);
|
||||
return List.copyOf(defs);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(
|
||||
"Failed to read the AWS managed key catalog: " + CATALOG_RESOURCE_NAME, e);
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
static final class Catalog {
|
||||
public List<CatalogEntry> keys;
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
static final class CatalogEntry {
|
||||
public String alias;
|
||||
public String description;
|
||||
}
|
||||
}
|
||||
@@ -771,7 +771,7 @@ public class KmsJsonHandler {
|
||||
if (k.getValidTo() > 0) {
|
||||
keyMetadata.put("ValidTo", k.getValidTo());
|
||||
}
|
||||
keyMetadata.put("KeyManager", "CUSTOMER");
|
||||
keyMetadata.put("KeyManager", k.getKeyManager());
|
||||
keyMetadata.put("CustomerMasterKeySpec", k.getKeySpec().name());
|
||||
keyMetadata.put("KeySpec", k.getKeySpec().name());
|
||||
keyMetadata.put("MultiRegion", k.isMultiRegion());
|
||||
|
||||
@@ -43,6 +43,7 @@ public class KmsService implements ResourceProvider {
|
||||
private static final Logger LOG = Logger.getLogger(KmsService.class);
|
||||
|
||||
private static final String AWS_KMS_ORIGIN = "AWS_KMS";
|
||||
private static final String AWS_KEY_MANAGER = "AWS";
|
||||
private static final String EXTERNAL_ORIGIN = "EXTERNAL";
|
||||
private static final String PENDING_IMPORT = "PendingImport";
|
||||
private static final String PENDING_DELETION = "PendingDeletion";
|
||||
@@ -63,6 +64,9 @@ public class KmsService implements ResourceProvider {
|
||||
// Guards the check-generate-put sequence in ensureBackingKeyMaterial so two concurrent
|
||||
// first uses of the same legacy key cannot each mint a different backing key.
|
||||
private final Object backingKeyMaterialLock = new Object();
|
||||
// Guards the scan-and-mint sequence in ensureAwsManagedKeys so two concurrent first reads of
|
||||
// a region's alias namespace cannot each mint a key for the same alias/aws/* name.
|
||||
private final Object awsManagedKeyLock = new Object();
|
||||
|
||||
@Inject
|
||||
public KmsService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
@@ -366,6 +370,11 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public List<KmsKey> listKeys(String region) {
|
||||
// Real AWS returns the AWS managed keys from ListKeys unconditionally. Without this, a
|
||||
// caller that filters on KeyManager (cloud-nuke reads ListKeys, then DescribeKey, then
|
||||
// skips anything that is not CUSTOMER) would see them or not depending on whether
|
||||
// something else had read the alias namespace earlier in the same process.
|
||||
ensureAwsManagedKeys(region);
|
||||
String prefix = region + "::";
|
||||
return keyStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
@@ -491,6 +500,9 @@ public class KmsService implements ResourceProvider {
|
||||
validateGrantConstraints(constraints);
|
||||
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
// An AWS managed key's policy allows kms:CreateGrant only through the service that owns
|
||||
// it (a kms:ViaService condition), never to the account directly.
|
||||
requireCustomerManaged(key, "kms:CreateGrant");
|
||||
requireNotPendingDeletion(key);
|
||||
String grantId = UUID.randomUUID().toString();
|
||||
byte[] tokenBytes = new byte[32];
|
||||
@@ -684,6 +696,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void scheduleKeyDeletion(String keyId, int pendingWindowInDays, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:ScheduleKeyDeletion");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setKeyState("PendingDeletion");
|
||||
key.setDeletionDate(Instant.now().plusSeconds((long) pendingWindowInDays * 86400).getEpochSecond());
|
||||
@@ -697,6 +710,7 @@ public class KmsService implements ResourceProvider {
|
||||
*/
|
||||
public void cancelKeyDeletion(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:CancelKeyDeletion");
|
||||
if (lacksImportedKeyMaterial(key)) {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
@@ -721,6 +735,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void putKeyPolicy(String keyId, String policy, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:PutKeyPolicy");
|
||||
key.setPolicy(policy);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
@@ -749,6 +764,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void updateKeyDescription(String keyId, String description, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:UpdateKeyDescription");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setDescription(description);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
@@ -768,6 +784,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void enableKeyRotation(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
validateRotationKeySpec(key);
|
||||
@@ -778,6 +795,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void disableKeyRotation(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
key.setKeyRotationEnabled(false);
|
||||
@@ -787,6 +805,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void enableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(true);
|
||||
@@ -797,6 +816,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void disableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(false);
|
||||
@@ -809,6 +829,7 @@ public class KmsService implements ResourceProvider {
|
||||
public String rotateKeyOnDemand(String keyId, String region) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:RotateKeyOnDemand");
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
if (MULTI_REGION_REPLICA.equals(key.getMultiRegionKeyType())) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
@@ -1163,13 +1184,178 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────── AWS Managed Keys ───────────────────────
|
||||
|
||||
/**
|
||||
* Materializes this region's AWS managed keys, the per-service keys every account has under
|
||||
* the reserved {@code alias/aws/*} aliases.
|
||||
*
|
||||
* <p>Nothing creates these: AWS ships them, and a module that leaves a service's encryption at
|
||||
* its default resolves one through a data source. Without them a lookup that succeeds against
|
||||
* any real account fails here, which is what makes their absence expensive: no template
|
||||
* declares them, so no dependency analysis reports them missing.
|
||||
*
|
||||
* <p>They are minted on first read of a region's key or alias namespace rather than at
|
||||
* startup, which keeps an unused region free and mirrors AWS, where the key for a service
|
||||
* appears once the account first uses that service. The entry points are the three reads of
|
||||
* those namespaces: {@link #listKeys}, {@link #listAliases(String, String)} and
|
||||
* {@link #resolveKey} for an identifier naming a reserved alias. The keys are written to the
|
||||
* key store rather than held beside it as the IAM managed policy catalog is, because unlike a
|
||||
* policy document a key carries per-account state: an id, an ARN and backing key material
|
||||
* that ciphertext is bound to. A few dozen keys of state is a different proposition from a
|
||||
* megabyte of policy documents.
|
||||
*
|
||||
* <p>An entry is skipped only when its alias is present <em>and</em> the key that alias
|
||||
* targets is present. Gating on the alias alone is not enough, because the two live in
|
||||
* different files and {@code PersistentStorage} quarantines an unreadable one and starts that
|
||||
* store empty on its own: a lost key store would leave every alias dangling for the life of
|
||||
* the instance, with {@code DescribeKey alias/aws/s3} reporting NotFoundException while
|
||||
* {@code ListAliases} still advertised it. The catalog is checked into the repository, so this
|
||||
* is the one part of a damaged store that can rebuild itself, and it does. This follows
|
||||
* {@link #ensureBackingKeyMaterial}, which self-heals a key persisted before the backing-key
|
||||
* envelope existed the same way.
|
||||
*
|
||||
* <p>Both stores are written once, as a batch, rather than per entry: {@code PersistentStorage}
|
||||
* serialises its whole map on every {@code put}, so per-entry writes cost two file rewrites per
|
||||
* catalog entry, on the first read of every region, while holding this lock. The aliases are
|
||||
* written before the keys deliberately, for the case of a crash between the two batches: it
|
||||
* then leaves aliases whose target is missing, which the gate above repairs on the next read,
|
||||
* where the other order would leave keys with no alias that sit in ListKeys forever. The order
|
||||
* does not cover {@code kms-aliases.json} alone being lost; for that, a surviving AWS managed
|
||||
* key no reserved alias targets is re-aliased by description rather than minted again. A
|
||||
* reserved alias whose target is a customer key (one created before the namespace was
|
||||
* reserved) counts as missing and is re-pointed.
|
||||
*/
|
||||
private void ensureAwsManagedKeys(String region) {
|
||||
synchronized (awsManagedKeyLock) {
|
||||
Map<String, KmsKey> newKeys = new LinkedHashMap<>();
|
||||
Map<String, KmsAlias> newAliases = new LinkedHashMap<>();
|
||||
Map<String, KmsKey> orphans = null;
|
||||
for (AwsManagedKeys.AwsManagedKeyDef def : AwsManagedKeys.KEYS) {
|
||||
String aliasStorageKey = region + "::" + def.aliasName();
|
||||
if (resolvesToAnAwsManagedKey(aliasStore.get(aliasStorageKey), region)) {
|
||||
continue;
|
||||
}
|
||||
if (orphans == null) {
|
||||
orphans = unaliasedAwsManagedKeysByDescription(region);
|
||||
}
|
||||
KmsKey orphan = orphans.remove(def.description());
|
||||
if (orphan != null) {
|
||||
newAliases.put(aliasStorageKey, new KmsAlias(def.aliasName(),
|
||||
regionResolver.buildArn("kms", region, def.aliasName()), orphan.getKeyId()));
|
||||
continue;
|
||||
}
|
||||
String keyId = UUID.randomUUID().toString();
|
||||
KmsKey key = new KmsKey();
|
||||
key.setKeyId(keyId);
|
||||
key.setArn(regionResolver.buildArn("kms", region, "key/" + keyId));
|
||||
key.setDescription(def.description());
|
||||
key.setKeyManager(AWS_KEY_MANAGER);
|
||||
key.setPolicy(buildDefaultKeyPolicy());
|
||||
// AWS rotates its own keys yearly and the customer cannot turn that off, so
|
||||
// GetKeyRotationStatus reports true for every AWS managed key.
|
||||
key.setKeyRotationEnabled(true);
|
||||
generateKeyMaterial(key, region);
|
||||
newKeys.put(region + "::" + keyId, key);
|
||||
newAliases.put(aliasStorageKey, new KmsAlias(def.aliasName(),
|
||||
regionResolver.buildArn("kms", region, def.aliasName()), keyId));
|
||||
}
|
||||
if (newAliases.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
aliasStore.putAll(newAliases);
|
||||
keyStore.putAll(newKeys);
|
||||
LOG.debugv("Minted {0} AWS managed keys in {1}", newKeys.size(), region);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a reserved alias already points at an AWS managed key. Presence of the target alone
|
||||
* is not enough: before {@code alias/aws/*} was reserved a customer could create
|
||||
* {@code alias/aws/s3} on their own key, and a store persisted then would otherwise keep that
|
||||
* customer key behind a reserved alias, where the AWS managed key guards do not apply.
|
||||
*/
|
||||
private boolean resolvesToAnAwsManagedKey(Optional<KmsAlias> alias, String region) {
|
||||
return alias.filter(a -> a.getTargetKeyId() != null)
|
||||
.flatMap(a -> keyStore.get(region + "::" + a.getTargetKeyId()))
|
||||
.map(k -> AWS_KEY_MANAGER.equals(k.getKeyManager()))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS managed keys no reserved alias targets, keyed by description. They are what a lost
|
||||
* {@code kms-aliases.json} leaves behind; re-aliasing them instead of minting keeps ciphertext
|
||||
* bound to them decryptable through the alias and keeps ListKeys free of duplicates. The
|
||||
* catalog's descriptions are unique per service, which is what makes them usable as the key.
|
||||
*/
|
||||
private Map<String, KmsKey> unaliasedAwsManagedKeysByDescription(String region) {
|
||||
String prefix = region + "::";
|
||||
Set<String> aliased = new HashSet<>();
|
||||
for (KmsAlias a : aliasStore.scan(k -> k.startsWith(prefix + AwsManagedKeys.RESERVED_ALIAS_PREFIX))) {
|
||||
aliased.add(a.getTargetKeyId());
|
||||
}
|
||||
Map<String, KmsKey> byDescription = new HashMap<>();
|
||||
for (KmsKey k : keyStore.scan(key -> key.startsWith(prefix))) {
|
||||
if (AWS_KEY_MANAGER.equals(k.getKeyManager()) && !aliased.contains(k.getKeyId())) {
|
||||
byDescription.putIfAbsent(k.getDescription(), k);
|
||||
}
|
||||
}
|
||||
return byDescription;
|
||||
}
|
||||
|
||||
private static boolean referencesReservedAlias(String keyIdOrArn) {
|
||||
return keyIdOrArn != null
|
||||
&& (keyIdOrArn.startsWith(AwsManagedKeys.RESERVED_ALIAS_PREFIX)
|
||||
|| keyIdOrArn.contains(":" + AwsManagedKeys.RESERVED_ALIAS_PREFIX));
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses an operation that only a key's owner may perform when the key is AWS managed.
|
||||
*
|
||||
* <p>AWS states it plainly: you cannot manage an AWS managed key. You cannot rotate it,
|
||||
* change its key policy, edit its description, enable or disable it, tag it, or schedule it
|
||||
* for deletion. Cryptographic operations are unaffected, which is the entire point of the
|
||||
* key existing. The single error code here is a deliberate simplification: the customer is
|
||||
* not a principal the key's policy grants these actions to, so access denied is the honest
|
||||
* answer for all of them, even though AWS distinguishes some as unsupported operations.
|
||||
*/
|
||||
private static void requireCustomerManaged(KmsKey key, String operation) {
|
||||
if (AWS_KEY_MANAGER.equals(key.getKeyManager())) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"User is not authorized to perform " + operation + " on resource "
|
||||
+ key.getArn() + " because it is an AWS managed key.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireUnreservedAlias(String aliasName, String operation) {
|
||||
if (AwsManagedKeys.isReservedAlias(aliasName)) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"User is not authorized to perform " + operation + " on alias " + aliasName
|
||||
+ " because the " + AwsManagedKeys.RESERVED_ALIAS_PREFIX
|
||||
+ " namespace is reserved for AWS managed keys.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Aliases ────────────────────────────
|
||||
|
||||
public void createAlias(String aliasName, String targetKeyId, String region) {
|
||||
if (!aliasName.startsWith("alias/")) {
|
||||
throw new AwsException("InvalidAliasNameException", "Alias name must begin with 'alias/'", 400);
|
||||
}
|
||||
if (AwsManagedKeys.isReservedAlias(aliasName)) {
|
||||
// AWS reserves the whole alias/aws/ namespace for its own keys and rejects the name
|
||||
// outright, so a module that tries to claim one fails here exactly as it would there.
|
||||
throw new AwsException("InvalidAliasNameException",
|
||||
"Alias name cannot begin with '" + AwsManagedKeys.RESERVED_ALIAS_PREFIX + "'", 400);
|
||||
}
|
||||
KmsKey key = resolveKey(targetKeyId, region); // Validate key exists and normalize to plain key ID
|
||||
// AWS managed keys already carry the only alias they may have, the reserved one, and
|
||||
// CreateAlias cannot give them a second. Letting it through would be the same class of
|
||||
// error as seeding a key AWS does not have: the call succeeds here and fails there.
|
||||
// AccessDeniedException rather than a CreateAlias-specific code, because the refusal is an
|
||||
// authorization outcome (the key's policy grants the account nothing) and CreateAlias
|
||||
// publishes no error for this case.
|
||||
requireCustomerManaged(key, "kms:CreateAlias");
|
||||
requireNotPendingDeletion(key);
|
||||
|
||||
String aliasArn = regionResolver.buildArn("kms", region, aliasName);
|
||||
@@ -1179,6 +1365,7 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void updateAlias(String aliasName, String targetKeyId, String region) {
|
||||
requireUnreservedAlias(aliasName, "kms:UpdateAlias");
|
||||
String storageKey = region + "::" + aliasName;
|
||||
KmsAlias existing = aliasStore.get(storageKey)
|
||||
.orElseThrow(() -> aliasNotFound(aliasName, region));
|
||||
@@ -1186,6 +1373,9 @@ public class KmsService implements ResourceProvider {
|
||||
KmsKey currentKey = resolveKey(existing.getTargetKeyId(), region);
|
||||
KmsKey newKey = resolveKey(targetKeyId, region); // Validate key exists and normalize to plain key ID
|
||||
|
||||
// Same reason as CreateAlias: repointing a customer alias at an AWS managed key would
|
||||
// succeed here and fail against AWS.
|
||||
requireCustomerManaged(newKey, "kms:UpdateAlias");
|
||||
requireNotPendingDeletion(newKey);
|
||||
if (currentKey.getKeyUsage() != newKey.getKeyUsage() || !sameKeyFamily(currentKey.getKeySpec(), newKey.getKeySpec())) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -1200,6 +1390,7 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void deleteAlias(String aliasName, String region) {
|
||||
requireUnreservedAlias(aliasName, "kms:DeleteAlias");
|
||||
String key = region + "::" + aliasName;
|
||||
if (aliasStore.get(key).isEmpty()) {
|
||||
throw aliasNotFound(aliasName, region);
|
||||
@@ -1212,6 +1403,7 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public List<KmsAlias> listAliases(String keyId, String region) {
|
||||
ensureAwsManagedKeys(region);
|
||||
String prefix = region + "::";
|
||||
List<KmsAlias> all = aliasStore.scan(k -> k.startsWith(prefix));
|
||||
if (keyId == null || keyId.isBlank()) {
|
||||
@@ -1880,6 +2072,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void tagResource(String keyId, Map<String, String> tags, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:TagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
ReservedTags.rejectReservedTagsOnUpdate(tags);
|
||||
key.getTags().putAll(tags);
|
||||
@@ -1888,6 +2081,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void untagResource(String keyId, List<String> tagKeys, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:UntagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
tagKeys.forEach(key.getTags()::remove);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
@@ -1896,6 +2090,9 @@ public class KmsService implements ResourceProvider {
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
private KmsKey resolveKey(String keyIdOrArn, String region) {
|
||||
if (referencesReservedAlias(keyIdOrArn)) {
|
||||
ensureAwsManagedKeys(region);
|
||||
}
|
||||
if (AwsArnUtils.isArnFor(keyIdOrArn, "kms")) {
|
||||
String arnRegion = AwsArnUtils.parse(keyIdOrArn).region();
|
||||
if (!region.equals(arnRegion)) {
|
||||
|
||||
@@ -16,6 +16,10 @@ public class KmsKey {
|
||||
private boolean enabled = true;
|
||||
private String keyState = "Enabled"; // Enabled, Disabled, PendingDeletion, PendingImport
|
||||
private String origin = "AWS_KMS";
|
||||
// CUSTOMER for keys a customer created, AWS for the per-service keys AWS ships under the
|
||||
// reserved alias/aws/* aliases. DescribeKey reports it, and it is what tells a caller the
|
||||
// key cannot be deleted, retagged or repolicied.
|
||||
private String keyManager = "CUSTOMER";
|
||||
private String expirationModel;
|
||||
private long validTo;
|
||||
private KmsImportParameters importParameters;
|
||||
@@ -64,6 +68,9 @@ public class KmsKey {
|
||||
public String getOrigin() { return origin; }
|
||||
public void setOrigin(String origin) { this.origin = origin; }
|
||||
|
||||
public String getKeyManager() { return keyManager; }
|
||||
public void setKeyManager(String keyManager) { this.keyManager = keyManager; }
|
||||
|
||||
public String getExpirationModel() { return expirationModel; }
|
||||
public void setExpirationModel(String expirationModel) { this.expirationModel = expirationModel; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user