feat(secretsmanager): persist and return secret resource policies (#2587)

PutResourcePolicy and DeleteResourcePolicy were inert stubs returning an
empty object, and GetResourcePolicy never returned a policy, so
aws_secretsmanager_secret_policy could not be created through Terraform
or Pulumi: the provider uses the ARN echoed by PutResourcePolicy as the
resource id, and an empty response fails the create outright.

The policy is now stored on the secret and round-trips through the three
ops, which also share the existing recovery-window guard: real AWS
rejects all three on a secret marked for deletion with
InvalidRequestException, and the provider substring-matches that message
on GetResourcePolicy to treat the policy as gone.

BlockPublicPolicy is accepted but not enforced, and
MalformedPolicyDocumentException fires on JSON-parse failure only.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-08-26 11:41:55 -04:00
1 parent 09d7502b28
commit 8b23ed8f4a
5 files changed
+301 -3

No files matched your search

@@ -626,4 +626,171 @@ class SecretsManagerJsonHandlerTest {
assertThat(body.get("RotationRules").get("AutomaticallyAfterDays").asInt(), is(45));
assertThat(body.get("RotationRules").get("Duration").asText(), is("2h"));
}
// ─── Resource policy ─────────────────────────────────────────────────────
private static final String RESOURCE_POLICY =
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"AWS\":\"*\"},\"Action\":\"secretsmanager:GetSecretValue\",\"Resource\":\"*\"}]}";
private String createSecretReturningArn(String name) {
ObjectNode request = MAPPER.createObjectNode();
request.put("Name", name);
request.put("SecretString", "value");
Response response = handler.handle("CreateSecret", request, REGION);
assertThat(response.getStatus(), is(200));
return ((ObjectNode) response.getEntity()).get("ARN").asText();
}
@Test
void putResourcePolicyReturnsArnAndNameAndRoundTripsThroughGet() {
// Address the secret by full ARN: the terraform provider always sends secret_arn as
// SecretId, and uses the ARN echoed back by PutResourcePolicy as the resource id.
String arn = createSecretReturningArn("policy-secret");
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", arn);
put.put("ResourcePolicy", RESOURCE_POLICY);
put.put("BlockPublicPolicy", true);
Response putResponse = handler.handle("PutResourcePolicy", put, REGION);
assertThat(putResponse.getStatus(), is(200));
ObjectNode putBody = (ObjectNode) putResponse.getEntity();
assertThat(putBody.get("ARN").asText(), is(arn));
assertThat(putBody.get("Name").asText(), is("policy-secret"));
ObjectNode get = MAPPER.createObjectNode();
get.put("SecretId", arn);
Response getResponse = handler.handle("GetResourcePolicy", get, REGION);
assertThat(getResponse.getStatus(), is(200));
ObjectNode getBody = (ObjectNode) getResponse.getEntity();
assertThat(getBody.get("ARN").asText(), is(arn));
assertThat(getBody.get("Name").asText(), is("policy-secret"));
assertThat(getBody.get("ResourcePolicy").asText(), is(RESOURCE_POLICY));
}
@Test
void getResourcePolicyOmitsResourcePolicyWhenNoneAttached() {
createSecretReturningArn("no-policy-secret");
ObjectNode get = MAPPER.createObjectNode();
get.put("SecretId", "no-policy-secret");
Response response = handler.handle("GetResourcePolicy", get, REGION);
assertThat(response.getStatus(), is(200));
ObjectNode body = (ObjectNode) response.getEntity();
// AWS omits the member entirely when no policy is attached; the terraform provider
// reads the absent field as "no policy", not as an error.
assertThat(body.has("ResourcePolicy"), is(false));
assertThat(body.get("Name").asText(), is("no-policy-secret"));
}
@Test
void deleteResourcePolicyClearsPolicyAndReturnsArnAndName() {
String arn = createSecretReturningArn("delete-policy-secret");
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", arn);
put.put("ResourcePolicy", RESOURCE_POLICY);
assertThat(handler.handle("PutResourcePolicy", put, REGION).getStatus(), is(200));
ObjectNode delete = MAPPER.createObjectNode();
delete.put("SecretId", arn);
Response deleteResponse = handler.handle("DeleteResourcePolicy", delete, REGION);
assertThat(deleteResponse.getStatus(), is(200));
ObjectNode deleteBody = (ObjectNode) deleteResponse.getEntity();
assertThat(deleteBody.get("ARN").asText(), is(arn));
assertThat(deleteBody.get("Name").asText(), is("delete-policy-secret"));
ObjectNode get = MAPPER.createObjectNode();
get.put("SecretId", arn);
ObjectNode getBody = (ObjectNode) handler.handle("GetResourcePolicy", get, REGION).getEntity();
assertThat(getBody.has("ResourcePolicy"), is(false));
}
@Test
void deleteResourcePolicyWithoutPolicyAttachedSucceeds() {
String arn = createSecretReturningArn("never-had-policy-secret");
ObjectNode delete = MAPPER.createObjectNode();
delete.put("SecretId", arn);
Response response = handler.handle("DeleteResourcePolicy", delete, REGION);
assertThat(response.getStatus(), is(200));
assertThat(((ObjectNode) response.getEntity()).get("ARN").asText(), is(arn));
}
@Test
void putResourcePolicyRejectsMissingOrEmptyPolicy() {
String arn = createSecretReturningArn("missing-policy-secret");
ObjectNode missing = MAPPER.createObjectNode();
missing.put("SecretId", arn);
Response missingResponse = handler.handle("PutResourcePolicy", missing, REGION);
assertThat(missingResponse.getStatus(), is(400));
assertThat(((AwsErrorResponse) missingResponse.getEntity()).type(), is("InvalidParameterException"));
ObjectNode empty = MAPPER.createObjectNode();
empty.put("SecretId", arn);
empty.put("ResourcePolicy", "");
assertThat(handler.handle("PutResourcePolicy", empty, REGION).getStatus(), is(400));
}
@Test
void putResourcePolicyRejectsMalformedPolicyJson() {
String arn = createSecretReturningArn("malformed-policy-secret");
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", arn);
put.put("ResourcePolicy", "not-a-json-policy");
Response response = handler.handle("PutResourcePolicy", put, REGION);
assertThat(response.getStatus(), is(400));
assertThat(((AwsErrorResponse) response.getEntity()).type(), is("MalformedPolicyDocumentException"));
}
@Test
void resourcePolicyOpsOnUnknownSecretThrowResourceNotFound() {
for (String action : new String[] { "GetResourcePolicy", "DeleteResourcePolicy" }) {
ObjectNode request = MAPPER.createObjectNode();
request.put("SecretId", "missing-secret");
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle(action, request, REGION));
assertThat(ex.getErrorCode(), is("ResourceNotFoundException"));
}
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", "missing-secret");
put.put("ResourcePolicy", RESOURCE_POLICY);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle("PutResourcePolicy", put, REGION));
assertThat(ex.getErrorCode(), is("ResourceNotFoundException"));
}
@Test
void resourcePolicyOpsOnSecretMarkedForDeletionThrowInvalidRequest() {
String arn = createSecretReturningArn("pending-deletion-policy-secret");
ObjectNode deleteSecret = MAPPER.createObjectNode();
deleteSecret.put("SecretId", arn);
assertThat(handler.handle("DeleteSecret", deleteSecret, REGION).getStatus(), is(200));
// The message substring is a compatibility contract: the terraform provider matches
// "marked for deletion" on GetResourcePolicy to treat the policy as gone.
for (String action : new String[] { "GetResourcePolicy", "DeleteResourcePolicy" }) {
ObjectNode request = MAPPER.createObjectNode();
request.put("SecretId", arn);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle(action, request, REGION));
assertThat(ex.getErrorCode(), is("InvalidRequestException"));
assertThat(ex.getMessage(), containsString("marked for deletion"));
}
ObjectNode put = MAPPER.createObjectNode();
put.put("SecretId", arn);
put.put("ResourcePolicy", RESOURCE_POLICY);
io.github.hectorvent.floci.core.common.AwsException ex = org.junit.jupiter.api.Assertions.assertThrows(
io.github.hectorvent.floci.core.common.AwsException.class,
() -> handler.handle("PutResourcePolicy", put, REGION));
assertThat(ex.getErrorCode(), is("InvalidRequestException"));
assertThat(ex.getMessage(), containsString("marked for deletion"));
}
}