mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(iam): check Redshift requests on the resources they name (#7287)
Under IAM enforcement, Redshift requests were checked on `*`, so a grant scoped to a cluster, a snapshot or a database user was refused, and a deny scoped to one never applied. The redshift namespace moves from NOT_COVERED to NAMESPACES in the request resources generator, with corrections where the generated rule checks the wrong resource. GetClusterCredentials now also gives the redshift:DbUser condition key, from a new generated table, to its own check and to the CreateClusterUser check that AutoCreate adds. Refs #5907 fix(redshift): act only on a snapshot of the cluster the request names A snapshot request is checked on snapshot:<cluster>/<name> for the cluster it names, but DeleteClusterSnapshot, RestoreFromClusterSnapshot and DescribeClusterSnapshots found the snapshot by its name alone. A caller allowed on one cluster's snapshots could act on a snapshot of another cluster by naming the allowed one. A snapshot taken from another cluster than the one the request names, in SnapshotClusterIdentifier, the cluster of a SnapshotArn, or DescribeClusterSnapshots' ClusterIdentifier, is now not found. Refs #5907 chore(iam): generate the Lambda Web and Redshift condition keys into one table chore(redshift): read a restore's snapshot ARN once
This commit is contained in:
1 parent
ddfc63f6b6
commit
5bcb83b932
22 files changed
+1026
-328
No files matched your search
+19
-2
@@ -156,6 +156,7 @@ public class IamConditionContextResolver {
|
||||
case "iam" -> iamConditionContext(action, ctx);
|
||||
case "logs" -> logsConditionContext(action, ctx);
|
||||
case "lambda" -> lambdaConditionContext(action, ctx);
|
||||
case "redshift" -> redshiftConditionContext(action, ctx);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
@@ -267,6 +268,22 @@ public class IamConditionContextResolver {
|
||||
};
|
||||
}
|
||||
|
||||
/** The {@code redshift:} keys a Redshift request carries in its own members, such as its DbUser. */
|
||||
private Map<String, List<String>> redshiftConditionContext(String action, ContainerRequestContext ctx) {
|
||||
List<String> keys = RequestConditionKeys.requestKeys(action);
|
||||
if (keys.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> form = formParameters(ctx);
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
for (String key : keys) {
|
||||
for (String member : RequestConditionKeys.members(action, key)) {
|
||||
putIfPresent(conditions, key, form.get(member));
|
||||
}
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
public static Map<String, List<String>> withGlobalContext(Map<String, List<String>> serviceContext,
|
||||
String resourceArn, String region,
|
||||
String accountId, String resourceOwnerAccountId) {
|
||||
@@ -350,11 +367,11 @@ public class IamConditionContextResolver {
|
||||
/** The authType a web function endpoint request names, and the one the endpoint has before it. */
|
||||
private Map<String, List<String>> lambdaConditionContext(String action, ContainerRequestContext ctx) {
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
for (String member : LambdaWebConditionKeys.members(action, REQUEST_AUTH_TYPE)) {
|
||||
for (String member : RequestConditionKeys.members(action, REQUEST_AUTH_TYPE)) {
|
||||
PassedRoles.values(PassedRoles.jsonBody(ctx), member.split("\\."))
|
||||
.forEach(value -> putIfPresent(conditions, REQUEST_AUTH_TYPE, value));
|
||||
}
|
||||
if (LambdaWebConditionKeys.takes(action, RESOURCE_AUTH_TYPE)) {
|
||||
if (RequestConditionKeys.takes(action, RESOURCE_AUTH_TYPE)) {
|
||||
putIfPresent(conditions, RESOURCE_AUTH_TYPE, storedAuthType(ctx));
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
|
||||
@@ -1429,7 +1429,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
ResourcePolicyDecision resourcePolicyDecision,
|
||||
String resourceOwnerAccountId) {
|
||||
authorizeAdditionalResource(authorizationHeader, action, resource, resourcePolicyDecision,
|
||||
resourceOwnerAccountId, null);
|
||||
resourceOwnerAccountId, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorizes a secondary resource with the condition keys {@code requestConditions} gives, values its
|
||||
* handler reads from the request {@link #filter} has authorized: GetClusterCredentials checks
|
||||
* {@code redshift:CreateClusterUser} with the {@code redshift:DbUser} of its own {@code DbUser}, so a
|
||||
* policy can scope it to {@code dbuser:<cluster>/${redshift:DbUser}}.
|
||||
*/
|
||||
public void authorizeAdditionalResource(
|
||||
String authorizationHeader,
|
||||
String action,
|
||||
String resource,
|
||||
Map<String, List<String>> requestConditions) {
|
||||
authorizeAdditionalResource(authorizationHeader, action, resource, null, null, null, requestConditions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1443,21 +1457,25 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
*/
|
||||
public void authorizePostObject(String authorizationHeader, String resource, String region) {
|
||||
authorizeAdditionalResource(authorizationHeader, "s3:PutObject", resource, null, null,
|
||||
new DeniedEvent("PostObject", region));
|
||||
new DeniedEvent("PostObject", region), null);
|
||||
}
|
||||
|
||||
/** A CloudTrail record to write when IAM refuses a request: its event name and its Region. */
|
||||
private record DeniedEvent(String name, String region) {
|
||||
}
|
||||
|
||||
/** As the public overload; a denial is also recorded in CloudTrail when {@code deniedEvent} is given. */
|
||||
/**
|
||||
* As the public overloads; a denial is also recorded in CloudTrail when {@code deniedEvent} is given, and
|
||||
* {@code requestConditions}, when given, joins the condition context.
|
||||
*/
|
||||
private void authorizeAdditionalResource(
|
||||
String authorizationHeader,
|
||||
String action,
|
||||
String resource,
|
||||
ResourcePolicyDecision resourcePolicyDecision,
|
||||
String resourceOwnerAccountId,
|
||||
DeniedEvent deniedEvent) {
|
||||
DeniedEvent deniedEvent,
|
||||
Map<String, List<String>> requestConditions) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return;
|
||||
}
|
||||
@@ -1532,6 +1550,12 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
conditionContext = new HashMap<>();
|
||||
callerArns.get().putPrincipalKeys(conditionContext);
|
||||
}
|
||||
if (requestConditions != null && !requestConditions.isEmpty()) {
|
||||
if (conditionContext == null) {
|
||||
conditionContext = new HashMap<>();
|
||||
}
|
||||
conditionContext.putAll(requestConditions);
|
||||
}
|
||||
|
||||
ResourcePolicyDecision effectiveDecision = resourcePolicyDecision;
|
||||
String effectiveOwnerAccountId = resourceOwnerAccountId;
|
||||
|
||||
+15
-8
@@ -7,23 +7,23 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Lambda Web Functions authType condition keys each action takes, and the request members that
|
||||
* carry the request key, generated from the AWS Service Reference and pinned botocore by
|
||||
* {@code tools/aws/regen_lambda_web_condition_keys.py}.
|
||||
* The service condition keys each action takes that Floci reads from the request or from the resource it acts on,
|
||||
* and the request members that carry them, generated from the AWS Service Reference and pinned botocore by
|
||||
* {@code tools/aws/regen_request_condition_keys.py}.
|
||||
*/
|
||||
final class LambdaWebConditionKeys {
|
||||
final class RequestConditionKeys {
|
||||
|
||||
private static final JsonNode TABLE = VendoredResources.read("aws/lambda-web-condition-keys.json",
|
||||
"Lambda Web condition keys", input -> {
|
||||
private static final JsonNode TABLE = VendoredResources.read("aws/request-condition-keys.json",
|
||||
"request condition keys", input -> {
|
||||
JsonNode document = new ObjectMapper().readTree(input);
|
||||
if (!document.path("actions").isObject() || document.path("actions").isEmpty()
|
||||
|| !document.path("members").isObject() || document.path("members").isEmpty()) {
|
||||
throw new IllegalStateException("Malformed Lambda Web condition key table");
|
||||
throw new IllegalStateException("Malformed request condition key table");
|
||||
}
|
||||
return document;
|
||||
});
|
||||
|
||||
private LambdaWebConditionKeys() {}
|
||||
private RequestConditionKeys() {}
|
||||
|
||||
static boolean takes(String action, String key) {
|
||||
for (JsonNode taken : TABLE.path("actions").path(action)) {
|
||||
@@ -40,4 +40,11 @@ final class LambdaWebConditionKeys {
|
||||
TABLE.path("members").path(action).path(key).forEach(member -> members.add(member.asText()));
|
||||
return members;
|
||||
}
|
||||
|
||||
/** The keys a request for {@code action} carries in its own members; empty when none. */
|
||||
static List<String> requestKeys(String action) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
TABLE.path("members").path(action).properties().forEach(key -> keys.add(key.getKey()));
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
+26
-9
@@ -228,7 +228,7 @@ public class RedshiftQueryHandler {
|
||||
}
|
||||
case "DeleteClusterSnapshot" -> {
|
||||
String snapshotIdentifier = params.getFirst("SnapshotIdentifier");
|
||||
Snapshot snapshot = service.deleteSnapshot(snapshotIdentifier);
|
||||
Snapshot snapshot = service.deleteSnapshot(snapshotIdentifier, params.getFirst("SnapshotClusterIdentifier"));
|
||||
String xml = new XmlBuilder()
|
||||
.start("DeleteClusterSnapshotResponse")
|
||||
.start("DeleteClusterSnapshotResult")
|
||||
@@ -246,8 +246,10 @@ public class RedshiftQueryHandler {
|
||||
String snapshotIdentifier = params.getFirst("SnapshotIdentifier");
|
||||
String snapshotArn = params.getFirst("SnapshotArn");
|
||||
String nodeType = params.getFirst("NodeType");
|
||||
Cluster cluster = service.restoreFromClusterSnapshot(
|
||||
clusterIdentifier, resolveSnapshotIdentifier(snapshotIdentifier, snapshotArn, authorizationHeader), nodeType);
|
||||
RestoredSnapshot snapshot = resolveSnapshot(snapshotIdentifier,
|
||||
params.getFirst("SnapshotClusterIdentifier"), snapshotArn, authorizationHeader);
|
||||
Cluster cluster = service.restoreFromClusterSnapshot(clusterIdentifier, snapshot.name(),
|
||||
snapshot.cluster(), nodeType);
|
||||
String xml = new XmlBuilder()
|
||||
.start("RestoreFromClusterSnapshotResponse")
|
||||
.start("RestoreFromClusterSnapshotResult")
|
||||
@@ -1033,7 +1035,8 @@ public class RedshiftQueryHandler {
|
||||
/**
|
||||
* Authorizes the actions GetClusterCredentials needs besides redshift:GetClusterCredentials, which
|
||||
* the API reference lists for its options: redshift:JoinGroup on each group in {@code DbGroups},
|
||||
* and redshift:CreateClusterUser on the database user when {@code AutoCreate} is true. It runs
|
||||
* and redshift:CreateClusterUser on the database user when {@code AutoCreate} is true, with that
|
||||
* user as {@code redshift:DbUser}, which the Service Reference lists for CreateClusterUser. It runs
|
||||
* before the cluster lookup, so a denied caller learns nothing about the cluster.
|
||||
*/
|
||||
private void authorizeCredentialOptions(String authorizationHeader, String clusterId, String dbUser,
|
||||
@@ -1048,7 +1051,8 @@ public class RedshiftQueryHandler {
|
||||
}
|
||||
if (autoCreate) {
|
||||
iamEnforcementFilter.authorizeAdditionalResource(authorizationHeader, "redshift:CreateClusterUser",
|
||||
regionResolver.buildArn("redshift", region, "dbuser:" + clusterId + "/" + dbUser));
|
||||
regionResolver.buildArn("redshift", region, "dbuser:" + clusterId + "/" + dbUser),
|
||||
Map.of("redshift:DbUser", List.of(dbUser)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1215,8 +1219,16 @@ public class RedshiftQueryHandler {
|
||||
};
|
||||
}
|
||||
|
||||
// RestoreFromClusterSnapshot accepts SnapshotIdentifier or SnapshotArn.
|
||||
private String resolveSnapshotIdentifier(String snapshotIdentifier, String snapshotArn, String authorizationHeader) {
|
||||
/** The snapshot a restore names: its name, and the cluster it was taken from, which the request is checked on. */
|
||||
private record RestoredSnapshot(String cluster, String name) {
|
||||
}
|
||||
|
||||
/**
|
||||
* RestoreFromClusterSnapshot accepts SnapshotIdentifier, with SnapshotClusterIdentifier, or SnapshotArn. An ARN
|
||||
* naming no cluster, or another cluster than SnapshotClusterIdentifier, names no snapshot.
|
||||
*/
|
||||
private RestoredSnapshot resolveSnapshot(String snapshotIdentifier, String snapshotClusterIdentifier,
|
||||
String snapshotArn, String authorizationHeader) {
|
||||
boolean hasIdentifier = snapshotIdentifier != null && !snapshotIdentifier.isBlank();
|
||||
boolean hasArn = snapshotArn != null && !snapshotArn.isBlank();
|
||||
if (hasIdentifier && hasArn) {
|
||||
@@ -1224,7 +1236,7 @@ public class RedshiftQueryHandler {
|
||||
"You must specify either SnapshotIdentifier or SnapshotArn, but not both.", 400);
|
||||
}
|
||||
if (hasIdentifier) {
|
||||
return snapshotIdentifier;
|
||||
return new RestoredSnapshot(snapshotClusterIdentifier, snapshotIdentifier);
|
||||
}
|
||||
if (!hasArn) {
|
||||
throw new AwsException("InvalidParameterValue", "SnapshotIdentifier or SnapshotArn is required", 400);
|
||||
@@ -1247,7 +1259,12 @@ public class RedshiftQueryHandler {
|
||||
|| !arn.region().equals(regionResolver.resolveRegionFromAuth(authorizationHeader))) {
|
||||
throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotArn + " not found", 404);
|
||||
}
|
||||
return resource.substring(slash + 1);
|
||||
String cluster = resource.substring("snapshot:".length(), slash);
|
||||
if (cluster.isBlank() || snapshotClusterIdentifier != null && !snapshotClusterIdentifier.isBlank()
|
||||
&& !snapshotClusterIdentifier.equals(cluster)) {
|
||||
throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotArn + " not found", 404);
|
||||
}
|
||||
return new RestoredSnapshot(cluster, resource.substring(slash + 1));
|
||||
}
|
||||
|
||||
private String buildSnapshotXml(Snapshot snapshot) {
|
||||
|
||||
@@ -1078,7 +1078,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
// ClusterSnapshotAlreadyExists. Once the container teardown starts the snapshot may be the only copy.
|
||||
private void discardUnusedFinalSnapshot(String finalSnapshotIdentifier, RuntimeException deletionFailure) {
|
||||
try {
|
||||
removeSnapshot(finalSnapshotIdentifier);
|
||||
removeSnapshot(finalSnapshotIdentifier, null);
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
deletionFailure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
@@ -1833,7 +1833,8 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
|
||||
public List<Snapshot> describeSnapshots(String snapshotIdentifier, String clusterIdentifier) {
|
||||
if (snapshotIdentifier != null && !snapshotIdentifier.isBlank()) {
|
||||
Optional<Snapshot> snapshot = findSnapshot(snapshotIdentifier);
|
||||
Optional<Snapshot> snapshot = findSnapshot(snapshotIdentifier)
|
||||
.filter(found -> takenFrom(found, clusterIdentifier));
|
||||
if (snapshot.isEmpty()) {
|
||||
throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotIdentifier + " not found", 404);
|
||||
}
|
||||
@@ -1874,14 +1875,31 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
return findSnapshot(snapshotIdentifier);
|
||||
}
|
||||
|
||||
public Snapshot deleteSnapshot(String snapshotIdentifier) {
|
||||
return withResourceLock("snapshot", snapshotIdentifier, () -> mutate(() -> removeSnapshot(snapshotIdentifier)));
|
||||
/**
|
||||
* Whether the snapshot was taken from the cluster a request names, or the request names none. A request naming the
|
||||
* cluster a snapshot was taken from is authorized on {@code snapshot:<cluster>/<name>}, so a snapshot of another
|
||||
* cluster is not acted on, as if it did not exist.
|
||||
*/
|
||||
private static boolean takenFrom(Snapshot snapshot, String clusterIdentifier) {
|
||||
return clusterIdentifier == null || clusterIdentifier.isBlank()
|
||||
|| clusterIdentifier.equals(snapshot.getClusterIdentifier());
|
||||
}
|
||||
|
||||
private Snapshot removeSnapshot(String snapshotIdentifier) {
|
||||
public Snapshot deleteSnapshot(String snapshotIdentifier) {
|
||||
return deleteSnapshot(snapshotIdentifier, null);
|
||||
}
|
||||
|
||||
/** Deletes the snapshot, which must have been taken from {@code snapshotClusterIdentifier} when one is given. */
|
||||
public Snapshot deleteSnapshot(String snapshotIdentifier, String snapshotClusterIdentifier) {
|
||||
return withResourceLock("snapshot", snapshotIdentifier,
|
||||
() -> mutate(() -> removeSnapshot(snapshotIdentifier, snapshotClusterIdentifier)));
|
||||
}
|
||||
|
||||
private Snapshot removeSnapshot(String snapshotIdentifier, String snapshotClusterIdentifier) {
|
||||
String copyKey = copyKey(regionResolver.getRegion(), snapshotIdentifier);
|
||||
Optional<Snapshot> copyOpt = snapshotCopies.get(copyKey);
|
||||
Optional<Snapshot> snapshotOpt = copyOpt.or(() -> snapshots.get(snapshotIdentifier));
|
||||
Optional<Snapshot> snapshotOpt = copyOpt.or(() -> snapshots.get(snapshotIdentifier))
|
||||
.filter(found -> takenFrom(found, snapshotClusterIdentifier));
|
||||
if (snapshotOpt.isEmpty()) {
|
||||
throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotIdentifier + " not found", 404);
|
||||
}
|
||||
@@ -1915,10 +1933,16 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
}
|
||||
|
||||
public Cluster restoreFromClusterSnapshot(String clusterIdentifier, String snapshotIdentifier, String nodeType) {
|
||||
return restoreFromClusterSnapshot(clusterIdentifier, snapshotIdentifier, null, nodeType);
|
||||
}
|
||||
|
||||
/** Restores the snapshot, which must have been taken from {@code snapshotClusterIdentifier} when one is given. */
|
||||
public Cluster restoreFromClusterSnapshot(String clusterIdentifier, String snapshotIdentifier,
|
||||
String snapshotClusterIdentifier, String nodeType) {
|
||||
return withResourceLock("cluster", clusterIdentifier, () -> {
|
||||
ensureNoManagedRotation(clusters.accountId(), clusterIdentifier);
|
||||
return withResourceLock("snapshot", snapshotIdentifier,
|
||||
() -> mutate(() -> restoreSnapshot(clusterIdentifier, snapshotIdentifier, nodeType)));
|
||||
return withResourceLock("snapshot", snapshotIdentifier, () -> mutate(
|
||||
() -> restoreSnapshot(clusterIdentifier, snapshotIdentifier, snapshotClusterIdentifier, nodeType)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1931,7 +1955,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
return withResourceLock("cluster", clusterIdentifier, () -> withResourceLock("snapshot", snapshotIdentifier,
|
||||
() -> {
|
||||
ensureNoManagedRotation(clusters.accountId(), clusterIdentifier);
|
||||
Cluster cluster = restoreSnapshot(clusterIdentifier, snapshotIdentifier, nodeType);
|
||||
Cluster cluster = restoreSnapshot(clusterIdentifier, snapshotIdentifier, null, nodeType);
|
||||
try {
|
||||
cluster.setClusterSubnetGroupName(clusterSubnetGroupName);
|
||||
if (manageMasterPassword) {
|
||||
@@ -1973,12 +1997,14 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
});
|
||||
}
|
||||
|
||||
private Cluster restoreSnapshot(String clusterIdentifier, String snapshotIdentifier, String nodeType) {
|
||||
private Cluster restoreSnapshot(String clusterIdentifier, String snapshotIdentifier,
|
||||
String snapshotClusterIdentifier, String nodeType) {
|
||||
if (clusters.get(clusterIdentifier).isPresent()) {
|
||||
throw new AwsException("ClusterAlreadyExists", "Cluster " + clusterIdentifier + " already exists", 400);
|
||||
}
|
||||
|
||||
Optional<Snapshot> snapshotOpt = findSnapshot(snapshotIdentifier);
|
||||
Optional<Snapshot> snapshotOpt = findSnapshot(snapshotIdentifier)
|
||||
.filter(found -> takenFrom(found, snapshotClusterIdentifier));
|
||||
if (snapshotOpt.isEmpty()) {
|
||||
throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotIdentifier + " not found", 404);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user