mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): check iam:PassRole for RDS and DMS (#6389)
Under IAM enforcement an RDS or DMS request that names a role went through without iam:PassRole on
it. AWS's Service Reference lists iam:PassRole, passed to rds.amazonaws.com or dms.amazonaws.com,
for each of these operations, so a caller allowed to change a database could attach any role in
the account to it.
RDS, to rds.amazonaws.com:
- AddRoleToDBCluster, AddRoleToDBInstance, RemoveRoleFromDBCluster, RemoveRoleFromDBInstance,
CreateDBProxy and ModifyDBProxy pass RoleArn, and StartExportTask passes IamRoleArn;
- CreateDBInstance, CreateDBInstanceReadReplica, ModifyDBInstance, CreateDBCluster,
ModifyDBCluster, RestoreDBClusterFromSnapshot and RestoreDBClusterToPointInTime pass the
Enhanced Monitoring role, MonitoringRoleArn ("The user that enables Enhanced Monitoring must be
granted the PassRole permission.");
- they, RestoreDBInstanceFromDBSnapshot and RestoreDBInstanceToPointInTime pass the directory role
DomainIAMRoleName names, checked on the ARN IAM holds it under, since the name carries no path;
- CreateDBCluster and the two cluster restores pass each role in AssociatedRoles, at any index and
in either list form, as the RDS handler reads them;
- ModifyOptionGroup passes the role each option it includes takes in its IAM_ROLE_ARN setting, as
SQL Server's native backup and audit options do, read as the handler reads OptionsToInclude.
DMS, to dms.amazonaws.com: CreateEndpoint and ModifyEndpoint pass ServiceAccessRoleArn and the
role members the DMS model gives each engine's settings structure (ServiceAccessRoleArn,
SecretsManagerAccessRoleArn, SecretsManagerOracleAsmAccessRoleArn), and CreateReplicationInstance
and ModifyReplicationInstance pass KerberosAuthenticationSettings.KeyCacheSecretIamArn.
docs/services/iam.md lists the operations and the roles they pass.
This commit is contained in:
1 parent
ec5597eb5c
commit
6215b6fd38
4 files changed
+315
-2
No files matched your search
@@ -32,6 +32,8 @@ class PassedRolesTest {
|
||||
private static final String DEFAULT_ADMINISTRATION = ROLES + "AWSCloudFormationStackSetAdministrationRole";
|
||||
private static final String EC2 = "ec2.amazonaws.com";
|
||||
private static final String AUTOSCALING = "autoscaling.amazonaws.com";
|
||||
private static final String RDS = "rds.amazonaws.com";
|
||||
private static final String DMS = "dms.amazonaws.com";
|
||||
private static final String PROFILE_ARN = "arn:aws:iam::111122223333:instance-profile/arn-profile";
|
||||
|
||||
/**
|
||||
@@ -418,6 +420,81 @@ class PassedRolesTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rdsPassesEachRoleARequestNamesForAnInstanceOrClusterToRds() {
|
||||
for (String action : new String[] {"rds:AddRoleToDBCluster", "rds:AddRoleToDBInstance",
|
||||
"rds:RemoveRoleFromDBCluster", "rds:RemoveRoleFromDBInstance", "rds:CreateDBProxy", "rds:ModifyDBProxy"}) {
|
||||
assertEquals(List.of(passed("feature", RDS)), PassedRoles.of(action,
|
||||
form("DBInstanceIdentifier=d&RoleArn=" + ROLES + "feature&FeatureName=s3Import"), LOOKUPS), action);
|
||||
}
|
||||
assertEquals(List.of(passed("export", RDS)), PassedRoles.of("rds:StartExportTask",
|
||||
form("ExportTaskIdentifier=e&IamRoleArn=" + ROLES + "export"), LOOKUPS));
|
||||
// The directory role is named without its path, so it is checked on the ARN IAM holds it under.
|
||||
assertEquals(List.of(new PassedRole(ROLES + "app/app-role", RDS), passed("monitoring", RDS)),
|
||||
PassedRoles.of("rds:CreateDBInstance", form("DBInstanceIdentifier=d&DomainIAMRoleName=app-role"
|
||||
+ "&MonitoringRoleArn=" + ROLES + "monitoring"), LOOKUPS));
|
||||
assertEquals(List.of(passed("gone", RDS)), PassedRoles.of("rds:ModifyDBCluster",
|
||||
form("DBClusterIdentifier=c&DomainIAMRoleName=gone"), LOOKUPS));
|
||||
// A cluster's associated roles at any index, in either list form; a restore of an instance takes no
|
||||
// monitoring role, and a modify of a cluster no associated roles.
|
||||
assertEquals(List.of(passed("monitoring", RDS), passed("s3", RDS), passed("lambda", RDS)),
|
||||
PassedRoles.of("rds:RestoreDBClusterToPointInTime", form("DBClusterIdentifier=c&MonitoringRoleArn="
|
||||
+ ROLES + "monitoring&AssociatedRoles.DBClusterAssociatedRole.1.RoleArn=" + ROLES
|
||||
+ "s3&AssociatedRoles.member.3.RoleArn=" + ROLES + "lambda"), LOOKUPS));
|
||||
assertEquals(List.of(passed("gone", RDS)), PassedRoles.of("rds:RestoreDBInstanceFromDBSnapshot",
|
||||
form("DBInstanceIdentifier=d&DomainIAMRoleName=gone&MonitoringRoleArn=" + ROLES + "monitoring"), LOOKUPS));
|
||||
assertEquals(List.of(), PassedRoles.of("rds:ModifyDBCluster",
|
||||
form("DBClusterIdentifier=c&AssociatedRoles.member.1.RoleArn=" + ROLES + "s3"), LOOKUPS));
|
||||
assertEquals(List.of(), PassedRoles.of("rds:CreateDBCluster", form("DBClusterIdentifier=c"), LOOKUPS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOptionGroupPassesTheRoleEachOptionTakesInItsIamRoleSettingToRds() {
|
||||
String backup = "OptionsToInclude.OptionConfiguration.1.";
|
||||
String audit = "OptionsToInclude.OptionConfiguration.2.";
|
||||
String legacy = "OptionsToInclude.member.1.";
|
||||
// Read as the handler reads it: both list forms, up to the first gap, and a setting named twice
|
||||
// as its last value.
|
||||
assertEquals(List.of(passed("audit", RDS), passed("legacy", RDS)), PassedRoles.of("rds:ModifyOptionGroup",
|
||||
form("OptionGroupName=g&" + backup + "OptionName=SQLSERVER_BACKUP_RESTORE&" + backup
|
||||
+ "OptionSettings.OptionSetting.2.Name=IAM_ROLE_ARN&" + backup
|
||||
+ "OptionSettings.OptionSetting.2.Value=" + ROLES + "after-gap&" + audit
|
||||
+ "OptionName=SQLSERVER_AUDIT&" + audit + "OptionSettings.OptionSetting.1.Name=S3_BUCKET_ARN&"
|
||||
+ audit + "OptionSettings.OptionSetting.1.Value=" + ROLES + "bucket&" + audit
|
||||
+ "OptionSettings.OptionSetting.2.Name=IAM_ROLE_ARN&" + audit
|
||||
+ "OptionSettings.OptionSetting.2.Value=" + ROLES + "first&" + audit
|
||||
+ "OptionSettings.member.1.Name=IAM_ROLE_ARN&" + audit + "OptionSettings.member.1.Value="
|
||||
+ ROLES + "audit&" + legacy + "OptionName=SQLSERVER_BACKUP_RESTORE&" + legacy
|
||||
+ "OptionSettings.member.1.Name=IAM_ROLE_ARN&" + legacy + "OptionSettings.member.1.Value="
|
||||
+ ROLES + "legacy&OptionsToInclude.OptionConfiguration.4.OptionName=SQLSERVER_AUDIT&"
|
||||
+ "OptionsToInclude.OptionConfiguration.4.OptionSettings.OptionSetting.1.Name=IAM_ROLE_ARN&"
|
||||
+ "OptionsToInclude.OptionConfiguration.4.OptionSettings.OptionSetting.1.Value=" + ROLES
|
||||
+ "option-after-gap"), LOOKUPS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dmsPassesItsEndpointsAndReplicationInstancesRolesToDms() {
|
||||
String endpoint = """
|
||||
{"EndpointIdentifier":"e","ServiceAccessRoleArn":"%1$saccess",
|
||||
"S3Settings":{"ServiceAccessRoleArn":"%1$ss3","BucketName":"b"},
|
||||
"PostgreSQLSettings":{"SecretsManagerAccessRoleArn":"%1$ssecrets"},
|
||||
"OracleSettings":{"SecretsManagerOracleAsmAccessRoleArn":"%1$sasm"},
|
||||
"Tags":[{"Key":"ServiceAccessRoleArn","Value":"%1$stag"}],
|
||||
"FooSettings":{"ServiceAccessRoleArn":"%1$sunknown-structure"},
|
||||
"KafkaSettings":{"ServiceAccessRoleArn":"%1$sno-role-member"},
|
||||
"DynamoDbSettings":{"SecretsManagerAccessRoleArn":"%1$snot-its-member"}}""".formatted(ROLES);
|
||||
// Only the role members the DMS model gives each settings structure the handler stores.
|
||||
for (String action : new String[] {"dms:CreateEndpoint", "dms:ModifyEndpoint"}) {
|
||||
assertEquals(List.of(passed("access", DMS), passed("s3", DMS), passed("secrets", DMS), passed("asm", DMS)),
|
||||
PassedRoles.of(action, json(endpoint), LOOKUPS), action);
|
||||
}
|
||||
for (String action : new String[] {"dms:CreateReplicationInstance", "dms:ModifyReplicationInstance"}) {
|
||||
assertEquals(List.of(passed("key-cache", DMS)), PassedRoles.of(action, json(
|
||||
"{\"KerberosAuthenticationSettings\":{\"KeyCacheSecretIamArn\":\"" + ROLES + "key-cache\"}}"),
|
||||
LOOKUPS), action);
|
||||
}
|
||||
}
|
||||
|
||||
private static PassedRole passed(String roleName, String service) {
|
||||
return new PassedRole(ROLES + roleName, service);
|
||||
}
|
||||
|
||||
+92
@@ -21,6 +21,8 @@ import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
@@ -53,6 +55,8 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
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 RDS = "rds.amazonaws.com";
|
||||
private static final String DMS = "dms.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());
|
||||
@@ -888,6 +892,90 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
serverless(user, "UpdateNamespace").body(defaultOnly).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anRdsRequestNeedsPassRoleOnEachRoleItNamesForRds() {
|
||||
String role = role("rds-feature");
|
||||
User user = user("passrole-rds");
|
||||
String missing = "passrole-missing-" + suffix();
|
||||
String option = "OptionsToInclude.OptionConfiguration.1.";
|
||||
Map<String, Map<String, String>> requests = new LinkedHashMap<>();
|
||||
requests.put("AddRoleToDBInstance", Map.of("DBInstanceIdentifier", missing, "RoleArn", role,
|
||||
"FeatureName", "s3Import"));
|
||||
requests.put("RemoveRoleFromDBCluster", Map.of("DBClusterIdentifier", missing, "RoleArn", role,
|
||||
"FeatureName", "s3Import"));
|
||||
requests.put("ModifyDBProxy", Map.of("DBProxyName", missing, "RoleArn", role));
|
||||
requests.put("ModifyDBInstance", Map.of("DBInstanceIdentifier", missing, "MonitoringInterval", "60",
|
||||
"MonitoringRoleArn", role));
|
||||
requests.put("RestoreDBClusterFromSnapshot", Map.of("DBClusterIdentifier", missing, "SnapshotIdentifier",
|
||||
missing, "Engine", "aurora-postgresql", "AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", role));
|
||||
requests.put("ModifyOptionGroup", Map.of("OptionGroupName", missing, option + "OptionName",
|
||||
"SQLSERVER_BACKUP_RESTORE", option + "OptionSettings.OptionSetting.1.Name", "IAM_ROLE_ARN",
|
||||
option + "OptionSettings.OptionSetting.1.Value", role));
|
||||
requests.put("StartExportTask", Map.of("ExportTaskIdentifier", missing, "SourceArn",
|
||||
"arn:aws:rds:us-east-1:" + ACCOUNT + ":snapshot:" + missing, "S3BucketName", missing,
|
||||
"IamRoleArn", role, "KmsKeyId", "alias/" + missing));
|
||||
|
||||
// A grant to pass the role to Redshift does not cover RDS; with one for RDS, each reaches RDS, which
|
||||
// has no such resource.
|
||||
user.policy(allow("rds:*", "*") + "," + passRole(role, REDSHIFT));
|
||||
requests.forEach((action, params) -> denied(user.rds(action).formParams(params).post("/").then()));
|
||||
user.policy(allow("rds:*", "*") + "," + passRole(role, RDS));
|
||||
requests.forEach((action, params) -> user.rds(action).formParams(params).post("/").then()
|
||||
.statusCode(not(403)).body(not(containsString("iam:PassRole"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anRdsDirectoryRoleNamedByNameNeedsPassRoleOnTheArnIamHoldsItUnder() {
|
||||
String role = role("/directory/", "rds-directory");
|
||||
User user = user("passrole-rds-directory");
|
||||
String pathless = "arn:aws:iam::" + ACCOUNT + ":role/" + roleName(role);
|
||||
String cluster = "passrole-missing-" + suffix();
|
||||
|
||||
user.policy(allow("rds:ModifyDBCluster", "*") + "," + passRole(pathless, RDS));
|
||||
denied(modifyClusterDirectoryRole(user, cluster, roleName(role)).then());
|
||||
user.policy(allow("rds:ModifyDBCluster", "*") + "," + passRole(role, RDS));
|
||||
modifyClusterDirectoryRole(user, cluster, roleName(role)).then()
|
||||
.statusCode(404).body(containsString("DBClusterNotFound"));
|
||||
}
|
||||
|
||||
private static Response modifyClusterDirectoryRole(User user, String cluster, String roleName) {
|
||||
return user.rds("ModifyDBCluster").formParam("DBClusterIdentifier", cluster)
|
||||
.formParam("DomainIAMRoleName", roleName).post("/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDmsEndpointAndReplicationInstanceNeedPassRoleOnTheirRolesForDms() {
|
||||
String role = role("dms-access");
|
||||
User user = user("passrole-dms");
|
||||
String endpoint = ("{\"EndpointIdentifier\":\"passrole-%s\",\"EndpointType\":\"target\",\"EngineName\":\"s3\","
|
||||
+ "\"S3Settings\":{\"ServiceAccessRoleArn\":\"%s\",\"BucketName\":\"passrole-bucket\"}}")
|
||||
.formatted(suffix(), role);
|
||||
String modifyEndpoint = ("{\"EndpointArn\":\"arn:aws:dms:us-east-1:%s:endpoint:PASSROLEMISSING\","
|
||||
+ "\"PostgreSQLSettings\":{\"SecretsManagerAccessRoleArn\":\"%s\"}}").formatted(ACCOUNT, role);
|
||||
String modifyInstance = ("{\"ReplicationInstanceArn\":\"arn:aws:dms:us-east-1:%s:rep:PASSROLEMISSING\","
|
||||
+ "\"KerberosAuthenticationSettings\":{\"KeyCacheSecretIamArn\":\"%s\"}}").formatted(ACCOUNT, role);
|
||||
|
||||
// A grant to pass the role to RDS does not cover DMS.
|
||||
user.policy(allow("dms:*", "*") + "," + passRole(role, RDS));
|
||||
deniedJson(dms(user, "CreateEndpoint").body(endpoint).post("/").then());
|
||||
deniedJson(dms(user, "ModifyEndpoint").body(modifyEndpoint).post("/").then());
|
||||
deniedJson(dms(user, "ModifyReplicationInstance").body(modifyInstance).post("/").then());
|
||||
|
||||
user.policy(allow("dms:*", "*") + "," + passRole(role, DMS));
|
||||
String endpointArn = dms(user, "CreateEndpoint").body(endpoint).post("/").then().statusCode(200)
|
||||
.extract().path("Endpoint.EndpointArn");
|
||||
cleanup.register(() -> admin("dms").header("X-Amz-Target", "AmazonDMSv20160101.DeleteEndpoint")
|
||||
.contentType("application/x-amz-json-1.1").body("{\"EndpointArn\":\"" + endpointArn + "\"}").post("/"));
|
||||
dms(user, "ModifyEndpoint").body(modifyEndpoint).post("/").then()
|
||||
.statusCode(400).body(containsString("ResourceNotFoundFault"));
|
||||
dms(user, "ModifyReplicationInstance").body(modifyInstance).post("/").then()
|
||||
.statusCode(400).body(containsString("ResourceNotFoundFault"));
|
||||
}
|
||||
|
||||
private static RequestSpecification dms(User user, String operation) {
|
||||
return user.json("dms", "AmazonDMSv20160101." + operation);
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -1156,6 +1244,10 @@ class IamPassRoleEnforcementIntegrationTest {
|
||||
return given().header("Authorization", auth(accessKeyId, "redshift")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification rds(String action) {
|
||||
return given().header("Authorization", auth(accessKeyId, "rds")).formParam("Action", action);
|
||||
}
|
||||
|
||||
RequestSpecification ecs(String action) {
|
||||
return json("ecs", "AmazonEC2ContainerServiceV20141113." + action);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user