mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): update a KMS key in place unless the engine says it must be replaced (#8718)
An AWS::KMS::Key update created a new key every time and left the old one enabled. The key now keeps its id: Description, KeyPolicy, Enabled, EnableKeyRotation and Tags are updated in place, keeping tags added outside the stack, and a failed stack update puts them back. A changed KeySpec, KeyUsage, Origin or MultiRegion fails the update, as the AWS::KMS::Key reference says. Create now honors KeyPolicy, Enabled, EnableKeyRotation, Origin and MultiRegion, and a key that a failed create or update leaves behind is scheduled for deletion. Part of #6606
This commit is contained in:
1 parent
0e782b62f2
commit
790b93e739
8 files changed
+430
-20
No files matched your search
+138
-1
@@ -9,10 +9,17 @@ import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationKmsKeyPropertiesIntegrationTest {
|
||||
@@ -59,7 +66,137 @@ class CloudFormationKmsKeyPropertiesIntegrationTest {
|
||||
() -> assertEquals("ENCRYPT_DECRYPT", key.getString("KeyMetadata.KeyUsage")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackChangesDescriptionAndTagsInPlace() {
|
||||
String account = TestAccounts.randomId();
|
||||
String stack = "kms-cfn-update-" + account;
|
||||
String auth = EnforcementFixtures.auth(account, "cloudformation");
|
||||
CfnRequests.deploy("CreateStack", stack, template("v1", tags("one")), auth, "CREATE_COMPLETE");
|
||||
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, auth);
|
||||
String arn = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId))
|
||||
.getString("KeyMetadata.Arn");
|
||||
int keysBefore = kms(account, "ListKeys", "{}").getList("Keys").size();
|
||||
|
||||
CfnRequests.deploy("UpdateStack", stack, template("v2", tags("two")), auth, "UPDATE_COMPLETE");
|
||||
|
||||
JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId));
|
||||
assertAll(
|
||||
() -> assertEquals(keyId, CfnRequests.physicalId(stack, KEY_RESOURCE, auth)),
|
||||
() -> assertEquals(arn, key.getString("KeyMetadata.Arn")),
|
||||
() -> assertEquals("v2", key.getString("KeyMetadata.Description")),
|
||||
() -> assertEquals(keysBefore, kms(account, "ListKeys", "{}").getList("Keys").size()),
|
||||
() -> assertEquals(List.of("two"), kms(account, "ListResourceTags",
|
||||
"{\"KeyId\":\"%s\"}".formatted(keyId)).getList("Tags.TagValue")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackRollsBackWhenKeyUsageChanges() {
|
||||
String account = TestAccounts.randomId();
|
||||
String stack = "kms-cfn-immutable-" + account;
|
||||
String auth = EnforcementFixtures.auth(account, "cloudformation");
|
||||
CfnRequests.deploy("CreateStack", stack, template(KEY_DESCRIPTION, ""), auth, "CREATE_COMPLETE");
|
||||
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, auth);
|
||||
int keysBefore = kms(account, "ListKeys", "{}").getList("Keys").size();
|
||||
|
||||
CfnRequests.deploy("UpdateStack", stack, template("v2", "\"KeySpec\": \"ECC_NIST_P256\",\n\"KeyUsage\": \"SIGN_VERIFY\""),
|
||||
auth, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId));
|
||||
assertAll(
|
||||
() -> assertEquals(keyId, CfnRequests.physicalId(stack, KEY_RESOURCE, auth)),
|
||||
() -> assertEquals(KEY_DESCRIPTION, key.getString("KeyMetadata.Description")),
|
||||
() -> assertEquals("ENCRYPT_DECRYPT", key.getString("KeyMetadata.KeyUsage")),
|
||||
() -> assertEquals(keysBefore, kms(account, "ListKeys", "{}").getList("Keys").size()));
|
||||
}
|
||||
|
||||
private static final String POLICY_ONE = policy("PolicyOne");
|
||||
private static final String POLICY_TWO = policy("PolicyTwo");
|
||||
|
||||
static Stream<Arguments> honoredCreateProperties() {
|
||||
return Stream.of(
|
||||
Arguments.of("\"KeyPolicy\": " + POLICY_ONE, "GetKeyPolicy", "Policy", "PolicyOne"),
|
||||
Arguments.of("\"Enabled\": false", "DescribeKey", "KeyMetadata.KeyState", "Disabled"),
|
||||
Arguments.of("\"Enabled\": false", "DescribeKey", "KeyMetadata.Enabled", "false"),
|
||||
Arguments.of("\"EnableKeyRotation\": true", "GetKeyRotationStatus", "KeyRotationEnabled", "true"),
|
||||
Arguments.of("\"Origin\": \"EXTERNAL\"", "DescribeKey", "KeyMetadata.Origin", "EXTERNAL"),
|
||||
Arguments.of("\"Origin\": \"EXTERNAL\"", "DescribeKey", "KeyMetadata.KeyState", "PendingImport"),
|
||||
Arguments.of("\"MultiRegion\": true", "DescribeKey", "KeyMetadata.MultiRegion", "true"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("honoredCreateProperties")
|
||||
void createStackHonorsKeyProperty(String property, String action, String path, String expected) {
|
||||
String account = TestAccounts.randomId();
|
||||
String stack = "kms-cfn-honor-" + account;
|
||||
String auth = EnforcementFixtures.auth(account, "cloudformation");
|
||||
CfnRequests.deploy("CreateStack", stack, template(property), auth, "CREATE_COMPLETE");
|
||||
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, auth);
|
||||
|
||||
String actual = kms(account, action, "{\"KeyId\":\"%s\"}".formatted(keyId)).getString(path);
|
||||
|
||||
assertTrue(actual.contains(expected), path + " was " + actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateChangesKeyPropertiesInPlaceAndARolledBackUpdateRestoresThem() {
|
||||
String account = TestAccounts.randomId();
|
||||
String stack = "kms-cfn-lifecycle-" + account;
|
||||
String auth = EnforcementFixtures.auth(account, "cloudformation");
|
||||
String keyBody = "{\"KeyId\":\"%s\"}";
|
||||
CfnRequests.deploy("CreateStack", stack, template("v1",
|
||||
"\"Enabled\": false,\n\"EnableKeyRotation\": false,\n\"KeyPolicy\": " + POLICY_ONE + ",\n" + tags("one")),
|
||||
auth, "CREATE_COMPLETE");
|
||||
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, auth);
|
||||
|
||||
CfnRequests.deploy("UpdateStack", stack, template("v2",
|
||||
"\"Enabled\": true,\n\"EnableKeyRotation\": true,\n\"KeyPolicy\": " + POLICY_TWO + ",\n" + tags("two")),
|
||||
auth, "UPDATE_COMPLETE");
|
||||
|
||||
JsonPath updated = kms(account, "DescribeKey", keyBody.formatted(keyId));
|
||||
assertAll(
|
||||
() -> assertEquals(keyId, CfnRequests.physicalId(stack, KEY_RESOURCE, auth)),
|
||||
() -> assertEquals("Enabled", updated.getString("KeyMetadata.KeyState")),
|
||||
() -> assertTrue(kms(account, "GetKeyRotationStatus", keyBody.formatted(keyId))
|
||||
.getBoolean("KeyRotationEnabled")),
|
||||
() -> assertTrue(kms(account, "GetKeyPolicy", keyBody.formatted(keyId))
|
||||
.getString("Policy").contains("PolicyTwo")));
|
||||
|
||||
String failing = template("v3", "\"Enabled\": false,\n\"EnableKeyRotation\": false,\n" + tags("three"))
|
||||
.replace("\n }\n}", ",\n"
|
||||
+ "\"BadSecret\": {\"Type\": \"AWS::SecretsManager::Secret\", \"Properties\": "
|
||||
+ "{\"SecretString\": \"x\", \"GenerateSecretString\": {\"PasswordLength\": 8}}}\n }\n}");
|
||||
CfnRequests.deploy("UpdateStack", stack, failing, auth, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
JsonPath restored = kms(account, "DescribeKey", keyBody.formatted(keyId));
|
||||
assertAll(
|
||||
() -> assertEquals(keyId, CfnRequests.physicalId(stack, KEY_RESOURCE, auth)),
|
||||
() -> assertEquals("v2", restored.getString("KeyMetadata.Description")),
|
||||
() -> assertEquals("Enabled", restored.getString("KeyMetadata.KeyState")),
|
||||
() -> assertTrue(kms(account, "GetKeyRotationStatus", keyBody.formatted(keyId))
|
||||
.getBoolean("KeyRotationEnabled")),
|
||||
() -> assertEquals(List.of("two"), kms(account, "ListResourceTags", keyBody.formatted(keyId))
|
||||
.getList("Tags.TagValue")));
|
||||
|
||||
CfnRequests.deploy("UpdateStack", stack, template("v4", ""), auth, "UPDATE_COMPLETE");
|
||||
|
||||
List<String> tagKeys = kms(account, "ListResourceTags", keyBody.formatted(keyId)).getList("Tags.TagKey");
|
||||
assertTrue(tagKeys == null || !tagKeys.contains("env"), "tags after the drop: " + tagKeys);
|
||||
}
|
||||
|
||||
private static String policy(String sid) {
|
||||
return "{\"Version\": \"2012-10-17\", \"Statement\": [{\"Sid\": \"%s\", \"Effect\": \"Allow\", "
|
||||
.formatted(sid) + "\"Principal\": {\"AWS\": \"*\"}, \"Action\": \"kms:*\", \"Resource\": \"*\"}]}";
|
||||
}
|
||||
|
||||
private static String tags(String value) {
|
||||
return "\"Tags\": [{\"Key\": \"env\", \"Value\": \"%s\"}]".formatted(value);
|
||||
}
|
||||
|
||||
private static String template(String additionalProperties) {
|
||||
return template(KEY_DESCRIPTION, additionalProperties);
|
||||
}
|
||||
|
||||
private static String template(String description, String additionalProperties) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
@@ -71,7 +208,7 @@ class CloudFormationKmsKeyPropertiesIntegrationTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(KEY_DESCRIPTION, additionalProperties.isEmpty() ? ""
|
||||
""".formatted(description, additionalProperties.isEmpty() ? ""
|
||||
: ",\n" + additionalProperties.strip());
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -100,8 +100,8 @@ class LeafCfnProvisionerTest {
|
||||
|
||||
assertEquals("k-123", r.getPhysicalId(), "Ref is the key id (schema primaryIdentifier)");
|
||||
// aws-kms-key.json readOnlyProperties
|
||||
assertEquals(Map.of("Arn", "arn:aws:kms:us-east-1:000000000000:key/k-123", "KeyId", "k-123"),
|
||||
r.getAttributes());
|
||||
assertEquals(Map.of("Arn", "arn:aws:kms:us-east-1:000000000000:key/k-123", "KeyId", "k-123",
|
||||
KmsCfnProvisioner.TAG_KEYS_ATTR, "[\"env\"]"), r.getAttributes());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,7 +138,8 @@ class LeafCfnProvisionerTest {
|
||||
|
||||
verify(kms, never()).describeKey(anyString(), anyString());
|
||||
assertEquals("k-real", r.getPhysicalId());
|
||||
assertEquals(Map.of("Arn", key.getArn(), "KeyId", "k-real"), r.getAttributes());
|
||||
assertEquals(Map.of("Arn", key.getArn(), "KeyId", "k-real", KmsCfnProvisioner.TAG_KEYS_ATTR, "[]"),
|
||||
r.getAttributes());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user