mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
feat(rds): IAM role associations for DB clusters and instances (#5367)
* feat(rds): IAM role associations for DB clusters and instances AddRoleToDBCluster, RemoveRoleFromDBCluster, AddRoleToDBInstance and RemoveRoleFromDBInstance answered UnsupportedOperation, and DBCluster and DBInstance carried no AssociatedRoles, so Terraform's aws_rds_cluster_role_association and aws_db_instance_role_association could not apply. Clusters and instances now keep their associated roles and report them as AssociatedRoles, ACTIVE as soon as they are added. The four actions raise the faults the API model lists, and CreateDBCluster accepts AssociatedRoles, checked before the cluster is created. Feature names are not yet validated against the engine. Closes #5343 * fix(rds): route the instance role actions, and refuse only a repeated cluster role AddRoleToDBInstance and RemoveRoleFromDBInstance were missing from RDS_ACTIONS, so a request with no credential scope reached SQS. DBClusterRoleAlreadyExists covers the role ARN only, so a cluster now takes a second role for a feature that already has one. Instances keep refusing a repeated role or feature, as their fault documents. CreateDBCluster reads every AssociatedRoles index, gaps included, and refuses an entry without a RoleArn. It associates the roles in one step and deletes the new cluster if that is refused. * refactor(rds): import the types parseAssociatedRoles uses The regex and sorted-set types were written with their packages inline; AGENTS.md asks for imports. The role association compat test gains a DisplayName like the others in its class. * docs(rds): count the role association actions alongside the catalogue reads #5196 and this branch each added four RDS actions, so the service matrix count is 98.
This commit is contained in:
1 parent
a017c94ecc
commit
5e48cbb80b
12 files changed
+731
-2
No files matched your search
@@ -732,6 +732,29 @@ class RdsQueryHandlerTest {
|
||||
assertFalse(body.contains("<MasterUserSecret>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbClusterDeletesTheClusterWhenItsRolesAreRefused() {
|
||||
DbCluster cluster = makeCluster("mycluster");
|
||||
when(service.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
|
||||
any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(),
|
||||
any(), anyBoolean()))
|
||||
.thenReturn(cluster);
|
||||
// Another request associated the role between the create and the roles being added.
|
||||
when(service.addRolesToNewDbCluster(any(), eq("mycluster"), anyList()))
|
||||
.thenThrow(new AwsException("DBClusterRoleAlreadyExists", "already associated", 400));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBClusterIdentifier", "mycluster");
|
||||
p.add("Engine", "aurora-postgresql");
|
||||
p.add("MasterUsername", "admin");
|
||||
p.add("AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", "arn:aws:iam::000000000000:role/s3");
|
||||
Response response = handler.handle("CreateDBCluster", p);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("<Code>DBClusterRoleAlreadyExists</Code>"));
|
||||
verify(service).deleteDbCluster(eq("mycluster"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbClusterPassesManagedMasterUserSecretOptions() {
|
||||
DbCluster cluster = makeCluster("mycluster");
|
||||
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.restassured.http.ContentType.URLENC;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* IAM role associations over the Query protocol: add and remove for DB clusters and DB instances,
|
||||
* the {@code AssociatedRoles} the describe calls return, {@code CreateDBCluster}'s own
|
||||
* {@code AssociatedRoles}, and the API reference's errors.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class RdsRoleAssociationIntegrationTest {
|
||||
|
||||
private static final String CLUSTER = "roles-cluster";
|
||||
private static final String INSTANCE = "roles-instance";
|
||||
private static final String S3_ROLE = "arn:aws:iam::000000000000:role/rds-s3";
|
||||
private static final String LAMBDA_ROLE = "arn:aws:iam::000000000000:role/rds-lambda";
|
||||
private static final String CLUSTER_ROLES =
|
||||
"DescribeDBClustersResponse.DescribeDBClustersResult.DBClusters.DBCluster.AssociatedRoles.DBClusterRole";
|
||||
private static final String INSTANCE_ROLES =
|
||||
"DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.AssociatedRoles.DBInstanceRole";
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261009/us-east-1/rds/aws4_request, "
|
||||
+ "SignedHeaders=content-type;host, Signature=test";
|
||||
|
||||
private static RequestSpecification query(String action) {
|
||||
return given().header("Authorization", AUTH)
|
||||
.contentType(URLENC)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
|
||||
private static RequestSpecification clusterRole(String action, String roleArn) {
|
||||
return query(action).formParam("DBClusterIdentifier", CLUSTER).formParam("RoleArn", roleArn);
|
||||
}
|
||||
|
||||
private static RequestSpecification instanceRole(String action, String roleArn, String feature) {
|
||||
return query(action).formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.formParam("RoleArn", roleArn).formParam("FeatureName", feature);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createsTheResources() {
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", CLUSTER)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<AssociatedRoles></AssociatedRoles>"));
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.formParam("Engine", "postgres")
|
||||
.formParam("DBInstanceClass", "db.t3.micro")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("AllocatedStorage", "20")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void addsRolesToAClusterAndDescribesThem() {
|
||||
clusterRole("AddRoleToDBCluster", S3_ROLE).formParam("FeatureName", "s3Import")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<AddRoleToDBClusterResponse"));
|
||||
// The feature name is optional for a cluster.
|
||||
clusterRole("AddRoleToDBCluster", LAMBDA_ROLE)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
query("DescribeDBClusters").formParam("DBClusterIdentifier", CLUSTER)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(CLUSTER_ROLES + "[0].RoleArn", equalTo(S3_ROLE))
|
||||
.body(CLUSTER_ROLES + "[0].FeatureName", equalTo("s3Import"))
|
||||
.body(CLUSTER_ROLES + "[0].Status", equalTo("ACTIVE"))
|
||||
.body(CLUSTER_ROLES + "[1].RoleArn", equalTo(LAMBDA_ROLE))
|
||||
.body(CLUSTER_ROLES + "[1].Status", equalTo("ACTIVE"))
|
||||
.body(not(containsString("<FeatureName></FeatureName>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void refusesAClusterRoleAlreadyAssociated() {
|
||||
clusterRole("AddRoleToDBCluster", S3_ROLE).formParam("FeatureName", "s3Export")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBClusterRoleAlreadyExists"));
|
||||
// DBClusterRoleAlreadyExists covers the role ARN only, so another role for the same feature is taken.
|
||||
String other = "arn:aws:iam::000000000000:role/other";
|
||||
clusterRole("AddRoleToDBCluster", other).formParam("FeatureName", "s3Import")
|
||||
.when().post("/").then().statusCode(200);
|
||||
clusterRole("RemoveRoleFromDBCluster", other)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void removesAClusterRoleAndRefusesOneNotAssociated() {
|
||||
// A feature name that does not match the association is not that association.
|
||||
clusterRole("RemoveRoleFromDBCluster", S3_ROLE).formParam("FeatureName", "Lambda")
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBClusterRoleNotFound"));
|
||||
|
||||
clusterRole("RemoveRoleFromDBCluster", S3_ROLE)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<RemoveRoleFromDBClusterResponse"));
|
||||
clusterRole("RemoveRoleFromDBCluster", S3_ROLE)
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBClusterRoleNotFound"));
|
||||
|
||||
query("DescribeDBClusters").formParam("DBClusterIdentifier", CLUSTER)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(CLUSTER_ROLES + ".RoleArn", equalTo(LAMBDA_ROLE))
|
||||
.body(not(containsString(S3_ROLE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void addsAndRemovesInstanceRolesByFeature() {
|
||||
instanceRole("AddRoleToDBInstance", S3_ROLE, "s3Import")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<AddRoleToDBInstanceResponse"));
|
||||
instanceRole("AddRoleToDBInstance", LAMBDA_ROLE, "Lambda")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
query("DescribeDBInstances").formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(INSTANCE_ROLES + "[0].RoleArn", equalTo(S3_ROLE))
|
||||
.body(INSTANCE_ROLES + "[0].FeatureName", equalTo("s3Import"))
|
||||
.body(INSTANCE_ROLES + "[0].Status", equalTo("ACTIVE"))
|
||||
.body(INSTANCE_ROLES + "[1].FeatureName", equalTo("Lambda"));
|
||||
|
||||
// The same role for another feature, or another role for the same feature.
|
||||
instanceRole("AddRoleToDBInstance", S3_ROLE, "s3Export")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBInstanceRoleAlreadyExists"));
|
||||
instanceRole("AddRoleToDBInstance", "arn:aws:iam::000000000000:role/other", "Lambda")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBInstanceRoleAlreadyExists"));
|
||||
|
||||
instanceRole("RemoveRoleFromDBInstance", S3_ROLE, "Lambda")
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBInstanceRoleNotFound"));
|
||||
instanceRole("RemoveRoleFromDBInstance", S3_ROLE, "s3Import")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<RemoveRoleFromDBInstanceResponse"));
|
||||
|
||||
query("DescribeDBInstances").formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(INSTANCE_ROLES + ".RoleArn", equalTo(LAMBDA_ROLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void requiresTheParametersTheModelRequires() {
|
||||
query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", INSTANCE).formParam("RoleArn", S3_ROLE)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MissingParameter"));
|
||||
query("AddRoleToDBCluster").formParam("DBClusterIdentifier", CLUSTER)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MissingParameter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void refusesUnknownResources() {
|
||||
query("AddRoleToDBCluster").formParam("DBClusterIdentifier", "no-such-cluster").formParam("RoleArn", S3_ROLE)
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBClusterNotFoundFault"));
|
||||
query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", "no-such-instance")
|
||||
.formParam("RoleArn", S3_ROLE).formParam("FeatureName", "s3Import")
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBInstanceNotFound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void refusesAStoppedResource() {
|
||||
query("StopDBInstance").formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.when().post("/").then().statusCode(200);
|
||||
instanceRole("AddRoleToDBInstance", S3_ROLE, "s3Import")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidDBInstanceState"));
|
||||
query("StartDBInstance").formParam("DBInstanceIdentifier", INSTANCE)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
query("StopDBCluster").formParam("DBClusterIdentifier", CLUSTER)
|
||||
.when().post("/").then().statusCode(200);
|
||||
clusterRole("AddRoleToDBCluster", S3_ROLE)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidDBClusterStateFault"));
|
||||
query("StartDBCluster").formParam("DBClusterIdentifier", CLUSTER)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void createDbClusterAssociatesTheRolesItNames() {
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", "roles-at-create")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", S3_ROLE)
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.1.FeatureName", "s3Import")
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.2.RoleArn", LAMBDA_ROLE)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.AssociatedRoles.DBClusterRole[0].RoleArn",
|
||||
equalTo(S3_ROLE))
|
||||
.body("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.AssociatedRoles.DBClusterRole[1].RoleArn",
|
||||
equalTo(LAMBDA_ROLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void createDbClusterNamingARoleTwiceCreatesNothing() {
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", "roles-duplicate")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("AssociatedRoles.member.1.RoleArn", S3_ROLE)
|
||||
.formParam("AssociatedRoles.member.2.RoleArn", S3_ROLE)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("DBClusterRoleAlreadyExists"));
|
||||
query("DescribeDBClusters").formParam("DBClusterIdentifier", "roles-duplicate")
|
||||
.when().post("/").then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void createDbClusterRefusesAnEntryWithoutARoleArn() {
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", "roles-no-arn")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.1.FeatureName", "s3Import")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MissingParameter"));
|
||||
query("DescribeDBClusters").formParam("DBClusterIdentifier", "roles-no-arn")
|
||||
.when().post("/").then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void createDbClusterReadsEveryEntryPastAGap() {
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", "roles-gap")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", S3_ROLE)
|
||||
.formParam("AssociatedRoles.DBClusterAssociatedRole.3.RoleArn", LAMBDA_ROLE)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.AssociatedRoles.DBClusterRole.RoleArn",
|
||||
contains(S3_ROLE, LAMBDA_ROLE));
|
||||
}
|
||||
}
|
||||
+50
@@ -5,11 +5,14 @@ import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DbClusterPersistenceTest {
|
||||
@@ -39,4 +42,51 @@ class DbClusterPersistenceTest {
|
||||
assertEquals(16.0, restored.get().getServerlessV2MaxCapacity());
|
||||
assertEquals(600, restored.get().getServerlessV2SecondsUntilAutoPause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void associatedRolesPersistAcrossInstances(@TempDir Path directory) {
|
||||
DbCluster cluster = new DbCluster();
|
||||
cluster.setDbClusterIdentifier("roles-cluster");
|
||||
cluster.getAssociatedRoles().add(new DbRoleAssociation("arn:aws:iam::000000000000:role/a", "s3Import"));
|
||||
cluster.getAssociatedRoles().add(new DbRoleAssociation("arn:aws:iam::000000000000:role/b", null));
|
||||
DbInstance instance = new DbInstance();
|
||||
instance.setDbInstanceIdentifier("roles-instance");
|
||||
instance.getAssociatedRoles().add(new DbRoleAssociation("arn:aws:iam::000000000000:role/c", "Lambda"));
|
||||
|
||||
Path clusters = directory.resolve("rds-clusters.json");
|
||||
new PersistentStorage<String, DbCluster>(clusters, new TypeReference<Map<String, DbCluster>>() {})
|
||||
.put(cluster.getDbClusterIdentifier(), cluster);
|
||||
Path instances = directory.resolve("rds-instances.json");
|
||||
new PersistentStorage<String, DbInstance>(instances, new TypeReference<Map<String, DbInstance>>() {})
|
||||
.put(instance.getDbInstanceIdentifier(), instance);
|
||||
|
||||
PersistentStorage<String, DbCluster> clusterReader = new PersistentStorage<>(
|
||||
clusters, new TypeReference<Map<String, DbCluster>>() {});
|
||||
clusterReader.load();
|
||||
List<DbRoleAssociation> clusterRoles = clusterReader.get("roles-cluster").orElseThrow().getAssociatedRoles();
|
||||
assertEquals(2, clusterRoles.size());
|
||||
assertEquals("arn:aws:iam::000000000000:role/a", clusterRoles.get(0).getRoleArn());
|
||||
assertEquals("s3Import", clusterRoles.get(0).getFeatureName());
|
||||
assertEquals(DbRoleAssociation.ACTIVE, clusterRoles.get(0).getStatus());
|
||||
assertNull(clusterRoles.get(1).getFeatureName());
|
||||
|
||||
PersistentStorage<String, DbInstance> instanceReader = new PersistentStorage<>(
|
||||
instances, new TypeReference<Map<String, DbInstance>>() {});
|
||||
instanceReader.load();
|
||||
List<DbRoleAssociation> instanceRoles = instanceReader.get("roles-instance").orElseThrow().getAssociatedRoles();
|
||||
assertEquals(1, instanceRoles.size());
|
||||
assertEquals("Lambda", instanceRoles.get(0).getFeatureName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateSavedBeforeRoleAssociationsLoadsWithNone(@TempDir Path directory) throws Exception {
|
||||
Path file = directory.resolve("rds-clusters.json");
|
||||
Files.writeString(file, "{\"legacy-cluster\":{\"dbClusterIdentifier\":\"legacy-cluster\"}}");
|
||||
|
||||
PersistentStorage<String, DbCluster> reader = new PersistentStorage<>(
|
||||
file, new TypeReference<Map<String, DbCluster>>() {});
|
||||
reader.load();
|
||||
|
||||
assertTrue(reader.get("legacy-cluster").orElseThrow().getAssociatedRoles().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user