mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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