diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java index ea4a944e5b..50be9c0d3d 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java @@ -275,10 +275,15 @@ public class RegionResolver { * {@link #buildGlobalArn(String, String)}. */ public String buildArn(String service, String region, String resource) { + return buildArn(service, region, getAccountId(), resource); + } + + /** Mints an ARN for an explicit owner, including during background restoration. */ + public String buildArn(String service, String region, String accountId, String resource) { if (region == null || region.isBlank()) { - return buildGlobalArn(service, resource); + return buildGlobalArn(service, accountId, resource); } - return new AwsArnUtils.Arn(partitionForRegion(region), service, region, getAccountId(), resource).toString(); + return new AwsArnUtils.Arn(partitionForRegion(region), service, region, accountId, resource).toString(); } /** Mints a regionless ARN ({@code arn::iam:::...}) in the request's partition. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index 1b54b122be..c69241a7fe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -2283,6 +2283,9 @@ public class RdsService implements Resettable, ResourceProvider { if (!"rds".equals(arn.service())) { throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400); } + if (!regionResolver.partitionForRegion(effectiveRegion).equals(arn.partition())) { + throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400); + } if (!effectiveRegion.equals(arn.region())) { throw new AwsException("InvalidParameterValue", "ResourceName is not in region " + effectiveRegion + ": " + resourceName, 400); @@ -7103,7 +7106,7 @@ public class RdsService implements Resettable, ResourceProvider { } try { AwsArnUtils.Arn arn = AwsArnUtils.parse(targetGroup.getTargetGroupArn()); - return AwsRegions.partitionFor(region).equals(arn.partition()) + return regionResolver.partitionForRegion(region).equals(arn.partition()) && "rds".equals(arn.service()) && Objects.equals(accountId, arn.accountId()) && Objects.equals(region, arn.region()) @@ -8017,9 +8020,8 @@ public class RdsService implements Resettable, ResourceProvider { targetGroup = new DbProxyTargetGroup(); targetGroup.setDbProxyName(proxy.getDbProxyName()); targetGroup.setTargetGroupName("default"); - targetGroup.setTargetGroupArn(AwsArnUtils.Arn.of("rds", - region, - accountId, "target-group:prx-tg-" + randomResourceSuffix()).toString()); + targetGroup.setTargetGroupArn(regionResolver.buildArn("rds", region, + accountId, "target-group:prx-tg-" + randomResourceSuffix())); targetGroup.setDefaultTargetGroup(true); targetGroup.setCreatedAt(now); targetGroup.setUpdatedAt(now); @@ -8587,7 +8589,7 @@ public class RdsService implements Resettable, ResourceProvider { String resourceType, String resourceId) { try { AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); - return AwsRegions.partitionFor(region).equals(parsed.partition()) + return regionResolver.partitionForRegion(region).equals(parsed.partition()) && "rds".equals(parsed.service()) && Objects.equals(accountId, parsed.accountId()) && Objects.equals(region, parsed.region()) @@ -9184,8 +9186,8 @@ public class RdsService implements Resettable, ResourceProvider { // The model documents the subscription as created but inactive when Enabled is false, and // says nothing about a default, so an omitted Enabled activates it as the console does. subscription.setEnabled(enabled == null || enabled); - subscription.setEventSubscriptionArn(AwsArnUtils.Arn.of("rds", region, accountId, - "es:" + subscriptionName).toString()); + subscription.setEventSubscriptionArn(regionResolver.buildArn("rds", region, + "es:" + subscriptionName)); subscription.setTags(tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags)); eventSubscriptions.put(key, subscription); return subscription; @@ -9297,6 +9299,7 @@ public class RdsService implements Resettable, ResourceProvider { } String prefix = eventSubscriptionKey(region, ""); List all = eventSubscriptions.scan(k -> k.startsWith(prefix)).stream() + .map(subscription -> normalizeEventSubscriptionArn(region, subscription)) .sorted(Comparator.comparing(EventSubscription::getCustSubscriptionId)) .toList(); // The marker is the last name of the previous page and the next page starts after it. @@ -9319,10 +9322,29 @@ public class RdsService implements Resettable, ResourceProvider { private EventSubscription requireEventSubscription(String region, String subscriptionName) { return eventSubscriptions.get(eventSubscriptionKey(region, subscriptionName)) + .map(subscription -> normalizeEventSubscriptionArn(region, subscription)) .orElseThrow(() -> new AwsException("SubscriptionNotFound", "Subscription " + subscriptionName + " not found.", 404)); } + private EventSubscription normalizeEventSubscriptionArn(String region, EventSubscription subscription) { + String accountId = subscription.getCustomerAwsId(); + String name = subscription.getCustSubscriptionId(); + if (accountId == null || name == null) { + return subscription; + } + String resource = "es:" + name; + String legacyArn = AwsArnUtils.Arn.of("rds", region, accountId, resource).toString(); + String expectedArn = regionResolver.buildArn("rds", region, accountId, resource); + // Earlier versions minted custom-region subscriptions with the catalog's fallback + // partition. Repair only that exact legacy identity, preserving its stored owner. + if (legacyArn.equals(subscription.getEventSubscriptionArn()) && !legacyArn.equals(expectedArn)) { + subscription.setEventSubscriptionArn(expectedArn); + eventSubscriptions.put(eventSubscriptionKey(region, name), subscription); + } + return subscription; + } + // ── Cluster endpoints ───────────────────────────────────────────────────── private static final int MAX_CUSTOM_ENDPOINTS_PER_CLUSTER = 5; diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java index 6423efb761..44aa114182 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java @@ -251,6 +251,20 @@ class RegionResolverTest { assertEquals("arn:aws:sqs:xx-nowhere-9:000000000000:q", resolver.buildArn("sqs", "xx-nowhere-9", "q")); } + @Test + void buildArnPreservesAnExplicitOwnerAcrossRegionalAndGlobalScopes() { + RegionResolver china = new RegionResolver("cn-north-1", "000000000000"); + assertEquals("arn:aws-cn:rds:xx-nowhere-9:222222222222:db:db1", + china.buildArn("rds", "xx-nowhere-9", "222222222222", "db:db1")); + assertEquals("arn:aws:rds:us-east-1:222222222222:db:db1", + china.buildArn("rds", "us-east-1", "222222222222", "db:db1")); + assertEquals("arn:aws-cn:iam::222222222222:role/r", + china.buildArn("iam", "", "222222222222", "role/r")); + assertEquals("arn:aws-cn:iam::222222222222:role/r", + china.buildArn("iam", null, "222222222222", "role/r")); + assertEquals("arn:aws-cn:s3:::bucket", china.buildArn("s3", "", "", "bucket")); + } + @Test void getPartitionOutsideARequestIsTheDeploymentPartition() { assertEquals("aws", resolver.getPartition()); diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java index 3c9d798b88..ddaeb38dc3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java @@ -1865,6 +1865,100 @@ class RdsServiceTest { assertEquals(Map.of("env", "test"), rdsService.listTagsForResource(targetGroupArn)); } + @Test + void tagOperationsUseDeploymentPartitionForUnknownRegions() { + regionResolver = new RegionResolver("cn-north-1", "123456789012"); + rdsService = newService(containerManager, proxyManager, + new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); + String region = "xx-nowhere-9"; + DbParameterGroup group = rdsService.createDbParameterGroup( + "custom-region-parameters", "postgres16", "test", region); + String arn = group.getDbParameterGroupArn(); + assertEquals("arn:aws-cn:rds:xx-nowhere-9:123456789012:pg:custom-region-parameters", arn); + assertEquals(arn, rdsService.getDbParameterGroup("custom-region-parameters", region) + .getDbParameterGroupArn()); + + rdsService.addTagsToResource(arn, Map.of("env", "original"), region); + assertEquals(Map.of("env", "original"), rdsService.listTagsForResource(arn, region)); + String wrongPartition = arn.replace("arn:aws-cn:", "arn:aws:"); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.listTagsForResource(wrongPartition, region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.addTagsToResource(wrongPartition, Map.of("env", "changed"), region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.removeTagsFromResource(wrongPartition, List.of("env"), region)).getErrorCode()); + assertEquals(Map.of("env", "original"), rdsService.listTagsForResource(arn, region)); + rdsService.removeTagsFromResource(arn, List.of("env"), region); + assertEquals(Map.of(), rdsService.listTagsForResource(arn, region)); + } + + @ParameterizedTest + @CsvSource({"cn-north-1", "xx-nowhere-9"}) + void chinaEventSubscriptionTagsUseTheDeploymentPartition(String region) { + regionResolver = new RegionResolver("cn-north-1", "123456789012"); + rdsService = newService(containerManager, proxyManager, + new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); + String arn = rdsService.createEventSubscription(region, "events", + regionResolver.buildArn("sns", region, "rds-events"), null, + List.of(), List.of(), true, Map.of("env", "original")).getEventSubscriptionArn(); + assertEquals("arn:aws-cn:rds:" + region + ":123456789012:es:events", arn); + assertEquals(Map.of("env", "original"), rdsService.listTagsForResource(arn, region)); + rdsService.addTagsToResource(arn, Map.of("env", "changed"), region); + assertEquals(Map.of("env", "changed"), rdsService.listTagsForResource(arn, region)); + String wrongPartition = arn.replace("arn:aws-cn:", "arn:aws:"); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.listTagsForResource(wrongPartition, region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.addTagsToResource(wrongPartition, Map.of("env", "wrong"), region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.removeTagsFromResource(wrongPartition, List.of("env"), region)).getErrorCode()); + assertEquals(Map.of("env", "changed"), rdsService.listTagsForResource(arn, region)); + rdsService.removeTagsFromResource(arn, List.of("env"), region); + assertEquals(Map.of(), rdsService.listTagsForResource(arn, region)); + } + + @ParameterizedTest + @CsvSource({"true", "false"}) + void describeRepairsLegacyCustomRegionSubscriptionArns(boolean named) { + regionResolver = new RegionResolver("cn-north-1", "222222222222"); + rdsService = newService(containerManager, proxyManager, + new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); + String region = "xx-nowhere-9"; + String arn = rdsService.createEventSubscription(region, "legacy-events", + regionResolver.buildArn("sns", region, "rds-events"), null, + List.of(), List.of(), true, Map.of("env", "original")).getEventSubscriptionArn(); + rdsService.describeEventSubscriptions(region, "legacy-events", null, null) + .subscriptions().getFirst().setEventSubscriptionArn(arn.replace("arn:aws-cn:", "arn:aws:")); + + String restoredArn = rdsService.describeEventSubscriptions( + region, named ? "legacy-events" : null, null, null) + .subscriptions().getFirst().getEventSubscriptionArn(); + + assertEquals("arn:aws-cn:rds:xx-nowhere-9:222222222222:es:legacy-events", restoredArn); + assertEquals(Map.of("env", "original"), rdsService.listTagsForResource(restoredArn, region)); + rdsService.addTagsToResource(restoredArn, Map.of("env", "changed"), region); + assertEquals(Map.of("env", "changed"), rdsService.listTagsForResource(restoredArn, region)); + rdsService.removeTagsFromResource(restoredArn, List.of("env"), region); + assertEquals(Map.of(), rdsService.listTagsForResource(restoredArn, region)); + String legacyArn = restoredArn.replace("arn:aws-cn:", "arn:aws:"); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.listTagsForResource(legacyArn, region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.addTagsToResource(legacyArn, Map.of("env", "wrong"), region)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.removeTagsFromResource(legacyArn, List.of("env"), region)).getErrorCode()); + assertEquals(Map.of(), rdsService.listTagsForResource(restoredArn, region)); + String unrelatedArn = legacyArn.replace(":es:legacy-events", ":es:other-events"); + rdsService.describeEventSubscriptions(region, "legacy-events", null, null) + .subscriptions().getFirst().setEventSubscriptionArn(unrelatedArn); + assertEquals(unrelatedArn, rdsService.describeEventSubscriptions( + region, named ? "legacy-events" : null, null, null) + .subscriptions().getFirst().getEventSubscriptionArn()); + } + @Test void dbProxyTargetGroupTagOperationsRejectMissingTargetGroup() { AwsException exception = assertThrows(AwsException.class, () -> @@ -1894,6 +1988,30 @@ class RdsServiceTest { assertTrue(unsupportedType.getMessage().contains("not yet implemented by Floci")); } + @Test + void tagOperationsRejectWrongPartitionWithoutChangingResources() { + DbInstance instance = rdsService.createDbInstance("partition-db", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", 20, false, null, null, null); + DbCluster cluster = rdsService.createDbCluster("partition-cluster", "postgres", "13", + "admin", "password", "dbname", false, null); + DbSubnetGroup subnetGroup = rdsService.createDbSubnetGroup( + "partition-subnets", "test", PROXY_SUBNET_IDS, "us-east-1"); + DbParameterGroup parameterGroup = rdsService.createDbParameterGroup( + "partition-parameters", "postgres16", "test"); + for (String arn : List.of(instance.getDbInstanceArn(), cluster.getDbClusterArn(), + subnetGroup.getDbSubnetGroupArn(), parameterGroup.getDbParameterGroupArn())) { + rdsService.addTagsToResource(arn, Map.of("env", "original")); + String wrongPartition = arn.replace("arn:aws:", "arn:aws-cn:"); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.listTagsForResource(wrongPartition)).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.addTagsToResource(wrongPartition, Map.of("env", "changed"))).getErrorCode()); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> rdsService.removeTagsFromResource(wrongPartition, List.of("env"))).getErrorCode()); + assertEquals(Map.of("env", "original"), rdsService.listTagsForResource(arn)); + } + } + @Test void tagOperationsRejectTypelessRdsArn() { // Real AWS rejects an RDS ARN whose resource part is not : with InvalidParameterValue; @@ -6107,6 +6225,50 @@ class RdsServiceTest { assertTrue(rawProxies.get(accountId + "/app-proxy").isEmpty()); } + @ParameterizedTest + @CsvSource({"xx-nowhere-9,123456789012,aws-cn", "xx-nowhere-9,222222222222,aws-cn", + "cn-north-1,222222222222,aws-cn", "us-east-1,222222222222,aws"}) + void restoredProxyTargetGroupUsesDeploymentPartitionAndSavedOwner( + String region, String accountId, String partition) { + when(config.services().rds().mock()).thenReturn(true); + RegionResolver startupResolver = new RegionResolver("cn-north-1", "123456789012"); + InMemoryStorage rawProxies = new InMemoryStorage<>(); + InMemoryStorage rawGroups = new InMemoryStorage<>(); + AccountAwareStorageBackend proxies = + new AccountAwareStorageBackend<>(rawProxies, null, "123456789012"); + AccountAwareStorageBackend groups = + new AccountAwareStorageBackend<>(rawGroups, null, "123456789012"); + DbProxy proxy = persistedProxy("app-proxy", region, accountId, "current", 5432); + proxy.setDbProxyArn("arn:" + partition + ":rds:" + region + ":" + accountId + ":db-proxy:prx-current"); + proxies.putForAccount(accountId, region + "::app-proxy", proxy); + RdsService startup = proxyStoreService(startupResolver, config, proxies, groups, + new InMemoryStorage<>(), new InMemoryStorage<>()); + + startup.restorePersistedRuntime(); + + DbProxyTargetGroup restored = groups.getForAccount(accountId, region + "::app-proxy").orElseThrow(); + String arn = restored.getTargetGroupArn(); + assertTrue(arn.startsWith("arn:" + partition + ":rds:" + region + ":" + accountId + ":target-group:"), arn); + RdsService reader = proxyStoreService(new RegionResolver("cn-north-1", accountId), config, + new AccountAwareStorageBackend<>(rawProxies, null, accountId), + new AccountAwareStorageBackend<>(rawGroups, null, accountId), + new InMemoryStorage<>(), new InMemoryStorage<>()); + assertSame(restored, reader.describeDbProxyTargetGroups("app-proxy", "default", region).iterator().next()); + reader.addTagsToResource(arn, Map.of("env", "restored"), region); + assertEquals(Map.of("env", "restored"), reader.listTagsForResource(arn, region)); + String foreignPartition = partition.equals("aws") ? "aws-cn" : "aws"; + String wrongArn = arn.replace("arn:" + partition + ":", "arn:" + foreignPartition + ":"); + assertThrows(AwsException.class, () -> reader.addTagsToResource(wrongArn, Map.of("env", "wrong"), region)); + assertThrows(AwsException.class, () -> reader.listTagsForResource(wrongArn, region)); + assertThrows(AwsException.class, () -> reader.removeTagsFromResource(wrongArn, List.of("env"), region)); + assertEquals(Map.of("env", "restored"), reader.listTagsForResource(arn, region)); + reader.removeTagsFromResource(arn, List.of("env"), region); + assertEquals(Map.of(), reader.listTagsForResource(arn, region)); + startup.restorePersistedRuntime(); + assertEquals(arn, groups.getForAccount(accountId, region + "::app-proxy") + .orElseThrow().getTargetGroupArn()); + } + @Test void proxyRestoreUsesRegionalLegacyGenerationDeterministically() { when(config.services().rds().mock()).thenReturn(true);