Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): resolve SecretTargetAttachment targets in the stack region (#3740)

Chris Jovanovskicf235cfb8182
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SecretTargetAttachmentCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SecretTargetAttachmentCfnProvisioner.java
index 67a2e91c03..2db6fce57d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SecretTargetAttachmentCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SecretTargetAttachmentCfnProvisioner.java
@@ -109,7 +109,7 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
String targetId = requireSecretTargetProperty(props, "TargetId", ctx);
String targetType = requireSecretTargetProperty(props, "TargetType", ctx);
validateSecretTargetType(targetType);
- SecretTargetConnection connection = resolveSecretTargetConnection(targetType, targetId);
+ SecretTargetConnection connection = resolveSecretTargetConnection(targetType, targetId, region);
String previousSecretId = r.getPhysicalId();
String previousManagedKeys = r.getAttributes().get(SECRET_TARGET_MANAGED_KEYS_ATTR);
@@ -246,18 +246,18 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
"SecretString for AWS::SecretsManager::SecretTargetAttachment must be a JSON object.", 400);
}
- private SecretTargetConnection resolveSecretTargetConnection(String targetType, String targetId) {
+ private SecretTargetConnection resolveSecretTargetConnection(String targetType, String targetId, String region) {
return switch (targetType) {
- case "AWS::RDS::DBInstance" -> dbInstanceConnection(targetId);
- case "AWS::RDS::DBCluster" -> dbClusterConnection(targetId);
- case "AWS::DocDB::DBInstance" -> docDbInstanceConnection(targetId);
- case "AWS::DocDB::DBCluster" -> docDbClusterConnection(targetId);
+ case "AWS::RDS::DBInstance" -> dbInstanceConnection(targetId, region);
+ case "AWS::RDS::DBCluster" -> dbClusterConnection(targetId, region);
+ case "AWS::DocDB::DBInstance" -> docDbInstanceConnection(targetId, region);
+ case "AWS::DocDB::DBCluster" -> docDbClusterConnection(targetId, region);
default -> throw new IllegalStateException("Validated target type was not handled: " + targetType);
};
}
- private SecretTargetConnection dbInstanceConnection(String targetId) {
- DbInstance instance = rdsService.getDbInstance(targetId);
+ private SecretTargetConnection dbInstanceConnection(String targetId, String region) {
+ DbInstance instance = rdsService.getDbInstance(targetId, region);
if (instance == null || instance.getEngine() == null || instance.getEndpoint() == null
|| instance.getEndpoint().address() == null
|| instance.getEndpoint().address().isBlank()
@@ -275,8 +275,8 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
instance.getDbInstanceIdentifier());
}
- private SecretTargetConnection dbClusterConnection(String targetId) {
- DbCluster cluster = rdsService.getDbCluster(targetId);
+ private SecretTargetConnection dbClusterConnection(String targetId, String region) {
+ DbCluster cluster = rdsService.getDbCluster(targetId, region);
if (cluster == null || cluster.getEngine() == null || cluster.getEndpoint() == null
|| cluster.getEndpoint().address() == null
|| cluster.getEndpoint().address().isBlank()
@@ -294,8 +294,8 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
cluster.getDbClusterIdentifier());
}
- private SecretTargetConnection docDbInstanceConnection(String targetId) {
- DocDbInstance instance = docDbService.getDbInstance(targetId);
+ private SecretTargetConnection docDbInstanceConnection(String targetId, String region) {
+ DocDbInstance instance = docDbService.getDbInstance(targetId, region);
if (instance == null || instance.getEndpoint() == null
|| instance.getEndpoint().isBlank()
|| instance.getPort() <= 0
@@ -312,8 +312,8 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
instance.getDbInstanceIdentifier());
}
- private SecretTargetConnection docDbClusterConnection(String targetId) {
- DocDbCluster cluster = docDbService.getDbCluster(targetId);
+ private SecretTargetConnection docDbClusterConnection(String targetId, String region) {
+ DocDbCluster cluster = docDbService.getDbCluster(targetId, region);
if (cluster == null || cluster.getEndpoint() == null
|| cluster.getEndpoint().isBlank()
|| cluster.getPort() <= 0
diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java
index 6ec1a63077..ccdb16ae14 100644
--- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java
@@ -457,7 +457,10 @@ public class DocDbService {
}
public DocDbCluster getDbCluster(String id) {
- String region = regionResolver.getRegion();
+ return getDbCluster(id, regionResolver.getRegion());
+ }
+
+ public DocDbCluster getDbCluster(String id, String region) {
DocDbCluster cluster = findCluster(region, id).orElseThrow(() ->
new AwsException("DBClusterNotFoundFault",
"DocDB cluster " + id + " not found.", 404));
@@ -706,7 +709,10 @@ public class DocDbService {
}
public DocDbInstance getDbInstance(String id) {
- String region = regionResolver.getRegion();
+ return getDbInstance(id, regionResolver.getRegion());
+ }
+
+ public DocDbInstance getDbInstance(String id, String region) {
DocDbInstance instance = findInstance(region, id).orElseThrow(() ->
new AwsException("DBInstanceNotFound",
"DocDB instance " + id + " not found.", 404));
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SecretTargetAttachmentCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SecretTargetAttachmentCfnProvisionerTest.java
index f6d85a119a..f44e212c22 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SecretTargetAttachmentCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SecretTargetAttachmentCfnProvisionerTest.java
@@ -45,10 +45,10 @@ import static org.mockito.Mockito.when;
class SecretTargetAttachmentCfnProvisionerTest {
- private static final String REGION = "us-east-1";
+ private static final String REGION = "eu-west-1";
private static final String SECRET_ID = "database-secret";
private static final String SECRET_ARN =
- "arn:aws:secretsmanager:us-east-1:000000000000:secret:database-secret-a1b2c3";
+ "arn:aws:secretsmanager:eu-west-1:000000000000:secret:database-secret-a1b2c3";
private final ObjectMapper mapper = new ObjectMapper();
private SecretsManagerService secretsManagerService;
@@ -79,7 +79,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
"{\"username\":\"admin\",\"password\":\"secret\",\"custom\":\"keep\","
+ "\"engine\":\"postgres\",\"host\":\"db.local\",\"port\":5432,"
+ "\"dbname\":\"app\",\"dbInstanceIdentifier\":\"database\"}");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
StackResource resource = provision(instanceProperties());
assertEquals("CREATE_COMPLETE", resource.getStatus());
@@ -127,7 +127,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
cluster.setEngine(DatabaseEngine.MYSQL);
cluster.setEndpoint(new DbEndpoint("cluster.local", 3306));
cluster.setDatabaseName("orders");
- when(rdsService.getDbCluster("cluster")).thenReturn(cluster);
+ when(rdsService.getDbCluster("cluster", REGION)).thenReturn(cluster);
StackResource resource = provision(properties("AWS::RDS::DBCluster", "cluster"));
@@ -151,7 +151,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
instance.setDbInstanceIdentifier("document-instance");
instance.setEndpoint("document.local");
instance.setPort(27017);
- when(docDbService.getDbInstance("document-instance")).thenReturn(instance);
+ when(docDbService.getDbInstance("document-instance", REGION)).thenReturn(instance);
StackResource resource = provision(
properties("AWS::DocDB::DBInstance", "document-instance"));
@@ -176,7 +176,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
cluster.setDbClusterIdentifier("document-cluster");
cluster.setEndpoint("document-cluster.local");
cluster.setPort(27017);
- when(docDbService.getDbCluster("document-cluster")).thenReturn(cluster);
+ when(docDbService.getDbCluster("document-cluster", REGION)).thenReturn(cluster);
StackResource resource = provision(
properties("AWS::DocDB::DBCluster", "document-cluster"));
@@ -211,7 +211,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void missingSecretFailsInsteadOfUsingTheUnresolvedId() {
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret(SECRET_ID, REGION))
.thenThrow(new AwsException("ResourceNotFoundException", "missing", 400));
@@ -230,7 +230,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
stubSecretValues("{\"username\":\"admin\",\"password\":\"secret\","
+ "\"engine\":\"postgres\",\"host\":\"db.local\",\"port\":5432,"
+ "\"dbname\":\"app\",\"dbInstanceIdentifier\":\"database\"}");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
StackResource resource = provision(instanceProperties());
@@ -250,7 +250,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
cluster.setEngine(DatabaseEngine.POSTGRES);
cluster.setEndpoint(new DbEndpoint("replacement.local", 6432));
cluster.setDatabaseName("replacement");
- when(rdsService.getDbCluster("replacement-cluster")).thenReturn(cluster);
+ when(rdsService.getDbCluster("replacement-cluster", REGION)).thenReturn(cluster);
StackResource resource = provision(
properties("AWS::RDS::DBCluster", "replacement-cluster"),
@@ -282,7 +282,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
stubSecretValues("{\"username\":\"admin\",\"password\":\"secret\","
+ "\"engine\":\"postgres\",\"host\":\"db.local\",\"port\":5432,"
+ "\"dbname\":\"app\",\"dbInstanceIdentifier\":\"database\"}");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
StackResource resource = provision(instanceProperties(), SECRET_ID,
Map.of("__FlociSecretTargetManagedKeys",
@@ -297,7 +297,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void failedLegacyUpdateReleasesTheClaimCreatedByThatAttempt() {
stubSecretValues("not-json");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
StackResource resource = provision(
instanceProperties(),
@@ -313,7 +313,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void missingTargetFailsBeforeReadingTheSecret() {
- when(rdsService.getDbInstance("database"))
+ when(rdsService.getDbInstance("database", REGION))
.thenThrow(new AwsException("DBInstanceNotFound", "database is missing", 404));
StackResource resource = provision(instanceProperties());
@@ -327,7 +327,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
void incompleteTargetFailsBeforeReadingTheSecret() {
DbInstance instance = dbInstance();
instance.setEndpoint(null);
- when(rdsService.getDbInstance("database")).thenReturn(instance);
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(instance);
StackResource resource = provision(instanceProperties());
@@ -338,11 +338,11 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void replacingTheSecretAttachesTheNewSecretBeforeDetachingTheOldSecret() throws Exception {
- String oldArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:old-a1b2c3";
- String newArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:new-d4e5f6";
+ String oldArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:old-a1b2c3";
+ String newArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:new-d4e5f6";
ObjectNode properties = instanceProperties();
properties.put("SecretId", "new-secret");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret("new-secret", REGION)).thenReturn(secret(newArn));
when(secretsManagerService.describeSecret(oldArn, REGION)).thenReturn(secret(oldArn));
when(secretsManagerService.getSecretValue(newArn, null, null, REGION))
@@ -380,11 +380,11 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void invalidOldSecretIsTreatedAsDetachedDuringReplacement() {
- String oldArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:old-a1b2c3";
- String newArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:new-d4e5f6";
+ String oldArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:old-a1b2c3";
+ String newArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:new-d4e5f6";
ObjectNode properties = instanceProperties();
properties.put("SecretId", "new-secret");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret("new-secret", REGION)).thenReturn(secret(newArn));
when(secretsManagerService.describeSecret(oldArn, REGION)).thenReturn(secret(oldArn));
when(secretsManagerService.getSecretValue(newArn, null, null, REGION))
@@ -405,12 +405,12 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void failedPreviousDetachRestoresTheNewSecretAndReleasesItsClaim() throws Exception {
- String oldArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:old-a1b2c3";
- String newArn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:new-d4e5f6";
+ String oldArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:old-a1b2c3";
+ String newArn = "arn:aws:secretsmanager:eu-west-1:000000000000:secret:new-d4e5f6";
String newOriginal = "{\"username\":\"new-user\",\"password\":\"new-password\"}";
ObjectNode properties = instanceProperties();
properties.put("SecretId", "new-secret");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret("new-secret", REGION)).thenReturn(secret(newArn));
when(secretsManagerService.describeSecret(oldArn, REGION)).thenReturn(secret(oldArn));
when(secretsManagerService.getSecretValue(newArn, null, null, REGION))
@@ -537,7 +537,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
@ValueSource(strings = {"not-json", "\"plain-text\"", "[]"})
void secretValueMustBeAJsonObject(String secretString) {
stubSecretValues(secretString);
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
StackResource resource = provision(instanceProperties());
@@ -551,7 +551,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
void binarySecretStillFailsProvisioning() {
SecretVersion binary = new SecretVersion();
binary.setSecretBinary("AQID");
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret(SECRET_ID, REGION)).thenReturn(secret(SECRET_ARN));
when(secretsManagerService.getSecretValue(SECRET_ARN, null, null, REGION))
.thenReturn(binary);
@@ -583,7 +583,7 @@ class SecretTargetAttachmentCfnProvisionerTest {
@Test
void aSecondAttachmentToTheSameSecretFailsBeforeReadingOrWritingItsValue() {
- when(rdsService.getDbInstance("database")).thenReturn(dbInstance());
+ when(rdsService.getDbInstance("database", REGION)).thenReturn(dbInstance());
when(secretsManagerService.describeSecret(SECRET_ID, REGION)).thenReturn(secret(SECRET_ARN));
when(secretsManagerService.claimTargetAttachment(
SECRET_ARN, "stack/Attachment", REGION))