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

Use the provisioning context region for RDS and DocumentDB target lookups.
This commit is contained in:
Chris Jovanovski authored and GitHub committed 2026-09-17 18:36:48 -05:00
1 parent 573094720d
commit cf235cfb81
3 files changed
+47 -41

No files matched your search

@@ -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))