mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ssm): support parameter selectors in GetParameter and align LabelParameterVersion with AWS (#4461)
* fix(ssm): support name:version and name:label selectors in GetParameter - Support name:version and name:label selectors in GetParameter and GetParameters - Resolve selector against parameter history copies without mutating stored parameters - Validate labels in LabelParameterVersion per AWS spec (reject numeric and reserved labels) - Return invalid labels in InvalidLabels response - Synchronize labelParameterVersion and clone history entries to handle concurrent updates without in-place mutation - Enforce 10-label maximum limit per parameter version - Default to latest version only when ParameterVersion is omitted - Guard against NPE and trailing colons in getParameter selector lookup Closes #4396 * fix(ssm): enforce label validation constraints in LabelParameterVersion - Reject empty labels and labels exceeding 100 characters with ValidationException - Reject empty label lists and lists with more than 10 labels with ValidationException - Update findSelectedVersion comment to reflect that labels are unique across versions
This commit is contained in:
1 parent
dce7983b15
commit
5ae46c2bd3
7 files changed
+632
-39
No files matched your search
@@ -149,7 +149,7 @@ public class SsmJsonHandler {
|
||||
}
|
||||
response.set("Parameters", parametersArray);
|
||||
response.set("InvalidParameters", invalidParameterNames(names,
|
||||
params.stream().map(Parameter::getName).toList()));
|
||||
params.stream().map(p -> p.getName() + (p.getSelector() == null ? "" : p.getSelector())).toList()));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -929,17 +929,61 @@ public class SsmJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static final int MAX_LABELS = 10;
|
||||
private static final int MAX_LABEL_LENGTH = 100;
|
||||
|
||||
private List<String> requireLabels(JsonNode request) {
|
||||
JsonNode labelsNode = request.path("Labels");
|
||||
if (labelsNode.isMissingNode() || labelsNode.isNull()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value null at 'labels' failed to satisfy constraint: Member must not be null",
|
||||
400);
|
||||
}
|
||||
if (!labelsNode.isArray() || labelsNode.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length greater than or equal to 1",
|
||||
400);
|
||||
}
|
||||
if (labelsNode.size() > MAX_LABELS) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length less than or equal to "
|
||||
+ MAX_LABELS,
|
||||
400);
|
||||
}
|
||||
List<String> labels = new ArrayList<>(labelsNode.size());
|
||||
for (JsonNode l : labelsNode) {
|
||||
String label = l.asText();
|
||||
if (!l.isTextual() || label.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length greater than or equal to 1",
|
||||
400);
|
||||
}
|
||||
if (label.length() > MAX_LABEL_LENGTH) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length less than or equal to "
|
||||
+ MAX_LABEL_LENGTH,
|
||||
400);
|
||||
}
|
||||
labels.add(label);
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
|
||||
private Response handleLabelParameterVersion(JsonNode request, String region) {
|
||||
String name = request.path("Name").asText();
|
||||
long parameterVersion = request.path("ParameterVersion").asLong();
|
||||
List<String> labels = new ArrayList<>();
|
||||
request.path("Labels").forEach(l -> labels.add(l.asText()));
|
||||
Long parameterVersion = request.hasNonNull("ParameterVersion")
|
||||
? request.path("ParameterVersion").asLong()
|
||||
: null;
|
||||
List<String> labels = requireLabels(request);
|
||||
|
||||
ssmService.labelParameterVersion(name, parameterVersion, labels, region);
|
||||
SsmService.LabelParameterVersionResult result = ssmService.labelParameterVersion(
|
||||
name, parameterVersion, labels, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("InvalidLabels", objectMapper.createArrayNode());
|
||||
response.put("ParameterVersion", parameterVersion);
|
||||
ArrayNode invalidArray = objectMapper.createArrayNode();
|
||||
result.invalidLabels().forEach(invalidArray::add);
|
||||
response.set("InvalidLabels", invalidArray);
|
||||
response.put("ParameterVersion", result.parameterVersion());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -987,6 +1031,9 @@ public class SsmJsonHandler {
|
||||
node.put("LastModifiedDate", p.getLastModifiedDate().toEpochMilli() / 1000.0);
|
||||
node.put("ARN", p.getArn());
|
||||
node.put("DataType", p.getDataType());
|
||||
if (p.getSelector() != null) {
|
||||
node.put("Selector", p.getSelector());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@@ -205,9 +205,16 @@ public class SsmService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public Parameter getParameter(String name, String region) {
|
||||
return findParameter(name, region)
|
||||
.orElseThrow(() -> new AwsException("ParameterNotFound",
|
||||
"Parameter " + name + " not found.", 400));
|
||||
return findParameter(name, region).orElseThrow(() -> {
|
||||
int separator = name == null ? -1 : name.lastIndexOf(':');
|
||||
if (separator > 0 && separator < name.length() - 1
|
||||
&& parameterStore.get(regionKey(region, name.substring(0, separator))).isPresent()) {
|
||||
return new AwsException("ParameterVersionNotFound",
|
||||
"Systems Manager could not find version or label " + name.substring(separator + 1)
|
||||
+ " of " + name.substring(0, separator) + ". Verify the version and try again.", 400);
|
||||
}
|
||||
return new AwsException("ParameterNotFound", "Parameter " + name + " not found.", 400);
|
||||
});
|
||||
}
|
||||
|
||||
public List<Parameter> getParameters(List<String> names, String region) {
|
||||
@@ -243,9 +250,50 @@ public class SsmService implements ResourceProvider {
|
||||
if (stored.isPresent()) {
|
||||
return stored;
|
||||
}
|
||||
Optional<Parameter> selected = findSelectedVersion(name, region);
|
||||
if (selected.isPresent()) {
|
||||
return selected;
|
||||
}
|
||||
return publicParameter(name, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* A read may name a version or label as {@code name:version} or {@code name:label}. A
|
||||
* parameter name cannot contain a colon, so the last one always starts the selector. The
|
||||
* answer is a copy built from the history entry: it carries the base name and the selector
|
||||
* as AWS returns them, and nothing is written back.
|
||||
*/
|
||||
private Optional<Parameter> findSelectedVersion(String name, String region) {
|
||||
int separator = name == null ? -1 : name.lastIndexOf(':');
|
||||
if (separator <= 0 || separator == name.length() - 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String baseName = name.substring(0, separator);
|
||||
String selector = name.substring(separator + 1);
|
||||
String storageKey = regionKey(region, baseName);
|
||||
Optional<Parameter> current = parameterStore.get(storageKey);
|
||||
if (current.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
boolean isVersion = selector.chars().allMatch(Character::isDigit);
|
||||
// A label is unique across parameter versions because attaching it moves it from any prior version.
|
||||
return historyStore.get(storageKey).orElse(List.of()).stream()
|
||||
.filter(h -> isVersion
|
||||
? String.valueOf(h.getVersion()).equals(selector)
|
||||
: h.getLabels() != null && h.getLabels().contains(selector))
|
||||
.max(Comparator.comparingLong(ParameterHistory::getVersion))
|
||||
.map(h -> {
|
||||
Parameter parameter = new Parameter(baseName, h.getValue(), h.getType());
|
||||
parameter.setVersion(h.getVersion());
|
||||
parameter.setDescription(h.getDescription());
|
||||
parameter.setLastModifiedDate(h.getLastModifiedDate());
|
||||
parameter.setArn(current.get().getArn());
|
||||
parameter.setDataType(current.get().getDataType());
|
||||
parameter.setSelector(":" + selector);
|
||||
return parameter;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS publishes read-only AMI id parameters under {@code /aws/service/} in every account
|
||||
* with no setup, so a read of one of those names is answered from the EC2 image catalog.
|
||||
@@ -408,39 +456,134 @@ public class SsmService implements ResourceProvider {
|
||||
});
|
||||
}
|
||||
|
||||
public void labelParameterVersion(String name, long parameterVersion, List<String> labels, String region) {
|
||||
String storageKey = regionKey(region, name);
|
||||
if (parameterStore.get(storageKey).isEmpty()) {
|
||||
throw new AwsException("ParameterNotFound",
|
||||
"Parameter " + name + " not found.", 400);
|
||||
public record LabelParameterVersionResult(long parameterVersion, List<String> invalidLabels) {}
|
||||
|
||||
public synchronized LabelParameterVersionResult labelParameterVersion(String name, Long parameterVersion,
|
||||
List<String> labels, String region) {
|
||||
if (labels == null || labels.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length greater than or equal to 1",
|
||||
400);
|
||||
}
|
||||
|
||||
List<ParameterHistory> history = historyStore.get(storageKey)
|
||||
.orElse(List.of());
|
||||
|
||||
history = new ArrayList<>(history);
|
||||
|
||||
boolean found = false;
|
||||
for (ParameterHistory h : history) {
|
||||
if (h.getVersion() == parameterVersion) {
|
||||
List<String> existing = h.getLabels() != null ? new ArrayList<>(h.getLabels()) : new ArrayList<>();
|
||||
for (String label : labels) {
|
||||
if (!existing.contains(label)) {
|
||||
existing.add(label);
|
||||
}
|
||||
}
|
||||
h.setLabels(existing);
|
||||
found = true;
|
||||
break;
|
||||
if (labels.size() > 10) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length less than or equal to 10",
|
||||
400);
|
||||
}
|
||||
for (String label : labels) {
|
||||
if (label == null || label.isEmpty()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length greater than or equal to 1",
|
||||
400);
|
||||
}
|
||||
if (label.length() > 100) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'labels' failed to satisfy constraint: Member must have length less than or equal to 100",
|
||||
400);
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
throw new AwsException("ParameterVersionNotFound", "Parameter version " + parameterVersion + " not found.", 400);
|
||||
String storageKey = regionKey(region, name);
|
||||
Parameter current = parameterStore.get(storageKey).orElseThrow(() ->
|
||||
new AwsException("ParameterNotFound", "Parameter " + name + " not found.", 400));
|
||||
|
||||
long targetVersion = parameterVersion == null
|
||||
? current.getVersion()
|
||||
: parameterVersion;
|
||||
|
||||
List<ParameterHistory> existingHistory = historyStore.get(storageKey).orElse(List.of());
|
||||
List<ParameterHistory> updatedHistory = new ArrayList<>(existingHistory.size());
|
||||
|
||||
ParameterHistory targetCopy = null;
|
||||
for (ParameterHistory h : existingHistory) {
|
||||
ParameterHistory copy = new ParameterHistory(h);
|
||||
if (copy.getVersion() == targetVersion) {
|
||||
targetCopy = copy;
|
||||
}
|
||||
updatedHistory.add(copy);
|
||||
}
|
||||
|
||||
historyStore.put(storageKey, history);
|
||||
LOG.infov("Labeled parameter {0} version {1} with labels {2}", name, parameterVersion, labels);
|
||||
if (targetCopy == null) {
|
||||
throw new AwsException("ParameterVersionNotFound",
|
||||
"Parameter version " + targetVersion + " not found.", 400);
|
||||
}
|
||||
|
||||
List<String> invalidLabels = new ArrayList<>();
|
||||
List<String> validLabels = new ArrayList<>();
|
||||
if (labels != null) {
|
||||
for (String label : labels) {
|
||||
if (isValidLabel(label)) {
|
||||
if (!validLabels.contains(label)) {
|
||||
validLabels.add(label);
|
||||
}
|
||||
} else {
|
||||
if (!invalidLabels.contains(label)) {
|
||||
invalidLabels.add(label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<String> targetLabels = targetCopy.getLabels() != null
|
||||
? new ArrayList<>(targetCopy.getLabels())
|
||||
: new ArrayList<>();
|
||||
|
||||
int newLabelCount = targetLabels.size();
|
||||
for (String validLabel : validLabels) {
|
||||
if (!targetLabels.contains(validLabel)) {
|
||||
newLabelCount++;
|
||||
}
|
||||
}
|
||||
if (newLabelCount > 10) {
|
||||
throw new AwsException("ParameterVersionLabelLimitExceeded",
|
||||
"The parameter version already has the maximum number of labels (10).", 400);
|
||||
}
|
||||
|
||||
for (ParameterHistory h : updatedHistory) {
|
||||
if (h.getVersion() != targetVersion && h.getLabels() != null) {
|
||||
List<String> otherLabels = new ArrayList<>(h.getLabels());
|
||||
if (otherLabels.removeAll(validLabels)) {
|
||||
h.setLabels(otherLabels);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String validLabel : validLabels) {
|
||||
if (!targetLabels.contains(validLabel)) {
|
||||
targetLabels.add(validLabel);
|
||||
}
|
||||
}
|
||||
targetCopy.setLabels(targetLabels);
|
||||
|
||||
historyStore.put(storageKey, updatedHistory);
|
||||
LOG.infov("Labeled parameter {0} version {1} with labels {2}", name, targetVersion, validLabels);
|
||||
return new LabelParameterVersionResult(targetVersion, invalidLabels);
|
||||
}
|
||||
|
||||
public synchronized LabelParameterVersionResult labelParameterVersion(String name, long parameterVersion,
|
||||
List<String> labels, String region) {
|
||||
return labelParameterVersion(name, Long.valueOf(parameterVersion), labels, region);
|
||||
}
|
||||
|
||||
private static boolean isValidLabel(String label) {
|
||||
if (label == null || label.isEmpty() || label.length() > 100) {
|
||||
return false;
|
||||
}
|
||||
if (Character.isDigit(label.charAt(0))) {
|
||||
return false;
|
||||
}
|
||||
String lower = label.toLowerCase(Locale.ROOT);
|
||||
if (lower.startsWith("aws") || lower.startsWith("ssm")) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < label.length(); i++) {
|
||||
char c = label.charAt(i);
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
||||
|| c == '.' || c == '-' || c == '_')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void addTagsToResource(String resourceId, Map<String, String> tags, String region) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.ssm.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
@@ -41,6 +42,10 @@ public class Parameter {
|
||||
@JsonAlias({"tags", "Tags"})
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
|
||||
// Only set on a copy answering a name:version or name:label read; never persisted.
|
||||
@JsonIgnore
|
||||
private String selector;
|
||||
|
||||
public Parameter() {}
|
||||
|
||||
public Parameter(String name, String value, String type) {
|
||||
@@ -77,4 +82,7 @@ public class Parameter {
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
|
||||
public String getSelector() { return selector; }
|
||||
public void setSelector(String selector) { this.selector = selector; }
|
||||
}
|
||||
@@ -44,6 +44,16 @@ public class ParameterHistory {
|
||||
this.description = parameter.getDescription();
|
||||
}
|
||||
|
||||
public ParameterHistory(ParameterHistory other) {
|
||||
this.name = other.name;
|
||||
this.version = other.version;
|
||||
this.value = other.value;
|
||||
this.type = other.type;
|
||||
this.lastModifiedDate = other.lastModifiedDate;
|
||||
this.description = other.description;
|
||||
this.labels = other.labels != null ? new ArrayList<>(other.labels) : new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user