diff --git a/docs/services/iam.md b/docs/services/iam.md index 663427e3de..cd5711cc83 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1551,6 +1551,21 @@ role each passes: **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`. +- **RDS:** `AddRoleToDBCluster`, `AddRoleToDBInstance`, `RemoveRoleFromDBCluster`, + `RemoveRoleFromDBInstance`, `CreateDBProxy` and `ModifyDBProxy` pass `RoleArn`, and + `StartExportTask` passes `IamRoleArn`, to `rds.amazonaws.com`. `CreateDBInstance`, + `CreateDBInstanceReadReplica`, `ModifyDBInstance`, `CreateDBCluster`, `ModifyDBCluster`, + `RestoreDBClusterFromSnapshot` and `RestoreDBClusterToPointInTime` pass the Enhanced Monitoring + role, `MonitoringRoleArn`, there too. 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`, and `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. **DMS:** `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`, to `dms.amazonaws.com`. Launching an instance from a launch template does not check the template's role, as on AWS: the EC2 user guide warns that a user who can create launch template versions "can potentially update a launch @@ -1568,8 +1583,11 @@ a user must have iam:PassRole permission for that IAM role." A role named only a default, `DefaultIamRoleArn`, is not checked on its own, since the default is one of the roles the cluster holds. A namespace's `defaultIamRoleArn` is, since Floci keeps it whether or not `iamRoles` holds it. Redshift takes a role only under the ARN IAM holds it under, path included, so the check -on the ARN as the request names it covers the role `COPY`, `UNLOAD` and Spectrum then use. The role -parameters of other services are not checked yet. +on the ARN as the request names it covers the role `COPY`, `UNLOAD` and Spectrum then use. The RDS +user guide has the Enhanced Monitoring role checked: "The user that enables Enhanced Monitoring must +be granted the PassRole permission." Removing a role from an RDS instance or cluster is checked as +adding one is, since the Service Reference lists `iam:PassRole` for both. The role parameters of +other services are not checked yet. A request that names its operation, in a Query `Action`, an `X-Amz-Target` header or a Smithy RPC v2 path (`/service//operation/`), is authorized as the action AWS checks for that diff --git a/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java b/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java index 3f1cdc5878..65e3a0aa63 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.regex.Pattern; /** @@ -80,6 +81,14 @@ import java.util.regex.Pattern; * default, which Floci keeps whether or not the namespace's roles hold it. Redshift takes a role only * under the ARN IAM holds it under, path included, so the ARN as the request names it is the role it * passes. + * + *

