mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ssm): keep document and association tags and tag them by resource type (#8132)
CreateDocument and CreateAssociation now keep their Tags, and AddTagsToResource, ListTagsForResource and RemoveTagsFromResource pick a document by name or an association by ID from ResourceType instead of always looking up a parameter. A document's tags are returned in its DocumentDescription and ListDocuments entry. Read the SSM tag calls, CreateDocument and CreateAssociation from the generated request-resource table instead of building their ARNs by hand. A ResourceType enum value now picks its resource type ignoring case and hyphens, ResourceId fills it whatever its kind, and a variable named for leaving out the leading slash drops it, as a parameter ARN does. Closes #7959
This commit is contained in:
1 parent
410ed8e817
commit
236e20003f
12 files changed
+501
-36
No files matched your search
@@ -70,7 +70,7 @@ class RequestResourceMembersTest {
|
||||
RequestResourceMembers.Table table = RequestResourceMembers.table();
|
||||
assertTrue(table.namespaces().containsAll(List.of("cognito-idp", "ecr", "elasticloadbalancing", "states")));
|
||||
assertTrue(table.handBuilt().contains("lambda"));
|
||||
assertEquals(Set.of("dynamodb", "logs"), table.namespaces().stream().filter(table.handBuilt()::contains)
|
||||
assertEquals(Set.of("dynamodb", "logs", "ssm"), table.namespaces().stream().filter(table.handBuilt()::contains)
|
||||
.collect(Collectors.toSet()));
|
||||
Set<String> logsDelivery = Stream.of("PutDeliverySource", "GetDeliverySource", "DeleteDeliverySource",
|
||||
"DescribeDeliverySources", "PutDeliveryDestination", "GetDeliveryDestination",
|
||||
@@ -80,6 +80,8 @@ class RequestResourceMembersTest {
|
||||
Set<String> expected = new HashSet<>(logsDelivery);
|
||||
expected.add("dynamodb:ListTables");
|
||||
expected.add("logs:PutDestination");
|
||||
Stream.of("AddTagsToResource", "ListTagsForResource", "RemoveTagsFromResource", "CreateDocument",
|
||||
"CreateAssociation").forEach(operation -> expected.add("ssm:" + operation));
|
||||
assertEquals(expected, table.operations().keySet().stream()
|
||||
.filter(operation -> table.handBuilt().contains(operation.substring(0, operation.indexOf(':'))))
|
||||
.collect(Collectors.toSet()),
|
||||
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
package io.floci.aws.services.ssm;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EnforcementFixtures;
|
||||
import io.floci.aws.testing.IamEnforcementProfile;
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ssm.SsmClient;
|
||||
import software.amazon.awssdk.services.ssm.model.DocumentType;
|
||||
import software.amazon.awssdk.services.ssm.model.InvalidDocumentException;
|
||||
import software.amazon.awssdk.services.ssm.model.ResourceTypeForTagging;
|
||||
import software.amazon.awssdk.services.ssm.model.SsmException;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** Tagging a document or an association is checked against that document's or association's ARN. */
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class SsmDocumentAssociationTagsIamEnforcementIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String CONTENT = "{\"schemaVersion\":\"2.2\",\"description\":\"d\",\"mainSteps\":[{"
|
||||
+ "\"action\":\"aws:runShellScript\",\"name\":\"s\",\"inputs\":{\"runCommand\":[\"echo hi\"]}}]}";
|
||||
private static final String TAG_ACTIONS =
|
||||
"[\"ssm:AddTagsToResource\",\"ssm:ListTagsForResource\",\"ssm:RemoveTagsFromResource\"]";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void aGrantOnParametersDoesNotCoverDocumentsOrAssociations() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String allowedDocument = "iam-tags-allowed-" + suffix;
|
||||
String deniedDocument = "iam-tags-denied-" + suffix;
|
||||
try (SsmClient root = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1)) {
|
||||
try {
|
||||
createDocument(root, allowedDocument);
|
||||
createDocument(root, deniedDocument);
|
||||
String allowedAssociation = createAssociation(root, allowedDocument);
|
||||
String deniedAssociation = createAssociation(root, deniedDocument);
|
||||
String userName = "ssm-tags-" + suffix;
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, RestAssured.port, auth(ACCOUNT, "iam"), userName);
|
||||
EnforcementFixtures.putUserPolicy(RestAssured.port, auth(ACCOUNT, "iam"), userName, "p", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":%s,"Resource":[
|
||||
"arn:aws:ssm:*:*:parameter/*",
|
||||
"arn:aws:ssm:*:*:document/%s",
|
||||
"arn:aws:ssm:*:*:association/%s"]}]}"""
|
||||
.formatted(TAG_ACTIONS, allowedDocument, allowedAssociation));
|
||||
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
|
||||
Region.US_EAST_1)) {
|
||||
assertDoesNotThrow(() -> tag(user, ResourceTypeForTagging.DOCUMENT, allowedDocument));
|
||||
assertDoesNotThrow(() -> tag(user, ResourceTypeForTagging.ASSOCIATION, allowedAssociation));
|
||||
assertDoesNotThrow(() -> user.removeTagsFromResource(r -> r
|
||||
.resourceType(ResourceTypeForTagging.DOCUMENT).resourceId(allowedDocument).tagKeys("team")));
|
||||
|
||||
assertDenied(() -> tag(user, ResourceTypeForTagging.DOCUMENT, deniedDocument));
|
||||
assertDenied(() -> tag(user, ResourceTypeForTagging.ASSOCIATION, deniedAssociation));
|
||||
assertDenied(() -> user.listTagsForResource(r -> r
|
||||
.resourceType(ResourceTypeForTagging.DOCUMENT).resourceId(deniedDocument)));
|
||||
}
|
||||
} finally {
|
||||
root.deleteDocument(r -> r.name(allowedDocument).force(true));
|
||||
root.deleteDocument(r -> r.name(deniedDocument).force(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aParameterIsTaggedOnItsOwnArn() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String allowedParameter = "/iam-tags-" + suffix + "/allowed";
|
||||
String deniedParameter = "/iam-tags-" + suffix + "-other/denied";
|
||||
try (SsmClient root = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1)) {
|
||||
try {
|
||||
root.putParameter(r -> r.name(allowedParameter).value("v").type("String"));
|
||||
root.putParameter(r -> r.name(deniedParameter).value("v").type("String"));
|
||||
String userName = "ssm-param-tags-" + suffix;
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, RestAssured.port, auth(ACCOUNT, "iam"), userName);
|
||||
EnforcementFixtures.putUserPolicy(RestAssured.port, auth(ACCOUNT, "iam"), userName, "p", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":%s,
|
||||
"Resource":"arn:aws:ssm:*:*:parameter/iam-tags-%s/*"}]}"""
|
||||
.formatted(TAG_ACTIONS, suffix));
|
||||
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
|
||||
Region.US_EAST_1)) {
|
||||
assertDoesNotThrow(() -> tag(user, ResourceTypeForTagging.PARAMETER, allowedParameter));
|
||||
assertDenied(() -> tag(user, ResourceTypeForTagging.PARAMETER, deniedParameter));
|
||||
}
|
||||
} finally {
|
||||
root.deleteParameters(r -> r.names(allowedParameter, deniedParameter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDocumentIsCreatedWithTagsOnItsOwnArn() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String allowedDocument = "iam-create-allowed-" + suffix;
|
||||
String deniedDocument = "iam-create-denied-" + suffix;
|
||||
try (SsmClient root = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1)) {
|
||||
try {
|
||||
String userName = "ssm-create-" + suffix;
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, RestAssured.port, auth(ACCOUNT, "iam"), userName);
|
||||
EnforcementFixtures.putUserPolicy(RestAssured.port, auth(ACCOUNT, "iam"), userName, "p", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":["ssm:CreateDocument","ssm:AddTagsToResource"],
|
||||
"Resource":"arn:aws:ssm:*:*:document/%s"}]}"""
|
||||
.formatted(allowedDocument));
|
||||
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
|
||||
Region.US_EAST_1)) {
|
||||
assertDoesNotThrow(() -> user.createDocument(r -> r.name(allowedDocument)
|
||||
.documentType(DocumentType.COMMAND).content(CONTENT)
|
||||
.tags(t -> t.key("team").value("red"))));
|
||||
assertDenied(() -> user.createDocument(r -> r.name(deniedDocument)
|
||||
.documentType(DocumentType.COMMAND).content(CONTENT)
|
||||
.tags(t -> t.key("team").value("red"))));
|
||||
}
|
||||
} finally {
|
||||
deleteIfPresent(root, allowedDocument);
|
||||
deleteIfPresent(root, deniedDocument);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteIfPresent(SsmClient root, String document) {
|
||||
try {
|
||||
root.deleteDocument(r -> r.name(document).force(true));
|
||||
} catch (InvalidDocumentException expected) {
|
||||
// The document was never created, which is what a denied create leaves.
|
||||
}
|
||||
}
|
||||
|
||||
private static void tag(SsmClient ssm, ResourceTypeForTagging type, String id) {
|
||||
ssm.addTagsToResource(r -> r.resourceType(type).resourceId(id).tags(t -> t.key("team").value("red")));
|
||||
}
|
||||
|
||||
private static void assertDenied(Runnable call) {
|
||||
SsmException error = assertThrows(SsmException.class, call::run);
|
||||
assertEquals("AccessDeniedException", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
|
||||
private static void createDocument(SsmClient root, String name) {
|
||||
root.createDocument(r -> r.name(name).documentType(DocumentType.COMMAND).content(CONTENT));
|
||||
}
|
||||
|
||||
private static String createAssociation(SsmClient root, String document) {
|
||||
return root.createAssociation(r -> r.name(document)
|
||||
.targets(t -> t.key("InstanceIds").values("i-0123456789abcdef0")))
|
||||
.associationDescription().associationId();
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.floci.aws.services.ssm;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ssm.SsmClient;
|
||||
import software.amazon.awssdk.services.ssm.model.DocumentIdentifier;
|
||||
import software.amazon.awssdk.services.ssm.model.DocumentType;
|
||||
import software.amazon.awssdk.services.ssm.model.InvalidResourceIdException;
|
||||
import software.amazon.awssdk.services.ssm.model.ResourceTypeForTagging;
|
||||
import software.amazon.awssdk.services.ssm.model.Tag;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class SsmDocumentAssociationTagsIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "100000007959";
|
||||
private static final String CONTENT = "{\"schemaVersion\":\"2.2\",\"description\":\"d\",\"mainSteps\":[{"
|
||||
+ "\"action\":\"aws:runShellScript\",\"name\":\"s\",\"inputs\":{\"runCommand\":[\"echo hi\"]}}]}";
|
||||
|
||||
private SsmClient ssm;
|
||||
private String documentName;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createClient() {
|
||||
ssm = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1);
|
||||
documentName = "tag-doc-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClient() {
|
||||
ssm.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsGivenToCreateDocumentAreListedAndReturnedWithTheDocument() {
|
||||
List<Tag> created = ssm.createDocument(r -> r.name(documentName).documentType(DocumentType.COMMAND)
|
||||
.content(CONTENT).tags(tag("team", "red"))).documentDescription().tags();
|
||||
|
||||
assertEquals(List.of(tag("team", "red")), created);
|
||||
assertEquals(List.of(tag("team", "red")), listTags(ResourceTypeForTagging.DOCUMENT, documentName));
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AmazonSSM.DescribeDocument")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/ssm/aws4_request")
|
||||
.body("{\"Name\":\"" + documentName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Document.Tags[0].Key", equalTo("team"))
|
||||
.body("Document.Tags[0].Value", equalTo("red"));
|
||||
DocumentIdentifier listed = ssm.listDocuments(r -> r.filters(f -> f.key("Name").values(documentName)))
|
||||
.documentIdentifiers().get(0);
|
||||
assertEquals(List.of(tag("team", "red")), listed.tags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDocumentIsTaggedAndUntaggedByName() {
|
||||
createDocument();
|
||||
|
||||
ssm.addTagsToResource(r -> r.resourceType(ResourceTypeForTagging.DOCUMENT).resourceId(documentName)
|
||||
.tags(tag("env", "dev"), tag("team", "blue")));
|
||||
ssm.removeTagsFromResource(r -> r.resourceType(ResourceTypeForTagging.DOCUMENT).resourceId(documentName)
|
||||
.tagKeys("env"));
|
||||
|
||||
assertEquals(List.of(tag("team", "blue")), listTags(ResourceTypeForTagging.DOCUMENT, documentName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsGivenToCreateAssociationAreListedAndTheAssociationCanBeRetagged() {
|
||||
createDocument();
|
||||
String associationId = ssm.createAssociation(r -> r.name(documentName)
|
||||
.targets(t -> t.key("InstanceIds").values("i-0123456789abcdef0"))
|
||||
.tags(tag("team", "red")))
|
||||
.associationDescription().associationId();
|
||||
|
||||
assertEquals(List.of(tag("team", "red")), listTags(ResourceTypeForTagging.ASSOCIATION, associationId));
|
||||
|
||||
ssm.addTagsToResource(r -> r.resourceType(ResourceTypeForTagging.ASSOCIATION).resourceId(associationId)
|
||||
.tags(tag("team", "blue"), tag("env", "dev")));
|
||||
ssm.removeTagsFromResource(r -> r.resourceType(ResourceTypeForTagging.ASSOCIATION)
|
||||
.resourceId(associationId).tagKeys("env"));
|
||||
|
||||
assertEquals(List.of(tag("team", "blue")), listTags(ResourceTypeForTagging.ASSOCIATION, associationId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMissingDocumentOrAssociationAnswersInvalidResourceId() {
|
||||
assertThrows(InvalidResourceIdException.class,
|
||||
() -> listTags(ResourceTypeForTagging.DOCUMENT, documentName + "-missing"));
|
||||
assertThrows(InvalidResourceIdException.class,
|
||||
() -> listTags(ResourceTypeForTagging.ASSOCIATION, UUID.randomUUID().toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aParameterIsStillTaggedByName() {
|
||||
String parameter = "/tags/" + documentName;
|
||||
ssm.putParameter(r -> r.name(parameter).value("v").type("String").tags(tag("team", "red")));
|
||||
|
||||
assertEquals(List.of(tag("team", "red")), listTags(ResourceTypeForTagging.PARAMETER, parameter));
|
||||
}
|
||||
|
||||
private void createDocument() {
|
||||
ssm.createDocument(r -> r.name(documentName).documentType(DocumentType.COMMAND).content(CONTENT));
|
||||
}
|
||||
|
||||
private List<Tag> listTags(ResourceTypeForTagging type, String id) {
|
||||
return ssm.listTagsForResource(r -> r.resourceType(type).resourceId(id)).tagList();
|
||||
}
|
||||
|
||||
private static Tag tag(String key, String value) {
|
||||
return Tag.builder().key(key).value(value).build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user