fix(ssm): return a SecureString's KMS ciphertext when a read does not decrypt (#7335)

* fix(ssm): return a SecureString's KMS ciphertext when a read does not decrypt

GetParameter, GetParameters, GetParametersByPath and GetParameterHistory
returned a SecureString's plaintext whether or not the request set
WithDecryption. AWS encrypts the value with KMS on PutParameter, under the
parameter's key with the PARAMETER_ARN encryption context, and returns
that ciphertext unless the read asks for decryption.

PutParameter now encrypts a SecureString with Floci's KMS and stores the
ciphertext with the version, so every read of a version returns the same
value and KMS Decrypt with the parameter's ARN as context returns the
plaintext. A key KMS cannot use, a value over KMS's 4096-byte limit, and
a version stored before this change are still returned unencrypted.

* fix(ssm): log a SecureString stored without a ciphertext and keep the at-rest note

* fix(ssm): say how AWS encrypts Advanced SecureStrings and reuse the shared test client
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-02 15:07:34 -04:00
1 parent cb093f87cb
commit d44e96eab8
8 files changed
+278 -11

No files matched your search

@@ -170,7 +170,7 @@ public class SsmJsonHandler {
authorizeSecretValues(List.of(param), authorization);
ObjectNode response = objectMapper.createObjectNode();
response.set("Parameter", parameterToNode(param));
response.set("Parameter", parameterToNode(param, withDecryption));
return Response.ok(response).build();
}
@@ -186,7 +186,7 @@ public class SsmJsonHandler {
ObjectNode response = objectMapper.createObjectNode();
ArrayNode parametersArray = objectMapper.createArrayNode();
for (Parameter p : params) {
parametersArray.add(parameterToNode(p));
parametersArray.add(parameterToNode(p, withDecryption));
}
response.set("Parameters", parametersArray);
response.set("InvalidParameters", invalidParameterNames(names,
@@ -239,6 +239,7 @@ public class SsmJsonHandler {
PageRequest pageRequest = pageRequest(request, GET_PARAMETERS_BY_PATH_MAX_RESULTS);
String path = request.path("Path").asText();
boolean recursive = request.path("Recursive").asBoolean(false);
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
List<ParameterStringFilter> filters = parameterFilters(request);
List<Parameter> params = ssmService.getParametersByPath(path, recursive, filters, region);
@@ -254,7 +255,7 @@ public class SsmJsonHandler {
GET_PARAMETERS_BY_PATH_TOKEN + ssmService.accountId() + ":" + region + ":" + recursive + ":"
+ (path.endsWith("/") ? path : path + "/") + (labeledVersions ? ":labels" : ""));
for (Parameter p : page.items()) {
parametersArray.add(parameterToNode(p));
parametersArray.add(parameterToNode(p, withDecryption));
}
response.set("Parameters", parametersArray);
putNextToken(response, page);
@@ -298,6 +299,7 @@ public class SsmJsonHandler {
private Response handleGetParameterHistory(JsonNode request, String region) {
PageRequest pageRequest = pageRequest(request, GET_PARAMETER_HISTORY_MAX_RESULTS);
String name = request.path("Name").asText();
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
List<ParameterHistory> history = ssmService.getParameterHistory(name, region);
ObjectNode response = objectMapper.createObjectNode();
@@ -310,7 +312,7 @@ public class SsmJsonHandler {
ObjectNode node = objectMapper.createObjectNode();
node.put("Name", h.getName());
node.put("Version", h.getVersion());
node.put("Value", h.getValue());
node.put("Value", responseValue(withDecryption, h.getValue(), h.getEncryptedValue()));
node.put("Type", h.getType());
if (h.getKeyId() != null) {
node.put("KeyId", h.getKeyId());
@@ -1312,11 +1314,16 @@ public class SsmJsonHandler {
}
}
private ObjectNode parameterToNode(Parameter p) {
/** A SecureString read without decryption answers the ciphertext PutParameter stored, as AWS does. */
private static String responseValue(boolean withDecryption, String value, String encryptedValue) {
return withDecryption || encryptedValue == null ? value : encryptedValue;
}
private ObjectNode parameterToNode(Parameter p, boolean withDecryption) {
ObjectNode node = objectMapper.createObjectNode();
node.put("Name", p.getName());
if (p.getValue() != null) {
node.put("Value", p.getValue());
node.put("Value", responseValue(withDecryption, p.getValue(), p.getEncryptedValue()));
}
node.put("Type", p.getType());
node.put("Version", p.getVersion());
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2ImageCatalog;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
@@ -105,11 +106,12 @@ public class SsmService implements ResourceProvider {
private final Ec2ImageCatalog imageCatalog;
private final SecretsManagerService secretsManager;
private final SsmEventPublisher eventPublisher;
private final KmsService kmsService;
@Inject
public SsmService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
Ec2ImageCatalog imageCatalog, SecretsManagerService secretsManager,
SsmEventPublisher eventPublisher) {
SsmEventPublisher eventPublisher, KmsService kmsService) {
this(
storageFactory.create("ssm", "ssm-parameters.json",
new TypeReference<>() {
@@ -133,7 +135,8 @@ public class SsmService implements ResourceProvider {
regionResolver,
imageCatalog,
secretsManager,
eventPublisher
eventPublisher,
kmsService
);
}
@@ -207,6 +210,21 @@ public class SsmService implements ResourceProvider {
int maxParameterHistory, RegionResolver regionResolver,
Ec2ImageCatalog imageCatalog, SecretsManagerService secretsManager,
SsmEventPublisher eventPublisher) {
this(parameterStore, historyStore, documentPermissionStore, documentStore, serviceSettingStore,
associationStore, maxParameterHistory, regionResolver, imageCatalog, secretsManager,
eventPublisher, null);
}
/** A null KMS service stores SecureString values without a ciphertext. */
SsmService(StorageBackend<String, Parameter> parameterStore,
StorageBackend<String, List<ParameterHistory>> historyStore,
StorageBackend<String, List<String>> documentPermissionStore,
StorageBackend<String, SsmDocument> documentStore,
StorageBackend<String, ServiceSetting> serviceSettingStore,
StorageBackend<String, SsmAssociation> associationStore,
int maxParameterHistory, RegionResolver regionResolver,
Ec2ImageCatalog imageCatalog, SecretsManagerService secretsManager,
SsmEventPublisher eventPublisher, KmsService kmsService) {
this.parameterStore = parameterStore;
this.historyStore = historyStore;
this.documentPermissionStore = documentPermissionStore;
@@ -218,6 +236,7 @@ public class SsmService implements ResourceProvider {
this.imageCatalog = imageCatalog;
this.secretsManager = secretsManager;
this.eventPublisher = eventPublisher;
this.kmsService = kmsService;
}
/**
@@ -332,6 +351,7 @@ public class SsmService implements ResourceProvider {
// or asks for Intelligent-Tiering.
parameter.setTier(tier != null ? tier : existing != null ? tierOf(existing) : "Standard");
parameter.setArn(regionResolver.buildArn("ssm", region, parameterResource(name)));
parameter.setEncryptedValue(encryptedValueOf(parameter, region));
parameter.setLastModifiedDate(Instant.now());
if (existing != null && existing.getTags() != null) {
@@ -351,6 +371,27 @@ public class SsmService implements ResourceProvider {
return parameter;
}
/**
* AWS encrypts a SecureString under its KMS key with the {@code PARAMETER_ARN} encryption context
* and returns that ciphertext to a read without decryption. A key KMS cannot use, which AWS refuses
* on PutParameter, leaves the parameter without one.
*/
private String encryptedValueOf(Parameter parameter, String region) {
if (kmsService == null || !"SecureString".equals(parameter.getType())) {
return null;
}
try {
byte[] ciphertext = kmsService.encrypt(parameter.getKeyId(),
parameter.getValue().getBytes(StandardCharsets.UTF_8),
Map.of("PARAMETER_ARN", parameter.getArn()), region);
return Base64.getEncoder().encodeToString(ciphertext);
} catch (AwsException e) {
LOG.warnv("Storing SecureString {0} without a ciphertext: KMS cannot encrypt under {1}: {2}",
parameter.getArn(), parameter.getKeyId(), e.getMessage());
return null;
}
}
/** AWS's form is {@code parameter/<name>} whether or not the name starts with a slash. */
public static String parameterResource(String name) {
return "parameter" + (name.startsWith("/") ? name : "/" + name);
@@ -554,6 +595,7 @@ public class SsmService implements ResourceProvider {
private static Parameter historicalParameter(Parameter current, ParameterHistory history) {
Parameter parameter = new Parameter(current.getName(), history.getValue(), history.getType());
parameter.setEncryptedValue(history.getEncryptedValue());
parameter.setVersion(history.getVersion());
parameter.setDescription(history.getDescription());
parameter.setLastModifiedDate(history.getLastModifiedDate());
@@ -60,6 +60,10 @@ public class Parameter {
@JsonAlias({"tags", "Tags"})
private Map<String, String> tags = new HashMap<>();
// The SecureString value as KMS encrypted it on PutParameter, returned by a read without WithDecryption.
@JsonProperty("EncryptedValue")
private String encryptedValue;
// Only set on a copy answering a name:version or name:label read; never persisted.
@JsonIgnore
private String selector;
@@ -84,6 +88,9 @@ public class Parameter {
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
public String getEncryptedValue() { return encryptedValue; }
public void setEncryptedValue(String encryptedValue) { this.encryptedValue = encryptedValue; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
@@ -21,6 +21,9 @@ public class ParameterHistory {
@JsonProperty("Value")
private String value;
@JsonProperty("EncryptedValue")
private String encryptedValue;
@JsonProperty("Type")
private String type;
@@ -42,6 +45,7 @@ public class ParameterHistory {
this.name = parameter.getName();
this.version = parameter.getVersion();
this.value = parameter.getValue();
this.encryptedValue = parameter.getEncryptedValue();
this.type = parameter.getType();
this.keyId = parameter.getKeyId();
this.lastModifiedDate = parameter.getLastModifiedDate();
@@ -52,6 +56,7 @@ public class ParameterHistory {
this.name = other.name;
this.version = other.version;
this.value = other.value;
this.encryptedValue = other.encryptedValue;
this.type = other.type;
this.keyId = other.keyId;
this.lastModifiedDate = other.lastModifiedDate;
@@ -68,6 +73,9 @@ public class ParameterHistory {
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
public String getEncryptedValue() { return encryptedValue; }
public void setEncryptedValue(String encryptedValue) { this.encryptedValue = encryptedValue; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }