mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(redshift-serverless): add resource policy actions for snapshots (#5730)
* feat(redshift-serverless): add resource policy actions for snapshots Add PutResourcePolicy, GetResourcePolicy and DeleteResourcePolicy. A policy is stored per account and Region and echoed in the shape of the API model, not evaluated. It is tied to the snapshot it was put on by the snapshot's creation time, so a snapshot recreated under the same name does not inherit it. Required members come from the AWS model through RedshiftServerlessRequestShapes. * fix(redshift-serverless): validate resource ARN before policy lookup and add account isolation tests Validate resourceArn with snapshotNameFromArn at the start of require() so that foreign-account or non-snapshot ARNs return ValidationException on GetResourcePolicy and DeleteResourcePolicy even when no policy is stored. Parameterize snapshotNameFromArn so error messages reference resourceArn. Add regression tests covering missing resourceArn on PutResourcePolicy and foreign ARN reads/deletes. Add integration test verifying policy isolation across accounts under real request contexts.
This commit is contained in:
1 parent
209ecd799b
commit
a68e6bc768
11 files changed
+478
-9
No files matched your search
+25
-1
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.ResourcePolicy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -25,14 +26,26 @@ public class RedshiftServerlessAdminJsonHandler {
|
||||
private static final List<String> TRACK_NAMES = RedshiftServerlessService.TRACK_NAMES;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RedshiftServerlessResourcePolicyService resourcePolicies;
|
||||
|
||||
@Inject
|
||||
public RedshiftServerlessAdminJsonHandler(ObjectMapper objectMapper) {
|
||||
public RedshiftServerlessAdminJsonHandler(ObjectMapper objectMapper,
|
||||
RedshiftServerlessResourcePolicyService resourcePolicies) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.resourcePolicies = resourcePolicies;
|
||||
}
|
||||
|
||||
Supplier<Response> operation(String action, JsonNode request, String region) {
|
||||
return switch (action) {
|
||||
case "PutResourcePolicy" -> () -> resourcePolicyResponse(resourcePolicies.putResourcePolicy(
|
||||
ServerlessSupport.text(request, "resourceArn"), ServerlessSupport.text(request, "policy"),
|
||||
region));
|
||||
case "GetResourcePolicy" -> () -> resourcePolicyResponse(resourcePolicies.getResourcePolicy(
|
||||
ServerlessSupport.text(request, "resourceArn"), region));
|
||||
case "DeleteResourcePolicy" -> () -> {
|
||||
resourcePolicies.deleteResourcePolicy(ServerlessSupport.text(request, "resourceArn"), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
};
|
||||
case "GetTrack" -> () -> getTrack(ServerlessSupport.text(request, "trackName"));
|
||||
case "ListTracks" -> () -> listTracks(request);
|
||||
case "ListManagedWorkgroups" -> () -> listWithoutItems(request, "managedWorkgroups");
|
||||
@@ -62,6 +75,17 @@ public class RedshiftServerlessAdminJsonHandler {
|
||||
+ ServerlessSupport.text(request, "offeringId") + " was not found.");
|
||||
}
|
||||
|
||||
// ── Resource policy ────────────────────────────────────────────────────
|
||||
|
||||
private Response resourcePolicyResponse(ResourcePolicy policy) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("policy", policy.getPolicy());
|
||||
node.put("resourceArn", policy.getResourceArn());
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("resourcePolicy", node);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
// ── Tracks, managed workgroups and reservations ────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package io.github.hectorvent.floci.services.redshiftserverless;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.ResourcePolicy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Resource policies for Redshift Serverless snapshots. A policy is stored and echoed, not
|
||||
* evaluated: no principal named in it gains access.
|
||||
*
|
||||
* <p>A policy belongs to a snapshot but lives in its own store, so deleting the snapshot needs no
|
||||
* hook here. Each read checks that the snapshot is still there and drops the policy otherwise.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RedshiftServerlessResourcePolicyService implements Resettable {
|
||||
|
||||
private final AccountAwareStorageBackend<ResourcePolicy> policies;
|
||||
private final RedshiftServerlessService serverless;
|
||||
private final ObjectReader strictPolicyReader;
|
||||
|
||||
@Inject
|
||||
public RedshiftServerlessResourcePolicyService(StorageFactory storageFactory,
|
||||
RedshiftServerlessService serverless,
|
||||
ObjectMapper objectMapper) {
|
||||
this.policies = storageFactory.create("redshiftserverless", "redshiftserverless-resourcepolicies.json",
|
||||
new TypeReference<Map<String, ResourcePolicy>>() {});
|
||||
this.serverless = serverless;
|
||||
this.strictPolicyReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
}
|
||||
|
||||
/** Replaces the policy on the snapshot {@code resourceArn} names. */
|
||||
public synchronized ResourcePolicy putResourcePolicy(String resourceArn, String policy, String region) {
|
||||
requireJsonObject(policy);
|
||||
RedshiftServerlessSnapshot snapshot = findSnapshot(resourceArn, region)
|
||||
.orElseThrow(() -> ServerlessSupport.notFound("The resource " + resourceArn + " was not found."));
|
||||
ResourcePolicy stored = new ResourcePolicy(resourceArn, policy);
|
||||
stored.setSnapshotCreateTime(snapshot.getSnapshotCreateTime());
|
||||
policies.put(storageKey(region, resourceArn), stored);
|
||||
return new ResourcePolicy(resourceArn, policy);
|
||||
}
|
||||
|
||||
public synchronized ResourcePolicy getResourcePolicy(String resourceArn, String region) {
|
||||
ResourcePolicy stored = require(resourceArn, region);
|
||||
return new ResourcePolicy(stored.getResourceArn(), stored.getPolicy());
|
||||
}
|
||||
|
||||
public synchronized void deleteResourcePolicy(String resourceArn, String region) {
|
||||
require(resourceArn, region);
|
||||
policies.delete(storageKey(region, resourceArn));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
policies.clear();
|
||||
}
|
||||
|
||||
private ResourcePolicy require(String resourceArn, String region) {
|
||||
serverless.snapshotNameFromArn(resourceArn, region, "resourceArn");
|
||||
ResourcePolicy stored = policies.get(storageKey(region, resourceArn)).orElse(null);
|
||||
if (stored != null && !isOnSameSnapshot(stored, region)) {
|
||||
policies.delete(storageKey(region, resourceArn));
|
||||
stored = null;
|
||||
}
|
||||
if (stored == null) {
|
||||
throw ServerlessSupport.notFound("The resource " + resourceArn + " has no resource policy.");
|
||||
}
|
||||
return stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* A snapshot ARN carries only the snapshot's name, so a snapshot recreated under the same name
|
||||
* is told apart by its creation time: a policy left by its predecessor is not this snapshot's.
|
||||
*/
|
||||
private boolean isOnSameSnapshot(ResourcePolicy stored, String region) {
|
||||
return findSnapshot(stored.getResourceArn(), region)
|
||||
.map(snapshot -> Objects.equals(snapshot.getSnapshotCreateTime(), stored.getSnapshotCreateTime()))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
private Optional<RedshiftServerlessSnapshot> findSnapshot(String resourceArn, String region) {
|
||||
return serverless.findSnapshot(serverless.snapshotNameFromArn(resourceArn, region, "resourceArn"), region);
|
||||
}
|
||||
|
||||
private void requireJsonObject(String policy) {
|
||||
try {
|
||||
JsonNode parsed = strictPolicyReader.readTree(policy);
|
||||
if (parsed == null || !parsed.isObject()) {
|
||||
throw ServerlessSupport.validation("policy must be a JSON policy document.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw ServerlessSupport.validation("policy must be a JSON policy document.");
|
||||
}
|
||||
}
|
||||
|
||||
private static String storageKey(String region, String resourceArn) {
|
||||
return region + "::" + resourceArn;
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -464,13 +464,22 @@ public class RedshiftServerlessService implements Resettable {
|
||||
* Region or resource type rather than resolving it to a local snapshot of the same name.
|
||||
*/
|
||||
public String snapshotNameFromArn(String snapshotArn, String region) {
|
||||
return snapshotNameFromArn(snapshotArn, region, "snapshotArn");
|
||||
}
|
||||
|
||||
public String snapshotNameFromArn(String snapshotArn, String region, String memberName) {
|
||||
String prefix = regionResolver.buildArn("redshift-serverless", region, "snapshot/");
|
||||
if (snapshotArn == null || !snapshotArn.startsWith(prefix) || snapshotArn.length() == prefix.length()) {
|
||||
throw ServerlessSupport.validation("snapshotArn does not identify a snapshot in this account and Region.");
|
||||
throw ServerlessSupport.validation(memberName + " does not identify a snapshot in this account and Region.");
|
||||
}
|
||||
return snapshotArn.substring(prefix.length());
|
||||
}
|
||||
|
||||
/** The snapshot called {@code snapshotName} in {@code region}, empty when there is none. */
|
||||
public Optional<RedshiftServerlessSnapshot> findSnapshot(String snapshotName, String region) {
|
||||
return snapshotName == null ? Optional.empty() : snapshots.get(storageKey(region, snapshotName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes a snapshot's retention. An omitted period leaves the snapshot as it is, and -1 makes
|
||||
* it indefinite. {@code snapshotRemainingDays} keeps counting from the creation time, because
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package io.github.hectorvent.floci.services.redshiftserverless.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/** The resource policy attached to a Redshift Serverless snapshot. Floci stores it but does not evaluate it. */
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ResourcePolicy {
|
||||
|
||||
private String resourceArn;
|
||||
private String policy;
|
||||
private Instant snapshotCreateTime;
|
||||
|
||||
public ResourcePolicy() {
|
||||
}
|
||||
|
||||
public ResourcePolicy(String resourceArn, String policy) {
|
||||
this.resourceArn = resourceArn;
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
/** The creation time of the snapshot the policy was put on, which tells a recreated snapshot apart. */
|
||||
public Instant getSnapshotCreateTime() {
|
||||
return snapshotCreateTime;
|
||||
}
|
||||
|
||||
public void setSnapshotCreateTime(Instant snapshotCreateTime) {
|
||||
this.snapshotCreateTime = snapshotCreateTime;
|
||||
}
|
||||
|
||||
public String getResourceArn() {
|
||||
return resourceArn;
|
||||
}
|
||||
|
||||
public void setResourceArn(String resourceArn) {
|
||||
this.resourceArn = resourceArn;
|
||||
}
|
||||
|
||||
public String getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
public void setPolicy(String policy) {
|
||||
this.policy = policy;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user