feat(ssm): support UnlabelParameterVersion (#5339)

* feat(ssm): support UnlabelParameterVersion

Remove one or more labels from a parameter version. Labels attached to
the version are returned in RemovedLabels; labels that are not on that
version, including ones attached to a different version, are returned in
InvalidLabels and left in place. A missing parameter is ParameterNotFound,
a missing version ParameterVersionNotFound, and an omitted ParameterVersion
or an empty or oversized label list a ValidationException.

* fix(ssm): match AWS errors and ordering in UnlabelParameterVersion

Verified against real AWS (us-east-1):

- Report every missing or invalid member in one ValidationException, in
  the order name, parameterVersion, labels, and render the label list
  the way AWS does ('[prod, ]').
- Reject a string or boolean ParameterVersion and a non-string Name or
  label with SerializationException; truncate a fractional version and
  clamp one outside the range of a long, as AWS does.
- Reject an ARN, a malformed or ssm-prefixed name, and deny an
  aws-prefixed one before the lookup; ignore surrounding whitespace in
  the lookup but echo the name as sent in error messages.
- Word ParameterVersionNotFound as AWS does.
- Return RemovedLabels in the order the labels are stored on the
  version, and InvalidLabels in request order.

LabelParameterVersion shares the label-list parsing, so its label
validation messages and non-string labels now match AWS too.

* fix(ssm): take the service monitor when deleting parameters

PutParameter, LabelParameterVersion and UnlabelParameterVersion already
run under the SsmService monitor, but DeleteParameter and
DeleteParameters did not. A delete landing between a label or unlabel
call's lookup and its history write left orphaned history that a later
PutParameter of the same name appended to.

Also pin LabelParameterVersion's AWS-verified label validation messages
and its SerializationException for a non-string label in its own
integration test, and share the parameter-name format rule between the
two reserved-name messages.
This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-10-09 13:11:29 -04:00
1 parent 07ebc33d28
commit 4f34a903fc
7 files changed
+651 -81

No files matched your search

@@ -1988,53 +1988,217 @@ class SsmIntegrationTest {
@Test
void labelParameterVersion_validationErrors() {
given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
String prefix = "1 validation error detected: ";
String memberRange = "Member must satisfy constraint: [Member must have length less than or equal to 100, "
+ "Member must have length greater than or equal to 1]";
labelParameterVersion("""
{ "Name": "/test/param", "Labels": [] }
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(prefix + "Value '[]' at 'labels' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 1"));
given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
labelParameterVersion("""
{ "Name": "/test/param", "Labels": [""] }
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(prefix + "Value '[]' at 'labels' failed to satisfy constraint: " + memberRange));
given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
String tooLong = "a".repeat(101);
labelParameterVersion("""
{ "Name": "/test/param", "Labels": ["%s"] }
""".formatted("a".repeat(101)))
""".formatted(tooLong))
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(prefix + "Value '[" + tooLong + "]' at 'labels' failed to satisfy constraint: "
+ memberRange));
labelParameterVersion("""
{ "Name": "/test/param", "Labels": ["l1","l2","l3","l4","l5","l6","l7","l8","l9","l10","l11"] }
""")
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(prefix + "Value '[l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11]' at 'labels' "
+ "failed to satisfy constraint: Member must have length less than or equal to 10"));
labelParameterVersion("""
{ "Name": "/test/param", "Labels": [5] }
""")
.statusCode(400)
.body("__type", equalTo("SerializationException"))
.body("message", equalTo("NUMBER_VALUE can not be converted to a String"));
}
private io.restassured.response.ValidatableResponse labelParameterVersion(String body) {
return given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body(body)
.when()
.post("/")
.then();
}
@Test
void unlabelParameterVersionRemovesLabels() {
given()
.header("X-Amz-Target", "AmazonSSM.PutParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/param", "Value": "v1", "Type": "String" }
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
.statusCode(200);
given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/test/param", "Labels": ["l1","l2","l3","l4","l5","l6","l7","l8","l9","l10","l11"] }
{ "Name": "/unlabel/param", "ParameterVersion": 1, "Labels": ["prod", "stable"] }
""")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", "AmazonSSM.UnlabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/param", "ParameterVersion": 1, "Labels": ["prod", "missing"] }
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("RemovedLabels", contains("prod"))
.body("InvalidLabels", contains("missing"));
given()
.header("X-Amz-Target", "AmazonSSM.GetParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/param:prod" }
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
.body("__type", equalTo("ParameterVersionNotFound"));
given()
.header("X-Amz-Target", "AmazonSSM.GetParameterHistory")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/param" }
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Parameters[0].Labels", contains("stable"));
}
@Test
void unlabelParameterVersion_errors() {
given()
.header("X-Amz-Target", "AmazonSSM.PutParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/errors", "Value": "v1", "Type": "String" }
""")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", "AmazonSSM.LabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/errors", "ParameterVersion": 1, "Labels": ["prod"] }
""")
.when()
.post("/")
.then()
.statusCode(200);
unlabel("{}")
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("3 validation errors detected: "
+ "Value null at 'name' failed to satisfy constraint: Member must not be null; "
+ "Value null at 'parameterVersion' failed to satisfy constraint: Member must not be null; "
+ "Value null at 'labels' failed to satisfy constraint: Member must not be null"));
unlabel("""
{ "Name": "", "ParameterVersion": 1, "Labels": [] }
""")
.statusCode(400)
.body("message", equalTo("2 validation errors detected: "
+ "Value '' at 'name' failed to satisfy constraint: Member must have length greater than or equal to 1; "
+ "Value '[]' at 'labels' failed to satisfy constraint: Member must have length greater than or equal to 1"));
unlabel("""
{ "Name": "/unlabel/errors", "ParameterVersion": "1", "Labels": ["prod"] }
""")
.statusCode(400)
.body("__type", equalTo("SerializationException"))
.body("message", equalTo("STRING_VALUE can not be converted to a Long"));
unlabel("""
{ "Name": "/unlabel/errors", "ParameterVersion": 1, "Labels": [5] }
""")
.statusCode(400)
.body("__type", equalTo("SerializationException"))
.body("message", equalTo("NUMBER_VALUE can not be converted to a String"));
unlabel("""
{ "Name": "arn:aws:ssm:us-east-1:000000000000:parameter/unlabel/errors", "ParameterVersion": 1, "Labels": ["prod"] }
""")
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("Parameter ARN is not supported for this operation."));
unlabel("""
{ "Name": "/unlabel/errors", "ParameterVersion": 5, "Labels": ["prod"] }
""")
.statusCode(400)
.body("__type", equalTo("ParameterVersionNotFound"))
.body("message", equalTo("Systems Manager could not find version 5 of /unlabel/errors. "
+ "Verify the version and try again."));
unlabel("""
{ "Name": "/unlabel/no-such-param", "ParameterVersion": 1, "Labels": ["prod"] }
""")
.statusCode(400)
.body("__type", equalTo("ParameterNotFound"))
.body("message", equalTo("Parameter /unlabel/no-such-param not found."));
given()
.header("X-Amz-Target", "AmazonSSM.GetParameterHistory")
.contentType(SSM_CONTENT_TYPE)
.body("""
{ "Name": "/unlabel/errors" }
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Parameters[0].Labels", contains("prod"));
// AWS truncates a fractional version rather than rejecting it.
unlabel("""
{ "Name": "/unlabel/errors", "ParameterVersion": 1.9, "Labels": ["prod"] }
""")
.statusCode(200)
.body("RemovedLabels", contains("prod"));
}
private io.restassured.response.ValidatableResponse unlabel(String body) {
return given()
.header("X-Amz-Target", "AmazonSSM.UnlabelParameterVersion")
.contentType(SSM_CONTENT_TYPE)
.body(body)
.when()
.post("/")
.then();
}
private io.restassured.response.ValidatableResponse describeParameters(String body) {
@@ -499,6 +499,210 @@ class SsmServiceTest {
assertEquals("v2", p2.getValue());
}
@Test
void unlabelParameterVersionRemovesLabelsAndReportsMissingOnes() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
ssmService.labelParameterVersion("/app/key", 1L, List.of("prod", "stable"), region);
SsmService.UnlabelParameterVersionResult result = ssmService.unlabelParameterVersion(
"/app/key", 1L, List.of("prod", "never-attached", "prod"), region);
assertEquals(List.of("prod"), result.removedLabels());
assertEquals(List.of("never-attached"), result.invalidLabels());
ParameterHistory v1 = ssmService.getParameterHistory("/app/key", region).get(0);
assertEquals(List.of("stable"), v1.getLabels());
AwsException ex = assertThrows(AwsException.class,
() -> ssmService.getParameter("/app/key:prod", region));
assertEquals("ParameterVersionNotFound", ex.getErrorCode());
assertEquals("v1", ssmService.getParameter("/app/key:stable", region).getValue());
}
@Test
void unlabelParameterVersionLeavesLabelOnAnotherVersionInPlace() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
ssmService.putParameter("/app/key", "v2", "String", null, true, region);
ssmService.labelParameterVersion("/app/key", 2L, List.of("prod"), region);
SsmService.UnlabelParameterVersionResult result = ssmService.unlabelParameterVersion(
"/app/key", 1L, List.of("prod"), region);
assertEquals(List.of(), result.removedLabels());
assertEquals(List.of("prod"), result.invalidLabels());
assertEquals("v2", ssmService.getParameter("/app/key:prod", region).getValue());
}
@Test
void unlabelParameterVersionMissingParameterThrowsParameterNotFound() {
AwsException ex = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/app/missing", 1L, List.of("prod"), "eu-west-1"));
assertEquals("ParameterNotFound", ex.getErrorCode());
}
@Test
void unlabelParameterVersionUnknownVersionThrowsParameterVersionNotFound() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
AwsException unknown = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/app/key", 2L, List.of("prod"), region));
assertEquals("ParameterVersionNotFound", unknown.getErrorCode());
assertEquals("Systems Manager could not find version 2 of /app/key. Verify the version and try again.",
unknown.getMessage());
AwsException zero = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/app/key", 0L, List.of("prod"), region));
assertEquals("ParameterVersionNotFound", zero.getErrorCode());
}
@Test
void unlabelParameterVersionRejectsEmptyOrOversizedLabelLists() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
AwsException empty = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/app/key", 1L, List.of(), region));
assertEquals("ValidationException", empty.getErrorCode());
List<String> elevenLabels = List.of(
"l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9", "l10", "l11");
AwsException tooMany = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/app/key", 1L, elevenLabels, region));
assertEquals("ValidationException", tooMany.getErrorCode());
}
@Test
void deletesWaitForTheMonitorThatLabelUpdatesHold() throws Exception {
String region = "eu-west-1";
ssmService.putParameter("/app/one", "v1", "String", null, false, region);
ssmService.putParameter("/app/two", "v1", "String", null, false, region);
List<Thread> deleters = List.of(
new Thread(() -> ssmService.deleteParameter("/app/one", region)),
new Thread(() -> ssmService.deleteParameters(List.of("/app/two"), region)));
// Holding the monitor stands in for a label or unlabel call between its lookup and its history write.
synchronized (ssmService) {
for (Thread deleter : deleters) {
deleter.start();
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (deleter.getState() != Thread.State.BLOCKED) {
assertTrue(System.nanoTime() < deadline, "delete did not wait for the service monitor");
Thread.onSpinWait();
}
}
assertEquals("v1", ssmService.getParameter("/app/one", region).getValue());
assertEquals("v1", ssmService.getParameter("/app/two", region).getValue());
}
for (Thread deleter : deleters) {
deleter.join(TimeUnit.SECONDS.toMillis(5));
}
assertThrows(AwsException.class, () -> ssmService.getParameter("/app/one", region));
assertThrows(AwsException.class, () -> ssmService.getParameter("/app/two", region));
}
@Test
void unlabelParameterVersionReturnsRemovedLabelsInStoredOrder() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
ssmService.labelParameterVersion("/app/key", 1L, List.of("mike", "zulu", "alpha"), region);
SsmService.UnlabelParameterVersionResult result = ssmService.unlabelParameterVersion(
"/app/key", 1L, List.of("alpha", "yankee", "mike", "charlie"), region);
assertEquals(List.of("mike", "alpha"), result.removedLabels());
assertEquals(List.of("yankee", "charlie"), result.invalidLabels());
}
@Test
void unlabelParameterVersionIgnoresSurroundingWhitespaceButEchoesTheNameAsSent() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
ssmService.labelParameterVersion("/app/key", 1L, List.of("prod"), region);
SsmService.UnlabelParameterVersionResult result = ssmService.unlabelParameterVersion(
" \t/app/key ", 1L, List.of("prod"), region);
assertEquals(List.of("prod"), result.removedLabels());
AwsException missing = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion(" /app/nope ", 1L, List.of("prod"), region));
assertEquals("Parameter /app/nope not found.", missing.getMessage());
AwsException version = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion(" /app/key ", 9L, List.of("prod"), region));
assertEquals("Systems Manager could not find version 9 of /app/key . Verify the version and try again.",
version.getMessage());
}
@Test
void unlabelParameterVersionRejectsAnArnBeforeLookingItUp() {
AwsException ex = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("arn:aws:ssm:us-east-1:123456789012:parameter/nope",
9L, List.of("prod"), "us-east-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals("Parameter ARN is not supported for this operation.", ex.getMessage());
}
@Test
void unlabelParameterVersionRejectsMalformedAndSsmPrefixedNames() {
String expected = "Parameter name: can't be prefixed with \"ssm\" (case-insensitive). If formed as a path, "
+ "it can consist of sub-paths divided by slash symbol; each sub-path can be formed as a mix of "
+ "letters, numbers and the following 3 symbols .-_";
for (String name : List.of("ssm-foo", "/ssm", "/SSM/x", "/a#b", "/a//b", "/a/b/", "a/b", "/a b", " ",
"/aws/a#b")) {
AwsException ex = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion(name, 1L, List.of("prod"), "us-east-1"), name);
assertEquals("ValidationException", ex.getErrorCode(), name);
assertEquals(expected, ex.getMessage(), name);
}
for (String name : List.of("/a:b/c", "plain-name", "/x/ssm-y", "/x/aws")) {
AwsException ex = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion(name, 1L, List.of("prod"), "us-east-1"), name);
assertEquals("ParameterNotFound", ex.getErrorCode(), name);
}
}
@Test
void unlabelParameterVersionDeniesReservedAwsNames() {
AwsException path = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("/AWS/foo", 1L, List.of("prod"), "us-east-1"));
assertEquals("AccessDeniedException", path.getErrorCode());
assertEquals("No access to reserved parameter name: AWS/foo.", path.getMessage());
AwsException bare = assertThrows(AwsException.class,
() -> ssmService.unlabelParameterVersion("awsfoo", 1L, List.of("prod"), "us-east-1"));
assertEquals("No access to reserved parameter name: awsfoo.", bare.getMessage());
}
@Test
void labelListViolationWordsEachConstraintAsAws() {
String prefix = "failed to satisfy constraint: ";
String member = "Member must satisfy constraint: [Member must have length less than or equal to 100, "
+ "Member must have length greater than or equal to 1]";
assertEquals("Value null at 'labels' " + prefix + "Member must not be null",
SsmService.labelListViolation(null));
assertEquals("Value '[]' at 'labels' " + prefix + "Member must have length greater than or equal to 1",
SsmService.labelListViolation(List.of()));
assertEquals("Value '[l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11]' at 'labels' " + prefix
+ "Member must have length less than or equal to 10",
SsmService.labelListViolation(List.of("l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9", "l10", "l11")));
assertEquals("Value '[]' at 'labels' " + prefix + member, SsmService.labelListViolation(List.of("")));
assertEquals("Value '[prod, ]' at 'labels' " + prefix + member,
SsmService.labelListViolation(List.of("prod", "")));
assertNull(SsmService.labelListViolation(List.of("prod")));
}
@Test
void unlabelParameterVersionDoesNotMutateStoredHistoryObjectsInPlace() {
String region = "eu-west-1";
ssmService.putParameter("/app/key", "v1", "String", null, false, region);
ssmService.labelParameterVersion("/app/key", 1L, List.of("prod"), region);
ParameterHistory before = ssmService.getParameterHistory("/app/key", region).get(0);
ssmService.unlabelParameterVersion("/app/key", 1L, List.of("prod"), region);
assertTrue(before.getLabels().contains("prod"));
ParameterHistory after = ssmService.getParameterHistory("/app/key", region).get(0);
assertFalse(after.getLabels().contains("prod"));
}
@Test
void getParameterWithUnknownVersionThrowsParameterVersionNotFound() {
String region = "eu-west-1";