mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+1
@@ -83,6 +83,7 @@ class EventBridgeInvokerTest {
|
||||
new HostVolumePolicy(emulatorConfig)),
|
||||
stepFunctionsService,
|
||||
redshiftDataService,
|
||||
null,
|
||||
serviceRegistry,
|
||||
regionResolver,
|
||||
new ObjectMapper(),
|
||||
|
||||
+1
-1
@@ -222,7 +222,7 @@ class EventBridgeRedshiftDataTargetTest {
|
||||
EventBridgeInvoker scopedInvoker = new EventBridgeInvoker(
|
||||
mock(LambdaService.class), mock(SqsService.class), mock(SnsService.class),
|
||||
null, null, null, null, null, null,
|
||||
redshiftDataService, serviceRegistry, resolver, new ObjectMapper(), config);
|
||||
redshiftDataService, null, serviceRegistry, resolver, new ObjectMapper(), config);
|
||||
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RedshiftDataParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
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.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EventBridgeRedshiftTargetAuthorizationTest {
|
||||
private static final String ACCOUNT = "123456789012";
|
||||
private static final String CLUSTER_ARN = "arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:warehouse";
|
||||
private static final String WORKGROUP_ARN =
|
||||
"arn:aws:redshift-serverless:us-west-2:" + ACCOUNT + ":workgroup/warehouse";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::" + ACCOUNT + ":role/EventBridgeRedshiftRole";
|
||||
private static final String SECRET_ARN = "arn:aws:secretsmanager:us-east-1:" + ACCOUNT + ":secret:db-AbCdEf";
|
||||
private static final String EVENT = "{\"id\":\"event-1\"}";
|
||||
|
||||
private RedshiftDataService redshiftDataService;
|
||||
private EventBridgeRedshiftTargetAuthorizer authorizer;
|
||||
private EventBridgeInvoker invoker;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
redshiftDataService = mock(RedshiftDataService.class);
|
||||
authorizer = mock(EventBridgeRedshiftTargetAuthorizer.class);
|
||||
when(authorizer.refusal(any(), any(), any(), anyList())).thenReturn(Optional.empty());
|
||||
ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(true);
|
||||
RegionResolver resolver = mock(RegionResolver.class);
|
||||
when(resolver.getAccountId()).thenReturn(ACCOUNT);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
invoker = new EventBridgeInvoker(mock(LambdaService.class), mock(SqsService.class), mock(SnsService.class),
|
||||
null, null, null, null, null, null, redshiftDataService, authorizer, serviceRegistry, resolver,
|
||||
new ObjectMapper(), config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterTargetWithADbUserNeedsTheDataApiActionAndTemporaryCredentials() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setDbUser("admin");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(
|
||||
new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN),
|
||||
new Permission("redshift:GetClusterCredentials",
|
||||
"arn:aws:redshift:us-east-1:" + ACCOUNT + ":dbuser:warehouse/admin"),
|
||||
new Permission("redshift:GetClusterCredentials",
|
||||
"arn:aws:redshift:us-east-1:" + ACCOUNT + ":dbname:warehouse/dev")), permissions());
|
||||
verify(redshiftDataService).executeStatement(any(JsonNode.class), eq("us-east-1"), eq(ROLE_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void secretReplacesTheCredentialsPermission() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setDbUser("admin");
|
||||
parameters.setSecretManagerArn(SECRET_ARN);
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(
|
||||
new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN),
|
||||
new Permission("secretsmanager:GetSecretValue", SECRET_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterTargetNamingAnotherAccountIsCheckedAgainstTheClusterThatRuns() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target("arn:aws:redshift:us-east-1:999999999999:cluster:warehouse", parameters),
|
||||
EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankSecretIsIgnoredAndTheDbUserCredentialsAreChecked() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setDbUser("admin");
|
||||
parameters.setSecretManagerArn("");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(3, permissions().size());
|
||||
assertEquals("redshift:GetClusterCredentials", permissions().get(1).action());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankSecretOnAServerlessTargetStillNeedsTheWorkgroupCredentials() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSecretManagerArn(" ");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(WORKGROUP_ARN, parameters), EVENT, "us-west-2");
|
||||
|
||||
assertEquals(new Permission("redshift-serverless:GetCredentials", WORKGROUP_ARN), permissions().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankDbUserAddsNoCredentialsPermission() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setDbUser("");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void secretNamedWithoutAnArnAddsNoPermissionBecauseTheDataApiRejectsIt() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSecretManagerArn("db");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverlessTargetWithASecretNeedsTheSecretAndNotTheWorkgroupCredentials() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSecretManagerArn(SECRET_ARN);
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(WORKGROUP_ARN, parameters), EVENT, "us-west-2");
|
||||
|
||||
assertEquals(List.of(
|
||||
new Permission("redshift-data:ExecuteStatement", WORKGROUP_ARN),
|
||||
new Permission("secretsmanager:GetSecretValue", SECRET_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchNeedsTheBatchAction() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSqls(List.of("select 1", "select 2"));
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(new Permission("redshift-data:BatchExecuteStatement", CLUSTER_ARN), permissions().getFirst());
|
||||
verify(redshiftDataService).batchExecuteStatement(any(JsonNode.class), eq("us-east-1"), eq(ROLE_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverlessTargetNeedsTheWorkgroupCredentials() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(WORKGROUP_ARN, parameters), EVENT, "us-west-2");
|
||||
|
||||
assertEquals(List.of(
|
||||
new Permission("redshift-data:ExecuteStatement", WORKGROUP_ARN),
|
||||
new Permission("redshift-serverless:GetCredentials", WORKGROUP_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterTargetWithoutADbUserOrSecretNeedsOnlyTheDataApiAction() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", CLUSTER_ARN)), permissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusedRoleIsAccessDeniedAndNothingIsSubmitted() {
|
||||
when(authorizer.refusal(any(), any(), any(), anyList())).thenReturn(Optional.of("not authorized"));
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1"));
|
||||
|
||||
assertEquals("AccessDeniedException", failure.getErrorCode());
|
||||
assertEquals(403, failure.getHttpStatus());
|
||||
assertEquals("not authorized", failure.getMessage());
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusedRoleIsReportedBeforeABadPathInTheSql() {
|
||||
when(authorizer.refusal(any(), any(), any(), anyList())).thenReturn(Optional.of("not authorized"));
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("$.detail.missing");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1"));
|
||||
|
||||
assertEquals("AccessDeniedException", failure.getErrorCode());
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void allowedRoleStillGetsTheValidationErrorForABadPathInTheSql() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("$.detail.missing");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1"));
|
||||
|
||||
assertEquals("ValidationException", failure.getErrorCode());
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleRuleAccountAndMatchedRuleAreHandedToTheAuthorizer() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
String ruleArn = "arn:aws:events:us-east-1:" + ACCOUNT + ":rule/refresh";
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1", ruleArn);
|
||||
|
||||
verify(authorizer).refusal(eq(ROLE_ARN), eq(ACCOUNT), eq(ruleArn), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deliveryWithoutAMatchedRuleHandsOverNone() {
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
|
||||
invoker.invokeTarget(target(CLUSTER_ARN, parameters), EVENT, "us-east-1");
|
||||
|
||||
verify(authorizer).refusal(eq(ROLE_ARN), eq(ACCOUNT), isNull(), anyList());
|
||||
}
|
||||
|
||||
private List<Permission> permissions() {
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<Permission>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(authorizer).refusal(eq(ROLE_ARN), eq(ACCOUNT), any(), captor.capture());
|
||||
return captor.getValue();
|
||||
}
|
||||
|
||||
private static RedshiftDataParameters parameters(String database) {
|
||||
RedshiftDataParameters parameters = new RedshiftDataParameters();
|
||||
parameters.setDatabase(database);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private static Target target(String arn, RedshiftDataParameters parameters) {
|
||||
Target target = new Target();
|
||||
target.setId("redshift");
|
||||
target.setArn(arn);
|
||||
target.setRoleArn(ROLE_ARN);
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
return target;
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EventBridgeRedshiftTargetAuthorizerTest {
|
||||
private static final String ACCOUNT_ID = "123456789012";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::" + ACCOUNT_ID + ":role/events-redshift";
|
||||
private static final String CLUSTER = "arn:aws:redshift:us-east-1:" + ACCOUNT_ID + ":cluster:warehouse";
|
||||
private static final String SECRET = "arn:aws:secretsmanager:us-east-1:" + ACCOUNT_ID + ":secret:db-AbCdEf";
|
||||
private static final String TRUST = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String ALLOW_EXECUTE = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"redshift-data:ExecuteStatement","Resource":"*"}]}""";
|
||||
private static final String RULE_ARN = "arn:aws:events:us-east-1:" + ACCOUNT_ID + ":rule/refresh";
|
||||
private static final Permission EXECUTE = new Permission("redshift-data:ExecuteStatement", CLUSTER);
|
||||
|
||||
private EmulatorConfig config;
|
||||
private IamService iamService;
|
||||
private ScpProvider scps;
|
||||
private EventBridgeRedshiftTargetAuthorizer authorizer;
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void setUp() {
|
||||
config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().iam().enforcementEnabled()).thenReturn(true);
|
||||
iamService = mock(IamService.class);
|
||||
IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
|
||||
Instance<ScpProvider> scpProvider = mock(Instance.class);
|
||||
scps = mock(ScpProvider.class);
|
||||
when(scpProvider.isResolvable()).thenReturn(true);
|
||||
when(scpProvider.get()).thenReturn(scps);
|
||||
ServiceRoleAuthorizer serviceRoleAuthorizer = new ServiceRoleAuthorizer(config, iamService, evaluator,
|
||||
new AssumeRolePolicyEvaluator(new ObjectMapper(), evaluator), scpProvider);
|
||||
authorizer = new EventBridgeRedshiftTargetAuthorizer(config, serviceRoleAuthorizer);
|
||||
}
|
||||
|
||||
private void role(String trust, String... policies) {
|
||||
IamRole role = new IamRole("id", "events-redshift", "/", ROLE_ARN, trust);
|
||||
when(iamService.findRole(ACCOUNT_ID, "events-redshift")).thenReturn(Optional.of(role));
|
||||
when(iamService.resolvePrincipalContext(ROLE_ARN)).thenReturn(CallerContext.of(List.of(policies)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustedRoleWithThePermissionIsAllowed() {
|
||||
role(TRUST, ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingIsCheckedWhileIamEnforcementIsOff() {
|
||||
when(config.services().iam().enforcementEnabled()).thenReturn(false);
|
||||
|
||||
assertTrue(authorizer.refusal("not-an-arn", ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isEmpty());
|
||||
verifyNoInteractions(iamService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingRoleIsRefused() {
|
||||
when(iamService.findRole(ACCOUNT_ID, "events-redshift")).thenReturn(Optional.empty());
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).orElseThrow()
|
||||
.contains("does not exist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleWithoutAnArnIsRefused() {
|
||||
assertTrue(authorizer.refusal(null, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).orElseThrow()
|
||||
.contains("requires an IAM role"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleOfAnotherAccountIsRefused() {
|
||||
assertTrue(authorizer.refusal("arn:aws:iam::999999999999:role/events-redshift", ACCOUNT_ID, RULE_ARN,
|
||||
List.of(EXECUTE)).orElseThrow().contains("belongs to another account"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleThatDoesNotTrustEventBridgeIsRefused() {
|
||||
role("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}""", ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).orElseThrow()
|
||||
.contains("does not trust events.amazonaws.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleWithoutTheActionIsRefusedAndTheMessageNamesIt() {
|
||||
role(TRUST, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"redshift-data:DescribeStatement","Resource":"*"}]}""");
|
||||
|
||||
assertEquals("The role " + ROLE_ARN + " is not authorized to perform redshift-data:ExecuteStatement on "
|
||||
+ CLUSTER + ".",
|
||||
authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyPermissionHasToBeAllowed() {
|
||||
role(TRUST, ALLOW_EXECUTE);
|
||||
Permission secret = new Permission("secretsmanager:GetSecretValue", SECRET);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE, secret)).orElseThrow()
|
||||
.contains("secretsmanager:GetSecretValue on " + SECRET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void permissionsGrantedAcrossSeveralPoliciesAreAllowed() {
|
||||
role(TRUST, ALLOW_EXECUTE, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"secretsmanager:GetSecretValue","Resource":"%s"}]}""".formatted(SECRET));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN,
|
||||
List.of(EXECUTE, new Permission("secretsmanager:GetSecretValue", SECRET))).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyScopedToAnotherClusterIsRefused() {
|
||||
role(TRUST, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"redshift-data:ExecuteStatement",
|
||||
"Resource":"arn:aws:redshift:us-east-1:%s:cluster:other"}]}""".formatted(ACCOUNT_ID));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitDenyBeatsAnAllow() {
|
||||
role(TRUST, ALLOW_EXECUTE, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Deny",
|
||||
"Action":"redshift-data:ExecuteStatement","Resource":"*"}]}""");
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceControlPolicyDenyIsRefused() {
|
||||
role(TRUST, ALLOW_EXECUTE);
|
||||
when(scps.effectiveScpLevels(ACCOUNT_ID)).thenReturn(List.of(List.of("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Deny",
|
||||
"Action":"redshift-data:ExecuteStatement","Resource":"*"}]}""")));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void permissionsBoundaryThatDoesNotAllowTheActionIsRefused() {
|
||||
role(TRUST, ALLOW_EXECUTE);
|
||||
when(iamService.resolvePrincipalContext(ROLE_ARN)).thenReturn(new CallerContext(
|
||||
List.of(ALLOW_EXECUTE), null, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"s3:*","Resource":"*"}]}"""));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustPolicyNamingTheMatchedRuleInSourceArnIsAllowed() {
|
||||
role(sourceArnTrust(RULE_ARN), ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustPolicyNamingAnotherRuleInSourceArnIsRefused() {
|
||||
role(sourceArnTrust("arn:aws:events:us-east-1:" + ACCOUNT_ID + ":rule/other"), ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).orElseThrow()
|
||||
.contains("does not trust events.amazonaws.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sourceArnConditionDoesNotMatchWhenTheRuleIsUnknown() {
|
||||
role(sourceArnTrust(RULE_ARN), ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, null, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
|
||||
private static String sourceArnTrust(String ruleArn) {
|
||||
return """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"ArnEquals":{"aws:SourceArn":"%s"}}}]}""".formatted(ruleArn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustPolicyWithMismatchedSourceAccountConditionIsRefused() {
|
||||
role("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"StringEquals":{"aws:SourceAccount":"999999999999"}}}]}""", ALLOW_EXECUTE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, RULE_ARN, List.of(EXECUTE)).isPresent());
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RedshiftDataParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* With IAM enforcement on, an EventBridge Redshift target runs only when its role trusts
|
||||
* events.amazonaws.com and may call the Data API and authenticate to the cluster.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class EventBridgeRedshiftTargetRoleEnforcementIntegrationTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String CLUSTER_ARN = "arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:warehouse";
|
||||
private static final String EVENTS_TRUST = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String LAMBDA_TRUST = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String DATA_API_AND_CREDENTIALS = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"redshift-data:ExecuteStatement","Resource":"%s"},
|
||||
{"Effect":"Allow","Action":"redshift:GetClusterCredentials","Resource":"*"}]}""".formatted(CLUSTER_ARN);
|
||||
|
||||
@Inject
|
||||
EventBridgeInvoker invoker;
|
||||
|
||||
@Inject
|
||||
TargetDispatcher dispatcher;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@InjectMock
|
||||
RedshiftDataService redshiftDataService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
clearInvocations(redshiftDataService);
|
||||
when(redshiftDataService.executeStatement(any(), any(), any()))
|
||||
.thenReturn(JsonNodeFactory.instance.objectNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleThatTrustsEventBridgeAndHoldsThePermissionsRunsTheStatement() {
|
||||
String roleArn = role("events-redshift-allowed", EVENTS_TRUST, DATA_API_AND_CREDENTIALS);
|
||||
|
||||
invoker.invokeTarget(target(roleArn), "{\"id\":\"event-1\"}", "us-east-1");
|
||||
|
||||
verify(redshiftDataService).executeStatement(any(JsonNode.class), eq("us-east-1"), eq(roleArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleThatDoesNotTrustEventBridgeIsRefused() {
|
||||
String roleArn = role("events-redshift-untrusted", LAMBDA_TRUST, DATA_API_AND_CREDENTIALS);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target(roleArn), "{\"id\":\"event-1\"}", "us-east-1"));
|
||||
|
||||
assertEquals("AccessDeniedException", failure.getErrorCode());
|
||||
assertTrue(failure.getMessage().contains("does not trust events.amazonaws.com"), failure.getMessage());
|
||||
verify(redshiftDataService, never()).executeStatement(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleWithoutTheCredentialsPermissionIsRefused() {
|
||||
String roleArn = role("events-redshift-no-credentials", EVENTS_TRUST, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"redshift-data:ExecuteStatement","Resource":"*"}]}""");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target(roleArn), "{\"id\":\"event-1\"}", "us-east-1"));
|
||||
|
||||
assertEquals(403, failure.getHttpStatus());
|
||||
assertTrue(failure.getMessage().contains("redshift:GetClusterCredentials"), failure.getMessage());
|
||||
verify(redshiftDataService, never()).executeStatement(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustConditionOnTheRuleArnIsMatchedForADeliveryThroughTheDispatcher() {
|
||||
String ruleArn = "arn:aws:events:us-east-1:" + ACCOUNT + ":rule/refresh";
|
||||
String roleArn = role("events-redshift-source-arn", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"ArnEquals":{"aws:SourceArn":"%s"}}}]}""".formatted(ruleArn),
|
||||
DATA_API_AND_CREDENTIALS);
|
||||
Target target = target(roleArn);
|
||||
|
||||
dispatcher.dispatch(ruleArn, target, "{\"id\":\"event-1\"}", "us-east-1", () -> List.of(target));
|
||||
verify(redshiftDataService).executeStatement(any(JsonNode.class), eq("us-east-1"), eq(roleArn));
|
||||
|
||||
clearInvocations(redshiftDataService);
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:" + ACCOUNT + ":rule/other", target,
|
||||
"{\"id\":\"event-2\"}", "us-east-1", () -> List.of(target));
|
||||
verify(redshiftDataService, never()).executeStatement(any(), any(), any());
|
||||
}
|
||||
|
||||
private String role(String name, String trust, String policy) {
|
||||
String arn = iamService.createRole(name, "/", trust, null, 3600, Map.of()).getArn();
|
||||
iamService.putRolePolicy(name, "redshift", policy);
|
||||
return arn;
|
||||
}
|
||||
|
||||
private static Target target(String roleArn) {
|
||||
RedshiftDataParameters parameters = new RedshiftDataParameters();
|
||||
parameters.setDatabase("dev");
|
||||
parameters.setDbUser("admin");
|
||||
parameters.setSql("select 1");
|
||||
Target target = new Target();
|
||||
target.setId("redshift");
|
||||
target.setArn(CLUSTER_ARN);
|
||||
target.setRoleArn(roleArn);
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
return target;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -218,7 +218,7 @@ class RuleSchedulerTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invokeTarget(Target target, String eventJson, String region) {
|
||||
public void invokeTarget(Target target, String eventJson, String region, String ruleArn) {
|
||||
invocationCount.incrementAndGet();
|
||||
onDelivery.run();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer.Permission;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ServiceRoleAuthorizerTest {
|
||||
private static final String ACCOUNT_ID = "123456789012";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::" + ACCOUNT_ID + ":role/service-role";
|
||||
private static final String RESOURCE = "arn:aws:s3:::my-bucket";
|
||||
private static final String SERVICE_PRINCIPAL = "events.amazonaws.com";
|
||||
private static final String SOURCE_ARN = "arn:aws:events:us-east-1:" + ACCOUNT_ID + ":rule/trigger";
|
||||
private static final String TRUST = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String ALLOW = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"s3:PutObject","Resource":"*"}]}""";
|
||||
private static final Permission PERMISSION = new Permission("s3:PutObject", RESOURCE);
|
||||
|
||||
private EmulatorConfig config;
|
||||
private IamService iamService;
|
||||
private ScpProvider scps;
|
||||
private ServiceRoleAuthorizer authorizer;
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void setUp() {
|
||||
config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().iam().enforcementEnabled()).thenReturn(true);
|
||||
iamService = mock(IamService.class);
|
||||
IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
|
||||
Instance<ScpProvider> scpProvider = mock(Instance.class);
|
||||
scps = mock(ScpProvider.class);
|
||||
when(scpProvider.isResolvable()).thenReturn(true);
|
||||
when(scpProvider.get()).thenReturn(scps);
|
||||
authorizer = new ServiceRoleAuthorizer(config, iamService, evaluator,
|
||||
new AssumeRolePolicyEvaluator(new ObjectMapper(), evaluator), scpProvider);
|
||||
}
|
||||
|
||||
private void role(String trust, String... policies) {
|
||||
IamRole role = new IamRole("id", "service-role", "/", ROLE_ARN, trust);
|
||||
when(iamService.findRole(ACCOUNT_ID, "service-role")).thenReturn(Optional.of(role));
|
||||
when(iamService.resolvePrincipalContext(ROLE_ARN)).thenReturn(CallerContext.of(List.of(policies)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void trustedRoleWithPermissionsIsAllowed() {
|
||||
role(TRUST, ALLOW);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void enforcementOffAllowsEverything() {
|
||||
when(config.services().iam().enforcementEnabled()).thenReturn(false);
|
||||
|
||||
assertTrue(authorizer.refusal("not-an-arn", ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isEmpty());
|
||||
verifyNoInteractions(iamService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingRoleIsRefused() {
|
||||
when(iamService.findRole(ACCOUNT_ID, "service-role")).thenReturn(Optional.empty());
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).orElseThrow().contains("does not exist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleOfAnotherAccountIsRefusedWithoutLookup() {
|
||||
Optional<String> refusal = authorizer.refusal("arn:aws:iam::999999999999:role/service-role",
|
||||
ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN, List.of(PERMISSION));
|
||||
|
||||
assertTrue(refusal.orElseThrow().contains("belongs to another account"));
|
||||
verify(iamService, never()).findRole(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonRoleArnIsRefused() {
|
||||
assertTrue(authorizer.refusal("arn:aws:iam::" + ACCOUNT_ID + ":user/someone",
|
||||
ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN, List.of(PERMISSION)).orElseThrow()
|
||||
.contains("is not an IAM role"));
|
||||
assertTrue(authorizer.refusal("invalid-arn",
|
||||
ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN, List.of(PERMISSION)).orElseThrow()
|
||||
.contains("is not a valid ARN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void untrustedServicePrincipalIsRefused() {
|
||||
role(TRUST.replace("events.amazonaws.com", "lambda.amazonaws.com"), ALLOW);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).orElseThrow().contains("does not trust events.amazonaws.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingPermissionIsRefusedAndNamesActionAndResource() {
|
||||
role(TRUST, ALLOW);
|
||||
Permission other = new Permission("s3:GetObject", RESOURCE);
|
||||
|
||||
assertEquals("The role " + ROLE_ARN + " is not authorized to perform s3:GetObject on " + RESOURCE + ".",
|
||||
authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(other)).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyPermissionMustBeAllowed() {
|
||||
role(TRUST, ALLOW);
|
||||
Permission denied = new Permission("s3:DeleteObject", RESOURCE);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION, denied)).orElseThrow().contains("s3:DeleteObject"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scpDenyIsRefused() {
|
||||
role(TRUST, ALLOW);
|
||||
when(scps.effectiveScpLevels(ACCOUNT_ID)).thenReturn(List.of(List.of("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Deny",
|
||||
"Action":"s3:PutObject","Resource":"*"}]}""")));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void permissionsBoundaryDenyIsRefused() {
|
||||
role(TRUST, ALLOW);
|
||||
when(iamService.resolvePrincipalContext(ROLE_ARN)).thenReturn(new CallerContext(
|
||||
List.of(ALLOW), null, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":"sqs:*","Resource":"*"}]}"""));
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sourceArnTrustConditionIsEvaluated() {
|
||||
String conditionalTrust = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"ArnEquals":{"aws:SourceArn":"%s"}}}]}""".formatted(SOURCE_ARN);
|
||||
role(conditionalTrust, ALLOW);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isEmpty());
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, "arn:aws:events:us-east-1:123456789012:rule/other",
|
||||
List.of(PERMISSION)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sourceAccountTrustConditionIsEvaluated() {
|
||||
String conditionalTrust = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"StringEquals":{"aws:SourceAccount":"%s"}}}]}""".formatted(ACCOUNT_ID);
|
||||
role(conditionalTrust, ALLOW);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isEmpty());
|
||||
|
||||
String mismatchTrust = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"events.amazonaws.com"},"Action":"sts:AssumeRole",
|
||||
"Condition":{"StringEquals":{"aws:SourceAccount":"999999999999"}}}]}""";
|
||||
role(mismatchTrust, ALLOW);
|
||||
|
||||
assertTrue(authorizer.refusal(ROLE_ARN, ACCOUNT_ID, SERVICE_PRINCIPAL, SOURCE_ARN,
|
||||
List.of(PERMISSION)).isPresent());
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceRoleAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -51,8 +52,9 @@ class RedshiftServerlessScheduledActionAuthorizerTest {
|
||||
scps = mock(ScpProvider.class);
|
||||
when(scpProvider.isResolvable()).thenReturn(true);
|
||||
when(scpProvider.get()).thenReturn(scps);
|
||||
authorizer = new RedshiftServerlessScheduledActionAuthorizer(config, iamService, evaluator,
|
||||
ServiceRoleAuthorizer serviceRoleAuthorizer = new ServiceRoleAuthorizer(config, iamService, evaluator,
|
||||
new AssumeRolePolicyEvaluator(new ObjectMapper(), evaluator), scpProvider);
|
||||
authorizer = new RedshiftServerlessScheduledActionAuthorizer(serviceRoleAuthorizer);
|
||||
}
|
||||
|
||||
private void role(String arn, String trust, String policy) {
|
||||
|
||||
Reference in new issue
Block a user