mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(eventbridge): authorize the role of Redshift Data API targets (#7095)
This commit is contained in:
1 parent
e3cdb72171
commit
4d99aa9639
14 files changed
+1115
-85
No files matched your search
+75
-9
@@ -27,6 +27,7 @@ import io.github.hectorvent.floci.services.eventbridge.model.RedshiftDataParamet
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
@@ -49,11 +50,13 @@ import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -83,6 +86,7 @@ public class EventBridgeInvoker {
|
||||
private final EcsJsonHandler ecsJsonHandler;
|
||||
private final StepFunctionsService stepFunctionsService;
|
||||
private final RedshiftDataService redshiftDataService;
|
||||
private final EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer;
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -101,13 +105,14 @@ public class EventBridgeInvoker {
|
||||
EcsJsonHandler ecsJsonHandler,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataService redshiftDataService,
|
||||
EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
ServiceRegistry serviceRegistry,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
EmulatorConfig config) {
|
||||
this(lambdaService, sqsService, snsService, batchService, firehoseService, eventBridgeService,
|
||||
ecsService, ecsJsonHandler, stepFunctionsService, redshiftDataService, serviceRegistry,
|
||||
regionResolver, objectMapper, config,
|
||||
ecsService, ecsJsonHandler, stepFunctionsService, redshiftDataService, redshiftTargetAuthorizer,
|
||||
serviceRegistry, regionResolver, objectMapper, config,
|
||||
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
|
||||
}
|
||||
|
||||
@@ -121,6 +126,7 @@ public class EventBridgeInvoker {
|
||||
EcsJsonHandler ecsJsonHandler,
|
||||
StepFunctionsService stepFunctionsService,
|
||||
RedshiftDataService redshiftDataService,
|
||||
EventBridgeRedshiftTargetAuthorizer redshiftTargetAuthorizer,
|
||||
ServiceRegistry serviceRegistry,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
@@ -136,6 +142,7 @@ public class EventBridgeInvoker {
|
||||
this.ecsJsonHandler = ecsJsonHandler;
|
||||
this.stepFunctionsService = stepFunctionsService;
|
||||
this.redshiftDataService = redshiftDataService;
|
||||
this.redshiftTargetAuthorizer = redshiftTargetAuthorizer;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
@@ -150,7 +157,8 @@ public class EventBridgeInvoker {
|
||||
EmulatorConfig config) {
|
||||
this(lambdaService, sqsService, snsService,
|
||||
null /* batch */, null /* firehose */, null /* eventBridge */, null /* ecs */,
|
||||
null /* ecsJsonHandler */, null /* stepFunctions */, null /* redshiftData */, null /* registry */,
|
||||
null /* ecsJsonHandler */, null /* stepFunctions */, null /* redshiftData */,
|
||||
null /* redshiftTargetAuthorizer */, null /* registry */,
|
||||
null /* regionResolver */,
|
||||
objectMapper, config);
|
||||
}
|
||||
@@ -164,12 +172,18 @@ public class EventBridgeInvoker {
|
||||
EmulatorConfig config) {
|
||||
this(lambdaService, sqsService, snsService,
|
||||
null /* batch */, null /* firehose */, null /* eventBridge */, null /* ecs */,
|
||||
null /* ecsJsonHandler */, null /* stepFunctions */, redshiftDataService, serviceRegistry,
|
||||
null /* ecsJsonHandler */, null /* stepFunctions */, redshiftDataService,
|
||||
null /* redshiftTargetAuthorizer */, serviceRegistry,
|
||||
null /* regionResolver */,
|
||||
objectMapper, config);
|
||||
}
|
||||
|
||||
public void invokeTarget(Target target, String eventJson, String region) {
|
||||
invokeTarget(target, eventJson, region, null);
|
||||
}
|
||||
|
||||
/** {@code ruleArn} is the rule that matched, which a role's trust policy may name in {@code aws:SourceArn}. */
|
||||
public void invokeTarget(Target target, String eventJson, String region, String ruleArn) {
|
||||
String arn = target.getArn();
|
||||
String payload;
|
||||
if (target.getInput() != null) {
|
||||
@@ -241,7 +255,7 @@ public class EventBridgeInvoker {
|
||||
LOG.debugv("EventBridge delivered to Firehose: {0}", arn);
|
||||
} else if (AwsArnUtils.isArnFor(arn, "redshift")
|
||||
|| AwsArnUtils.isArnFor(arn, "redshift-serverless")) {
|
||||
deliverToRedshiftData(target, arn, eventJson);
|
||||
deliverToRedshiftData(target, arn, eventJson, ruleArn);
|
||||
} else if (isStateMachineArn(arn)) {
|
||||
String targetRegion = extractRegionFromArn(arn, region);
|
||||
String targetAccount = AwsArnUtils.parse(arn).accountId();
|
||||
@@ -335,7 +349,7 @@ public class EventBridgeInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
private void deliverToRedshiftData(Target target, String targetArn, String eventJson) {
|
||||
private void deliverToRedshiftData(Target target, String targetArn, String eventJson, String ruleArn) {
|
||||
if (serviceRegistry != null && !serviceRegistry.isServiceEnabled("redshift-data")) {
|
||||
throw new AwsException("ServiceNotAvailableException", "Service redshift-data is not enabled.", 400);
|
||||
}
|
||||
@@ -361,6 +375,13 @@ public class EventBridgeInvoker {
|
||||
putIfPresent(request, "Database", resolveRedshiftParameter(parameters.getDatabase(), eventJson));
|
||||
putIfPresent(request, "DbUser", resolveRedshiftParameter(parameters.getDbUser(), eventJson));
|
||||
putIfPresent(request, "SecretArn", resolveRedshiftParameter(parameters.getSecretManagerArn(), eventJson));
|
||||
|
||||
String ruleAccount = regionResolver != null && regionResolver.getAccountId() != null
|
||||
? regionResolver.getAccountId()
|
||||
: arn.accountId();
|
||||
// The SQL does not change what the role needs, so the role is checked before a bad path in it is reported.
|
||||
authorizeRedshiftTarget(target, targetArn, request, parameters.getSqls() != null, ruleAccount, targetRegion,
|
||||
ruleArn);
|
||||
putIfPresent(request, "Sql", resolveRedshiftParameter(parameters.getSql(), eventJson));
|
||||
putIfPresent(request, "StatementName", resolveRedshiftParameter(parameters.getStatementName(), eventJson));
|
||||
if (parameters.getSqls() != null) {
|
||||
@@ -373,9 +394,6 @@ public class EventBridgeInvoker {
|
||||
request.put("WithEvent", parameters.getWithEvent());
|
||||
}
|
||||
|
||||
String ruleAccount = regionResolver != null && regionResolver.getAccountId() != null
|
||||
? regionResolver.getAccountId()
|
||||
: arn.accountId();
|
||||
if (parameters.getSqls() != null) {
|
||||
RequestScopes.callAs(ruleAccount, targetRegion,
|
||||
() -> redshiftDataService.batchExecuteStatement(request, targetRegion, target.getRoleArn()));
|
||||
@@ -386,6 +404,54 @@ public class EventBridgeInvoker {
|
||||
LOG.debugv("EventBridge submitted SQL to Redshift Data API: {0}", targetArn);
|
||||
}
|
||||
|
||||
/**
|
||||
* EventBridge assumes the target's role to call the Data API, so the role has to be allowed the Data
|
||||
* API action and whatever authenticates it: a secret, temporary credentials for the database user,
|
||||
* or the Serverless workgroup's credentials. A refusal is an {@code AccessDeniedException}, which the
|
||||
* dispatcher classifies as NO_PERMISSIONS and sends to the dead-letter queue without retrying.
|
||||
*/
|
||||
private void authorizeRedshiftTarget(Target target, String targetArn, ObjectNode request, boolean batch,
|
||||
String ruleAccount, String targetRegion, String ruleArn) {
|
||||
if (redshiftTargetAuthorizer == null) {
|
||||
return;
|
||||
}
|
||||
// A cluster runs by name in the rule's account, whatever account the target ARN names, so that is the
|
||||
// cluster the role has to be allowed on.
|
||||
String statementTarget = request.has("ClusterIdentifier")
|
||||
? AwsArnUtils.Arn.of("redshift", targetRegion, ruleAccount,
|
||||
"cluster:" + request.path("ClusterIdentifier").asText()).toString()
|
||||
: targetArn;
|
||||
List<Permission> permissions = new ArrayList<>();
|
||||
permissions.add(new Permission(
|
||||
batch ? "redshift-data:BatchExecuteStatement" : "redshift-data:ExecuteStatement", statementTarget));
|
||||
// The Data API ignores a blank value, so it picks the credentials the same way.
|
||||
String secret = request.path("SecretArn").asText("");
|
||||
if (!secret.isBlank()) {
|
||||
// A secret named without its ARN has no resource to authorize against, and the Data API
|
||||
// rejects it as an invalid SecretArn before reading it, so no permission is checked for it.
|
||||
if (AwsArnUtils.isArn(secret)) {
|
||||
permissions.add(new Permission(
|
||||
"secretsmanager:GetSecretValue", secret));
|
||||
}
|
||||
} else if (request.has("WorkgroupName")) {
|
||||
permissions.add(new Permission(
|
||||
"redshift-serverless:GetCredentials", targetArn));
|
||||
} else if (!request.path("DbUser").asText("").isBlank()) {
|
||||
String cluster = request.path("ClusterIdentifier").asText();
|
||||
permissions.add(new Permission("redshift:GetClusterCredentials",
|
||||
AwsArnUtils.Arn.of("redshift", targetRegion, ruleAccount,
|
||||
"dbuser:" + cluster + "/" + request.path("DbUser").asText()).toString()));
|
||||
permissions.add(new Permission("redshift:GetClusterCredentials",
|
||||
AwsArnUtils.Arn.of("redshift", targetRegion, ruleAccount,
|
||||
"dbname:" + cluster + "/" + request.path("Database").asText()).toString()));
|
||||
}
|
||||
Optional<String> refusal = redshiftTargetAuthorizer.refusal(target.getRoleArn(), ruleAccount, ruleArn,
|
||||
permissions);
|
||||
if (refusal.isPresent()) {
|
||||
throw new AwsException("AccessDeniedException", refusal.get(), 403);
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveRedshiftParameter(String value, String eventJson) {
|
||||
if (value == null || !DYNAMIC_PARAMETER_PATH.matcher(value).matches()) {
|
||||
return value;
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Decides whether the role of an EventBridge Redshift Data API target may submit its SQL. EventBridge
|
||||
* assumes the target's role, so the role has to trust {@code events.amazonaws.com}, and its identity
|
||||
* policies, permissions boundary and SCPs have to allow every {@link Permission} the submission needs:
|
||||
* the Data API action itself and the action that authenticates it. Only active with IAM enforcement on,
|
||||
* as for every other API call. A role of another account is refused, because the rule's account does
|
||||
* not own it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EventBridgeRedshiftTargetAuthorizer {
|
||||
|
||||
private static final String EVENTS_PRINCIPAL = ServicePrincipals.of("events");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final ServiceRoleAuthorizer authorizer;
|
||||
|
||||
@Inject
|
||||
public EventBridgeRedshiftTargetAuthorizer(EmulatorConfig config, ServiceRoleAuthorizer authorizer) {
|
||||
this.config = config;
|
||||
this.authorizer = authorizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns why {@code roleArn} may not run the target, or nothing when it may.
|
||||
*
|
||||
* @param accountId the account that owns the rule
|
||||
* @param ruleArn the rule that matched, which a trust policy may require in {@code aws:SourceArn}; without
|
||||
* it such a condition does not match
|
||||
* @param permissions what the submission needs, each of which has to be allowed
|
||||
*/
|
||||
public Optional<String> refusal(String roleArn, String accountId, String ruleArn,
|
||||
List<Permission> permissions) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (roleArn == null || roleArn.isBlank()) {
|
||||
return Optional.of("A Redshift target requires an IAM role, and this one names none.");
|
||||
}
|
||||
return authorizer.refusal(roleArn, accountId, EVENTS_PRINCIPAL, ruleArn, permissions);
|
||||
}
|
||||
}
|
||||
@@ -157,7 +157,7 @@ public class TargetDispatcher implements Resettable {
|
||||
|
||||
private void attempt(Delivery delivery, Target target, Instant now) {
|
||||
try {
|
||||
invoker.invokeTarget(target, delivery.eventJson(), delivery.region());
|
||||
invoker.invokeTarget(target, delivery.eventJson(), delivery.region(), delivery.ruleArn());
|
||||
} catch (Exception e) {
|
||||
if (delivery.generation() != generation.get()) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Decides whether a customer's service role may be assumed by an AWS service to perform actions on
|
||||
* their behalf, such as EventBridge submitting statements to Redshift Data API or Redshift Serverless
|
||||
* taking scheduled snapshots. The role has to belong to the caller's account, trust the service
|
||||
* principal (with optional conditions such as {@code aws:SourceArn} and {@code aws:SourceAccount}),
|
||||
* and its identity policies, permissions boundary and SCPs must allow every required action.
|
||||
*
|
||||
* <p>Only active with IAM enforcement on. With it off, every role is allowed.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class ServiceRoleAuthorizer {
|
||||
|
||||
/** One IAM action the role needs, on the resource it acts on. */
|
||||
public record Permission(String action, String resourceArn) {
|
||||
}
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final IamService iamService;
|
||||
private final IamPolicyEvaluator evaluator;
|
||||
private final AssumeRolePolicyEvaluator trustEvaluator;
|
||||
private final Instance<ScpProvider> scpProvider;
|
||||
|
||||
@Inject
|
||||
public ServiceRoleAuthorizer(EmulatorConfig config, IamService iamService,
|
||||
IamPolicyEvaluator evaluator,
|
||||
AssumeRolePolicyEvaluator trustEvaluator,
|
||||
Instance<ScpProvider> scpProvider) {
|
||||
this.config = config;
|
||||
this.iamService = iamService;
|
||||
this.evaluator = evaluator;
|
||||
this.trustEvaluator = trustEvaluator;
|
||||
this.scpProvider = scpProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns why {@code roleArn} may not be used by {@code servicePrincipal}, or empty when it may.
|
||||
*
|
||||
* @param roleArn the IAM role the service assumes
|
||||
* @param accountId the account that owns the resource and the role
|
||||
* @param servicePrincipal the AWS service principal that assumes the role (e.g. {@code events.amazonaws.com})
|
||||
* @param sourceArn optional ARN of the trigger or resource, checked against {@code aws:SourceArn} trust conditions
|
||||
* @param permissions the actions and resource ARNs the service needs to execute
|
||||
*/
|
||||
public Optional<String> refusal(String roleArn, String accountId, String servicePrincipal,
|
||||
String sourceArn, List<Permission> permissions) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(roleArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return Optional.of("The role " + roleArn + " is not a valid ARN.");
|
||||
}
|
||||
if (!"iam".equals(parsed.service()) || !parsed.resource().startsWith("role/")) {
|
||||
return Optional.of("The role " + roleArn + " is not an IAM role.");
|
||||
}
|
||||
if (!accountId.equals(parsed.accountId())) {
|
||||
return Optional.of("The role " + roleArn + " belongs to another account.");
|
||||
}
|
||||
String roleName = parsed.resource().substring(parsed.resource().lastIndexOf('/') + 1);
|
||||
Optional<IamRole> role = iamService.findRole(accountId, roleName)
|
||||
.filter(found -> IamService.roleArnMatches(roleArn, found));
|
||||
if (role.isEmpty()) {
|
||||
return Optional.of("The role " + roleArn + " does not exist.");
|
||||
}
|
||||
if (!trustEvaluator.allowsService(role.get().getAssumeRolePolicyDocument(), servicePrincipal,
|
||||
sourceArn, accountId)) {
|
||||
return Optional.of("The role " + roleArn + " does not trust " + servicePrincipal + ".");
|
||||
}
|
||||
CallerContext caller = iamService.resolvePrincipalContext(role.get().getArn());
|
||||
List<List<String>> scpLevels = scpProvider.isResolvable()
|
||||
? scpProvider.get().effectiveScpLevels(accountId) : null;
|
||||
if (scpLevels != null) {
|
||||
caller = caller.withScpLevels(scpLevels);
|
||||
}
|
||||
for (Permission permission : permissions) {
|
||||
if (evaluate(caller, role.get().getArn(), accountId, permission) != Decision.ALLOW) {
|
||||
return Optional.of("The role " + roleArn + " is not authorized to perform "
|
||||
+ permission.action() + " on " + permission.resourceArn() + ".");
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private Decision evaluate(CallerContext caller, String roleArn, String accountId, Permission permission) {
|
||||
AwsArnUtils.Arn resource = AwsArnUtils.isArn(permission.resourceArn())
|
||||
? AwsArnUtils.parse(permission.resourceArn()) : null;
|
||||
String region = resource != null ? resource.region() : null;
|
||||
String resourceAccount = resource != null ? resource.accountId() : accountId;
|
||||
Map<String, List<String>> context = IamConditionContextResolver.withGlobalContext(
|
||||
Map.of("aws:PrincipalArn", List.of(roleArn)),
|
||||
permission.resourceArn(), region, accountId, resourceAccount);
|
||||
return evaluator.evaluate(caller, List.of(), permission.action(), permission.resourceArn(), context);
|
||||
}
|
||||
}
|
||||
+7
-69
@@ -1,22 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.redshiftserverless;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -32,22 +22,11 @@ public class RedshiftServerlessScheduledActionAuthorizer {
|
||||
static final String CREATE_SNAPSHOT = "redshift-serverless:CreateSnapshot";
|
||||
private static final String SCHEDULER_PRINCIPAL = ServicePrincipals.of("scheduler.redshift");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final IamService iamService;
|
||||
private final IamPolicyEvaluator evaluator;
|
||||
private final AssumeRolePolicyEvaluator trustEvaluator;
|
||||
private final Instance<ScpProvider> scpProvider;
|
||||
private final ServiceRoleAuthorizer authorizer;
|
||||
|
||||
@Inject
|
||||
public RedshiftServerlessScheduledActionAuthorizer(EmulatorConfig config, IamService iamService,
|
||||
IamPolicyEvaluator evaluator,
|
||||
AssumeRolePolicyEvaluator trustEvaluator,
|
||||
Instance<ScpProvider> scpProvider) {
|
||||
this.config = config;
|
||||
this.iamService = iamService;
|
||||
this.evaluator = evaluator;
|
||||
this.trustEvaluator = trustEvaluator;
|
||||
this.scpProvider = scpProvider;
|
||||
public RedshiftServerlessScheduledActionAuthorizer(ServiceRoleAuthorizer authorizer) {
|
||||
this.authorizer = authorizer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,48 +35,7 @@ public class RedshiftServerlessScheduledActionAuthorizer {
|
||||
* @param accountId the account that owns the action
|
||||
*/
|
||||
public Optional<String> refusal(String roleArn, String accountId, String targetArn) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(roleArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return Optional.of("The execution role " + roleArn + " is not a valid ARN.");
|
||||
}
|
||||
if (!"iam".equals(parsed.service()) || !parsed.resource().startsWith("role/")) {
|
||||
return Optional.of("The execution role " + roleArn + " is not an IAM role.");
|
||||
}
|
||||
if (!accountId.equals(parsed.accountId())) {
|
||||
return Optional.of("The execution role " + roleArn + " belongs to another account.");
|
||||
}
|
||||
String roleName = parsed.resource().substring(parsed.resource().lastIndexOf('/') + 1);
|
||||
Optional<IamRole> role = iamService.findRole(accountId, roleName)
|
||||
.filter(found -> IamService.roleArnMatches(roleArn, found));
|
||||
if (role.isEmpty()) {
|
||||
return Optional.of("The execution role " + roleArn + " does not exist.");
|
||||
}
|
||||
if (!trustEvaluator.allowsService(role.get().getAssumeRolePolicyDocument(), SCHEDULER_PRINCIPAL,
|
||||
null, accountId)) {
|
||||
return Optional.of("The execution role " + roleArn + " does not trust " + SCHEDULER_PRINCIPAL + ".");
|
||||
}
|
||||
CallerContext caller = iamService.resolvePrincipalContext(role.get().getArn());
|
||||
List<List<String>> scpLevels = scpProvider.isResolvable()
|
||||
? scpProvider.get().effectiveScpLevels(accountId) : null;
|
||||
if (scpLevels != null) {
|
||||
caller = caller.withScpLevels(scpLevels);
|
||||
}
|
||||
AwsArnUtils.Arn target = AwsArnUtils.isArn(targetArn) ? AwsArnUtils.parse(targetArn) : null;
|
||||
String targetRegion = target != null ? target.region() : null;
|
||||
String targetAccount = target != null ? target.accountId() : accountId;
|
||||
Map<String, List<String>> context = IamConditionContextResolver.withGlobalContext(
|
||||
Map.of("aws:PrincipalArn", List.of(role.get().getArn())),
|
||||
targetArn, targetRegion, accountId, targetAccount);
|
||||
Decision decision = evaluator.evaluate(caller, List.of(), CREATE_SNAPSHOT, targetArn, context);
|
||||
if (decision != Decision.ALLOW) {
|
||||
return Optional.of("The execution role " + roleArn + " is not authorized to perform "
|
||||
+ CREATE_SNAPSHOT + " on " + targetArn + ".");
|
||||
}
|
||||
return Optional.empty();
|
||||
return authorizer.refusal(roleArn, accountId, SCHEDULER_PRINCIPAL, null,
|
||||
List.of(new Permission(CREATE_SNAPSHOT, targetArn)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user