mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the multi-region keys (#8541)
* feat(paymentcryptography): the multi-region keys * fix(paymentcryptography): the multi-region replication refuses what it cannot carry out * test(compatibility-tests): the multi-region tests leave the account as they found it * fix(paymentcryptography): the default replication regions are the ones of the region * test(compatibility-tests): the multi-region tests keep their settings to their own region * docs(paymentcryptography): what the multi-region replication does * fix(paymentcryptography): the rules the multi-region replication follows * chore(paymentcryptography): the shape check settles the type of a list of names * style(paymentcryptography): the accessor the edit left on one line * docs(paymentcryptography): the page says what the replication does * fix(paymentcryptography): the replica answers as the reference answers * fix(paymentcryptography): a key is replicated within the partition it is in * style(paymentcryptography): the rules the replication shares are written once * fix(paymentcryptography): the partition of a region is the one its ARN carries * style(paymentcryptography): the tests of the service share the key they make * style(compatibility-tests): the compatibility tests share the key they make
This commit is contained in:
1 parent
c12db764e6
commit
057684feac
10 files changed
+1905
-166
No files matched your search
+93
-2
@@ -62,6 +62,11 @@ public class PaymentCryptographyJsonHandler {
|
||||
case "GetPublicKeyCertificate" -> () -> getPublicKeyCertificate(request, region);
|
||||
case "GetParametersForImport" -> () -> getParametersForImport(request, region);
|
||||
case "GetParametersForExport" -> () -> getParametersForExport(request, region);
|
||||
case "AddKeyReplicationRegions" -> () -> addKeyReplicationRegions(request, region);
|
||||
case "RemoveKeyReplicationRegions" -> () -> removeKeyReplicationRegions(request, region);
|
||||
case "GetDefaultKeyReplicationRegions" -> () -> getDefaultKeyReplicationRegions(request, region);
|
||||
case "EnableDefaultKeyReplicationRegions" -> () -> enableDefaultKeyReplicationRegions(request, region);
|
||||
case "DisableDefaultKeyReplicationRegions" -> () -> disableDefaultKeyReplicationRegions(request, region);
|
||||
case "TagResource" -> () -> tagResource(request, region);
|
||||
case "UntagResource" -> () -> untagResource(request, region);
|
||||
case "ListTagsForResource" -> () -> listTagsForResource(request, region);
|
||||
@@ -117,7 +122,8 @@ public class PaymentCryptographyJsonHandler {
|
||||
declared.put("DeriveKeyUsage", request.path("DeriveKeyUsage").asText());
|
||||
}
|
||||
PaymentCryptographyKey key = service.createKey(region, declared, modesOfUse(attributes),
|
||||
request.path("Exportable").asBoolean(false), enabled, tagsOf(request));
|
||||
request.path("Exportable").asBoolean(false), enabled, tagsOf(request),
|
||||
stringsOf(request, "ReplicationRegions"));
|
||||
return Map.of("Key", keyObject(key));
|
||||
}
|
||||
|
||||
@@ -360,6 +366,10 @@ public class PaymentCryptographyJsonHandler {
|
||||
out.put("KeyCheckValue", key.getKeyCheckValue());
|
||||
out.put("Enabled", key.isEnabled());
|
||||
out.put("Exportable", key.isExportable());
|
||||
if (key.getMultiRegionKeyType() != null) {
|
||||
out.put("MultiRegionKeyType", key.getMultiRegionKeyType());
|
||||
out.put("PrimaryRegion", key.getPrimaryRegion());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -385,6 +395,23 @@ public class PaymentCryptographyJsonHandler {
|
||||
if (key.getDeleteTimestamp() > 0) {
|
||||
out.put("DeleteTimestamp", key.getDeleteTimestamp());
|
||||
}
|
||||
if (key.getMultiRegionKeyType() != null) {
|
||||
out.put("MultiRegionKeyType", key.getMultiRegionKeyType());
|
||||
out.put("PrimaryRegion", key.getPrimaryRegion());
|
||||
if ("PRIMARY".equals(key.getMultiRegionKeyType())) {
|
||||
// The replication status and the defaults flag belong to the primary: a
|
||||
// replica answers with the kind of the key and its primary Region alone,
|
||||
// as the reference's own answer shows.
|
||||
Map<String, Object> status = new LinkedHashMap<>();
|
||||
for (String target : key.getReplicationRegions()) {
|
||||
// The model's replication states are IN_PROGRESS, DELETE_IN_PROGRESS, FAILED
|
||||
// and SYNCHRONIZED. A replica that is there is a key that is in sync.
|
||||
status.put(target, Map.of("Status", "SYNCHRONIZED"));
|
||||
}
|
||||
out.put("ReplicationStatus", status);
|
||||
out.put("UsingDefaultReplicationRegions", key.isUsingDefaultReplicationRegions());
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -463,7 +490,8 @@ public class PaymentCryptographyJsonHandler {
|
||||
"imports");
|
||||
PaymentCryptographyKey key = service.importKey(region, form, members(material.path(form)),
|
||||
request.path("KeyCheckValueAlgorithm").asText(),
|
||||
request.path("Enabled").asBoolean(true), tagsOf(request));
|
||||
request.path("Enabled").asBoolean(true), tagsOf(request),
|
||||
stringsOf(request, "ReplicationRegions"));
|
||||
return Map.of("Key", keyObject(key));
|
||||
}
|
||||
|
||||
@@ -546,4 +574,67 @@ public class PaymentCryptographyJsonHandler {
|
||||
}
|
||||
return node.asText();
|
||||
}
|
||||
|
||||
/**
|
||||
* The key with the regions the request names added to its replication.
|
||||
*/
|
||||
private Map<String, Object> addKeyReplicationRegions(JsonNode request, String region) {
|
||||
PaymentCryptographyKey key = service.addKeyReplicationRegions(region,
|
||||
request.path("KeyIdentifier").asText(), stringsOf(request, "ReplicationRegions"));
|
||||
return Map.of("Key", keyObject(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* The key with the regions the request names taken away from its replication.
|
||||
*/
|
||||
private Map<String, Object> removeKeyReplicationRegions(JsonNode request, String region) {
|
||||
PaymentCryptographyKey key = service.removeKeyReplicationRegions(region,
|
||||
request.path("KeyIdentifier").asText(), stringsOf(request, "ReplicationRegions"));
|
||||
return Map.of("Key", keyObject(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* The regions the account replicates a new key to by default.
|
||||
*
|
||||
* <p>The settings are the ones of the Region the request is in, so each Region has its
|
||||
* own.
|
||||
*/
|
||||
private Map<String, Object> getDefaultKeyReplicationRegions(JsonNode request, String region) {
|
||||
return Map.of("EnabledReplicationRegions", service.defaultReplicationRegions(region));
|
||||
}
|
||||
|
||||
/** Turns the default replication of the account on for the regions the request names. */
|
||||
private Map<String, Object> enableDefaultKeyReplicationRegions(JsonNode request, String region) {
|
||||
return Map.of("EnabledReplicationRegions",
|
||||
service.enableDefaultReplicationRegions(region,
|
||||
stringsOf(request, "ReplicationRegions")));
|
||||
}
|
||||
|
||||
/** Turns the default replication of the account off for the regions the request names. */
|
||||
private Map<String, Object> disableDefaultKeyReplicationRegions(JsonNode request, String region) {
|
||||
return Map.of("EnabledReplicationRegions",
|
||||
service.disableDefaultReplicationRegions(region,
|
||||
stringsOf(request, "ReplicationRegions")));
|
||||
}
|
||||
|
||||
/**
|
||||
* The strings of a request, such as the replication regions.
|
||||
*
|
||||
* <p>The shape check settles that the member is a list of names, so a request that names a
|
||||
* region that is not one is refused before this reads it.
|
||||
*
|
||||
* @param request the request the member is in
|
||||
* @param member the member to read
|
||||
*/
|
||||
private static List<String> stringsOf(JsonNode request, String member) {
|
||||
JsonNode declared = request.get(member);
|
||||
if (declared == null || declared.isNull()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<String> values = new ArrayList<>();
|
||||
for (JsonNode value : declared) {
|
||||
values.add(value.asText());
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
+521
-16
@@ -24,6 +24,7 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -73,11 +74,20 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
/** The origin of a key whose material came in from outside the service. */
|
||||
private static final String ORIGIN_EXTERNAL = "EXTERNAL";
|
||||
|
||||
/**
|
||||
* The storage key the account's default replication regions are kept under.
|
||||
*
|
||||
* <p>The store is account-aware, so what is under this key belongs to the account that
|
||||
* put it there and to no other.
|
||||
*/
|
||||
private static final String DEFAULT_REGIONS_KEY = "default-replication-regions";
|
||||
|
||||
private final StorageBackend<String, PaymentCryptographyKey> keyStore;
|
||||
private final StorageBackend<String, PaymentCryptographyAlias> aliasStore;
|
||||
|
||||
/** The key exchange: the forms a key may leave in, and the certificates that carry them. */
|
||||
private final PaymentCryptographyKeyExchange exchange;
|
||||
private final StorageBackend<String, List<String>> settingsStore;
|
||||
private final RegionResolver regionResolver;
|
||||
private final SecureRandom secureRandom;
|
||||
private final PaymentCryptographyRequestContext requestContext;
|
||||
@@ -90,6 +100,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
new TypeReference<>() {}),
|
||||
storageFactory.create(STORAGE_KEY, "payment-cryptography-aliases.json",
|
||||
new TypeReference<>() {}),
|
||||
storageFactory.create(STORAGE_KEY, "payment-cryptography-settings.json",
|
||||
new TypeReference<Map<String, List<String>>>() {}),
|
||||
regionResolver,
|
||||
new SecureRandom(),
|
||||
requestContext,
|
||||
@@ -101,7 +113,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
StorageBackend<String, PaymentCryptographyAlias> aliasStore,
|
||||
RegionResolver regionResolver,
|
||||
PaymentCryptographyRequestContext requestContext) {
|
||||
this(keyStore, aliasStore, regionResolver, new SecureRandom(), requestContext);
|
||||
this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, new SecureRandom(),
|
||||
requestContext, new CertificateGenerator(), Clock.systemUTC());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,8 +126,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
PaymentCryptographyService(StorageBackend<String, PaymentCryptographyKey> keyStore,
|
||||
StorageBackend<String, PaymentCryptographyAlias> aliasStore,
|
||||
RegionResolver regionResolver) {
|
||||
this(keyStore, aliasStore, regionResolver, new SecureRandom(),
|
||||
new PaymentCryptographyRequestContext());
|
||||
this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, new SecureRandom(),
|
||||
new PaymentCryptographyRequestContext(), new CertificateGenerator(), Clock.systemUTC());
|
||||
}
|
||||
|
||||
PaymentCryptographyService(StorageBackend<String, PaymentCryptographyKey> keyStore,
|
||||
@@ -122,8 +135,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
RegionResolver regionResolver,
|
||||
SecureRandom secureRandom,
|
||||
PaymentCryptographyRequestContext requestContext) {
|
||||
this(keyStore, aliasStore, regionResolver, secureRandom, requestContext,
|
||||
new CertificateGenerator(), Clock.systemUTC());
|
||||
this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, secureRandom,
|
||||
requestContext, new CertificateGenerator(), Clock.systemUTC());
|
||||
}
|
||||
|
||||
PaymentCryptographyService(StorageBackend<String, PaymentCryptographyKey> keyStore,
|
||||
@@ -133,8 +146,21 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
PaymentCryptographyRequestContext requestContext,
|
||||
CertificateGenerator certificates,
|
||||
Clock clock) {
|
||||
this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, secureRandom,
|
||||
requestContext, certificates, clock);
|
||||
}
|
||||
|
||||
PaymentCryptographyService(StorageBackend<String, PaymentCryptographyKey> keyStore,
|
||||
StorageBackend<String, PaymentCryptographyAlias> aliasStore,
|
||||
StorageBackend<String, List<String>> settingsStore,
|
||||
RegionResolver regionResolver,
|
||||
SecureRandom secureRandom,
|
||||
PaymentCryptographyRequestContext requestContext,
|
||||
CertificateGenerator certificates,
|
||||
Clock clock) {
|
||||
this.keyStore = keyStore;
|
||||
this.aliasStore = aliasStore;
|
||||
this.settingsStore = settingsStore;
|
||||
this.regionResolver = regionResolver;
|
||||
this.exchange = new PaymentCryptographyKeyExchange(this, certificates, clock);
|
||||
this.secureRandom = secureRandom;
|
||||
@@ -343,20 +369,25 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
* is being tagged cannot be written back after it was deleted.
|
||||
*/
|
||||
public synchronized PaymentCryptographyKey deleteKey(PaymentCryptographyKey key) {
|
||||
// A replica is read-only: only the primary may be deleted.
|
||||
requireNotReplica(key, "only the primary may be deleted");
|
||||
long now = Instant.now().getEpochSecond();
|
||||
key.setKeyState("DELETE_PENDING");
|
||||
key.setEnabled(false);
|
||||
key.setDeletePendingTimestamp(now);
|
||||
// The replicas of a key go when the key goes. A replica left behind would be a
|
||||
// record of a key that is gone, and it could not be deleted on its own.
|
||||
for (String target : key.getReplicationRegions()) {
|
||||
keyStore.delete(storageKey(target, key.getKeyId()));
|
||||
}
|
||||
keyStore.delete(storageKey(key.getRegion(), key.getKeyId()));
|
||||
// A key saved before the region became part of its name is under that name too,
|
||||
// and it goes with the key: a record left behind would be a key that was deleted
|
||||
// and still there.
|
||||
keyStore.delete(key.getKeyId());
|
||||
for (PaymentCryptographyAlias alias : aliasStore.scan(a -> true)) {
|
||||
if (key.getKeyId().equals(alias.getKeyId())) {
|
||||
aliasStore.delete(aliasKey(alias.getAliasName(), alias.getRegion()));
|
||||
}
|
||||
}
|
||||
// The aliases that name the key go with it, in every region: a replica shares the
|
||||
// identifier of its primary, so these are the aliases of the replicas too.
|
||||
deleteAliasesOf(null, key.getKeyId());
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -478,13 +509,16 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
/**
|
||||
* Adds tags to a key.
|
||||
*
|
||||
* <p>The tags are the region's: they are not part of the replication
|
||||
* process, so a key in another region carries its own tag set.
|
||||
* <p>A replica is a copy of the primary's metadata, so it carries the primary's tags,
|
||||
* and a change made on the primary reaches the replicas. A replica is read-only, so the
|
||||
* change is made on the primary.
|
||||
*/
|
||||
public synchronized void tag(PaymentCryptographyKey key, Map<String, String> tags) {
|
||||
PaymentCryptographyKey current = liveKey(key);
|
||||
requireNotReplica(current, "a replica is read-only");
|
||||
current.getTags().putAll(tags);
|
||||
storeKey(current);
|
||||
refreshReplicas(current, current.getReplicationRegions());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -521,13 +555,64 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes tags from a key, in this region only. */
|
||||
/**
|
||||
* Removes tags from a key.
|
||||
*
|
||||
* <p>A replica is read-only, so the change is made on the primary and reaches the
|
||||
* replicas.
|
||||
*/
|
||||
public synchronized void untag(PaymentCryptographyKey key, List<String> tagKeys) {
|
||||
PaymentCryptographyKey current = liveKey(key);
|
||||
requireNotReplica(current, "a replica is read-only");
|
||||
for (String tagKey : tagKeys) {
|
||||
current.getTags().remove(tagKey);
|
||||
}
|
||||
storeKey(current);
|
||||
refreshReplicas(current, current.getReplicationRegions());
|
||||
}
|
||||
|
||||
/**
|
||||
* The replicas of a primary brought up to date with it.
|
||||
*
|
||||
* <p>A replica is a copy of the primary's material and metadata, and a change to the
|
||||
* primary's metadata is replicated to the replicas, as the reference says. The copy is
|
||||
* in one place, so every change to a primary reaches its replicas the same way.
|
||||
*
|
||||
* @param primary the key the change was made on
|
||||
* @param regions the regions the replicas are in
|
||||
*/
|
||||
private void refreshReplicas(PaymentCryptographyKey primary, Collection<String> regions) {
|
||||
for (String target : regions) {
|
||||
PaymentCryptographyKey replica = findKeyRecord(keyStore, target, primary.getKeyId());
|
||||
if (replica == null) {
|
||||
continue;
|
||||
}
|
||||
replica.setTags(new LinkedHashMap<>(primary.getTags()));
|
||||
replica.setKeyState(primary.getKeyState());
|
||||
replica.setEnabled(primary.isEnabled());
|
||||
replica.setUsingDefaultReplicationRegions(primary.isUsingDefaultReplicationRegions());
|
||||
replica.setPrimaryRegion(primary.getPrimaryRegion());
|
||||
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
|
||||
keyStore.put(storageKey(target, replica.getKeyId()), replica);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The refusal of a request that names a replica.
|
||||
*
|
||||
* <p>A replica is read-only: only the primary may be deleted, replicated further or
|
||||
* changed, so a change made on the replica itself is refused. The check is in one place,
|
||||
* so every operation that names a replica refuses it in the same words.
|
||||
*
|
||||
* @param key the key the request names
|
||||
* @param what what the request wanted to do with the key, for the message
|
||||
*/
|
||||
private static void requireNotReplica(PaymentCryptographyKey key, String what) {
|
||||
if ("REPLICA".equals(key.getMultiRegionKeyType())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"the key " + key.getArn() + " is a replica of a key in "
|
||||
+ key.getPrimaryRegion() + ", and " + what, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -552,8 +637,6 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
maxResults, nextToken, DEFAULT_PAGE_SIZE, MAX_RESULTS, PAGINATION_ERROR);
|
||||
}
|
||||
|
||||
// ---- the replication ------------------------------------------------------
|
||||
|
||||
// ---- the key exchange ----------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -619,6 +702,18 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
public PaymentCryptographyKey importKey(String region, String form, Map<String, Object> material,
|
||||
String checkValueAlgorithm, boolean enabled,
|
||||
Map<String, String> tags) {
|
||||
return importKey(region, form, material, checkValueAlgorithm, enabled, tags, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a key and replicates it to the regions the request names.
|
||||
*
|
||||
* <p>The model gives an import the same replication regions a creation does, so a key
|
||||
* that comes in can be replicated as soon as it lands.
|
||||
*/
|
||||
public PaymentCryptographyKey importKey(String region, String form, Map<String, Object> material,
|
||||
String checkValueAlgorithm, boolean enabled,
|
||||
Map<String, String> tags, List<String> replicationRegions) {
|
||||
PaymentCryptographyKeyExchange.Imported imported = exchange.importKey(region, form, material);
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", imported.algorithm());
|
||||
@@ -634,11 +729,16 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
+ imported.algorithm() + " key has, which is " + own, 400);
|
||||
}
|
||||
}
|
||||
return makeKey(region, attributes, imported.modes(), imported.exportable(), enabled, tags,
|
||||
Replication replication = replicationOf(region, replicationRegions);
|
||||
// The key is checked before it is made, so a refused request leaves no key behind.
|
||||
requireExportable(imported.exportable(), replication.targets());
|
||||
PaymentCryptographyKey key = makeKey(region, attributes, imported.modes(),
|
||||
imported.exportable(), enabled, tags,
|
||||
imported.material() == null ? null
|
||||
: PaymentCryptographyCrypto.toHex(imported.material()),
|
||||
imported.publicKeyBase64(), imported.privateKeyBase64(), imported.certificatePem(),
|
||||
ORIGIN_EXTERNAL);
|
||||
return replicate(key, replication);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1084,6 +1184,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
@Override
|
||||
public void clear() {
|
||||
keyStore.clear();
|
||||
settingsStore.clear();
|
||||
aliasStore.clear();
|
||||
}
|
||||
|
||||
@@ -1108,4 +1209,408 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
boolean reuse) {
|
||||
return exchange.parametersForExport(materialType, algorithm, reuse);
|
||||
}
|
||||
|
||||
// ---- the replication ------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The regions the account replicates a new key to when the request names none.
|
||||
*
|
||||
* <p>They are kept in the account-aware store, so they belong to the account that set
|
||||
* them and to no other account. Each Region has its own settings, so a Region whose
|
||||
* keys are replicated is not the same as another Region whose keys are not.
|
||||
*
|
||||
* @param region the Region the settings are read in
|
||||
*/
|
||||
public List<String> defaultReplicationRegions(String region) {
|
||||
List<String> regions = settingsStore.get(defaultRegionsKey(region)).orElseGet(ArrayList::new);
|
||||
return new ArrayList<>(regions);
|
||||
}
|
||||
|
||||
/**
|
||||
* The name the default regions of a Region are kept under.
|
||||
*
|
||||
* @param region the Region the settings are of
|
||||
*/
|
||||
private static String defaultRegionsKey(String region) {
|
||||
return DEFAULT_REGIONS_KEY + ":" + region;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the default replication of the account on for the regions the request names.
|
||||
*
|
||||
* <p>The regions the account already had are left on, so the operation adds rather than
|
||||
* replaces. The settings are the ones of the Region the request is in, so each Region
|
||||
* has its own.
|
||||
*
|
||||
* @param region the Region the request is in
|
||||
* @param regions the regions the request names
|
||||
*/
|
||||
public synchronized List<String> enableDefaultReplicationRegions(String region,
|
||||
List<String> regions) {
|
||||
requireReplicationRegions(region, regions);
|
||||
// The region of the request is where the keys made there are primary, so it is not
|
||||
// a region they are replicated to.
|
||||
requireOtherRegion(region, regions, "enable");
|
||||
List<String> current = defaultReplicationRegions(region);
|
||||
for (String target : regions) {
|
||||
if (!current.contains(target)) {
|
||||
current.add(target);
|
||||
}
|
||||
}
|
||||
settingsStore.put(defaultRegionsKey(region), current);
|
||||
return defaultReplicationRegions(region);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the default replication of the account off for the regions the request names.
|
||||
*
|
||||
* <p>Only the regions the request names are turned off, so the others stay on. The
|
||||
* settings are the ones of the Region the request is in, so each Region has its own.
|
||||
*
|
||||
* @param region the Region the request is in
|
||||
* @param regions the regions the request names
|
||||
*/
|
||||
public synchronized List<String> disableDefaultReplicationRegions(String region,
|
||||
List<String> regions) {
|
||||
requireKnownRegions(regions);
|
||||
List<String> current = defaultReplicationRegions(region);
|
||||
current.removeAll(regions);
|
||||
settingsStore.put(defaultRegionsKey(region), current);
|
||||
return defaultReplicationRegions(region);
|
||||
}
|
||||
|
||||
/**
|
||||
* The regions the service serves, or the refusal of the ones it does not.
|
||||
*
|
||||
* <p>The check is in one place, so every operation that names a region refuses the same
|
||||
* regions in the same words.
|
||||
*
|
||||
* @param regions the regions the request names
|
||||
*/
|
||||
private static void requireKnownRegions(List<String> regions) {
|
||||
for (String region : regions) {
|
||||
if (!RegionResolver.isKnownRegion(region)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"the region " + region + " is not a region this service serves", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The regions a key may be replicated to, or the refusal of the ones it may not.
|
||||
*
|
||||
* <p>A key is replicated within the partition it is in and the account it belongs to, as
|
||||
* the reference says, so a region of another partition is refused. The account is the
|
||||
* one the request is made in, so a request cannot name a region of another account.
|
||||
* The partition of a region is the one its ARN is minted with, so a region the catalog
|
||||
* cannot place is in the partition of the deployment and not in the commercial one.
|
||||
* The check is in one place, so every operation that names a replication region
|
||||
* refuses the same regions in the same words.
|
||||
*
|
||||
* @param region the region the request is in
|
||||
* @param regions the regions the request names
|
||||
*/
|
||||
private void requireReplicationRegions(String region, List<String> regions) {
|
||||
requireKnownRegions(regions);
|
||||
for (String target : regions) {
|
||||
if (!regionResolver.partitionForRegion(region)
|
||||
.equals(regionResolver.partitionForRegion(target))) {
|
||||
throw new AwsException("ValidationException",
|
||||
"the region " + target + " is in another partition than the region "
|
||||
+ region + ", and a key is replicated within its own partition", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The refusal of a request that names the region of the request as a region to replicate to.
|
||||
*
|
||||
* <p>A key is in the region of the request already, so that region is not a region the key
|
||||
* is replicated to. The check is in one place, so every operation that names a region
|
||||
* refuses it in the same words.
|
||||
*
|
||||
* @param region the region the request is in
|
||||
* @param regions the regions the request names
|
||||
* @param what the operation, for the message
|
||||
*/
|
||||
private static void requireOtherRegion(String region, List<String> regions, String what) {
|
||||
if (regions.contains(region)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"the region " + region + " is the region of the request, and it is not a region to "
|
||||
+ what, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The key with the regions the request names added to its replication.
|
||||
*
|
||||
* <p>Only the primary of a multi-region key may be replicated further, and a key that is
|
||||
* replicated has to be exportable.
|
||||
*/
|
||||
public synchronized PaymentCryptographyKey addKeyReplicationRegions(String region, String keyIdentifier,
|
||||
List<String> regions) {
|
||||
PaymentCryptographyKey key = primaryOf(region, keyIdentifier);
|
||||
// The key has to be in an active state to be replicated, as the reference says.
|
||||
PaymentCryptographyKeyRules.requireEnabled(key);
|
||||
if (!key.isExportable()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"a key that is replicated has to be exportable, and " + key.getArn() + " is not", 400);
|
||||
}
|
||||
requireReplicationRegions(region, regions);
|
||||
// The key is in its own region already, so naming that region again is a request
|
||||
// that cannot be carried out.
|
||||
requireOtherRegion(region, regions, "add");
|
||||
Set<String> added = new LinkedHashSet<>(key.getReplicationRegions());
|
||||
for (String target : regions) {
|
||||
if (added.contains(target)) {
|
||||
// The key already replicates there, so adding it again is a request that
|
||||
// cannot be carried out rather than one that changes nothing.
|
||||
throw new AwsException("ValidationException",
|
||||
"the key " + key.getArn() + " already replicates into " + target, 400);
|
||||
}
|
||||
added.add(target);
|
||||
}
|
||||
if (added.isEmpty()) {
|
||||
// Nothing is left to replicate to, so the key would be a multi-region key with no
|
||||
// replica, which is not a thing: a request that names no region is refused.
|
||||
throw new AwsException("ValidationException",
|
||||
"the request names no region to add to the replication of " + key.getArn(), 400);
|
||||
}
|
||||
// A key with a replica is a primary, and says which region it is the primary of.
|
||||
key.setMultiRegionKeyType("PRIMARY");
|
||||
key.setPrimaryRegion(key.getPrimaryRegion() == null ? region : key.getPrimaryRegion());
|
||||
key.setReplicationRegions(new ArrayList<>(added));
|
||||
// The key has a configuration of its own now, so it no longer takes the account's
|
||||
// default regions.
|
||||
key.setUsingDefaultReplicationRegions(false);
|
||||
storeKey(key);
|
||||
// A region the key did not replicate into gets a replica of the key, and a replica
|
||||
// that is already there keeps its own region and its own ARN and takes the primary's
|
||||
// metadata, so it does not report regions the primary has left behind.
|
||||
for (String target : added) {
|
||||
if (findKeyRecord(keyStore, target, key.getKeyId()) == null) {
|
||||
keyStore.put(storageKey(target, key.getKeyId()), replicaOf(key, target));
|
||||
}
|
||||
}
|
||||
refreshReplicas(key, added);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key with the regions the request names taken away from its replication.
|
||||
*
|
||||
* <p>The key goes out of the region it was replicated to, and the record of it there is
|
||||
* gone.
|
||||
*/
|
||||
public synchronized PaymentCryptographyKey removeKeyReplicationRegions(String region,
|
||||
String keyIdentifier,
|
||||
List<String> regions) {
|
||||
PaymentCryptographyKey key = primaryOf(region, keyIdentifier);
|
||||
// The region the primary is in is not a replica, and taking it away would
|
||||
// delete the key without going through the deletion operation.
|
||||
requireOtherRegion(region, regions, "remove");
|
||||
Set<String> left = new LinkedHashSet<>(key.getReplicationRegions());
|
||||
for (String target : regions) {
|
||||
if (!left.contains(target)) {
|
||||
// The key does not replicate there, so there is nothing to take away.
|
||||
throw new AwsException("ValidationException",
|
||||
"the key " + key.getArn() + " does not replicate into " + target, 400);
|
||||
}
|
||||
}
|
||||
left.removeAll(regions);
|
||||
key.setReplicationRegions(new ArrayList<>(left));
|
||||
if (left.isEmpty()) {
|
||||
// A key that replicates nowhere is not part of a replication relationship, and
|
||||
// says so by carrying no type at all. Taking the last replica away is how the
|
||||
// replication is turned off for a key, so the key is a plain one afterwards.
|
||||
key.setMultiRegionKeyType(null);
|
||||
key.setPrimaryRegion(null);
|
||||
key.setUsingDefaultReplicationRegions(false);
|
||||
}
|
||||
storeKey(key);
|
||||
for (String target : regions) {
|
||||
keyStore.delete(storageKey(target, key.getKeyId()));
|
||||
// The aliases that named the replica in that region go with it: an alias that
|
||||
// names a key that is not there cannot be resolved, and its name would be taken
|
||||
// by a key that is not the one it named.
|
||||
deleteAliasesOf(target, key.getKeyId());
|
||||
}
|
||||
// The replicas that are left carry the regions the primary has now, so none of them
|
||||
// reports a region the primary has left behind. They carry the primary's tags too: a
|
||||
// replica is a copy of the primary's material and metadata.
|
||||
refreshReplicas(key, left);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The aliases that name a key, gone with the key.
|
||||
*
|
||||
* <p>An alias that names a key that is not there cannot be resolved, so it goes when the
|
||||
* key does, as it does when the key itself is deleted. A replica shares the identifier
|
||||
* of its primary, so the aliases that name the replica are the ones that name the key.
|
||||
*
|
||||
* @param region the region the aliases are in, or null for the aliases of every region
|
||||
* @param keyId the key the aliases name
|
||||
*/
|
||||
private void deleteAliasesOf(String region, String keyId) {
|
||||
for (PaymentCryptographyAlias alias : aliasStore.scan(a -> true)) {
|
||||
if (region != null && !region.equals(alias.getRegion())) {
|
||||
continue;
|
||||
}
|
||||
if (keyId.equals(alias.getKeyId())) {
|
||||
aliasStore.delete(aliasKey(alias.getAliasName(), alias.getRegion()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The primary of a key, which is the only one that may be replicated further.
|
||||
*
|
||||
* <p>A replica is read-only: it cannot be deleted on its own and it cannot be replicated
|
||||
* further, so the operations that change the replication of a key start at its primary.
|
||||
*/
|
||||
private PaymentCryptographyKey primaryOf(String region, String keyIdentifier) {
|
||||
PaymentCryptographyKey key = findKey(region, keyIdentifier);
|
||||
if (key == null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Key not found: " + keyIdentifier, 400);
|
||||
}
|
||||
requireNotReplica(key, "only the primary may be replicated");
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The record of a key in another region: the same key, with the ARN of that region.
|
||||
*
|
||||
* <p>The alias names are the region's own, but the tags are the primary's: a replica is
|
||||
* a copy of the primary's material and metadata.
|
||||
*/
|
||||
private PaymentCryptographyKey replicaOf(PaymentCryptographyKey primary, String region) {
|
||||
PaymentCryptographyKey replica = new PaymentCryptographyKey();
|
||||
replica.setKeyId(primary.getKeyId());
|
||||
replica.setArn(regionResolver.buildArn(SERVICE, region, "key/" + primary.getKeyId()));
|
||||
replica.setRegion(region);
|
||||
replica.setKeyAlgorithm(primary.getKeyAlgorithm());
|
||||
replica.setKeyClass(primary.getKeyClass());
|
||||
replica.setKeyUsage(primary.getKeyUsage());
|
||||
replica.setKeyModesOfUse(new LinkedHashMap<>(primary.getKeyModesOfUse()));
|
||||
replica.setKeyOrigin(primary.getKeyOrigin());
|
||||
replica.setKeyCheckValue(primary.getKeyCheckValue());
|
||||
replica.setKeyCheckValueAlgorithm(primary.getKeyCheckValueAlgorithm());
|
||||
replica.setKeyState(primary.getKeyState());
|
||||
replica.setExportable(primary.isExportable());
|
||||
replica.setEnabled(primary.isEnabled());
|
||||
replica.setKeyMaterialHex(primary.getKeyMaterialHex());
|
||||
// The halves of an asymmetric key travel with it, or the replica is a record of a
|
||||
// key that has none.
|
||||
replica.setKeyPublicKeyBase64(primary.getKeyPublicKeyBase64());
|
||||
replica.setKeyPrivateKeyBase64(primary.getKeyPrivateKeyBase64());
|
||||
replica.setKeyCertificatePem(primary.getKeyCertificatePem());
|
||||
replica.setDeriveKeyUsage(primary.getDeriveKeyUsage());
|
||||
// A replica is a copy of the primary's material and metadata, so it carries the
|
||||
// primary's tags too.
|
||||
replica.setTags(new LinkedHashMap<>(primary.getTags()));
|
||||
replica.setCreateTimestamp(primary.getCreateTimestamp());
|
||||
replica.setUsageStartTimestamp(primary.getUsageStartTimestamp());
|
||||
replica.setPrimaryRegion(primary.getPrimaryRegion());
|
||||
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
|
||||
replica.setMultiRegionKeyType("REPLICA");
|
||||
replica.setUsingDefaultReplicationRegions(primary.isUsingDefaultReplicationRegions());
|
||||
return replica;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a key, in the region of the request and in the regions it is replicated to.
|
||||
*
|
||||
* <p>A key that is replicated is one key with one identifier in several regions, and each
|
||||
* region has its own ARN of it. The regions the request names are the ones the key is
|
||||
* replicated to; when it names none, the account's default regions are the ones, and the
|
||||
* key says that it took them.
|
||||
*/
|
||||
public PaymentCryptographyKey createKey(String region, Map<String, Object> attributes,
|
||||
Map<String, Boolean> modesOfUse, boolean exportable,
|
||||
boolean enabled, Map<String, String> tags,
|
||||
List<String> replicationRegions) {
|
||||
Replication replication = replicationOf(region, replicationRegions);
|
||||
// The key is checked before it is made, so a refused request leaves no key behind.
|
||||
requireExportable(exportable, replication.targets());
|
||||
PaymentCryptographyKey primary = makeKey(region, attributes, modesOfUse, exportable,
|
||||
enabled, tags, null);
|
||||
return replicate(primary, replication);
|
||||
}
|
||||
|
||||
/**
|
||||
* The regions a key is replicated to, and whether they are the account's defaults.
|
||||
*
|
||||
* <p>The regions the request names are the ones the key is replicated to; when it names
|
||||
* none, the account's default regions are the ones. The region's own is never one of
|
||||
* them, since the key is in that region already.
|
||||
*
|
||||
* @param region the region the request is in
|
||||
* @param replicationRegions the regions the request names, or none for the account's
|
||||
*/
|
||||
private Replication replicationOf(String region, List<String> replicationRegions) {
|
||||
Set<String> targets = new LinkedHashSet<>();
|
||||
boolean named = replicationRegions != null && !replicationRegions.isEmpty();
|
||||
if (named) {
|
||||
// A key is in the region of the request already, so naming that region is a
|
||||
// request that cannot be carried out.
|
||||
requireOtherRegion(region, replicationRegions, "replicate to");
|
||||
targets.addAll(replicationRegions);
|
||||
} else {
|
||||
targets.addAll(defaultReplicationRegions(region));
|
||||
// The region of the request is never a replica of the key made there.
|
||||
targets.remove(region);
|
||||
}
|
||||
requireReplicationRegions(region, new ArrayList<>(targets));
|
||||
return new Replication(targets, !named);
|
||||
}
|
||||
|
||||
/**
|
||||
* A key that is replicated has to be exportable, since a replica is a copy of its
|
||||
* material.
|
||||
*
|
||||
* @param exportable whether the key may leave the service
|
||||
* @param targets the regions the key would be replicated to
|
||||
*/
|
||||
private static void requireExportable(boolean exportable, Set<String> targets) {
|
||||
if (!targets.isEmpty() && !exportable) {
|
||||
throw new AwsException("ValidationException",
|
||||
"a key that is replicated has to be exportable, and this one is not", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The regions a key is replicated to, and whether they are the account's defaults.
|
||||
*
|
||||
* @param targets the regions the key is replicated to
|
||||
* @param usingDefaults whether those regions are the account's default ones
|
||||
*/
|
||||
private record Replication(Set<String> targets, boolean usingDefaults) { }
|
||||
|
||||
/**
|
||||
* Replicates a key to the regions the request names.
|
||||
*
|
||||
* <p>A key that is replicated is one key with one identifier in several regions, and each
|
||||
* region has its own ARN of it. The regions the request names are the ones the key is
|
||||
* replicated to; when it names none, the account's default regions are the ones, and the
|
||||
* key says that it took them.
|
||||
*
|
||||
* @param primary the key that was made
|
||||
* @param replication the regions it is replicated to
|
||||
* @return the key, which is the primary of a multi-region key when it was replicated
|
||||
*/
|
||||
private PaymentCryptographyKey replicate(PaymentCryptographyKey primary, Replication replication) {
|
||||
if (replication.targets().isEmpty()) {
|
||||
return primary;
|
||||
}
|
||||
primary.setPrimaryRegion(primary.getRegion());
|
||||
primary.setMultiRegionKeyType("PRIMARY");
|
||||
primary.setReplicationRegions(new ArrayList<>(replication.targets()));
|
||||
primary.setUsingDefaultReplicationRegions(replication.usingDefaults());
|
||||
storeKey(primary);
|
||||
for (String target : replication.targets()) {
|
||||
keyStore.put(storageKey(target, primary.getKeyId()), replicaOf(primary, target));
|
||||
}
|
||||
return primary;
|
||||
}
|
||||
}
|
||||
+20
-3
@@ -4,7 +4,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -68,12 +70,13 @@ public class PaymentCryptographyKey {
|
||||
private long deleteTimestamp;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
/** The region of the primary of a multi-region key, and its own region otherwise. */
|
||||
|
||||
private String primaryRegion;
|
||||
/** The regions a multi-region key is in, besides the primary's. */
|
||||
|
||||
private List<String> replicationRegions = new ArrayList<>();
|
||||
/** Whether this record is the primary of a multi-region key or a replica of one. */
|
||||
|
||||
private String multiRegionKeyType;
|
||||
/** Whether the key took the account's default replication regions. */
|
||||
private boolean usingDefaultReplicationRegions;
|
||||
|
||||
public String getKeyId() { return keyId; }
|
||||
public void setKeyId(String keyId) { this.keyId = keyId; }
|
||||
@@ -164,6 +167,20 @@ public class PaymentCryptographyKey {
|
||||
public long getDeleteTimestamp() { return deleteTimestamp; }
|
||||
public void setDeleteTimestamp(long deleteTimestamp) { this.deleteTimestamp = deleteTimestamp; }
|
||||
|
||||
public String getPrimaryRegion() { return primaryRegion; }
|
||||
public void setPrimaryRegion(String primaryRegion) { this.primaryRegion = primaryRegion; }
|
||||
|
||||
public List<String> getReplicationRegions() { return replicationRegions; }
|
||||
public void setReplicationRegions(List<String> replicationRegions) { this.replicationRegions = replicationRegions; }
|
||||
|
||||
public String getMultiRegionKeyType() { return multiRegionKeyType; }
|
||||
public void setMultiRegionKeyType(String multiRegionKeyType) { this.multiRegionKeyType = multiRegionKeyType; }
|
||||
|
||||
public boolean isUsingDefaultReplicationRegions() { return usingDefaultReplicationRegions; }
|
||||
public void setUsingDefaultReplicationRegions(boolean usingDefaultReplicationRegions) {
|
||||
this.usingDefaultReplicationRegions = usingDefaultReplicationRegions;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
}
|
||||
Reference in new issue
Block a user