feat(ssm): add PutResourcePolicy, GetResourcePolicies and DeleteResourcePolicy for parameters (#6833)

AWS lets an Advanced-tier parameter carry one resource-based policy, used to
share it with other accounts. PutResourcePolicy returns a PolicyId and a
PolicyHash, and updating or deleting the policy takes both: a hash that is no
longer current is ResourcePolicyConflictException, an unknown id is
ResourcePolicyNotFoundException, and an unknown parameter is
ResourceNotFoundException. Floci answered all three actions with
UnsupportedOperation.

The parameter now stores its policy with an id and a hash of the document.
PutResourcePolicy rejects a Standard-tier parameter, a policy over 1024 bytes
and a document that is not a JSON object with the documented errors.
GetResourcePolicies pages with MaxResults (1 to 50) and NextToken. An
overwriting PutParameter keeps the policy and deleting the parameter removes
it. The policy is not evaluated against callers, and no AWS RAM resource share
is created.
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-10-30 11:09:39 -04:00
1 parent e29b1d884c
commit 045c65ec49
9 files changed
+612

No files matched your search

@@ -1447,6 +1447,23 @@ class ResourceArnBuilderTest {
assertEquals("arn:aws:ssm:us-east-1:000000000000:parameter/config-key", arn);
}
@Test
void ssmResourcePolicyActionsActOnTheParameterTheirResourceArnNames() {
when(ctx.getHeaderString("X-Amz-Target")).thenReturn("AmazonSSM.PutResourcePolicy");
setJsonBody("{\"ResourceArn\":\"arn:aws:ssm:us-east-1:000000000000:parameter/prod/db\",\"Policy\":\"{}\"}");
assertEquals("arn:aws:ssm:us-east-1:000000000000:parameter/prod/db",
builder.build("ssm", ctx, "us-east-1", "000000000000"));
// Any other action acts on the parameter its handler reads, its Name, whatever else the body carries.
when(ctx.getHeaderString("X-Amz-Target")).thenReturn("AmazonSSM.GetParameter");
setJsonBody("{\"Name\":\"/prod/db\",\"ResourceArn\":\"arn:aws:ssm:us-east-1:000000000000:parameter/dev/db\"}");
assertEquals("arn:aws:ssm:us-east-1:000000000000:parameter/prod/db",
builder.build("ssm", ctx, "us-east-1", "000000000000"));
when(ctx.getHeaderString("X-Amz-Target")).thenReturn(null);
assertEquals("arn:aws:ssm:us-east-1:000000000000:parameter/prod/db",
builder.build("ssm", ctx, "us-east-1", "000000000000"));
}
// ── SQS ─────────────────────────────────────────────────────────────────────
@Test
@@ -0,0 +1,98 @@
package io.github.hectorvent.floci.services.ssm;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeAll;
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.ParameterTier;
import software.amazon.awssdk.services.ssm.model.ParameterType;
import software.amazon.awssdk.services.ssm.model.SsmException;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** The resource policy actions are checked against the parameter their {@code ResourceArn} names. */
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class SsmResourcePolicyIamEnforcementIntegrationTest {
private static final String ACCOUNT = "000000000000";
private static final String POLICY = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"AWS":"arn:aws:iam::111122223333:root"},"Action":"ssm:GetParameter","Resource":"*"}]}""";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void aDenyOnAParameterPathRefusesThePolicyActionsOnIt() {
String prefix = "/iam-resource-policy-" + UUID.randomUUID();
String denied = prefix + "/prod/db";
String allowed = prefix + "/dev/db";
try (SsmClient root = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1)) {
try {
String deniedArn = advancedParameter(root, denied);
String allowedArn = advancedParameter(root, allowed);
String userName = "ssm-policy-" + UUID.randomUUID();
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:*","Resource":"*"},
{"Effect":"Deny","Action":["ssm:PutResourcePolicy","ssm:GetResourcePolicies",
"ssm:DeleteResourcePolicy","ssm:GetParameter"],
"Resource":"arn:aws:ssm:*:*:parameter%s/prod/*"}]}"""
.formatted(prefix));
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
Region.US_EAST_1)) {
assertDoesNotThrow(() -> user.putResourcePolicy(r -> r.resourceArn(allowedArn).policy(POLICY)));
assertDoesNotThrow(() -> user.getResourcePolicies(r -> r.resourceArn(allowedArn)));
SsmException put = assertThrows(SsmException.class,
() -> user.putResourcePolicy(r -> r.resourceArn(deniedArn).policy(POLICY)));
assertEquals("AccessDeniedException", put.awsErrorDetails().errorCode());
SsmException get = assertThrows(SsmException.class,
() -> user.getResourcePolicies(r -> r.resourceArn(deniedArn)));
assertEquals("AccessDeniedException", get.awsErrorDetails().errorCode());
}
// Another action still acts on the parameter its handler reads, whatever ResourceArn it carries.
given()
.header("Authorization", auth(credentials.accessKeyId(), "ssm"))
.header("X-Amz-Target", "AmazonSSM.GetParameter")
.contentType("application/x-amz-json-1.1")
.body("{\"Name\":\"" + denied + "\",\"ResourceArn\":\"" + allowedArn + "\"}")
.when().post("/")
.then().statusCode(400).body(containsString("AccessDeniedException"));
} finally {
root.deleteParameter(r -> r.name(denied));
root.deleteParameter(r -> r.name(allowed));
}
}
}
private static String advancedParameter(SsmClient root, String name) {
root.putParameter(r -> r.name(name).type(ParameterType.STRING).value("v").tier(ParameterTier.ADVANCED));
return root.getParameter(r -> r.name(name)).parameter().arn();
}
}
@@ -0,0 +1,229 @@
package io.github.hectorvent.floci.services.ssm;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
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.GetResourcePoliciesResponse;
import software.amazon.awssdk.services.ssm.model.GetResourcePoliciesResponseEntry;
import software.amazon.awssdk.services.ssm.model.MalformedResourcePolicyDocumentException;
import software.amazon.awssdk.services.ssm.model.ParameterTier;
import software.amazon.awssdk.services.ssm.model.ParameterType;
import software.amazon.awssdk.services.ssm.model.PutResourcePolicyResponse;
import software.amazon.awssdk.services.ssm.model.ResourceNotFoundException;
import software.amazon.awssdk.services.ssm.model.ResourcePolicyConflictException;
import software.amazon.awssdk.services.ssm.model.ResourcePolicyInvalidParameterException;
import software.amazon.awssdk.services.ssm.model.ResourcePolicyLimitExceededException;
import software.amazon.awssdk.services.ssm.model.ResourcePolicyNotFoundException;
import software.amazon.awssdk.services.ssm.model.SsmException;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class SsmResourcePolicyIntegrationTest {
private static final String POLICY = """
{"Version":"2012-10-17","Statement":[{"Sid":"Share","Effect":"Allow",\
"Principal":{"AWS":"arn:aws:iam::111122223333:root"},\
"Action":["ssm:GetParameter"],"Resource":"*"}]}""";
private static final String UPDATED_POLICY = POLICY.replace("\"Share\"", "\"ShareUpdated\"");
private SsmClient ssm;
@BeforeEach
void createClient() {
ssm = AwsTestClients.create(SsmClient.builder(), "000000000000", Region.US_EAST_1);
}
@AfterEach
void closeClient() {
ssm.close();
}
@Test
void putGetUpdateAndDeleteTheParameterPolicy() {
String arn = parameter(ParameterTier.ADVANCED);
assertTrue(policies(arn).isEmpty());
PutResourcePolicyResponse created = ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY));
assertFalse(created.policyId().isEmpty());
assertFalse(created.policyHash().isEmpty());
List<GetResourcePoliciesResponseEntry> attached = policies(arn);
assertEquals(1, attached.size());
assertEquals(created.policyId(), attached.get(0).policyId());
assertEquals(created.policyHash(), attached.get(0).policyHash());
assertEquals(POLICY, attached.get(0).policy());
PutResourcePolicyResponse updated = ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(UPDATED_POLICY)
.policyId(created.policyId()).policyHash(created.policyHash()));
assertEquals(created.policyId(), updated.policyId());
assertNotEquals(created.policyHash(), updated.policyHash());
assertEquals(UPDATED_POLICY, policies(arn).get(0).policy());
assertEquals(updated.policyHash(), policies(arn).get(0).policyHash());
ssm.deleteResourcePolicy(r -> r.resourceArn(arn)
.policyId(updated.policyId()).policyHash(updated.policyHash()));
assertTrue(policies(arn).isEmpty());
}
@Test
void aMissingOrStaleHashConflictsWithTheStoredPolicy() {
String arn = parameter(ParameterTier.ADVANCED);
PutResourcePolicyResponse created = ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY));
PutResourcePolicyResponse updated = ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(UPDATED_POLICY)
.policyId(created.policyId()).policyHash(created.policyHash()));
assertThrows(ResourcePolicyConflictException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)));
assertThrows(ResourcePolicyConflictException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)
.policyHash(updated.policyHash())));
assertThrows(ResourcePolicyConflictException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)
.policyId(created.policyId()).policyHash(created.policyHash())));
assertThrows(ResourcePolicyConflictException.class,
() -> ssm.deleteResourcePolicy(r -> r.resourceArn(arn)
.policyId(created.policyId()).policyHash(created.policyHash())));
assertThrows(ResourcePolicyNotFoundException.class,
() -> ssm.deleteResourcePolicy(r -> r.resourceArn(arn)
.policyId(UUID.randomUUID().toString()).policyHash(updated.policyHash())));
List<GetResourcePoliciesResponseEntry> attached = policies(arn);
assertEquals(1, attached.size());
assertEquals(updated.policyHash(), attached.get(0).policyHash());
assertEquals(UPDATED_POLICY, attached.get(0).policy());
}
@Test
void aPolicyIdIsNotFoundOnAParameterWithoutAPolicy() {
String arn = parameter(ParameterTier.ADVANCED);
assertThrows(ResourcePolicyNotFoundException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)
.policyId(UUID.randomUUID().toString()).policyHash("hash")));
assertThrows(ResourcePolicyNotFoundException.class,
() -> ssm.deleteResourcePolicy(r -> r.resourceArn(arn)
.policyId(UUID.randomUUID().toString()).policyHash("hash")));
assertTrue(policies(arn).isEmpty());
}
@Test
void anUnknownParameterIsResourceNotFound() {
String missing = parameter(ParameterTier.ADVANCED) + "-missing";
assertThrows(ResourceNotFoundException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(missing).policy(POLICY)));
assertThrows(ResourceNotFoundException.class,
() -> ssm.getResourcePolicies(r -> r.resourceArn(missing)));
assertThrows(ResourceNotFoundException.class,
() -> ssm.deleteResourcePolicy(r -> r.resourceArn(missing).policyId("id").policyHash("hash")));
}
@Test
void anOverwriteKeepsThePolicyAndADeleteRemovesIt() {
String name = "/resource-policy/" + UUID.randomUUID();
String arn = parameter(name, ParameterTier.ADVANCED);
PutResourcePolicyResponse created = ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY));
ssm.putParameter(r -> r.name(name).value("v2").type(ParameterType.STRING).overwrite(true));
List<GetResourcePoliciesResponseEntry> attached = policies(arn);
assertEquals(1, attached.size());
assertEquals(created.policyId(), attached.get(0).policyId());
ssm.deleteParameter(r -> r.name(name));
parameter(name, ParameterTier.ADVANCED);
assertTrue(policies(arn).isEmpty());
}
@Test
void onlyAnAdvancedParameterTakesAPolicy() {
String arn = parameter(ParameterTier.STANDARD);
assertThrows(ResourcePolicyInvalidParameterException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)));
assertTrue(policies(arn).isEmpty());
}
@Test
void anOverwriteThatWouldLeaveThePolicyOnAnIneligibleParameterIsRefused() {
String name = "/resource-policy/" + UUID.randomUUID();
String arn = parameter(name, ParameterTier.ADVANCED);
ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY));
assertThrows(ResourcePolicyInvalidParameterException.class, () -> ssm.putParameter(r -> r.name(name)
.value("v2").type(ParameterType.STRING).tier(ParameterTier.STANDARD).overwrite(true)));
assertThrows(ResourcePolicyInvalidParameterException.class, () -> ssm.putParameter(r -> r.name(name)
.value("v2").type(ParameterType.SECURE_STRING).overwrite(true)));
assertEquals("v1", ssm.getParameter(r -> r.name(name)).parameter().value());
assertEquals(1, policies(arn).size());
}
@Test
void aSecureStringUnderTheDefaultKeyTakesNoPolicy() {
for (String keyId : new String[] {null, "arn:aws:kms:us-east-1:000000000000:alias/aws/ssm"}) {
String name = "/resource-policy/" + UUID.randomUUID();
ssm.putParameter(r -> r.name(name).value("secret").type(ParameterType.SECURE_STRING).keyId(keyId)
.tier(ParameterTier.ADVANCED));
String arn = ssm.getParameter(r -> r.name(name)).parameter().arn();
assertThrows(ResourcePolicyInvalidParameterException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY)));
assertTrue(policies(arn).isEmpty());
}
}
@Test
void rejectsAnOversizedOrMalformedPolicy() {
String arn = parameter(ParameterTier.ADVANCED);
String oversized = "{\"Version\":\"2012-10-17\",\"Statement\":[],\"Sid\":\"" + "x".repeat(1024) + "\"}";
assertThrows(ResourcePolicyLimitExceededException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(oversized)));
assertThrows(MalformedResourcePolicyDocumentException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy("not-json")));
assertThrows(MalformedResourcePolicyDocumentException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy("[]")));
assertThrows(MalformedResourcePolicyDocumentException.class,
() -> ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY + " {}")));
assertTrue(policies(arn).isEmpty());
}
@Test
void getResourcePoliciesValidatesMaxResults() {
String arn = parameter(ParameterTier.ADVANCED);
ssm.putResourcePolicy(r -> r.resourceArn(arn).policy(POLICY));
for (int maxResults : new int[] {0, 51}) {
SsmException e = assertThrows(SsmException.class,
() -> ssm.getResourcePolicies(r -> r.resourceArn(arn).maxResults(maxResults)));
assertEquals("ValidationException", e.awsErrorDetails().errorCode());
}
GetResourcePoliciesResponse page = ssm.getResourcePolicies(r -> r.resourceArn(arn).maxResults(1));
assertEquals(1, page.policies().size());
assertNull(page.nextToken());
}
private String parameter(ParameterTier tier) {
return parameter("/resource-policy/" + UUID.randomUUID(), tier);
}
private String parameter(String name, ParameterTier tier) {
ssm.putParameter(r -> r.name(name).value("v1").type(ParameterType.STRING).tier(tier));
return ssm.getParameter(r -> r.name(name)).parameter().arn();
}
private List<GetResourcePoliciesResponseEntry> policies(String arn) {
return ssm.getResourcePolicies(r -> r.resourceArn(arn)).policies();
}
}