mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(kms): grant fidelity (#2639)
* fix: preserve KMS grant metadata * fix(kms): reject GrantOperation values outside the modeled enum CreateGrant stored the Operations list verbatim, so a grant could be persisted and echoed back with an operation name AWS would have refused. Validate each element against the GrantOperation enum and raise ValidationException, matching the other CreateGrant input guards. * fix(kms): validate CreateGrant Name against GrantNameType constraint Real AWS enforces GrantNameType's pattern (^[a-zA-Z0-9:/_-]+$) and 256-char max length on CreateGrant's Name field. Add the same validation so malformed names are rejected the way AWS rejects them. * fix(kms): validate CreateGrant Constraints and fix name-length error message Two review findings on CreateGrant: - Constraints previously passed through unvalidated: an unknown member, a non-map EncryptionContextSubset/EncryptionContextEquals, or a malformed SourceArn would be persisted and echoed back on ListGrants even though real KMS rejects the request. Validate against the modeled GrantConstraints shape (kms/2014-11-01/service-2.json). - Name's length check used the max-length message for both the empty and over-256 cases; an empty name now cites the min-length-1 constraint instead. * fix(kms): reject non-object CreateGrant Constraints instead of silently dropping it The handler converted Constraints to null whenever it wasn't a JSON object, so a raw string or array value was treated as absent and the grant was created without ever reaching validateGrantConstraints, instead of rejecting the malformed request as real KMS does.
This commit is contained in:
1 parent
bf6c7b001c
commit
5400204563
5 files changed
+277
-2
No files matched your search
@@ -563,6 +563,8 @@ class KmsIntegrationTest {
|
||||
{
|
||||
"KeyId": "%s",
|
||||
"GranteePrincipal": "arn:aws:iam::000000000000:user/grantee",
|
||||
"Name": "vellum-tenant-round-trip",
|
||||
"Constraints": {"EncryptionContextEquals": {"tenant_id": "tenant-001"}},
|
||||
"Operations": ["Encrypt", "Decrypt"]
|
||||
}
|
||||
""".formatted(keyId))
|
||||
@@ -587,6 +589,8 @@ class KmsIntegrationTest {
|
||||
.body("Grants[0].GrantId", equalTo(grantId))
|
||||
.body("Grants[0].KeyId", startsWith("arn:aws:kms:"))
|
||||
.body("Grants[0].GranteePrincipal", equalTo("arn:aws:iam::000000000000:user/grantee"))
|
||||
.body("Grants[0].Name", equalTo("vellum-tenant-round-trip"))
|
||||
.body("Grants[0].Constraints.EncryptionContextEquals.tenant_id", equalTo("tenant-001"))
|
||||
.body("Grants[0].Operations[0]", equalTo("Encrypt"))
|
||||
.body("Grants[0].Operations[1]", equalTo("Decrypt"))
|
||||
.body("Truncated", equalTo(false));
|
||||
@@ -784,6 +788,36 @@ class KmsIntegrationTest {
|
||||
.body("Truncated", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantWithNonObjectConstraintsReturnsValidationException() {
|
||||
// The handler previously converted any non-object Constraints (e.g. a raw string or
|
||||
// array) to null before it reached KmsService, so a malformed request was silently
|
||||
// treated as "no constraints" instead of rejected.
|
||||
String keyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"Description\":\"constraints-type-check\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateGrant")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"KeyId": "%s",
|
||||
"GranteePrincipal": "arn:aws:iam::000000000000:user/grantee",
|
||||
"Operations": ["Encrypt"],
|
||||
"Constraints": "not-an-object"
|
||||
}
|
||||
""".formatted(keyId))
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokeGrantReturnsNotFoundForUnknownGrant() {
|
||||
String keyId = given()
|
||||
|
||||
@@ -162,6 +162,113 @@ class KmsServiceTest {
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantUnknownOperationThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt", "NotARealOperation"), REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("NotARealOperation"),
|
||||
"message should name the rejected operation, was: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantInvalidNameCharacterThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, "invalid name with spaces", null, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("'name'"),
|
||||
"message should name the rejected field, was: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantNameTooLongThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
String tooLong = "a".repeat(257);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, tooLong, null, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("less than or equal to 256"),
|
||||
"too-long-name message should cite the max-length constraint, was: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantEmptyNameThrowsValidationWithMinLengthMessage() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, "", null, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("greater than or equal to 1"),
|
||||
"empty-name message should cite the min-length constraint, was: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantConstraintsUnknownMemberThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
Map<String, Object> constraints = new LinkedHashMap<>();
|
||||
constraints.put("NotARealConstraint", "value");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, null, constraints, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantConstraintsStringValuedEncryptionContextEqualsThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
Map<String, Object> constraints = new LinkedHashMap<>();
|
||||
constraints.put("EncryptionContextEquals", "not-a-map");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, null, constraints, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantConstraintsInvalidSourceArnThrowsValidation() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
Map<String, Object> constraints = new LinkedHashMap<>();
|
||||
constraints.put("SourceArn", "not-an-arn");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, null, constraints, REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantConstraintsValidEncryptionContextRoundTrips() {
|
||||
KmsKey key = kmsService.createKey("grant key", REGION);
|
||||
Map<String, Object> constraints = new LinkedHashMap<>();
|
||||
Map<String, String> encryptionContext = new LinkedHashMap<>();
|
||||
encryptionContext.put("purpose", "test");
|
||||
constraints.put("EncryptionContextEquals", encryptionContext);
|
||||
constraints.put("SourceArn", "arn:aws:iam::123456789012:role/test-role");
|
||||
|
||||
KmsGrant grant = kmsService.createGrant(key.getKeyId(), "arn:aws:iam::000000000000:user/grantee",
|
||||
List.of("Encrypt"), null, null, constraints, REGION);
|
||||
|
||||
assertEquals(constraints, grant.getConstraints());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGrantUnknownKeyThrowsNotFound() {
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
|
||||
Reference in new issue
Block a user