mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
09d7502b28
commit
8b23ed8f4a
5 files changed
+301
-3
No files matched your search
+52
-3
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.secretsmanager;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@@ -48,8 +49,8 @@ public class SecretsManagerJsonHandler {
|
||||
case "GetResourcePolicy" -> handleGetResourcePolicy(request, region);
|
||||
case "GetRandomPassword" -> handleGetRandomPassword(request, region);
|
||||
case "BatchGetSecretValue" -> handleBatchGetSecretValue(request, region);
|
||||
case "DeleteResourcePolicy" -> Response.ok(objectMapper.createObjectNode()).build();
|
||||
case "PutResourcePolicy" -> Response.ok(objectMapper.createObjectNode()).build();
|
||||
case "DeleteResourcePolicy" -> handleDeleteResourcePolicy(request, region);
|
||||
case "PutResourcePolicy" -> handlePutResourcePolicy(request, region);
|
||||
case "UpdateSecretVersionStage" -> handleUpdateSecretVersionStage(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
|
||||
@@ -538,7 +539,55 @@ public class SecretsManagerJsonHandler {
|
||||
|
||||
private Response handleGetResourcePolicy(JsonNode request, String region) {
|
||||
String secretId = request.path("SecretId").asText();
|
||||
Secret secret = service.describeSecret(secretId, region);
|
||||
Secret secret = service.getResourcePolicy(secretId, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("ARN", secret.getArn());
|
||||
response.put("Name", secret.getName());
|
||||
// AWS omits ResourcePolicy entirely when no policy is attached; the terraform provider
|
||||
// reads the absent field as "no policy" rather than as an error.
|
||||
if (secret.getResourcePolicy() != null) {
|
||||
response.put("ResourcePolicy", secret.getResourcePolicy());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
// BlockPublicPolicy is accepted but not enforced: the emulator does no public-policy
|
||||
// analysis, so PublicPolicyException is never raised.
|
||||
private Response handlePutResourcePolicy(JsonNode request, String region) {
|
||||
String secretId = request.path("SecretId").asText();
|
||||
String resourcePolicy = request.hasNonNull("ResourcePolicy") ? request.get("ResourcePolicy").asText() : null;
|
||||
if (resourcePolicy == null || resourcePolicy.isBlank()) {
|
||||
return Response.status(400)
|
||||
.entity(new AwsErrorResponse("InvalidParameterException",
|
||||
"Invalid parameter: ResourcePolicy must not be null or empty."))
|
||||
.build();
|
||||
}
|
||||
|
||||
JsonNode parsedPolicy;
|
||||
try {
|
||||
parsedPolicy = objectMapper.readTree(resourcePolicy);
|
||||
} catch (JsonProcessingException e) {
|
||||
parsedPolicy = null;
|
||||
}
|
||||
if (parsedPolicy == null || !parsedPolicy.isObject()) {
|
||||
return Response.status(400)
|
||||
.entity(new AwsErrorResponse("MalformedPolicyDocumentException",
|
||||
"The resource policy is not a valid JSON policy document."))
|
||||
.build();
|
||||
}
|
||||
|
||||
Secret secret = service.putResourcePolicy(secretId, resourcePolicy, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
// The terraform provider uses the returned ARN as the aws_secretsmanager_secret_policy
|
||||
// resource id, so an empty response breaks its create outright.
|
||||
response.put("ARN", secret.getArn());
|
||||
response.put("Name", secret.getName());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteResourcePolicy(JsonNode request, String region) {
|
||||
String secretId = request.path("SecretId").asText();
|
||||
Secret secret = service.deleteResourcePolicy(secretId, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("ARN", secret.getArn());
|
||||
response.put("Name", secret.getName());
|
||||
|
||||
+27
@@ -773,6 +773,33 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
store.put(regionKey(region, secret.getName()), secret);
|
||||
}
|
||||
|
||||
public Secret putResourcePolicy(String secretId, String resourcePolicy, String region) {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
throwIfPendingDeletion(secret);
|
||||
secret.setResourcePolicy(resourcePolicy);
|
||||
store.put(regionKey(region, secret.getName()), secret);
|
||||
LOG.infov("Put resource policy on secret: {0}", secret.getName());
|
||||
return secret;
|
||||
}
|
||||
|
||||
// Real AWS rejects GetResourcePolicy on a secret in its recovery window the same way it
|
||||
// rejects the mutating ops; the terraform provider matches that InvalidRequestException's
|
||||
// "marked for deletion" message to treat the policy as gone, so the guard applies here too.
|
||||
public Secret getResourcePolicy(String secretId, String region) {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
throwIfPendingDeletion(secret);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public Secret deleteResourcePolicy(String secretId, String region) {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
throwIfPendingDeletion(secret);
|
||||
secret.setResourcePolicy(null);
|
||||
store.put(regionKey(region, secret.getName()), secret);
|
||||
LOG.infov("Deleted resource policy on secret: {0}", secret.getName());
|
||||
return secret;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> listSecretVersionIds(String secretId, String region) {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ public class Secret {
|
||||
private String targetAttachmentOwner;
|
||||
/** The AWS service that owns this secret and rotates it itself, such as {@code rds}. */
|
||||
private String owningService;
|
||||
/** Resource-based policy JSON attached via PutResourcePolicy, or null when none is attached. */
|
||||
private String resourcePolicy;
|
||||
|
||||
@RegisterForReflection
|
||||
public record RotationRules(
|
||||
@@ -191,4 +193,12 @@ public class Secret {
|
||||
public void setOwningService(String owningService) {
|
||||
this.owningService = owningService;
|
||||
}
|
||||
|
||||
public String getResourcePolicy() {
|
||||
return resourcePolicy;
|
||||
}
|
||||
|
||||
public void setResourcePolicy(String resourcePolicy) {
|
||||
this.resourcePolicy = resourcePolicy;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user