fix(ssm): give resource-tag conditions the tags of the resource a request names (#8691)

The generated request resources give SSM's parameter, document and
association types the aws:ResourceTag/ key (and ssm:resourceTag/ for
the first two), but SsmService answered no tags, so a condition on an
SSM resource's tags saw none. SsmService.storedTags now reads the
parameter, document or association stored under the ARN in the account
and Region it names, in place, matched on the whole ARN; a parameter
ARN names /<name> before <name>, as tagging reads it. Tag writes now
put a new map in place of the held one, a parameter's filled one entry
at a time so the listed order holds, and the parameter tag writes the
CloudFormation provisioner calls take the service's lock as the others
do; the tag fields are volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 21:38:18 -04:00
1 parent 8b1de6cdbe
commit 66eb95661e
8 files changed
+191 -27

No files matched your search

@@ -664,16 +664,68 @@ class IamRequestResourceEnforcementIntegrationTest {
"{\"resourceArn\":\"" + arns + other + "\"}"), "ecr:ListTagsForResource");
}
/**
* An {@code ssm:resourceTag} or {@code aws:ResourceTag} condition reads the tags SSM holds on the parameter or the
* document a request names, so an allow on one team's tag lets the caller read that team's and not another's.
*/
@Test
void anSsmResourceTagConditionReadsTheTagsOfTheParameterAndTheDocument() {
String tagged = ssmParameter("/res-tag/" + suffix(), "data");
String other = ssmParameter("/res-tag/" + suffix(), "web");
String taggedDocument = ssmDocument("res-tag-doc-" + suffix(), "data");
String otherDocument = ssmDocument("res-tag-doc-" + suffix(), "web");
User user = user("res-ssm-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"ssm:GetParameter\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"ssm:resourceTag/team\":\"data\"}}},"
+ "{\"Effect\":\"Allow\",\"Action\":\"ssm:DescribeDocument\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
send(user, "ssm", "AmazonSSM.GetParameter", "1.1", "{\"Name\":\"" + tagged + "\"}").statusCode(200);
refusedJson(send(user, "ssm", "AmazonSSM.GetParameter", "1.1", "{\"Name\":\"" + other + "\"}"),
"ssm:GetParameter");
send(user, "ssm", "AmazonSSM.DescribeDocument", "1.1", "{\"Name\":\"" + taggedDocument + "\"}")
.statusCode(200);
refusedJson(send(user, "ssm", "AmazonSSM.DescribeDocument", "1.1", "{\"Name\":\"" + otherDocument + "\"}"),
"ssm:DescribeDocument");
}
/** An SSM String parameter of the name, tagged with the team, deleted afterwards. */
private String ssmParameter(String name, String team) {
return created("ssm", "AmazonSSM.PutParameter", "{\"Name\":\"" + name + "\",\"Value\":\"v\",\"Type\":"
+ "\"String\",\"Tags\":" + teamTag(team) + "}", "AmazonSSM.DeleteParameter",
"{\"Name\":\"" + name + "\"}", name);
}
/** An SSM Command document of the name, tagged with the team, deleted afterwards. */
private String ssmDocument(String name, String team) {
return created("ssm", "AmazonSSM.CreateDocument", "{\"Name\":\"" + name + "\",\"DocumentType\":\"Command\","
+ "\"Content\":\"{\\\"schemaVersion\\\":\\\"2.2\\\",\\\"mainSteps\\\":[]}\",\"Tags\":"
+ teamTag(team) + "}", "AmazonSSM.DeleteDocument", "{\"Name\":\"" + name + "\"}", name);
}
/** An ECR repository of the name, tagged with the team, deleted afterwards. */
private String ecrRepository(String name, String team) {
adminJson("ecr", "AmazonEC2ContainerRegistry_V20150921.CreateRepository").body("{\"repositoryName\":\""
+ name + "\",\"tags\":[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}").post("/").then()
.statusCode(200);
cleanup.register(() -> adminJson("ecr", "AmazonEC2ContainerRegistry_V20150921.DeleteRepository")
.body("{\"repositoryName\":\"" + name + "\",\"force\":true}").post("/"));
return created("ecr", "AmazonEC2ContainerRegistry_V20150921.CreateRepository", "{\"repositoryName\":\""
+ name + "\",\"tags\":" + teamTag(team) + "}", "AmazonEC2ContainerRegistry_V20150921.DeleteRepository",
"{\"repositoryName\":\"" + name + "\",\"force\":true}", name);
}
/**
* {@code name}, after an administrator creates the resource with the JSON 1.1 {@code create} call and registers
* the {@code delete} call that removes it after the test.
*/
private String created(String service, String create, String createBody, String delete, String deleteBody,
String name) {
adminJson(service, create).body(createBody).post("/").then().statusCode(200);
cleanup.register(() -> adminJson(service, delete).body(deleteBody).post("/"));
return name;
}
/** A tag list holding the one tag {@code team=<team>}. */
private static String teamTag(String team) {
return "[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]";
}
@Test
void aDeliveryStreamIsCheckedOnTheArnItsNameGives() {
String name = "res-stream-" + suffix();
@@ -1660,6 +1660,54 @@ class SsmServiceTest {
assertEquals(Map.of("env", "dev"), ssmService.listTagsForResource("/arn/tagged", "us-east-1"));
}
@Test
void storedTagsAreTheTagsOfTheParameterDocumentOrAssociationTheWholeArnNamesAndTaggingPutsANewMap() {
String region = "us-east-1";
String arns = "arn:aws:ssm:us-east-1:000000000000:";
ssmService.putParameter("/app/db", "v", "String", null, false, Map.of("team", "data"), region);
ssmService.putParameter("plain", "v", "String", null, false, Map.of("team", "web"), region);
ssmService.createDocument("RunIt", "{}", "Command", region);
ssmService.addTagsToResource("Document", "RunIt", Map.of("owner", "ops"), region);
String associationId = ssmService.createAssociation("RunIt", "nightly", null, "i-123", null, null, null, region)
.getAssociationId();
ssmService.addTagsToResource("Association", associationId, Map.of("cadence", "daily"), region);
assertEquals(Map.of("team", "data"), ssmService.storedTags(arns + "parameter/app/db"));
assertEquals(Map.of("team", "web"), ssmService.storedTags(arns + "parameter/plain"));
assertEquals(Map.of("owner", "ops"), ssmService.storedTags(arns + "document/RunIt"));
assertEquals(Map.of("cadence", "daily"), ssmService.storedTags(arns + "association/" + associationId));
// Another Region, another account's parameter of that name, an ARN whose partition is not its Region's and a
// type Floci keeps no record of hold none.
assertEquals(Map.of(), ssmService.storedTags("arn:aws:ssm:us-west-2:000000000000:parameter/app/db"));
assertEquals(Map.of(), ssmService.storedTags("arn:aws:ssm:us-east-1:111122223333:parameter/app/db"));
assertEquals(Map.of(), ssmService.storedTags("arn:aws-us-gov:ssm:us-east-1:000000000000:parameter/app/db"));
assertEquals(Map.of(), ssmService.storedTags(arns + "maintenancewindow/mw-0123456789abcdef0"));
Map<String, String> heldParameterTags = ssmService.listTagsForResource("/app/db", region);
Map<String, String> heldDocumentTags = ssmService.listTagsForResource("Document", "RunIt", region);
ssmService.addTagsToResource("/app/db", Map.of("env", "prod"), region);
ssmService.removeTagsFromResource("Document", "RunIt", List.of("owner"), region);
assertEquals(Map.of("team", "data"), heldParameterTags);
assertEquals(Map.of("owner", "ops"), heldDocumentTags);
assertEquals(Map.of("team", "data", "env", "prod"), ssmService.storedTags(arns + "parameter/app/db"));
assertEquals(Map.of(), ssmService.storedTags(arns + "document/RunIt"));
}
/**
* A top-level name stored both with and without its leading slash has one ARN, which AWS's format gives both; it
* names the slashed parameter, as the tagging operations read that ARN.
*/
@Test
void storedTagsOfATopLevelArnAreTheSlashedParametersWhenBothFormsAreStored() {
ssmService.putParameter("twin", "v", "String", null, false, Map.of("form", "bare"), "us-east-1");
ssmService.putParameter("/twin", "v", "String", null, false, Map.of("form", "slashed"), "us-east-1");
String arn = "arn:aws:ssm:us-east-1:000000000000:parameter/twin";
assertEquals(Map.of("form", "slashed"), ssmService.storedTags(arn));
assertEquals(Map.of("form", "slashed"), ssmService.listTagsForResource(arn, "us-east-1"));
}
@Test
void putParameterOverwriteClearsDescriptionWhenOmitted() {
String region = "eu-west-1";