Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ssm): include saved key ids in parameter history (#6575)
Jimmy Liu Jia0e955cf07b9e
diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.javaindex 2bc67ba963..bd51c7e28e 100644--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java@@ -301,6 +301,9 @@ public class SsmJsonHandler {node.put("Version", h.getVersion());node.put("Value", h.getValue());node.put("Type", h.getType());+ if (h.getKeyId() != null) {+ node.put("KeyId", h.getKeyId());+ }node.put("LastModifiedDate", h.getLastModifiedDate().toEpochMilli() / 1000.0);if (h.getDescription() != null) {node.put("Description", h.getDescription());@@ -394,8 +397,9 @@ public class SsmJsonHandler {node.put("Description", p.getDescription());}node.put("DataType", p.getDataType());- if (SsmService.keyIdOf(p) != null) {- node.put("KeyId", SsmService.keyIdOf(p));+ String keyId = SsmService.keyIdOf(p);+ if (keyId != null) {+ node.put("KeyId", keyId);}if (p.getAllowedPattern() != null) {node.put("AllowedPattern", p.getAllowedPattern());diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.javaindex e50d954d5a..2896f73bb8 100644--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java@@ -303,9 +303,7 @@ public class SsmService implements ResourceProvider {Parameter parameter = new Parameter(name, value, type != null ? type : "String");parameter.setVersion(version);parameter.setDescription(description);- if ("SecureString".equals(parameter.getType())) {- parameter.setKeyId(keyId);- }+ parameter.setKeyId(keyIdOf(parameter.getType(), keyId));parameter.setAllowedPattern(allowedPattern);// AWS keeps existing policies until new ones, or an empty list, are sent.if (policies == null) {@@ -764,10 +762,14 @@ public class SsmService implements ResourceProvider {/** A SecureString put without a KeyId is encrypted with the AWS managed key. */static String keyIdOf(Parameter parameter) {- if (!"SecureString".equals(parameter.getType())) {+ return keyIdOf(parameter.getType(), parameter.getKeyId());+ }++ static String keyIdOf(String type, String keyId) {+ if (!"SecureString".equals(type)) {return null;}- return parameter.getKeyId() != null ? parameter.getKeyId() : DEFAULT_SSM_KEY_ID;+ return keyId != null ? keyId : DEFAULT_SSM_KEY_ID;}private static boolean matchesAny(String actual, ParameterStringFilter filter) {diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterHistoryKeyIdIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterHistoryKeyIdIntegrationTest.javanew file mode 100644index 0000000000..67a0f460b6--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterHistoryKeyIdIntegrationTest.java@@ -0,0 +1,112 @@+package io.github.hectorvent.floci.services.ssm;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;+import io.restassured.path.json.JsonPath;+import jakarta.inject.Inject;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;+import software.amazon.awssdk.regions.Region;+import software.amazon.awssdk.services.ssm.SsmClient;+import software.amazon.awssdk.services.ssm.model.ParameterHistory;++import java.net.URI;+import java.util.List;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNull;++@QuarkusTest+class SsmParameterHistoryKeyIdIntegrationTest {++ private static final String CONTENT_TYPE = "application/x-amz-json-1.1";++ @Inject+ SsmService ssmService;++ @BeforeAll+ static void configureContentTypes() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void getParameterHistoryReturnsEachSecureVersionKeyId() {+ String name = "/history-key-id-" + UUID.randomUUID();+ try {+ put(name, "first", "alias/first-key", false);+ put(name, "second", "alias/second-key", true);+ put(name, "third", null, true);++ JsonPath history = given()+ .header("X-Amz-Target", "AmazonSSM.GetParameterHistory")+ .contentType(CONTENT_TYPE)+ .body("{\"Name\":\"" + name + "\"}")+ .when().post("/")+ .then().statusCode(200)+ .extract().jsonPath();++ assertEquals("alias/first-key", history.getString("Parameters[0].KeyId"));+ assertEquals("alias/second-key", history.getString("Parameters[1].KeyId"));+ assertEquals("alias/aws/ssm", history.getString("Parameters[2].KeyId"));++ try (SsmClient client = SsmClient.builder()+ .endpointOverride(URI.create("http://localhost:" + RestAssured.port))+ .httpClientBuilder(UrlConnectionHttpClient.builder())+ .region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))+ .build()) {+ List<ParameterHistory> versions = client.getParameterHistory(request -> request.name(name)).parameters();+ assertEquals("alias/first-key", versions.get(0).keyId());+ assertEquals("alias/second-key", versions.get(1).keyId());+ assertEquals("alias/aws/ssm", versions.get(2).keyId());+ }+ } finally {+ delete(name);+ }+ }++ @Test+ void legacyHistoryWithoutSavedKeyDoesNotGuessDefaultKey() {+ String name = "/legacy-history-key-id-" + UUID.randomUUID();+ try {+ put(name, "old", "alias/old-custom-key", false);+ ssmService.getParameterHistory(name, "us-east-1").getFirst().setKeyId(null);++ JsonPath history = given()+ .header("X-Amz-Target", "AmazonSSM.GetParameterHistory")+ .contentType(CONTENT_TYPE)+ .body("{\"Name\":\"" + name + "\"}")+ .when().post("/")+ .then().statusCode(200)+ .extract().jsonPath();+ assertNull(history.getString("Parameters[0].KeyId"));+ } finally {+ delete(name);+ }+ }++ private void delete(String name) {+ given()+ .header("X-Amz-Target", "AmazonSSM.DeleteParameter")+ .contentType(CONTENT_TYPE)+ .body("{\"Name\":\"" + name + "\"}")+ .when().post("/");+ }++ private void put(String name, String value, String keyId, boolean overwrite) {+ String keyIdField = keyId == null ? "" : ",\"KeyId\":\"" + keyId + "\"";+ given()+ .header("X-Amz-Target", "AmazonSSM.PutParameter")+ .contentType(CONTENT_TYPE)+ .body("{\"Name\":\"" + name + "\",\"Value\":\"" + value+ + "\",\"Type\":\"SecureString\"" + keyIdField + ",\"Overwrite\":" + overwrite + "}")+ .when().post("/")+ .then().statusCode(200);+ }+}