An RDS request passes to {@code rds.amazonaws.com} every role it names for the instance or cluster: + * the role it adds or removes, a proxy's role, an export task's role, the Enhanced Monitoring role (the + * RDS user guide: "The user that enables Enhanced Monitoring must be granted the PassRole permission."), + * the directory role it names by name, a cluster's associated roles, and the role an option takes in its + * {@code IAM_ROLE_ARN} setting, as SQL Server's backup and audit options do. A DMS endpoint passes to + * {@code dms.amazonaws.com} its service access role and the roles its settings name for each engine, and + * a replication instance the role its Kerberos settings name. */ final class PassedRoles { @@ -107,6 +116,45 @@ final class PassedRoles { private static final String APPLICATION_AUTOSCALING = ServicePrincipals.of("application-autoscaling"); private static final String REDSHIFT = ServicePrincipals.of("redshift"); private static final String REDSHIFT_SERVERLESS = ServicePrincipals.of("redshift-serverless"); + private static final String RDS = ServicePrincipals.of("rds"); + private static final String DMS = ServicePrincipals.of("dms"); + + /** The RDS operations whose request takes a {@code MonitoringRoleArn}. */ + private static final Set RDS_MONITORING_ROLE = Set.of("rds:CreateDBCluster", "rds:CreateDBInstance", + "rds:CreateDBInstanceReadReplica", "rds:ModifyDBCluster", "rds:ModifyDBInstance", + "rds:RestoreDBClusterFromSnapshot", "rds:RestoreDBClusterToPointInTime"); + /** The RDS operations whose request takes a cluster's {@code AssociatedRoles}. */ + private static final Set RDS_ASSOCIATED_ROLES = Set.of("rds:CreateDBCluster", + "rds:RestoreDBClusterFromSnapshot", "rds:RestoreDBClusterToPointInTime"); + /** An associated role's ARN, in either list form the RDS handler reads, at any index. */ + private static final Pattern ASSOCIATED_ROLE = + Pattern.compile("AssociatedRoles\\.(DBClusterAssociatedRole|member)\\.\\d+\\.RoleArn"); + /** The option setting SQL Server's backup and audit options take their role in. */ + private static final String OPTION_ROLE_SETTING = "IAM_ROLE_ARN"; + private static final String DMS_SERVICE_ACCESS_ROLE = "ServiceAccessRoleArn"; + private static final String DMS_SECRETS_ROLE = "SecretsManagerAccessRoleArn"; + /** + * The role members of each DMS endpoint settings structure, as the DMS model gives them. Every structure + * here is one the DMS handler stores; the others ({@code KafkaSettings}, {@code RedisSettings}, + * {@code TimestreamSettings}) name no role. + */ + private static final Map> DMS_SETTINGS_ROLES = Map.ofEntries( + Map.entry("DynamoDbSettings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("S3Settings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("DmsTransferSettings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("KinesisSettings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("ElasticsearchSettings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("NeptuneSettings", List.of(DMS_SERVICE_ACCESS_ROLE)), + Map.entry("RedshiftSettings", List.of(DMS_SERVICE_ACCESS_ROLE, DMS_SECRETS_ROLE)), + Map.entry("PostgreSQLSettings", List.of(DMS_SERVICE_ACCESS_ROLE, DMS_SECRETS_ROLE)), + Map.entry("MySQLSettings", List.of(DMS_SERVICE_ACCESS_ROLE, DMS_SECRETS_ROLE)), + Map.entry("MongoDbSettings", List.of(DMS_SECRETS_ROLE)), + Map.entry("OracleSettings", List.of(DMS_SECRETS_ROLE, "SecretsManagerOracleAsmAccessRoleArn")), + Map.entry("SybaseSettings", List.of(DMS_SECRETS_ROLE)), + Map.entry("MicrosoftSQLServerSettings", List.of(DMS_SECRETS_ROLE)), + Map.entry("IBMDb2Settings", List.of(DMS_SECRETS_ROLE)), + Map.entry("DocDbSettings", List.of(DMS_SECRETS_ROLE)), + Map.entry("GcpMySQLSettings", List.of(DMS_SECRETS_ROLE))); private static final String STACK_SET_ADMINISTRATION_ROLE = "AWSCloudFormationStackSetAdministrationRole"; private static final String SERVICE_MANAGED = "SERVICE_MANAGED"; @@ -208,6 +256,18 @@ final class PassedRoles { case "redshift-serverless:CreateNamespace", "redshift-serverless:UpdateNamespace" -> namespaceRoles(ctx); case "redshift-serverless:CreateScheduledAction", "redshift-serverless:UpdateScheduledAction" -> fromJson(ctx, REDSHIFT_SERVERLESS, "roleArn"); + case "rds:AddRoleToDBCluster", "rds:AddRoleToDBInstance", "rds:RemoveRoleFromDBCluster", + "rds:RemoveRoleFromDBInstance", "rds:CreateDBProxy", "rds:ModifyDBProxy" -> + fromForm(ctx, RDS, "RoleArn"); + case "rds:StartExportTask" -> fromForm(ctx, RDS, "IamRoleArn"); + case "rds:CreateDBInstance", "rds:CreateDBInstanceReadReplica", "rds:ModifyDBInstance", + "rds:RestoreDBInstanceFromDBSnapshot", "rds:RestoreDBInstanceToPointInTime", "rds:CreateDBCluster", + "rds:ModifyDBCluster", "rds:RestoreDBClusterFromSnapshot", "rds:RestoreDBClusterToPointInTime" -> + rdsDatabaseRoles(action, ctx, lookups); + case "rds:ModifyOptionGroup" -> optionGroupRoles(ctx); + case "dms:CreateEndpoint", "dms:ModifyEndpoint" -> dmsEndpointRoles(jsonBody(ctx)); + case "dms:CreateReplicationInstance", "dms:ModifyReplicationInstance" -> + fromJson(ctx, DMS, "KerberosAuthenticationSettings", "KeyCacheSecretIamArn"); default -> List.of(); }; } @@ -432,6 +492,72 @@ final class PassedRoles { return passed(roles, REDSHIFT_SERVERLESS); } + /** + * The roles an RDS instance or cluster request names: the directory role {@code DomainIAMRoleName} + * names without its path, which is checked on the ARN IAM holds it under as + * {@link #instanceProfileRole} checks a role name; and the {@code MonitoringRoleArn} and the + * {@code AssociatedRoles} where the operation takes them, every associated role at any index, as the RDS + * handler reads them. + */ + private static List rdsDatabaseRoles(String action, ContainerRequestContext ctx, Lookups lookups) { + Map form = IamConditionContextResolver.formParameters(ctx); + List roles = new ArrayList<>(); + String directoryRole = blankToNull(form.get("DomainIAMRoleName")); + if (directoryRole != null) { + roles.add(lookups.heldRoleArn(directoryRole).orElseGet(() -> lookups.roleArn(directoryRole))); + } + if (RDS_MONITORING_ROLE.contains(action)) { + roles.add(form.get("MonitoringRoleArn")); + } + if (RDS_ASSOCIATED_ROLES.contains(action)) { + form.forEach((key, value) -> { + if (ASSOCIATED_ROLE.matcher(key).matches()) { + roles.add(value); + } + }); + } + return passed(roles, RDS); + } + + /** + * The role each option of a ModifyOptionGroup request takes in its {@code IAM_ROLE_ARN} setting, read + * as the RDS handler reads {@code OptionsToInclude}: in either list form, each option from 1 up to the + * first with no {@code OptionName}, each setting up to the first with no {@code Name}, and of an option + * naming the setting twice the last. + */ + private static List optionGroupRoles(ContainerRequestContext ctx) { + Map form = IamConditionContextResolver.formParameters(ctx); + List roles = new ArrayList<>(); + for (String options : List.of("OptionsToInclude.OptionConfiguration.", "OptionsToInclude.member.")) { + for (int option = 1; form.get(options + option + ".OptionName") != null; option++) { + String role = null; + for (String settings : List.of(".OptionSettings.OptionSetting.", ".OptionSettings.member.")) { + String setting = options + option + settings; + for (int index = 1; form.get(setting + index + ".Name") != null; index++) { + if (OPTION_ROLE_SETTING.equals(form.get(setting + index + ".Name"))) { + role = form.get(setting + index + ".Value"); + } + } + } + roles.add(role); + } + } + return passed(roles, RDS); + } + + /** A DMS endpoint's service access role, and the role members of each engine's settings structure it sets. */ + private static List dmsEndpointRoles(JsonNode body) { + List roles = new ArrayList<>(values(body, DMS_SERVICE_ACCESS_ROLE)); + if (body != null) { + for (Map.Entry member : body.properties()) { + for (String role : DMS_SETTINGS_ROLES.getOrDefault(member.getKey(), List.of())) { + roles.addAll(values(member.getValue(), role)); + } + } + } + return passed(roles, DMS); + } + private static List fromJson(ContainerRequestContext ctx, String service, String... path) { return passed(values(jsonBody(ctx), path), service); } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PassedRolesTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PassedRolesTest.java index c2c17bc7c0..7869300c59 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PassedRolesTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PassedRolesTest.java @@ -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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamPassRoleEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamPassRoleEnforcementIntegrationTest.java index ecefb83a44..e2dd20ba38 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamPassRoleEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamPassRoleEnforcementIntegrationTest.java @@ -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> 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); }