mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the Payment Cryptography control plane (#7090)
This commit is contained in:
1 parent
9e270fbfe1
commit
6906759c9a
41 files changed
+3268
-25
No files matched your search
+2
-1
@@ -43,7 +43,8 @@ class AwsJsonControllerSerializationTest {
|
||||
when(sqs.handle(eq("Test"), any(), eq("us-east-1"))).thenReturn(original);
|
||||
|
||||
AwsJsonController controller = new AwsJsonController(mapper, catalog, regionResolver,
|
||||
null, null, sqs, null, null, null, null, null, null, null, null, null, null);
|
||||
null, null, sqs, null, null, null,
|
||||
null, null, null, null, null, null, null, null);
|
||||
Response result = controller.handleJsonRequest("AmazonSQS.Test", headers, "{}");
|
||||
|
||||
assertSame(original, result);
|
||||
|
||||
+24
-1
@@ -48,7 +48,8 @@ class PartitionCrossServiceSmokeIntegrationTest {
|
||||
"ecr", "ecs", "efs", "eks", "elasticache", "elasticbeanstalk", "elb", "elbv2", "emr", "emrserverless",
|
||||
"eventbridge", "eventbridgev2", "firehose", "fis", "glue", "guardduty", "iot", "kinesis", "kinesisanalytics", "lambda",
|
||||
"lambdamicrovms", "lambdaweb", "memorydb", "msk", "mwaa", "networkfirewall", "oam", "opensearch", "organizations",
|
||||
"pipes", "ram", "rds", "redshift", "redshiftserverless", "resourceexplorer2", "resourcegroups", "route53resolver",
|
||||
"paymentcryptography", "pipes", "ram", "rds", "redshift", "redshiftserverless",
|
||||
"resourceexplorer2", "resourcegroups", "route53resolver",
|
||||
"s3tables", "s3vectors", "sagemaker", "scheduler", "secretsmanager", "servicequotas", "ses", "sns", "sqs",
|
||||
"stepfunctions", "timestreaminfluxdb", "transfer", "verifiedpermissions", "wafv2");
|
||||
|
||||
@@ -80,6 +81,28 @@ class PartitionCrossServiceSmokeIntegrationTest {
|
||||
PartitionMatrix.assertArnIn(partitionCase, arn);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES)
|
||||
void paymentCryptographyKeyArn(PartitionCase partitionCase) {
|
||||
String region = offered(partitionCase, "payment-cryptography");
|
||||
String arn = signed(region, "payment-cryptography")
|
||||
.header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateKey")
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.body("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\","
|
||||
+ "\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true}},"
|
||||
+ "\"Exportable\":true}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
cleanup.register(() -> signed(region, "payment-cryptography")
|
||||
.header("X-Amz-Target", "PaymentCryptographyControlPlane.DeleteKey")
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.body("{\"KeyIdentifier\":\"" + arn + "\"}")
|
||||
.when().post("/"));
|
||||
PartitionMatrix.assertArnIn(partitionCase, arn);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES)
|
||||
void snsTopicArn(PartitionCase partitionCase) {
|
||||
|
||||
@@ -423,6 +423,52 @@ class RequestResourcesTest {
|
||||
+ "connection/web/1234\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
}
|
||||
|
||||
/**
|
||||
* A key is named by its id or by an alias, and an alias names the key it points at, so a request naming a key by
|
||||
* an alias is checked on the key the alias names, as the handler resolves it: an allow scoped to that key passes
|
||||
* and a deny scoped to it holds, whichever of the two names the request used. The alias itself is checked on its
|
||||
* own ARN, and the tagging operations on the key their {@code ResourceArn} names.
|
||||
*/
|
||||
@Test
|
||||
void aKeyIsCheckedOnTheKeyItsNameNames() {
|
||||
String key = "arn:aws:payment-cryptography:us-east-1:000000000000:key/abc123";
|
||||
String alias = "arn:aws:payment-cryptography:us-east-1:000000000000:alias/team/card-key";
|
||||
StoredResourceArns stored = new StoredResourceArns() {
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "payment-cryptography";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
if (!"us-east-1".equals(region)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return switch (type) {
|
||||
case "key" -> List.of("alias/team/card-key").equals(names) || List.of("abc123").equals(names)
|
||||
? Optional.of(key) : Optional.empty();
|
||||
case "alias" -> List.of("alias/team/card-key").equals(names) ? Optional.of(alias) : Optional.empty();
|
||||
default -> Optional.empty();
|
||||
};
|
||||
}
|
||||
};
|
||||
// A key named by an alias is checked on the key the alias points at.
|
||||
assertEquals(List.of(key), RequestResources.of("payment-cryptography:GetKey",
|
||||
json("{\"KeyIdentifier\":\"alias/team/card-key\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
// A key named by its id is checked on that key.
|
||||
assertEquals(List.of(key), RequestResources.of("payment-cryptography:GetKey",
|
||||
json("{\"KeyIdentifier\":\"abc123\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
// A key named by its ARN is checked on that ARN.
|
||||
assertEquals(List.of(key), RequestResources.of("payment-cryptography:GetKey",
|
||||
json("{\"KeyIdentifier\":\"" + key + "\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
// The alias operations are checked on the alias they name.
|
||||
assertEquals(List.of(alias), RequestResources.of("payment-cryptography:GetAlias",
|
||||
json("{\"AliasName\":\"alias/team/card-key\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
// The tagging operations are checked on the key their ResourceArn names.
|
||||
assertEquals(List.of(key), RequestResources.of("payment-cryptography:TagResource",
|
||||
json("{\"ResourceArn\":\"" + key + "\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
}
|
||||
|
||||
/**
|
||||
* A scalable target's ARN is all the id Application Auto Scaling mints, while its requests name it by its service
|
||||
* namespace, resource id and scalable dimension: it is the one stored under those, or else has its id open, as a
|
||||
|
||||
+73
@@ -33,6 +33,7 @@ import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -516,6 +517,49 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
refusedJson(describeUserPool(user, id + "x"), "cognito-idp:DescribeUserPool");
|
||||
}
|
||||
|
||||
/**
|
||||
* A key is named by its id or by an alias, and an alias names the key it points at, so a request naming a key by
|
||||
* an alias is checked on the key the alias names, as the handler resolves it: a grant on that key allows it, a
|
||||
* grant on the alias alone does not, and a deny on the key holds.
|
||||
*/
|
||||
@Test
|
||||
void aKeyNamedByAnAliasIsCheckedOnTheKeyTheAliasNames() {
|
||||
String key = createPaymentKey();
|
||||
String alias = "alias/res-card-" + suffix();
|
||||
createPaymentAlias(alias, key).statusCode(200);
|
||||
|
||||
User user = user("res-key");
|
||||
user.policy(allowOn("payment-cryptography:GetKey", key));
|
||||
getPaymentKey(user, alias).statusCode(200);
|
||||
getPaymentKey(user, key).statusCode(200);
|
||||
refusedJson(getPaymentKey(user, "alias/res-other-" + suffix()), "payment-cryptography:GetKey");
|
||||
|
||||
user.policy(allowOn("payment-cryptography:GetKey", "*") + ",{\"Effect\":\"Deny\",\"Action\":"
|
||||
+ "\"payment-cryptography:GetKey\",\"Resource\":\"" + key + "\"}");
|
||||
refusedJson(getPaymentKey(user, alias), "payment-cryptography:GetKey");
|
||||
}
|
||||
|
||||
/**
|
||||
* A tagging request names its key by the ARN of one, as the reference takes it there, so a request naming it by
|
||||
* its id names no key for the check and is refused: a deny on the key is not skipped by naming it another way.
|
||||
*/
|
||||
@Test
|
||||
void aTaggingRequestNamesItsKeyByItsArn() {
|
||||
String key = createPaymentKey();
|
||||
String id = key.substring(key.lastIndexOf('/') + 1);
|
||||
|
||||
User user = user("res-key-tag");
|
||||
user.policy(allowOn("payment-cryptography:TagResource", "*") + ",{\"Effect\":\"Deny\",\"Action\":"
|
||||
+ "\"payment-cryptography:TagResource\",\"Resource\":\"" + key + "\"}");
|
||||
// The key's own ARN is checked on the key, and the deny holds.
|
||||
refusedJson(tagPaymentKey(user, key), "payment-cryptography:TagResource");
|
||||
// Its id names no ARN for the check, so the request is refused rather than tagging the denied key.
|
||||
tagPaymentKey(user, id).statusCode(400).body(containsString("ValidationException"));
|
||||
admin("payment-cryptography").header("X-Amz-Target", "PaymentCryptographyControlPlane.ListTagsForResource")
|
||||
.contentType("application/x-amz-json-1.0").body("{\"ResourceArn\":\"" + key + "\"}")
|
||||
.post("/").then().statusCode(200).body("Tags", hasSize(0));
|
||||
}
|
||||
|
||||
/** A repository is checked on the ARN its name gives, whether or not it exists. */
|
||||
@Test
|
||||
void aRepositoryIsCheckedOnTheArnItsNameGives() {
|
||||
@@ -1591,6 +1635,35 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
.body("{\"stateMachineArn\":\"" + arn + "\"}").post("/").then();
|
||||
}
|
||||
|
||||
/** A key of the account, made as the administrator. */
|
||||
private String createPaymentKey() {
|
||||
return admin("payment-cryptography").header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateKey")
|
||||
.contentType("application/x-amz-json-1.0").body("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\","
|
||||
+ "\"KeyClass\":\"SYMMETRIC_KEY\",\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true}},\"Exportable\":true}")
|
||||
.post("/").then().statusCode(200).extract().path("Key.KeyArn");
|
||||
}
|
||||
|
||||
/** An alias naming a key, made as the administrator. */
|
||||
private static ValidatableResponse createPaymentAlias(String alias, String key) {
|
||||
return admin("payment-cryptography").header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateAlias")
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.body("{\"AliasName\":\"" + alias + "\",\"KeyArn\":\"" + key + "\"}").post("/").then();
|
||||
}
|
||||
|
||||
/** A tagging request naming its key the way the request gives it. */
|
||||
private static ValidatableResponse tagPaymentKey(User user, String resource) {
|
||||
return user.json("payment-cryptography", "PaymentCryptographyControlPlane.TagResource", "1.0")
|
||||
.body("{\"ResourceArn\":\"" + resource + "\",\"Tags\":[{\"Key\":\"owner\",\"Value\":\"team\"}]}")
|
||||
.post("/").then();
|
||||
}
|
||||
|
||||
/** A read of the key a request names by its id, its alias or its ARN. */
|
||||
private static ValidatableResponse getPaymentKey(User user, String identifier) {
|
||||
return user.json("payment-cryptography", "PaymentCryptographyControlPlane.GetKey", "1.0")
|
||||
.body("{\"KeyIdentifier\":\"" + identifier + "\"}").post("/").then();
|
||||
}
|
||||
|
||||
private static ValidatableResponse describeUserPool(User user, String id) {
|
||||
return user.json("cognito-idp", "AWSCognitoIdentityProviderService.DescribeUserPool", "1.1")
|
||||
.body("{\"UserPoolId\":\"" + id + "\"}").post("/").then();
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.StoredResourceArns;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.eks.EksService;
|
||||
@@ -1167,6 +1168,29 @@ class ResourceArnBuilderTest {
|
||||
return new ResourceArnBuilder(new ObjectMapper(), null, null, null, null, instance);
|
||||
}
|
||||
|
||||
/** A builder reading the stored ARNs of a namespace, for a request of that namespace. */
|
||||
private static ResourceArnBuilder reading(StoredResourceArns stored) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<StoredResourceArns> instance = mock(Instance.class);
|
||||
when(instance.iterator()).thenReturn(List.of(stored).iterator());
|
||||
return new ResourceArnBuilder(new ObjectMapper(), null, null, null, null, null, instance);
|
||||
}
|
||||
|
||||
/** The finder of the stored ARNs of a namespace, answering one name of one type with one ARN. */
|
||||
private static StoredResourceArns stored(String namespace, String type, String name, String arn) {
|
||||
return new StoredResourceArns() {
|
||||
@Override
|
||||
public String namespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String found, List<String> wanted, String region) {
|
||||
return type.equals(found) && List.of(name).equals(wanted) ? Optional.of(arn) : Optional.empty();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -1196,6 +1220,24 @@ class ResourceArnBuilderTest {
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
@Test
|
||||
void paymentCryptographyResolvesAnAliasToTheKeyItPointsAt() {
|
||||
ResourceArnBuilder reading = reading(stored("payment-cryptography", "key", "alias/team/card-key", PC_KEY_ARN));
|
||||
|
||||
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() {
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package io.github.hectorvent.floci.services.paymentcryptography;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* The AES-CMAC of RFC 4493, against the examples the RFC gives.
|
||||
*
|
||||
* <p>The examples cover the three cases the algorithm has: a message of no
|
||||
* length, which is padded and masked with K2; a message of whole blocks, which
|
||||
* is masked with K1; and a message of blocks and a part, whose last block is
|
||||
* padded and masked with K2.
|
||||
*/
|
||||
class PaymentCryptographyCryptoTest {
|
||||
|
||||
/** The key of every example the RFC gives. */
|
||||
private static final String KEY = "2B7E151628AED2A6ABF7158809CF4F3C";
|
||||
|
||||
/** The CMAC of a message under {@link #KEY}, as hexadecimal. */
|
||||
private static String cmac(String message) {
|
||||
return PaymentCryptographyCrypto.toHex(
|
||||
PaymentCryptographyCrypto.cmac(PaymentCryptographyCrypto.fromHex(KEY),
|
||||
PaymentCryptographyCrypto.fromHex(message)));
|
||||
}
|
||||
|
||||
/** RFC 4493's example 1: a message of no length. */
|
||||
@Test
|
||||
void anEmptyMessageIsTheMacTheRfcGives() {
|
||||
assertEquals("bb1d6929e95937287fa37d129b756746", cmac(""));
|
||||
}
|
||||
|
||||
/** RFC 4493's example 2: a message of one whole block. */
|
||||
@Test
|
||||
void aWholeBlockIsTheMacTheRfcGives() {
|
||||
assertEquals("070a16b46b4d4144f79bdd9dd04a287c", cmac("6bc1bee22e409f96e93d7e117393172a"));
|
||||
}
|
||||
|
||||
/** RFC 4493's example 3: two whole blocks and a padded one. */
|
||||
@Test
|
||||
void wholeBlocksAndAPaddedOneAreTheMacTheRfcGives() {
|
||||
assertEquals("dfa66747de9ae63030ca32611497c827", cmac(
|
||||
"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
+ "30c81c46a35ce411"));
|
||||
}
|
||||
|
||||
/** RFC 4493's example 4: four whole blocks. */
|
||||
@Test
|
||||
void severalWholeBlocksAreTheMacTheRfcGives() {
|
||||
assertEquals("51f0bebf7e3b9d92fc49741779363cfe", cmac(
|
||||
"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
+ "30c81c46a35ce411e5fbc1191a0a52ef"
|
||||
+ "f69f2445df4f9b17ad2b417be66c3710"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The check value of a key is the leftmost three bytes of the CMAC of
|
||||
* sixteen zero bytes under it, which is the CMAC of one whole block.
|
||||
*/
|
||||
@Test
|
||||
void theCheckValueIsTheLeftmostThreeBytesOfTheMacOfSixteenZeroBytes() {
|
||||
assertEquals("7AD386", PaymentCryptographyCrypto.checkValue(KEY));
|
||||
}
|
||||
}
|
||||
+414
@@ -0,0 +1,414 @@
|
||||
package io.github.hectorvent.floci.services.paymentcryptography;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class PaymentCryptographyIntegrationTest {
|
||||
|
||||
// The control plane of Payment Cryptography is an awsJson 1.0 service: the AWS SDK
|
||||
// sends this content type and targets PaymentCryptographyControlPlane.<Operation>.
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String TARGET = "PaymentCryptographyControlPlane.";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static Response call(String action, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", TARGET + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static final String CREATE_KEY = """
|
||||
{"KeyAttributes":{"KeyAlgorithm":"AES_256","KeyClass":"SYMMETRIC_KEY",
|
||||
"KeyUsage":"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY",
|
||||
"KeyModesOfUse":{"Encrypt":true,"Decrypt":true}},
|
||||
"Exportable":true}""";
|
||||
|
||||
@Test
|
||||
void aKeyIsCreatedAndReadBack() {
|
||||
String arn = call("CreateKey", CREATE_KEY)
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyArn", matchesPattern(
|
||||
"arn:aws:payment-cryptography:[a-z0-9-]+:[0-9]+:key/[0-9a-f]+"))
|
||||
.body("Key.KeyState", equalTo("CREATE_COMPLETE"))
|
||||
.body("Key.KeyCheckValue", matchesPattern("[0-9A-F]{6}"))
|
||||
.body("Key.KeyCheckValueAlgorithm", equalTo("CMAC"))
|
||||
.body("Key.Enabled", equalTo(true))
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("GetKey", "{\"KeyIdentifier\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyArn", equalTo(arn))
|
||||
.body("Key.KeyAttributes.KeyAlgorithm", equalTo("AES_256"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theKeysOfARegionAreListed() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("ListKeys", "{}")
|
||||
.then().statusCode(200)
|
||||
.body("Keys", hasSize(greaterThanOrEqualTo(1)))
|
||||
.body("Keys.KeyArn", hasItem(equalTo(arn)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAliasIsCreatedAndListed() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/card-key\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Alias.AliasName", equalTo("alias/team/card-key"))
|
||||
.body("Alias.KeyArn", equalTo(arn));
|
||||
|
||||
call("GetAlias", "{\"AliasName\":\"alias/team/card-key\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Alias.KeyArn", equalTo(arn));
|
||||
|
||||
call("ListAliases", "{}")
|
||||
.then().statusCode(200)
|
||||
.body("Aliases.AliasName", hasItem(equalTo("alias/team/card-key")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAliasNameIsUniqueInARegion() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/duplicate\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/duplicate\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ConflictException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyIsTaggedAndUntagged() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("TagResource", "{\"ResourceArn\":\"" + arn + "\","
|
||||
+ "\"Tags\":[{\"Key\":\"Origin\",\"Value\":\"PRIMARY\"}]}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags.Key", contains(equalTo("Origin")))
|
||||
.body("Tags.Value", contains(equalTo("PRIMARY")));
|
||||
|
||||
call("UntagResource", "{\"ResourceArn\":\"" + arn + "\",\"TagKeys\":[\"Origin\"]}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatDoesNotExistIsNotFound() {
|
||||
call("GetKey", "{\"KeyIdentifier\":\"arn:aws:payment-cryptography:us-east-1:000000000000:key/"
|
||||
+ "0123456789abcdef0123456789abcdef\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyIsDeleted() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("GetKey", "{\"KeyIdentifier\":\"" + arn + "\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsupportedOperationIsRefused() {
|
||||
call("EncryptKey", "{}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("UnknownOperationException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A key is created with the usage and the modes of use the reference
|
||||
* requires, and a request without either of them is refused.
|
||||
*/
|
||||
@Test
|
||||
void aKeyWithoutItsUsageOrItsModesIsRefused() {
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true}},\"Exportable\":true}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\"},\"Exportable\":true}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A delete answers with the key, its state DELETE_PENDING and the moment it
|
||||
* is deleted, as the reference does.
|
||||
*/
|
||||
@Test
|
||||
void aDeletedKeyIsAnsweredWithTheKey() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyArn", equalTo(arn))
|
||||
.body("Key.KeyState", equalTo("DELETE_PENDING"))
|
||||
.body("Key.Enabled", equalTo(false))
|
||||
.body("Key.DeletePendingTimestamp", notNullValue())
|
||||
.body("Key.DeleteTimestamp", nullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* The waiting period the reference offers is read, and a request naming one
|
||||
* outside it is refused.
|
||||
*/
|
||||
@Test
|
||||
void aWaitingPeriodOutsideTheServiceRangeIsRefused() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\",\"DeleteKeyInDays\":1}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\",\"DeleteKeyInDays\":7}")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* A listing names the state of the keys it wants, and a request naming
|
||||
* another state than the service knows is refused.
|
||||
*/
|
||||
@Test
|
||||
void theKeysAreListedByTheirState() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("ListKeys", "{\"KeyState\":\"CREATE_COMPLETE\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Keys.KeyArn", hasItem(equalTo(arn)));
|
||||
call("ListKeys", "{\"KeyState\":\"DELETE_PENDING\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Keys", hasSize(0));
|
||||
call("ListKeys", "{\"KeyState\":\"NO_SUCH_STATE\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The key of an alias is optional, so an update naming no key removes the
|
||||
* key the alias names, and the alias stays.
|
||||
*/
|
||||
@Test
|
||||
void anAliasIsDetachedFromItsKeyByAnUpdateNamingNoKey() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/detached\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("UpdateAlias", "{\"AliasName\":\"alias/team/detached\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Alias.AliasName", equalTo("alias/team/detached"))
|
||||
.body("Alias.KeyArn", nullValue());
|
||||
|
||||
call("GetAlias", "{\"AliasName\":\"alias/team/detached\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Alias.AliasName", equalTo("alias/team/detached"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyIsCreatedDisabledAndWithTags() {
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true}},\"Exportable\":false,\"Enabled\":false,"
|
||||
+ "\"Tags\":[{\"Key\":\"owner\",\"Value\":\"team\"}]}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.Enabled", equalTo(false))
|
||||
.body("Key.Exportable", equalTo(false));
|
||||
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaggingRequestNamesItsKeyInResourceArnOnly() {
|
||||
String named = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
String other = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
// The request names one key and another besides it. The change follows the member
|
||||
// the operation names its key in, which is the member the permission check reads.
|
||||
call("TagResource", "{\"ResourceArn\":\"" + other + "\",\"KeyIdentifier\":\"" + named + "\","
|
||||
+ "\"Tags\":[{\"Key\":\"owner\",\"Value\":\"team\"}]}")
|
||||
.then().statusCode(200);
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + other + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(1));
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + named + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* The key operations name their key by the ARN of a key, the ARN of an alias or the name of an alias, as the
|
||||
* reference takes it there, so a bare id names no key and is refused.
|
||||
*/
|
||||
@Test
|
||||
void aKeyIsNamedByItsArnOrByAnAliasAndNotByABareId() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
String id = arn.substring(arn.lastIndexOf('/') + 1);
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/named\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200);
|
||||
String aliasArn = "arn:aws:payment-cryptography:us-east-1:000000000000:alias/team/named";
|
||||
|
||||
for (String named : List.of(arn, "alias/team/named", aliasArn)) {
|
||||
call("GetKey", "{\"KeyIdentifier\":\"" + named + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyArn", equalTo(arn));
|
||||
}
|
||||
call("GetKey", "{\"KeyIdentifier\":\"" + id + "\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The model of an alias requires only its name, but the reference refuses an alias without a key, so the
|
||||
* request is refused the way it refuses it.
|
||||
*/
|
||||
@Test
|
||||
void anAliasMayNotBeCreatedWithoutAKey() {
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/reserved\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"))
|
||||
.body("message", containsString("You can't create an alias without a key."));
|
||||
|
||||
// The name was not taken by the refused request.
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/reserved\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Alias.AliasName", equalTo("alias/team/reserved"))
|
||||
.body("Alias.KeyArn", equalTo(arn));
|
||||
}
|
||||
|
||||
/**
|
||||
* The check value of an AES key is a CMAC, which is the only algorithm this service computes, so a request
|
||||
* naming another one is refused rather than answered with a check value of another kind.
|
||||
*/
|
||||
@Test
|
||||
void aCheckValueAlgorithmOtherThanCmacIsRefused() {
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true}},\"KeyCheckValueAlgorithm\":\"ANSI_X9_24\","
|
||||
+ "\"Exportable\":true}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true}},\"KeyCheckValueAlgorithm\":\"CMAC\","
|
||||
+ "\"Exportable\":true}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyCheckValueAlgorithm", equalTo("CMAC"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The answer carries every mode of use, the ones the request did not ask for as false, so a caller reading them
|
||||
* finds a boolean for each of them.
|
||||
*/
|
||||
@Test
|
||||
void everyModeOfUseIsAnswered() {
|
||||
call("CreateKey", "{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true}},\"Exportable\":true}")
|
||||
.then().statusCode(200)
|
||||
.body("Key.KeyAttributes.KeyModesOfUse", aMapWithSize(9))
|
||||
.body("Key.KeyAttributes.KeyModesOfUse.Encrypt", equalTo(true))
|
||||
.body("Key.KeyAttributes.KeyModesOfUse.Decrypt", equalTo(false))
|
||||
.body("Key.KeyAttributes.KeyModesOfUse.NoRestrictions", equalTo(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* The tagging operations name their key by the ARN of one, as the reference takes it there, so a request naming
|
||||
* it by its id or by an alias names no key for the permission check and is refused.
|
||||
*/
|
||||
@Test
|
||||
void aTaggingRequestNamingItsKeyByAnIdOrAnAliasIsRefused() {
|
||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
String id = arn.substring(arn.lastIndexOf('/') + 1);
|
||||
call("CreateAlias", "{\"AliasName\":\"alias/team/tagged\",\"KeyArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200);
|
||||
|
||||
for (String named : List.of(id, "alias/team/tagged")) {
|
||||
call("TagResource", "{\"ResourceArn\":\"" + named + "\","
|
||||
+ "\"Tags\":[{\"Key\":\"owner\",\"Value\":\"team\"}]}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaggingRequestWithoutItsKeyIsRefused() {
|
||||
call("TagResource", "{\"KeyIdentifier\":\"abc\"}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theKeysAreListedAPageAtATime() {
|
||||
call("CreateKey", CREATE_KEY).then().statusCode(200);
|
||||
call("CreateKey", CREATE_KEY).then().statusCode(200);
|
||||
|
||||
String first = call("ListKeys", "{\"MaxResults\":1}").then().statusCode(200)
|
||||
.body("Keys", hasSize(1))
|
||||
.extract().path("NextToken");
|
||||
|
||||
call("ListKeys", "{\"MaxResults\":1,\"NextToken\":\"" + first + "\"}")
|
||||
.then().statusCode(200)
|
||||
.body("Keys", hasSize(1));
|
||||
}
|
||||
}
|
||||
+357
@@ -0,0 +1,357 @@
|
||||
package io.github.hectorvent.floci.services.paymentcryptography;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.paymentcryptography.model.PaymentCryptographyAlias;
|
||||
import io.github.hectorvent.floci.services.paymentcryptography.model.PaymentCryptographyKey;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PaymentCryptographyServiceTest {
|
||||
|
||||
private PaymentCryptographyService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new PaymentCryptographyService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
new PaymentCryptographyRequestContext());
|
||||
}
|
||||
|
||||
private PaymentCryptographyKey createKey(String region) {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", "AES_256");
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
attributes.put("KeyUsage", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
Map<String, Boolean> modes = new LinkedHashMap<>();
|
||||
modes.put("Encrypt", Boolean.TRUE);
|
||||
modes.put("Decrypt", Boolean.TRUE);
|
||||
return service.createKey(region, attributes, modes, true, true, Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCreatedKeyIsInItsRegionAndCarriesMaterial() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
|
||||
assertTrue(key.getArn().startsWith("arn:aws:payment-cryptography:us-east-1:000000000000:key/"),
|
||||
key.getArn());
|
||||
assertEquals("CREATE_COMPLETE", key.getKeyState());
|
||||
assertEquals(64, key.getKeyMaterialHex().length());
|
||||
assertNotNull(key.getKeyCheckValue());
|
||||
assertEquals(6, key.getKeyCheckValue().length());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCheckValueIsTheLeftmostThreeBytesOfTheCmac() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
|
||||
// The CMAC of sixteen zero bytes under the key, leftmost three bytes.
|
||||
assertEquals(PaymentCryptographyCrypto.checkValue(key.getKeyMaterialHex()),
|
||||
key.getKeyCheckValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theKeyCarriesTheModesItWasAskedFor() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
|
||||
assertEquals(Boolean.TRUE, key.getKeyModesOfUse().get("Encrypt"));
|
||||
assertEquals(Boolean.TRUE, key.getKeyModesOfUse().get("Decrypt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyInAnotherRegionIsNotThisRegionsKey() {
|
||||
PaymentCryptographyKey west = createKey("us-west-2");
|
||||
|
||||
assertNull(service.findKey("us-east-1", west.getArn()),
|
||||
"a key in another region was found in this one");
|
||||
assertNull(service.findKey("us-east-1", west.getKeyId()),
|
||||
"a key in another region was found in this one");
|
||||
assertNotNull(service.findKey("us-west-2", west.getArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoKeysGetDifferentMaterial() {
|
||||
PaymentCryptographyKey first = createKey("us-east-1");
|
||||
PaymentCryptographyKey second = createKey("us-east-1");
|
||||
|
||||
assertTrue(!first.getKeyMaterialHex().equals(second.getKeyMaterialHex()),
|
||||
"two keys share their material");
|
||||
// The check value is three bytes of the CMAC of the key, so two keys may
|
||||
// legitimately share one. Only the material has to differ.
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAliasNameIsUniqueInARegion() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
|
||||
AwsException conflict = assertThrows(AwsException.class,
|
||||
() -> service.createAlias("alias/team/card-key", createKey("us-east-1")));
|
||||
assertEquals("ConflictException", conflict.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSameAliasNameInAnotherRegionIsAnotherAlias() {
|
||||
service.createAlias("alias/team/card-key", createKey("us-east-1"));
|
||||
PaymentCryptographyAlias second = service.createAlias("alias/team/card-key",
|
||||
createKey("us-west-2"));
|
||||
|
||||
assertEquals("us-west-2", second.getRegion());
|
||||
assertEquals(1, service.listAliases("us-east-1", null).size());
|
||||
assertEquals(1, service.listAliases("us-west-2", null).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theTagsAreTheKeysOwn() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.tag(key, Map.of("Origin", "PRIMARY"));
|
||||
|
||||
assertEquals("PRIMARY", service.listTags(key).get("Origin"));
|
||||
assertEquals(1, service.listTags(key).size());
|
||||
|
||||
service.untag(key, List.of("Origin"));
|
||||
assertTrue(service.listTags(key).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyIsFoundByItsArnAndByItsAlias() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
|
||||
assertEquals(key.getKeyId(), service.findKey("us-east-1", key.getArn()).getKeyId());
|
||||
assertEquals(key.getKeyId(),
|
||||
service.findKey("us-east-1", "alias/team/card-key").getKeyId());
|
||||
assertNull(service.findKey("us-west-2", "alias/team/card-key"),
|
||||
"an alias of one region is not an alias of another");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedKeyTakesItsAliasesWithIt() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
service.deleteKey(key);
|
||||
|
||||
assertNull(service.findKey("us-east-1", key.getArn()));
|
||||
assertNull(service.findAlias("us-east-1", "alias/team/card-key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyMayBeCreatedDisabledAndWithTags() {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", "AES_256");
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
attributes.put("KeyUsage", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
PaymentCryptographyKey key = service.createKey("us-east-1", attributes, Map.of(), false,
|
||||
false, Map.of("owner", "team"));
|
||||
|
||||
assertFalse(key.isEnabled(), "the key is enabled although it was created disabled");
|
||||
assertEquals("team", key.getTags().get("owner"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedKeyDoesNotComeBackWhenItIsTagged() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.deleteKey(key);
|
||||
|
||||
AwsException gone = assertThrows(AwsException.class,
|
||||
() -> service.tag(key, Map.of("owner", "team")));
|
||||
assertEquals("ResourceNotFoundException", gone.getErrorCode());
|
||||
assertNull(service.findKey("us-east-1", key.getKeyId()),
|
||||
"the key is in the store again after it was deleted");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theKeysAreListedAPageAtATime() {
|
||||
createKey("us-east-1");
|
||||
createKey("us-east-1");
|
||||
createKey("us-east-1");
|
||||
|
||||
PaginatedResult<PaymentCryptographyKey> first = service.listKeys("us-east-1", null, 2, null);
|
||||
assertEquals(2, first.items().size());
|
||||
assertNotNull(first.nextToken(), "there are more keys, so there is a token");
|
||||
|
||||
PaginatedResult<PaymentCryptographyKey> second =
|
||||
service.listKeys("us-east-1", null, 2, first.nextToken());
|
||||
assertEquals(1, second.items().size());
|
||||
assertNull(second.nextToken(), "the last page has no token");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatAsksForMaterialTheServiceDoesNotMakeIsRefused() {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", "AES_128");
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
attributes.put("KeyUsage", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
AwsException bad = assertThrows(AwsException.class,
|
||||
() -> service.createKey("us-east-1", attributes, Map.of(), true, true, Map.of()));
|
||||
assertEquals("ValidationException", bad.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPageSizeBelowOneIsRefused() {
|
||||
createKey("us-east-1");
|
||||
|
||||
AwsException bad = assertThrows(AwsException.class,
|
||||
() -> service.listKeys("us-east-1", null, 0, null));
|
||||
assertEquals("ValidationException", bad.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTokenThatNamesNoPageIsRefused() {
|
||||
createKey("us-east-1");
|
||||
|
||||
AwsException bad = assertThrows(AwsException.class,
|
||||
() -> service.listKeys("us-east-1", null, 2, "not a token"));
|
||||
assertEquals("ValidationException", bad.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* A key's usage says what the key is for, and the service requires it, as
|
||||
* the reference does.
|
||||
*/
|
||||
@Test
|
||||
void aKeyWithoutItsUsageIsRefused() {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", "AES_256");
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
|
||||
AwsException bad = assertThrows(AwsException.class,
|
||||
() -> service.createKey("us-east-1", attributes, Map.of(), true, true, Map.of()));
|
||||
assertEquals("ValidationException", bad.getErrorCode());
|
||||
}
|
||||
|
||||
/** A request naming a state is answered with the keys in that state only. */
|
||||
@Test
|
||||
void theKeysAreListedByTheirState() {
|
||||
createKey("us-east-1");
|
||||
createKey("us-east-1");
|
||||
|
||||
assertEquals(2, service.listKeys("us-east-1", "CREATE_COMPLETE").size());
|
||||
assertEquals(0, service.listKeys("us-east-1", "DELETE_PENDING").size());
|
||||
assertEquals(0, service.listKeys("us-east-1", "CREATE_IN_PROGRESS").size());
|
||||
}
|
||||
|
||||
/**
|
||||
* A deleted key is answered with the key, its state DELETE_PENDING and the
|
||||
* moment it is deleted.
|
||||
*/
|
||||
@Test
|
||||
void aDeletedKeyIsAnsweredWithTheKeyItWas() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
|
||||
PaymentCryptographyKey deleted = service.deleteKey(key);
|
||||
|
||||
assertEquals(key.getArn(), deleted.getArn());
|
||||
assertEquals("DELETE_PENDING", deleted.getKeyState());
|
||||
assertFalse(deleted.isEnabled());
|
||||
assertTrue(deleted.getDeletePendingTimestamp() > 0);
|
||||
assertEquals(0, deleted.getDeleteTimestamp(),
|
||||
"a key that is pending deletion carries no deletion timestamp");
|
||||
}
|
||||
|
||||
/**
|
||||
* An update naming no key removes the key the alias names, and the alias
|
||||
* stays.
|
||||
*/
|
||||
@Test
|
||||
void anAliasIsDetachedFromItsKey() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
|
||||
PaymentCryptographyAlias detached = service.detachAlias("us-east-1", "alias/team/card-key");
|
||||
|
||||
assertEquals("alias/team/card-key", detached.getAliasName());
|
||||
assertNull(detached.getKeyArn());
|
||||
assertNull(service.findKey("us-east-1", "alias/team/card-key"),
|
||||
"a detached alias still names a key");
|
||||
assertNotNull(service.findAlias("us-east-1", "alias/team/card-key"));
|
||||
}
|
||||
|
||||
/** The namespace of the resource types the service finds, as the request-resources table names it. */
|
||||
@Test
|
||||
void theNamespaceIsTheOneTheTableNames() {
|
||||
assertEquals("payment-cryptography", service.namespace());
|
||||
}
|
||||
|
||||
/**
|
||||
* A key is named by its id or by an alias, and an alias names the key it points at, so a request naming a key by
|
||||
* an alias is checked on the key the alias names, as the handler resolves it.
|
||||
*/
|
||||
@Test
|
||||
void aKeyIsFoundUnderItsIdAndUnderTheAliasThatNamesIt() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
|
||||
assertEquals(Optional.of(key.getArn()), service.find("key", List.of(key.getKeyId()), "us-east-1"));
|
||||
assertEquals(Optional.of(key.getArn()), service.find("key", List.of("alias/team/card-key"), "us-east-1"));
|
||||
assertEquals(Optional.of(key.getArn()), service.find("key", List.of(key.getArn()), "us-east-1"));
|
||||
assertEquals(Optional.empty(), service.find("key", List.of("alias/team/no-such-key"), "us-east-1"));
|
||||
assertEquals(Optional.empty(), service.find("key", List.of("no-such-key"), "us-east-1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias ARN names the alias in the Region, the account and the partition it was minted in, so an alias ARN
|
||||
* of another Region names no alias here and the key of this Region's alias is not the one it would select.
|
||||
*/
|
||||
@Test
|
||||
void anAliasArnOfAnotherRegionNamesNoAliasHere() {
|
||||
PaymentCryptographyKey key = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", key);
|
||||
String aliasArn = "arn:aws:payment-cryptography:us-east-1:000000000000:alias/team/card-key";
|
||||
|
||||
assertEquals(key, service.findKey("us-east-1", aliasArn));
|
||||
assertNull(service.findKey("us-east-1",
|
||||
"arn:aws:payment-cryptography:us-west-2:000000000000:alias/team/card-key"),
|
||||
"an alias ARN of another Region selected this Region's key");
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias is resolved once for the request, so that the request acts on the key the permission check was
|
||||
* made on: an alias moved to another key in between would otherwise let the request act on a key no policy
|
||||
* was checked against.
|
||||
*/
|
||||
@Test
|
||||
void anAliasIsResolvedOnceForTheRequest() {
|
||||
PaymentCryptographyKey allowed = createKey("us-east-1");
|
||||
PaymentCryptographyKey denied = createKey("us-east-1");
|
||||
service.createAlias("alias/team/card-key", allowed);
|
||||
|
||||
// The permission check resolves the alias, which is the key the request is checked on.
|
||||
assertEquals(Optional.of(allowed.getArn()), service.find("key", List.of("alias/team/card-key"), "us-east-1"));
|
||||
|
||||
// The alias is moved to another key while the request runs.
|
||||
service.updateAlias("alias/team/card-key", denied);
|
||||
|
||||
assertEquals(allowed, service.findKey("us-east-1", "alias/team/card-key"),
|
||||
"the request did not act on the key the permission check was made on");
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias's ARN is the name it was given, which carries the {@code alias/} its own resource part carries too,
|
||||
* so it is that name without the prefix.
|
||||
*/
|
||||
@Test
|
||||
void anAliasIsFoundUnderItsName() {
|
||||
assertEquals(Optional.of("arn:aws:payment-cryptography:us-east-1:000000000000:alias/team/card-key"),
|
||||
service.find("alias", List.of("alias/team/card-key"), "us-east-1"));
|
||||
assertEquals(Optional.empty(), service.find("approval-team", List.of("whatever"), "us-east-1"));
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package io.github.hectorvent.floci.services.paymentcryptography.model;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* How a key is written to the store.
|
||||
*/
|
||||
class PaymentCryptographyKeyTest {
|
||||
|
||||
/**
|
||||
* The material of a key is held by the key while the emulator runs, and is
|
||||
* not written to the store: no operation of the service returns it, so
|
||||
* leaving it out of the persisted key keeps it off the disk.
|
||||
*/
|
||||
@Test
|
||||
void theMaterialOfAKeyIsNotWrittenToTheStore() throws Exception {
|
||||
PaymentCryptographyKey key = new PaymentCryptographyKey();
|
||||
key.setKeyId("abc123");
|
||||
key.setKeyMaterialHex("0f1e2d3c4b5a69788796a5b4c3d2e1f0");
|
||||
key.setKeyCheckValue("7AD386");
|
||||
|
||||
String written = new ObjectMapper().writeValueAsString(key);
|
||||
|
||||
assertFalse(written.contains("keyMaterialHex"), written);
|
||||
assertFalse(written.contains("0f1e2d3c4b5a69788796a5b4c3d2e1f0"), written);
|
||||
assertTrue(written.contains("keyCheckValue"), written);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user