mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user