mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(iam): check iam:PassRole for Redshift, Redshift Serverless and AddRoleToInstanceProfile (#6250)
Under IAM enforcement a request that passes a role to a service needs iam:PassRole on that role, but no Redshift or Redshift Serverless operation was checked for it. A caller allowed to create a cluster or change its roles could attach any role to it, and Floci's COPY, UNLOAD and Spectrum statements then run as that role; statements run through the Data API on a Redshift Serverless workgroup can use its namespace's roles the same way. Now checked, with iam:PassedToService as the Service Authorization Reference gives it: - Redshift CreateCluster and RestoreFromClusterSnapshot pass the roles in IamRoles, and ModifyClusterIamRoles those in AddIamRoles, to redshift.amazonaws.com. The lists are read through the Redshift handler's own member pattern, so IamRoles.IamRoleArn.N, as the SDK sends it, IamRoles.member.N and IamRoles.N all count. RestoreFromClusterSnapshot is checked although Floci's restore attaches no role yet, as AWS checks it. - Redshift Serverless CreateNamespace and UpdateNamespace pass the roles in iamRoles and the one in defaultIamRoleArn, and CreateScheduledAction and UpdateScheduledAction pass roleArn, to redshift-serverless.amazonaws.com. The Redshift management guide: "To associate an IAM role with a cluster, a user must have iam:PassRole permission for that IAM role." A role named only as a cluster's default, DefaultIamRoleArn, is not checked on its own: the default is one of the roles the cluster holds, and Floci does not act on it. A namespace's defaultIamRoleArn is checked, since Floci keeps it whether or not iamRoles holds it. COPY, UNLOAD and Spectrum now take a role only under the ARN IAM holds it under, path included, as STS AssumeRole does. They looked the role up by its name alone, so a cluster given role/sandbox/AdminRole, which a PassRole grant on role/sandbox/* allows, ran its statements as the AdminRole IAM holds under another path. IAM's AddRoleToInstanceProfile now passes the role it adds to ec2.amazonaws.com. Every instance running with the profile gets that role's credentials from the instance metadata service, so adding a role to a profile already attached to a running instance handed it any role. The IAM API reference: "The caller of this operation must be granted the PassRole permission on the IAM role by a permissions policy." RoleName names the role without its path, so the check uses the ARN IAM holds the role under, or, for a role IAM does not hold, the ARN the name gives. docs/services/iam.md lists the new operations, and docs/services/redshift.md the path rule.
This commit is contained in:
1 parent
49ed87c815
commit
812c95499d
12 files changed
+332
-16
No files matched your search
+18
-2
@@ -1400,6 +1400,10 @@ role each passes:
|
||||
pass the roles of the instance profile they name, to `ec2.amazonaws.com`. `CreateFlowLogs` passes
|
||||
`DeliverLogsPermissionArn` to `vpc-flow-logs.amazonaws.com`. A profile that does not exist
|
||||
passes nothing, and EC2 refuses it as without enforcement.
|
||||
- **IAM:** `AddRoleToInstanceProfile` passes the role it adds to `ec2.amazonaws.com`, since every
|
||||
instance running with the profile then gets that role's credentials. `RoleName` names the role
|
||||
without its path, so the check uses the ARN IAM holds the role under, or, for a role IAM does not
|
||||
hold, the ARN the name gives.
|
||||
- **ECS:** `RegisterTaskDefinition` passes its `taskRoleArn` and `executionRoleArn` to
|
||||
`ecs-tasks.amazonaws.com`. `RunTask` and `StartTask` pass the task definition's task and execution
|
||||
roles and any role in `overrides` there too, and `CreateService`, `UpdateService` and
|
||||
@@ -1433,6 +1437,12 @@ role each passes:
|
||||
names) go to `autoscaling.amazonaws.com`.
|
||||
- **Application Auto Scaling:** `RegisterScalableTarget` passes `RoleARN` to
|
||||
`application-autoscaling.amazonaws.com` when the request names one.
|
||||
- **Redshift:** `CreateCluster` and `RestoreFromClusterSnapshot` pass the roles in `IamRoles`, and
|
||||
`ModifyClusterIamRoles` those in `AddIamRoles`, to `redshift.amazonaws.com`, in any list form the
|
||||
Redshift handler reads (`IamRoles.IamRoleArn.N`, `IamRoles.member.N` or `IamRoles.N`).
|
||||
**Redshift Serverless:** `CreateNamespace` and `UpdateNamespace` pass the roles in `iamRoles` and
|
||||
the one in `defaultIamRoleArn`, and `CreateScheduledAction` and `UpdateScheduledAction` pass
|
||||
`roleArn`, to `redshift-serverless.amazonaws.com`.
|
||||
|
||||
Launching an instance from a launch template does not check the template's role, as on AWS: the EC2
|
||||
user guide warns that a user who can create launch template versions "can potentially update a launch
|
||||
@@ -1444,8 +1454,14 @@ role having been checked when the configuration was created. A
|
||||
CloudFormation operation that names no role uses the stack's own without checking it, as the
|
||||
CloudFormation API reference says the service does "even if the users don't have permission to pass
|
||||
it". The CloudFormation user guide has the default StackSet administration role checked "Even when
|
||||
you don't specify the AdministrationRoleARN parameter". The role parameters of other services are
|
||||
not checked yet.
|
||||
you don't specify the AdministrationRoleARN parameter". A Redshift request is checked on the roles
|
||||
it associates, as the Redshift management guide puts it: "To associate an IAM role with a cluster,
|
||||
a user must have iam:PassRole permission for that IAM role." A role named only as a cluster's
|
||||
default, `DefaultIamRoleArn`, is not checked on its own, since the default is one of the roles the
|
||||
cluster holds. A namespace's `defaultIamRoleArn` is, since Floci keeps it whether or not `iamRoles`
|
||||
holds it. Redshift takes a role only under the ARN IAM holds it under, path included, so the check
|
||||
on the ARN as the request names it covers the role `COPY`, `UNLOAD` and Spectrum then use. The role
|
||||
parameters of other services are not checked yet.
|
||||
|
||||
A request that names its operation, in a Query `Action`, an `X-Amz-Target` header or a Smithy RPC v2
|
||||
path (`/service/<service>/operation/<operation>`), is authorized as the action AWS checks for that
|
||||
|
||||
@@ -446,7 +446,7 @@ order) through its own S3 service and streams the rows into the backing PostgreS
|
||||
- `IAM_ROLE '<role-arn>'` is supported. The role must be associated with the cluster (at
|
||||
`CreateCluster` or through `ModifyClusterIamRoles`; connections opened before a change keep the
|
||||
roles they started with), exist in
|
||||
the local IAM service, and trust Redshift to assume it. With
|
||||
the local IAM service under that ARN, path included, and trust Redshift to assume it. With
|
||||
`FLOCI_SERVICES_S3_ENFORCE_AUTH` off, S3 policy checks are skipped. With it on, the role's
|
||||
identity policy must allow the required S3 actions, and any bucket policy must not deny the
|
||||
request. `IAM_ROLE default` is not supported.
|
||||
@@ -546,7 +546,7 @@ the result to S3 as one or more objects under `<prefix>`.
|
||||
so they are not deleted); a `MANIFEST` request that fails this way can leave data objects without
|
||||
a manifest, and rerunning the same statement overwrites them.
|
||||
- `IAM_ROLE '<role-arn>'` is supported. The role must be associated with the cluster, exist in
|
||||
the local IAM service, and trust Redshift to assume it. With
|
||||
the local IAM service under that ARN, path included, and trust Redshift to assume it. With
|
||||
`FLOCI_SERVICES_S3_ENFORCE_AUTH` off, S3 policy checks are skipped. With it on, the role's
|
||||
identity policy must allow the required S3 actions, and any bucket policy must not deny the
|
||||
request. `IAM_ROLE default` is not supported.
|
||||
|
||||
@@ -975,6 +975,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
public String roleArn(String roleName) {
|
||||
return AwsArnUtils.Arn.global(requestPartition(), "iam", accountId, "role/" + roleName).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> heldRoleArn(String roleName) {
|
||||
return iamService.findRole(accountId, roleName).map(IamRole::getArn);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import io.github.hectorvent.floci.services.redshift.RedshiftQueryHandler;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
@@ -28,6 +29,11 @@ import java.util.regex.Pattern;
|
||||
* role to an instance even if they don't have the iam:PassRole permission", so the role is checked
|
||||
* where the template is created.
|
||||
*
|
||||
* <p>IAM's {@code AddRoleToInstanceProfile} passes the role it adds to {@code ec2.amazonaws.com}, since
|
||||
* every instance running with the profile then gets that role's credentials: "The caller of this
|
||||
* operation must be granted the PassRole permission on the IAM role by a permissions policy." The
|
||||
* request names the role without its path, so it is checked on the ARN IAM holds the role under.
|
||||
*
|
||||
* <p>An Auto Scaling group is different: the EC2 Auto Scaling user guide has the permission "needed on
|
||||
* the IAM identity (user or role) that creates or updates an Auto Scaling group using a launch template
|
||||
* that specifies an instance profile", and an instance refresh to a desired configuration needs it
|
||||
@@ -54,6 +60,16 @@ import java.util.regex.Pattern;
|
||||
* "Even when you don't specify the AdministrationRoleARN parameter, the IAM principal calling
|
||||
* CreateStackSet or UpdateStackSet must have iam:PassRole permission for the
|
||||
* AWSCloudFormationStackSetAdministrationRole role."
|
||||
*
|
||||
* <p>A Redshift request passes the roles it associates with a cluster, to {@code redshift.amazonaws.com}:
|
||||
* the Redshift management guide has "To associate an IAM role with a cluster, a user must have
|
||||
* iam:PassRole permission for that IAM role." A role a request names only as the cluster's default
|
||||
* passes nothing of its own, since the default is one of the roles the cluster holds ("To set an
|
||||
* associated IAM role as the default for the cluster"). A Redshift Serverless namespace's roles and a
|
||||
* scheduled action's role go to {@code redshift-serverless.amazonaws.com}, and so does a namespace's
|
||||
* default, which Floci keeps whether or not the namespace's roles hold it. Redshift takes a role only
|
||||
* under the ARN IAM holds it under, path included, so the ARN as the request names it is the role it
|
||||
* passes.
|
||||
*/
|
||||
final class PassedRoles {
|
||||
|
||||
@@ -77,6 +93,8 @@ final class PassedRoles {
|
||||
private static final String CLOUDFORMATION = ServicePrincipals.of("cloudformation");
|
||||
private static final String AUTOSCALING = ServicePrincipals.of("autoscaling");
|
||||
private static final String APPLICATION_AUTOSCALING = ServicePrincipals.of("application-autoscaling");
|
||||
private static final String REDSHIFT = ServicePrincipals.of("redshift");
|
||||
private static final String REDSHIFT_SERVERLESS = ServicePrincipals.of("redshift-serverless");
|
||||
|
||||
private static final String STACK_SET_ADMINISTRATION_ROLE = "AWSCloudFormationStackSetAdministrationRole";
|
||||
private static final String SERVICE_MANAGED = "SERVICE_MANAGED";
|
||||
@@ -118,6 +136,9 @@ final class PassedRoles {
|
||||
|
||||
/** The ARN of the role {@code roleName}, a name with any path, names in the request's account. */
|
||||
String roleArn(String roleName);
|
||||
|
||||
/** The ARN, path included, of the role IAM holds under {@code roleName}; empty when it holds none. */
|
||||
Optional<String> heldRoleArn(String roleName);
|
||||
}
|
||||
|
||||
private PassedRoles() {
|
||||
@@ -137,6 +158,7 @@ final class PassedRoles {
|
||||
case "ec2:CreateLaunchTemplate", "ec2:CreateLaunchTemplateVersion" ->
|
||||
fromInstanceProfile(ctx, lookups, "LaunchTemplateData.IamInstanceProfile");
|
||||
case "ec2:CreateFlowLogs" -> fromForm(ctx, VPC_FLOW_LOGS, "DeliverLogsPermissionArn");
|
||||
case "iam:AddRoleToInstanceProfile" -> instanceProfileRole(ctx, lookups);
|
||||
case "ecs:RegisterTaskDefinition" -> ecsTaskDefinition(jsonBody(ctx), lookups);
|
||||
case "ecs:RunTask", "ecs:StartTask" -> ecsTask(jsonBody(ctx), lookups);
|
||||
case "ecs:CreateService", "ecs:UpdateService" -> ecsService(jsonBody(ctx), lookups);
|
||||
@@ -162,10 +184,25 @@ final class PassedRoles {
|
||||
case "autoscaling:StartInstanceRefresh" -> instanceRefresh(ctx, lookups);
|
||||
case "autoscaling:PutLifecycleHook" -> fromForm(ctx, AUTOSCALING, "RoleARN");
|
||||
case "application-autoscaling:RegisterScalableTarget" -> fromJson(ctx, APPLICATION_AUTOSCALING, "RoleARN");
|
||||
case "redshift:CreateCluster", "redshift:RestoreFromClusterSnapshot" -> redshiftRoles(ctx, "IamRoles");
|
||||
case "redshift:ModifyClusterIamRoles" -> redshiftRoles(ctx, "AddIamRoles");
|
||||
case "redshift-serverless:CreateNamespace", "redshift-serverless:UpdateNamespace" -> namespaceRoles(ctx);
|
||||
case "redshift-serverless:CreateScheduledAction", "redshift-serverless:UpdateScheduledAction" ->
|
||||
fromJson(ctx, REDSHIFT_SERVERLESS, "roleArn");
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The role AddRoleToInstanceProfile adds, which {@code RoleName} names without its path: the ARN IAM
|
||||
* holds it under, or else the one the name gives, so a role IAM does not hold is checked as named.
|
||||
*/
|
||||
private static List<PassedRole> instanceProfileRole(ContainerRequestContext ctx, Lookups lookups) {
|
||||
String name = blankToNull(IamConditionContextResolver.formParameters(ctx).get("RoleName"));
|
||||
return name == null
|
||||
? List.of() : passed(List.of(lookups.heldRoleArn(name).orElseGet(() -> lookups.roleArn(name))), EC2);
|
||||
}
|
||||
|
||||
private static List<PassedRole> ecsTaskDefinition(JsonNode body, Lookups lookups) {
|
||||
List<String> roles = new ArrayList<>(values(body, "taskRoleArn"));
|
||||
roles.addAll(values(body, "executionRoleArn"));
|
||||
@@ -335,6 +372,30 @@ final class PassedRoles {
|
||||
}
|
||||
}
|
||||
|
||||
/** The roles a Redshift request lists under {@code list}, every numbered member the Redshift handler reads. */
|
||||
private static List<PassedRole> redshiftRoles(ContainerRequestContext ctx, String list) {
|
||||
String member = RedshiftQueryHandler.memberKeyRegex(list);
|
||||
List<String> roles = new ArrayList<>();
|
||||
IamConditionContextResolver.formParameters(ctx).forEach((key, value) -> {
|
||||
if (key.matches(member)) {
|
||||
roles.add(value);
|
||||
}
|
||||
});
|
||||
return passed(roles, REDSHIFT);
|
||||
}
|
||||
|
||||
/** A Redshift Serverless namespace's roles, and its default when they do not hold it. */
|
||||
private static List<PassedRole> namespaceRoles(ContainerRequestContext ctx) {
|
||||
JsonNode body = jsonBody(ctx);
|
||||
List<String> roles = new ArrayList<>(values(body, "iamRoles", "[]"));
|
||||
for (String defaultRole : values(body, "defaultIamRoleArn")) {
|
||||
if (!roles.contains(defaultRole)) {
|
||||
roles.add(defaultRole);
|
||||
}
|
||||
}
|
||||
return passed(roles, REDSHIFT_SERVERLESS);
|
||||
}
|
||||
|
||||
private static List<PassedRole> fromJson(ContainerRequestContext ctx, String service, String... path) {
|
||||
return passed(values(jsonBody(ctx), path), service);
|
||||
}
|
||||
|
||||
+7
-3
@@ -1361,9 +1361,13 @@ public class RedshiftQueryHandler {
|
||||
.toList();
|
||||
}
|
||||
|
||||
// The AWS Query protocol accepts both the generic ".member.N" form and each shape's own
|
||||
// locationName (e.g. the real Redshift SDK sends "SubnetIds.SubnetIdentifier.N", not "SubnetIds.member.N").
|
||||
private static String memberKeyRegex(String baseName) {
|
||||
/**
|
||||
* The keys of the numbered members of the list {@code baseName}. The AWS Query protocol accepts both
|
||||
* the generic ".member.N" form and each shape's own locationName (e.g. the real Redshift SDK sends
|
||||
* "SubnetIds.SubnetIdentifier.N", not "SubnetIds.member.N"). The {@code iam:PassRole} check reads a
|
||||
* cluster's roles through it, so it reads the same keys.
|
||||
*/
|
||||
public static String memberKeyRegex(String baseName) {
|
||||
String quoted = Pattern.quote(baseName);
|
||||
return switch (baseName) {
|
||||
case "SubnetIds" -> quoted + "(\\.member|\\.SubnetIdentifier)?\\.\\d+";
|
||||
|
||||
+4
-1
@@ -86,8 +86,11 @@ public final class RedshiftRoleAccess {
|
||||
"IAM Role '" + iamRoleArn
|
||||
+ "' could not be assumed: role is not associated with the Redshift cluster", null);
|
||||
}
|
||||
// The path counts, as STS AssumeRole takes it: an ARN naming the role under another path names no role,
|
||||
// so the iam:PassRole check on the ARN the cluster was given covers the role assumed here.
|
||||
String roleName = parsed.resource().substring(parsed.resource().lastIndexOf('/') + 1);
|
||||
Optional<IamRole> role = iamService.findRole(parsed.accountId(), roleName);
|
||||
Optional<IamRole> role = iamService.findRole(parsed.accountId(), roleName)
|
||||
.filter(found -> IamService.roleArnMatches(iamRoleArn, found));
|
||||
if (role.isEmpty()) {
|
||||
throw new S3CopySimulator.S3TransferException(SQLSTATE_INSUFFICIENT_PRIVILEGE,
|
||||
"IAM Role '" + iamRoleArn + "' could not be assumed: role does not exist", null);
|
||||
|
||||
@@ -40,7 +40,8 @@ class PassedRolesTest {
|
||||
* names one profile by name, and {@code arn-template}, whose version 2 names the other by ARN. The
|
||||
* instance {@code i-arn} runs with that other profile, and the group {@code g} launches from
|
||||
* {@code lt-web} once a refresh names only a version, or from {@code arn-template} at the group's
|
||||
* version 2 once a refresh names that template alone.
|
||||
* version 2 once a refresh names that template alone. IAM holds the role {@code app-role} under the
|
||||
* path {@code /app/}.
|
||||
*/
|
||||
private static final PassedRoles.Lookups LOOKUPS = new PassedRoles.Lookups() {
|
||||
@Override
|
||||
@@ -95,6 +96,11 @@ class PassedRolesTest {
|
||||
public String roleArn(String roleName) {
|
||||
return ROLES + roleName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> heldRoleArn(String roleName) {
|
||||
return "app-role".equals(roleName) ? Optional.of(ROLES + "app/app-role") : Optional.empty();
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
@@ -328,6 +334,56 @@ class PassedRolesTest {
|
||||
json("{\"ServiceNamespace\":\"ecs\"}"), LOOKUPS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRoleAddedToAnInstanceProfileIsPassedToEc2AsTheArnIamHoldsItUnder() {
|
||||
assertEquals(List.of(new PassedRole(ROLES + "app/app-role", EC2)), PassedRoles.of(
|
||||
"iam:AddRoleToInstanceProfile", form("InstanceProfileName=p&RoleName=app-role"), LOOKUPS));
|
||||
// A role IAM does not hold is checked as the one its name gives.
|
||||
assertEquals(List.of(passed("gone", EC2)), PassedRoles.of("iam:AddRoleToInstanceProfile",
|
||||
form("InstanceProfileName=p&RoleName=gone"), LOOKUPS));
|
||||
assertEquals(List.of(), PassedRoles.of("iam:AddRoleToInstanceProfile", form("InstanceProfileName=p"), LOOKUPS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftClusterPassesTheRolesItAssociatesToRedshift() {
|
||||
String redshift = "redshift.amazonaws.com";
|
||||
// Every list form the Redshift handler reads; the default names a role the cluster holds and passes nothing.
|
||||
assertEquals(List.of(passed("copy", redshift), passed("unload", redshift), passed("spectrum", redshift)),
|
||||
PassedRoles.of("redshift:CreateCluster", form("ClusterIdentifier=c&IamRoles.IamRoleArn.1=" + ROLES
|
||||
+ "copy&IamRoles.member.2=" + ROLES + "unload&IamRoles.5=" + ROLES + "spectrum"
|
||||
+ "&DefaultIamRoleArn=" + ROLES + "copy&IamRolesExtra.1=" + ROLES + "other"), LOOKUPS));
|
||||
assertEquals(List.of(passed("copy", redshift)), PassedRoles.of("redshift:RestoreFromClusterSnapshot",
|
||||
form("ClusterIdentifier=c&SnapshotIdentifier=s&IamRoles.IamRoleArn.1=" + ROLES + "copy"), LOOKUPS));
|
||||
// Removing a role, or setting one the cluster holds as the default, passes nothing.
|
||||
assertEquals(List.of(passed("added", redshift)), PassedRoles.of("redshift:ModifyClusterIamRoles",
|
||||
form("ClusterIdentifier=c&AddIamRoles.IamRoleArn.1=" + ROLES + "added&RemoveIamRoles.IamRoleArn.1="
|
||||
+ ROLES + "removed&DefaultIamRoleArn=" + ROLES + "kept"), LOOKUPS));
|
||||
assertEquals(List.of(), PassedRoles.of("redshift:CreateCluster", form("ClusterIdentifier=c"), LOOKUPS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftServerlessNamespaceAndScheduledActionPassTheirRolesToRedshiftServerless() {
|
||||
String serverless = "redshift-serverless.amazonaws.com";
|
||||
for (String action : new String[] {"redshift-serverless:CreateNamespace",
|
||||
"redshift-serverless:UpdateNamespace"}) {
|
||||
assertEquals(List.of(passed("copy", serverless), passed("unload", serverless)), PassedRoles.of(action,
|
||||
json("{\"namespaceName\":\"n\",\"iamRoles\":[\"" + ROLES + "copy\",\"" + ROLES + "unload\"],"
|
||||
+ "\"defaultIamRoleArn\":\"" + ROLES + "copy\"}"), LOOKUPS), action);
|
||||
// Floci keeps a default the namespace's roles do not hold, so that one is passed too.
|
||||
assertEquals(List.of(passed("copy", serverless), passed("other", serverless)), PassedRoles.of(action,
|
||||
json("{\"namespaceName\":\"n\",\"iamRoles\":[\"" + ROLES + "copy\"],"
|
||||
+ "\"defaultIamRoleArn\":\"" + ROLES + "other\"}"), LOOKUPS), action);
|
||||
assertEquals(List.of(passed("other", serverless)), PassedRoles.of(action,
|
||||
json("{\"namespaceName\":\"n\",\"defaultIamRoleArn\":\"" + ROLES + "other\"}"), LOOKUPS), action);
|
||||
assertEquals(List.of(), PassedRoles.of(action, json("{\"namespaceName\":\"n\"}"), LOOKUPS), action);
|
||||
}
|
||||
for (String action : new String[] {"redshift-serverless:CreateScheduledAction",
|
||||
"redshift-serverless:UpdateScheduledAction"}) {
|
||||
assertEquals(List.of(passed("snapshots", serverless)), PassedRoles.of(action,
|
||||
json("{\"scheduledActionName\":\"a\",\"roleArn\":\"" + ROLES + "snapshots\"}"), LOOKUPS), action);
|
||||
}
|
||||
}
|
||||
|
||||
private static PassedRole passed(String roleName, String service) {
|
||||
return new PassedRole(ROLES + roleName, service);
|
||||
}
|
||||
|
||||
+142
-3
@@ -51,6 +51,8 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
private static final String SCHEDULER = "scheduler.amazonaws.com";
|
||||
private static final String PIPES = "pipes.amazonaws.com";
|
||||
private static final String AUTOSCALING = "autoscaling.amazonaws.com";
|
||||
private static final String REDSHIFT = "redshift.amazonaws.com";
|
||||
private static final String REDSHIFT_SERVERLESS = "redshift-serverless.amazonaws.com";
|
||||
private static final String SERVICE_LINKED_ROLES =
|
||||
"arn:aws:iam::" + ACCOUNT + ":role/aws-service-role/autoscaling.amazonaws.com/";
|
||||
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
|
||||
@@ -181,6 +183,27 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
.formParam("IamInstanceProfile.Name", otherProfile).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addingARoleToAnInstanceProfileNeedsPassRoleOnItForEc2() {
|
||||
String role = role("/app/", "profile-role");
|
||||
String roleName = role.substring(role.lastIndexOf('/') + 1);
|
||||
String profile = emptyInstanceProfile();
|
||||
User user = user("passrole-profile-role");
|
||||
String add = allow("iam:AddRoleToInstanceProfile", "*");
|
||||
|
||||
// A grant to pass the role to Lambda does not cover EC2.
|
||||
user.policy(add + "," + passRole(role, LAMBDA));
|
||||
denied(addRoleToInstanceProfile(user, profile, roleName));
|
||||
// The request names the role without its path; a grant on the ARN that name gives misses it.
|
||||
user.policy(add + "," + passRole("arn:aws:iam::" + ACCOUNT + ":role/" + roleName, "ec2.amazonaws.com"));
|
||||
denied(addRoleToInstanceProfile(user, profile, roleName));
|
||||
// A role IAM does not hold is checked as the one its name gives.
|
||||
user.policy(add + "," + passRole(role, "ec2.amazonaws.com"));
|
||||
denied(addRoleToInstanceProfile(user, profile, "no-such-role"));
|
||||
|
||||
addRoleToInstanceProfile(user, profile, roleName).statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSpotInstanceRequestNeedsPassRoleOnItsInstanceProfilesRole() {
|
||||
String role = role("ec2-spot");
|
||||
@@ -704,6 +727,100 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
+ "\",\"ScalableDimension\":\"dynamodb:table:ReadCapacityUnits\"}").post("/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftClusterNeedsPassRoleOnEachRoleItAssociatesForRedshift() {
|
||||
String role = role("redshift-copy");
|
||||
User user = user("passrole-redshift");
|
||||
String cluster = "passrole-cluster-" + suffix();
|
||||
String actions = allow("redshift:CreateCluster", "*") + "," + allow("redshift:RestoreFromClusterSnapshot", "*")
|
||||
+ "," + allow("redshift:ModifyClusterIamRoles", "*");
|
||||
|
||||
// A grant to pass the role to Redshift Serverless does not cover Redshift.
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT_SERVERLESS));
|
||||
denied(restore(user, cluster, role).then());
|
||||
denied(user.redshift("ModifyClusterIamRoles").formParam("ClusterIdentifier", cluster)
|
||||
.formParam("AddIamRoles.member.1", role).post("/").then());
|
||||
// Refused before Redshift starts the cluster's container.
|
||||
denied(user.redshift("CreateCluster").formParam("ClusterIdentifier", cluster)
|
||||
.formParam("NodeType", "dc2.large").formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "Passw0rd1").formParam("IamRoles.IamRoleArn.1", role)
|
||||
.post("/").then());
|
||||
|
||||
// With it, each reaches Redshift, which has no such snapshot or cluster.
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT));
|
||||
restore(user, cluster, role).then().statusCode(404).body(containsString("ClusterSnapshotNotFound"));
|
||||
user.redshift("ModifyClusterIamRoles").formParam("ClusterIdentifier", cluster)
|
||||
.formParam("AddIamRoles.member.1", role).post("/").then()
|
||||
.statusCode(404).body(containsString("ClusterNotFound"));
|
||||
|
||||
// Removing a role, or naming one only as the default, passes nothing.
|
||||
user.policy(actions);
|
||||
user.redshift("ModifyClusterIamRoles").formParam("ClusterIdentifier", cluster)
|
||||
.formParam("RemoveIamRoles.IamRoleArn.1", role).formParam("DefaultIamRoleArn", role).post("/").then()
|
||||
.statusCode(404).body(containsString("ClusterNotFound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedshiftServerlessNamespaceAndScheduledActionNeedPassRoleOnTheirRolesForRedshiftServerless() {
|
||||
String role = role("serverless-copy");
|
||||
User user = user("passrole-serverless");
|
||||
String namespace = "passrole-ns-" + suffix();
|
||||
String action = "passrole-action-" + suffix();
|
||||
String createNamespace = "{\"namespaceName\":\"%s\",\"adminUsername\":\"admin\",\"iamRoles\":[\"%s\"]}"
|
||||
.formatted(namespace, role);
|
||||
String updateNamespace = "{\"namespaceName\":\"%s\",\"iamRoles\":[\"%s\"],\"defaultIamRoleArn\":\"%s\"}"
|
||||
.formatted(namespace, role, role);
|
||||
String createAction = ("{\"scheduledActionName\":\"%s\",\"namespaceName\":\"%s\",\"roleArn\":\"%s\","
|
||||
+ "\"schedule\":{\"cron\":\"cron(0 3 * * ? *)\"},\"targetAction\":{\"createSnapshot\":"
|
||||
+ "{\"namespaceName\":\"%s\",\"snapshotNamePrefix\":\"nightly\"}}}")
|
||||
.formatted(action, namespace, role, namespace);
|
||||
String updateAction = "{\"scheduledActionName\":\"%s\",\"roleArn\":\"%s\"}".formatted(action, role);
|
||||
String actions = allow("redshift-serverless:CreateNamespace", "*") + ","
|
||||
+ allow("redshift-serverless:UpdateNamespace", "*") + ","
|
||||
+ allow("redshift-serverless:CreateScheduledAction", "*") + ","
|
||||
+ allow("redshift-serverless:UpdateScheduledAction", "*");
|
||||
|
||||
// A grant to pass the role to Redshift does not cover Redshift Serverless.
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT));
|
||||
deniedJson(serverless(user, "CreateNamespace").body(createNamespace).post("/").then());
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT_SERVERLESS));
|
||||
serverless(user, "CreateNamespace").body(createNamespace).post("/").then().statusCode(200);
|
||||
serverless(user, "CreateScheduledAction").body(createAction).post("/").then().statusCode(200);
|
||||
cleanup.register(() -> {
|
||||
admin("redshift-serverless").header("X-Amz-Target", "RedshiftServerless.DeleteScheduledAction")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body("{\"scheduledActionName\":\"" + action + "\"}").post("/");
|
||||
admin("redshift-serverless").header("X-Amz-Target", "RedshiftServerless.DeleteNamespace")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body("{\"namespaceName\":\"" + namespace + "\"}").post("/");
|
||||
});
|
||||
|
||||
user.policy(actions);
|
||||
deniedJson(serverless(user, "UpdateNamespace").body(updateNamespace).post("/").then());
|
||||
deniedJson(serverless(user, "CreateScheduledAction").body(createAction).post("/").then());
|
||||
deniedJson(serverless(user, "UpdateScheduledAction").body(updateAction).post("/").then());
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT_SERVERLESS));
|
||||
serverless(user, "UpdateNamespace").body(updateNamespace).post("/").then().statusCode(200);
|
||||
serverless(user, "UpdateScheduledAction").body(updateAction).post("/").then().statusCode(200);
|
||||
|
||||
// Floci keeps a default the namespace's roles do not hold, so naming one needs PassRole on it too.
|
||||
String other = role("serverless-default");
|
||||
String defaultOnly = "{\"namespaceName\":\"%s\",\"defaultIamRoleArn\":\"%s\"}".formatted(namespace, other);
|
||||
deniedJson(serverless(user, "UpdateNamespace").body(defaultOnly).post("/").then());
|
||||
user.policy(actions + "," + passRole(role, REDSHIFT_SERVERLESS) + "," + passRole(other, REDSHIFT_SERVERLESS));
|
||||
serverless(user, "UpdateNamespace").body(defaultOnly).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static Response restore(User user, String cluster, String role) {
|
||||
return user.redshift("RestoreFromClusterSnapshot").formParam("ClusterIdentifier", cluster)
|
||||
.formParam("SnapshotIdentifier", "passrole-no-snapshot").formParam("IamRoles.IamRoleArn.1", role)
|
||||
.post("/");
|
||||
}
|
||||
|
||||
private static RequestSpecification serverless(User user, String operation) {
|
||||
return user.json("redshift-serverless", "RedshiftServerless." + operation);
|
||||
}
|
||||
|
||||
private static Response launchConfiguration(User user, String name, String profile) {
|
||||
return user.autoscaling("CreateLaunchConfiguration").formParam("LaunchConfigurationName", name)
|
||||
.formParam("ImageId", IMAGE).formParam("InstanceType", "t3.micro")
|
||||
@@ -884,21 +1001,35 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
}
|
||||
|
||||
private String role(String name) {
|
||||
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/", name + "-" + suffix(),
|
||||
return role("/", name);
|
||||
}
|
||||
|
||||
private String role(String path, String name) {
|
||||
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, path, name + "-" + suffix(),
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:*\","
|
||||
+ "\"Resource\":\"*\"}]}");
|
||||
}
|
||||
|
||||
private String instanceProfile(String roleName) {
|
||||
String name = emptyInstanceProfile();
|
||||
admin("iam").formParam("Action", "AddRoleToInstanceProfile").formParam("InstanceProfileName", name)
|
||||
.formParam("RoleName", roleName).post("/").then().statusCode(200);
|
||||
return name;
|
||||
}
|
||||
|
||||
private String emptyInstanceProfile() {
|
||||
String name = "passrole-profile-" + suffix();
|
||||
cleanup.register(() -> EnforcementFixtures.removeInstanceProfile(baseUri.getPort(), auth(ADMIN, "iam"), name));
|
||||
admin("iam").formParam("Action", "CreateInstanceProfile").formParam("InstanceProfileName", name)
|
||||
.post("/").then().statusCode(200);
|
||||
admin("iam").formParam("Action", "AddRoleToInstanceProfile").formParam("InstanceProfileName", name)
|
||||
.formParam("RoleName", roleName).post("/").then().statusCode(200);
|
||||
return name;
|
||||
}
|
||||
|
||||
private static ValidatableResponse addRoleToInstanceProfile(User user, String profile, String roleName) {
|
||||
return user.iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profile)
|
||||
.formParam("RoleName", roleName).post("/").then();
|
||||
}
|
||||
|
||||
private void terminateLater(String instanceId) {
|
||||
cleanup.register(() -> admin("ec2").formParam("Action", "TerminateInstances")
|
||||
.formParam("InstanceId.1", instanceId).post("/"));
|
||||
@@ -936,10 +1067,18 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
return given().header("Authorization", auth(accessKeyId, "ec2")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", auth(accessKeyId, "iam")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification autoscaling(String action) {
|
||||
return given().header("Authorization", auth(accessKeyId, "autoscaling")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification redshift(String action) {
|
||||
return given().header("Authorization", auth(accessKeyId, "redshift")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification ecs(String action) {
|
||||
return json("ecs", "AmazonEC2ContainerServiceV20141113." + action);
|
||||
}
|
||||
|
||||
+21
@@ -18,6 +18,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
@@ -65,8 +66,14 @@ class RedshiftRoleAccessTest {
|
||||
"Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
|
||||
private static IamService iamServiceWithRole(String trustPolicy) {
|
||||
return iamServiceWithRole(trustPolicy, ROLE_ARN);
|
||||
}
|
||||
|
||||
/** IAM holding {@code spectrum-role} under {@code heldArn}. */
|
||||
private static IamService iamServiceWithRole(String trustPolicy, String heldArn) {
|
||||
IamService iamService = mock(IamService.class);
|
||||
IamRole role = mock(IamRole.class);
|
||||
when(role.getArn()).thenReturn(heldArn);
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn(trustPolicy);
|
||||
when(iamService.findRole(ACCOUNT, "spectrum-role")).thenReturn(Optional.of(role));
|
||||
return iamService;
|
||||
@@ -98,6 +105,20 @@ class RedshiftRoleAccessTest {
|
||||
assertRefused("does not exist", ROLE_ARN, missing, ACCOUNT, List.of(ROLE_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveRoleSessionTakesTheRoleOnlyUnderTheArnIamHoldsItUnder() {
|
||||
String held = "arn:aws:iam::111111111111:role/app/spectrum-role";
|
||||
String otherPath = "arn:aws:iam::111111111111:role/sandbox/spectrum-role";
|
||||
IamService iamService = iamServiceWithRole(TRUST_POLICY, held);
|
||||
|
||||
assertRefused("does not exist", otherPath, iamService, ACCOUNT, List.of(otherPath));
|
||||
assertRefused("does not exist", ROLE_ARN, iamService, ACCOUNT, List.of(ROLE_ARN));
|
||||
RedshiftRoleAccess.RoleSession session =
|
||||
RedshiftRoleAccess.resolveRoleSession(held, iamService, ACCOUNT, List.of(held));
|
||||
verify(iamService).registerSessionForAccount(eq(ACCOUNT), eq(session.accessKeyId()), anyString(),
|
||||
eq(session.sessionToken()), eq(held), any(Instant.class), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveRoleSessionRefusesARoleWhoseTrustPolicyExcludesRedshift() {
|
||||
assertRefused("trust policy", ROLE_ARN, iamServiceWithRole(EC2_TRUST_POLICY), ACCOUNT, List.of(ROLE_ARN));
|
||||
|
||||
+13
-4
@@ -192,6 +192,13 @@ class S3CopySimulatorTest {
|
||||
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/CopyRole";
|
||||
|
||||
/** A role IAM holds under {@code arn}, which is the only ARN Redshift takes it under. */
|
||||
private static IamRole heldRole(String arn) {
|
||||
IamRole role = mock(IamRole.class);
|
||||
when(role.getArn()).thenReturn(arn);
|
||||
return role;
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareCopyWithMalformedIamRoleArnFailsBeforeTouchingS3() {
|
||||
CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom(
|
||||
@@ -258,7 +265,7 @@ class S3CopySimulatorTest {
|
||||
CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom(
|
||||
"t", List.of(), "b", "k", "|", 0, false, false, null, ROLE_ARN);
|
||||
IamService iamService = mock(IamService.class);
|
||||
IamRole role = mock(IamRole.class);
|
||||
IamRole role = heldRole(ROLE_ARN);
|
||||
when(iamService.findRole("000000000000", "CopyRole"))
|
||||
.thenReturn(Optional.of(role));
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn(""
|
||||
@@ -281,8 +288,9 @@ class S3CopySimulatorTest {
|
||||
CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom(
|
||||
"t", List.of(), "b", "k", "|", 0, false, false, null, ROLE_ARN);
|
||||
IamService iamService = mock(IamService.class);
|
||||
IamRole role = heldRole(ROLE_ARN);
|
||||
when(iamService.findRole(any(), any()))
|
||||
.thenReturn(Optional.of(mock(IamRole.class)));
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
S3CopySimulator.CopyInput input = S3CopySimulator.prepareCopy(spec, s3, iamService);
|
||||
|
||||
@@ -303,8 +311,9 @@ class S3CopySimulatorTest {
|
||||
CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom(
|
||||
"t", List.of(), "b", "k", "|", 0, false, false, null, ROLE_ARN);
|
||||
IamService iamService = mock(IamService.class);
|
||||
IamRole role = heldRole(ROLE_ARN);
|
||||
when(iamService.findRole(any(), any()))
|
||||
.thenReturn(Optional.of(mock(IamRole.class)));
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
S3CopySimulator.CopyInput input = S3CopySimulator.prepareCopy(spec, s3, iamService);
|
||||
S3CopySimulator.streamCopyInput(input, new ByteArrayOutputStream());
|
||||
@@ -1789,7 +1798,7 @@ class S3CopySimulatorTest {
|
||||
void runCopyFrom_withJsonAuto_releasesIamSessionEvenWhenCatalogDiscoveryFails() throws Exception {
|
||||
when(s3.objectExists("wh", "data.json")).thenReturn(true);
|
||||
IamService iamService = mock(IamService.class);
|
||||
IamRole role = mock(IamRole.class);
|
||||
IamRole role = heldRole("arn:aws:iam::000000000000:role/Role");
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn(
|
||||
"{\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"redshift.amazonaws.com\"}}]}");
|
||||
when(iamService.findRole("000000000000", "Role")).thenReturn(Optional.of(role));
|
||||
|
||||
+1
@@ -100,6 +100,7 @@ class ExternalTableMaterializerTest {
|
||||
s3 = mock(S3Service.class);
|
||||
iam = mock(IamService.class);
|
||||
IamRole role = mock(IamRole.class);
|
||||
when(role.getArn()).thenReturn("arn:aws:iam::000000000000:role/R");
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn(TRUST_POLICY);
|
||||
when(iam.findRole(ACCOUNT, "R")).thenReturn(Optional.of(role));
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, Answers.RETURNS_DEEP_STUBS);
|
||||
|
||||
+1
@@ -281,6 +281,7 @@ class SpectrumS3ReaderTest {
|
||||
private static IamService iamWithRole() {
|
||||
IamService iam = mock(IamService.class);
|
||||
IamRole role = mock(IamRole.class);
|
||||
when(role.getArn()).thenReturn("arn:aws:iam::000000000000:role/Reader");
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"redshift.amazonaws.com"},"Action":"sts:AssumeRole"}]}""");
|
||||
|
||||
Reference in new issue
Block a user