From a8842573119720357d6bb67026f4e55502c4bcab Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:13:12 -0600 Subject: [PATCH] fix(appconfig): isolate resources and sessions by region (#7621) Scope AppConfig stores, sessions and deployment cursors to the request's account and region. Keep legacy keys in the configured default region. Adapt the source change to current deployment and CloudFormation behavior, and cover storage reloads and SDK routing. Signed-off-by: Andre Kurait Signed-off-by: Andre Kurait Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> Co-authored-by: Andre Kurait --- .../tests/test_appconfig_region_isolation.py | 90 ++++++++++++++ docs/services/appconfig.md | 5 + .../appconfig/AppConfigDataService.java | 6 +- .../services/appconfig/AppConfigService.java | 39 ++++-- .../services/appconfig/AppConfigStorage.java | 116 ++++++++++++++++++ .../appconfig/AppConfigDataServiceTest.java | 8 +- .../AppConfigDeploymentLifecycleTest.java | 7 +- .../AppConfigRegionIntegrationTest.java | 82 +++++++++++++ .../AppConfigRegionPersistenceTest.java | 104 ++++++++++++++++ .../AppConfigServicePersistenceTest.java | 11 +- .../AppConfigServiceTaggingTest.java | 7 +- .../appconfig/AppConfigServiceTest.java | 47 ++++++- .../AppConfigServiceUpdateIsolationTest.java | 4 +- .../AppConfigServiceUpdateRaceTest.java | 4 +- .../appconfig/AppConfigStorageTest.java | 71 +++++++++++ .../CloudFormationAppConfigLifecycleTest.java | 15 ++- .../AppConfigCfnProvisionerTest.java | 13 +- 17 files changed, 587 insertions(+), 42 deletions(-) create mode 100644 compatibility-tests/sdk-test-python/tests/test_appconfig_region_isolation.py create mode 100644 src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorage.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionPersistenceTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorageTest.java diff --git a/compatibility-tests/sdk-test-python/tests/test_appconfig_region_isolation.py b/compatibility-tests/sdk-test-python/tests/test_appconfig_region_isolation.py new file mode 100644 index 0000000000..882779fe5b --- /dev/null +++ b/compatibility-tests/sdk-test-python/tests/test_appconfig_region_isolation.py @@ -0,0 +1,90 @@ +"""AppConfig names, deployed content and session tokens belong to their request scope.""" + +from contextlib import ExitStack +import json + +import boto3 +import pytest +from botocore.exceptions import ClientError + + +@pytest.mark.parametrize("regions", [ + ("us-east-1", "us-west-2"), + ("eu-west-1", "us-west-2"), + ("cn-north-1", "cn-northwest-1"), +]) +def test_same_names_in_other_accounts_and_regions_do_not_alias( + aws_config, client_config, unique_name, regions): + with ExitStack() as cleanup: + selections = [] + for account, region in [ + ("111111111111", regions[0]), + ("111111111111", regions[1]), + ("222222222222", regions[0])]: + config = { + **aws_config, "region_name": region, + "aws_access_key_id": account, "aws_secret_access_key": "test", + } + client = boto3.client("appconfig", config=client_config, **config) + data = boto3.client("appconfigdata", config=client_config, **config) + cleanup.callback(client.close) + cleanup.callback(data.close) + app = client.create_application(Name=unique_name)["Id"] + cleanup.callback(client.delete_application, ApplicationId=app) + profile = client.create_configuration_profile( + ApplicationId=app, Name="settings", LocationUri="hosted", Type="AWS.Freeform")["Id"] + cleanup.callback( + client.delete_configuration_profile, ApplicationId=app, ConfigurationProfileId=profile) + content = json.dumps({"account": account, "region": region}).encode() + version = client.create_hosted_configuration_version( + ApplicationId=app, ConfigurationProfileId=profile, Content=content, + ContentType="application/json")["VersionNumber"] + cleanup.callback( + client.delete_hosted_configuration_version, ApplicationId=app, + ConfigurationProfileId=profile, VersionNumber=version) + strategy = client.create_deployment_strategy( + Name=unique_name, DeploymentDurationInMinutes=0, GrowthFactor=100, + FinalBakeTimeInMinutes=0, ReplicateTo="NONE")["Id"] + cleanup.callback(client.delete_deployment_strategy, DeploymentStrategyId=strategy) + env = client.create_environment(ApplicationId=app, Name="environment")["Id"] + cleanup.callback(client.delete_environment, ApplicationId=app, EnvironmentId=env) + client.start_deployment( + ApplicationId=app, EnvironmentId=env, ConfigurationProfileId=profile, + ConfigurationVersion=str(version), DeploymentStrategyId=strategy) + token = data.start_configuration_session( + ApplicationIdentifier=app, EnvironmentIdentifier=env, + ConfigurationProfileIdentifier=profile)["InitialConfigurationToken"] + selections.append({ + "client": client, "data": data, "app": app, "strategy": strategy, + "content": content, "version": version, "token": token, + }) + + for selection in selections: + client = selection["client"] + data = selection["data"] + assert client.get_application(ApplicationId=unique_name)["Id"] == selection["app"] + assert [ + app["Id"] for app in client.list_applications()["Items"] + if app["Name"] == unique_name + ] == [selection["app"]] + response = client.get_configuration( + Application=unique_name, Environment="environment", Configuration="settings", + ClientId=unique_name) + with response["Content"] as stream: + assert stream.read() == selection["content"] + assert response["ConfigurationVersion"] == str(selection["version"]) + for other in selections: + if other is selection: + continue + with pytest.raises(ClientError) as error: + client.get_application(ApplicationId=other["app"]) + assert error.value.response["Error"]["Code"] == "ResourceNotFoundException" + with pytest.raises(ClientError) as error: + client.get_deployment_strategy(DeploymentStrategyId=other["strategy"]) + assert error.value.response["Error"]["Code"] == "ResourceNotFoundException" + with pytest.raises(ClientError) as error: + data.get_latest_configuration(ConfigurationToken=other["token"]) + assert error.value.response["Error"]["Code"] == "BadRequestException" + response = data.get_latest_configuration(ConfigurationToken=selection["token"]) + with response["Configuration"] as stream: + assert stream.read() == selection["content"] diff --git a/docs/services/appconfig.md b/docs/services/appconfig.md index 5ba24849cb..0e136b94fe 100644 --- a/docs/services/appconfig.md +++ b/docs/services/appconfig.md @@ -2,6 +2,11 @@ Floci supports AWS AppConfig and AppConfigData for local configuration management. +Resources, name lookups, pagination and configuration-session tokens are isolated by +the calling account and region. Persisted records from older versions that did not +record a region remain available only in `floci.default-region`. New writes store +their region explicitly in all storage modes. + ## Management Plane (AppConfig) The management plane allows you to create and manage applications, environments, configuration profiles, and hosted configuration versions. diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java index d7ebbb1c38..2ee4fbfc4f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.appconfig.model.Application; @@ -33,8 +34,9 @@ public class AppConfigDataService { @Inject public AppConfigDataService(StorageFactory storageFactory, AppConfigService appConfigService, - ObjectMapper objectMapper) { - this.sessionStore = storageFactory.create("appconfigdata", "appconfigdata-sessions.json", new TypeReference<>() {}); + ObjectMapper objectMapper, RegionResolver regions) { + this.sessionStore = new AppConfigStorage<>(storageFactory, regions, + "appconfigdata", "appconfigdata-sessions.json", new TypeReference<>() {}); this.appConfigService = appConfigService; this.objectMapper = objectMapper; } diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java index 5b017a6770..887a1c6a70 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java @@ -2,9 +2,9 @@ package io.github.hectorvent.floci.services.appconfig; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.core.storage.StorageOperations; @@ -46,6 +46,7 @@ public class AppConfigService { private final StorageBackend deploymentStore; private final StorageBackend activeConfigStore; // envId::profileId -> versionNumber private final Clock clock; + private final RegionResolver regions; private final Map deploymentPageTokens = new ConcurrentHashMap<>(); private static final int MAX_DEPLOYMENT_PAGE_TOKENS = 1000; private static final int MAX_MONITORS = 5; @@ -54,19 +55,27 @@ public class AppConfigService { private static final Pattern ROLE_ARN = Pattern.compile("arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::\\d{12}:role/.*"); @Inject - public AppConfigService(StorageFactory storageFactory, EmulatorConfig config) { - this(storageFactory, config, Clock.systemUTC()); + public AppConfigService(StorageFactory storageFactory, RegionResolver regions) { + this(storageFactory, regions, Clock.systemUTC()); } - AppConfigService(StorageFactory storageFactory, EmulatorConfig config, Clock clock) { + AppConfigService(StorageFactory storageFactory, RegionResolver regions, Clock clock) { this.clock = clock; - this.applicationStore = storageFactory.create("appconfig", "appconfig-applications.json", new TypeReference<>() {}); - this.environmentStore = storageFactory.create("appconfig", "appconfig-environments.json", new TypeReference<>() {}); - this.profileStore = storageFactory.create("appconfig", "appconfig-profiles.json", new TypeReference<>() {}); - this.strategyStore = storageFactory.create("appconfig", "appconfig-strategies.json", new TypeReference<>() {}); - this.versionStore = storageFactory.create("appconfig", "appconfig-versions.json", new TypeReference<>() {}); - this.deploymentStore = storageFactory.create("appconfig", "appconfig-deployments.json", new TypeReference<>() {}); - this.activeConfigStore = storageFactory.create("appconfig", "appconfig-active-configs.json", new TypeReference<>() {}); + this.regions = regions; + this.applicationStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-applications.json", new TypeReference<>() {}); + this.environmentStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-environments.json", new TypeReference<>() {}); + this.profileStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-profiles.json", new TypeReference<>() {}); + this.strategyStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-strategies.json", new TypeReference<>() {}); + this.versionStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-versions.json", new TypeReference<>() {}); + this.deploymentStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-deployments.json", new TypeReference<>() {}); + this.activeConfigStore = new AppConfigStorage<>(storageFactory, regions, + "appconfig", "appconfig-active-configs.json", new TypeReference<>() {}); } // ──────────────────────────── Application ──────────────────────────── @@ -192,7 +201,7 @@ public class AppConfigService { StorageOperations.deleteByPrefix(deploymentStore, appId + "::" + envId + "::"); StorageOperations.deleteByPrefix(activeConfigStore, envId + "::"); - String deploymentScope = appId + "::" + envId; + String deploymentScope = deploymentScope(appId, envId); deploymentPageTokens.entrySet().removeIf(entry -> entry.getValue().scope().equals(deploymentScope)); environmentStore.delete(envId); } @@ -694,7 +703,7 @@ public class AppConfigService { throw new AwsException("BadRequestException", "max_results must be between 1 and 50", 400); } - String scope = appId + "::" + envId; + String scope = deploymentScope(appId, envId); Integer afterDeploymentNumber = null; if (nextToken != null) { DeploymentPageToken token = deploymentPageTokens.get(nextToken); @@ -783,6 +792,10 @@ public class AppConfigService { return token; } + private String deploymentScope(String appId, String envId) { + return regions.getAccountId() + "/" + regions.getRegion() + "/" + appId + "::" + envId; + } + private record DeploymentPageToken(String scope, int lastDeploymentNumber) { } diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorage.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorage.java new file mode 100644 index 0000000000..5e78f62e61 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorage.java @@ -0,0 +1,116 @@ +package io.github.hectorvent.floci.services.appconfig; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; + +/** + * Region view of a factory-owned account store. Older, regionless keys belong only to + * the configured default region; new writes always record their region explicitly. + */ +final class AppConfigStorage implements StorageBackend { + private final StorageBackend delegate; + private final RegionResolver regions; + + AppConfigStorage(StorageFactory factory, RegionResolver regions, String service, String file, + TypeReference> type) { + this.delegate = factory.create(service, file, type); + this.regions = regions; + } + + @Override + public void put(String key, V value) { + if (!logicalKey(key)) { + throw new IllegalArgumentException("AppConfig storage keys cannot contain a region prefix"); + } + delegate.put(prefix() + key, value); + if (defaultRegion() && delegate.get(key).isPresent()) { + delegate.delete(key); + } + } + + @Override + public Optional get(String key) { + if (!logicalKey(key)) { + return Optional.empty(); + } + return delegate.get(prefix() + key) + .or(() -> defaultRegion() ? delegate.get(key) : Optional.empty()); + } + + @Override + public void delete(String key) { + if (logicalKey(key)) { + if (defaultRegion()) { + delegate.delete(key); + } + delegate.delete(prefix() + key); + } + } + + @Override + public List scan(Predicate keyFilter) { + List values = new ArrayList<>(); + for (String key : keys()) { + if (keyFilter.test(key)) { + get(key).ifPresent(values::add); + } + } + return values; + } + + @Override + public Set keys() { + String prefix = prefix(); + boolean includeLegacy = defaultRegion(); + Set keys = new HashSet<>(); + for (String stored : delegate.keys()) { + if (stored.startsWith(prefix)) { + String key = stored.substring(prefix.length()); + if (logicalKey(key)) { + keys.add(key); + } + } else if (includeLegacy && logicalKey(stored)) { + keys.add(stored); + } + } + return keys; + } + + @Override + public void clear() { + keys().forEach(this::delete); + } + + @Override + public void flush() { + delegate.flush(); + } + + @Override + public void load() { + delegate.load(); + } + + private String prefix() { + return regions.getRegion() + "/"; + } + + private boolean defaultRegion() { + return regions.getRegion().equals(regions.getDefaultRegion()); + } + + private static boolean logicalKey(String key) { + // Resource IDs, configuration tokens and the service's "::" composite keys contain no slash. + return key != null && !key.contains("/"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataServiceTest.java index 97c3dea313..54bfd82c8b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataServiceTest.java @@ -3,7 +3,8 @@ package io.github.hectorvent.floci.services.appconfig; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; -import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.appconfig.model.ConfigurationSession; import io.github.hectorvent.floci.testing.InMemoryStorageFactory; import org.junit.jupiter.api.Test; @@ -36,8 +37,9 @@ class AppConfigDataServiceTest { void concurrentPollsConsumeAConfigurationTokenOnlyOnce() throws Exception { InMemoryStorageFactory factory = new InMemoryStorageFactory("000000000000"); AppConfigService appConfig = mock(AppConfigService.class); - AppConfigDataService service = new AppConfigDataService(factory, appConfig, MAPPER); - AccountAwareStorageBackend sessions = factory.create("appconfigdata", + RegionResolver regions = new RegionResolver("us-east-1", "000000000000"); + AppConfigDataService service = new AppConfigDataService(factory, appConfig, MAPPER, regions); + StorageBackend sessions = new AppConfigStorage<>(factory, regions, "appconfigdata", "appconfigdata-sessions.json", new TypeReference>() {}); ConfigurationSession session = new ConfigurationSession(); session.setApplicationId("app"); diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDeploymentLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDeploymentLifecycleTest.java index 7869855f1a..89b4454791 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDeploymentLifecycleTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigDeploymentLifecycleTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.appconfig; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.ServiceConfigAccess; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.appconfig.model.Deployment; @@ -162,7 +163,8 @@ class AppConfigDeploymentLifecycleTest { StorageFactory storage = factory(); Clock clock = mock(Clock.class); when(clock.instant()).thenReturn(START); - AppConfigService service = new AppConfigService(storage, mock(EmulatorConfig.class), clock); + AppConfigService service = new AppConfigService( + storage, new RegionResolver("us-east-1", "000000000000"), clock); Scope scope = scope(service); Deployment baking = deploy(service, scope, "1", scope.bake()); Deployment response = service.getDeployment(scope.app(), scope.env(), baking.getDeploymentNumber()); @@ -238,7 +240,8 @@ class AppConfigDeploymentLifecycleTest { } private AppConfigService service(StorageFactory storage, Instant time) { - return new AppConfigService(storage, mock(EmulatorConfig.class), Clock.fixed(time, ZoneOffset.UTC)); + return new AppConfigService(storage, new RegionResolver("us-east-1", "000000000000"), + Clock.fixed(time, ZoneOffset.UTC)); } private StorageFactory factory() { diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionIntegrationTest.java new file mode 100644 index 0000000000..0165e961e9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionIntegrationTest.java @@ -0,0 +1,82 @@ +package io.github.hectorvent.floci.services.appconfig; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.RequestScopes; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@QuarkusTest +class AppConfigRegionIntegrationTest { + @Inject + AppConfigService service; + + @Inject + AppConfigDataService data; + + @Inject + RegionResolver regions; + + @Test + void identicalNamesResolveOnlyWithinTheirAccountAndRegion() { + String name = "regional-" + UUID.randomUUID(); + Map applications = new LinkedHashMap<>(); + try { + for (Scope scope : List.of( + new Scope("111111111111", regions.getDefaultRegion()), + new Scope("111111111111", "us-west-2"), + new Scope("222222222222", regions.getDefaultRegion()), + new Scope("111111111111", "cn-north-1"))) { + applications.put(scope, RequestScopes.callAs(scope.account(), scope.region(), + () -> service.createApplication(Map.of("Name", name)).getId())); + } + applications.forEach((scope, id) -> RequestScopes.runAs(scope.account(), scope.region(), () -> { + assertEquals(id, service.resolveApplication(name).getId()); + assertEquals(List.of(id), service.listApplications().stream() + .filter(application -> name.equals(application.getName())) + .map(application -> application.getId()).toList()); + applications.values().stream().filter(other -> !other.equals(id)).forEach(other -> + assertEquals("ResourceNotFoundException", + assertThrows(AwsException.class, () -> service.getApplication(other)).getErrorCode())); + })); + } finally { + applications.forEach((scope, id) -> RequestScopes.runAs(scope.account(), scope.region(), + () -> service.deleteApplication(id))); + } + } + + @Test + void foreignRegionCannotConsumeAConfigurationSession() { + String account = "333333333333"; + String region = regions.getDefaultRegion(); + RequestScopes.runAs(account, region, () -> { + String app = service.createApplication(Map.of("Name", "session-" + UUID.randomUUID())).getId(); + String env = service.createEnvironment(app, Map.of("Name", "environment")).getId(); + String profile = service.createConfigurationProfile(app, + Map.of("Name", "profile", "LocationUri", "hosted")).getId(); + try { + String token = data.startConfigurationSession(Map.of( + "ApplicationIdentifier", app, "EnvironmentIdentifier", env, + "ConfigurationProfileIdentifier", profile)); + RequestScopes.runAs(account, "us-west-2", () -> assertEquals("BadRequestException", + assertThrows(AwsException.class, () -> data.getLatestConfiguration(token)).getErrorCode())); + assertEquals(0, data.getLatestConfiguration(token).content().length); + } finally { + service.deleteEnvironment(app, env); + service.deleteConfigurationProfile(app, profile); + service.deleteApplication(app); + } + }); + } + + private record Scope(String account, String region) {} +} diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionPersistenceTest.java new file mode 100644 index 0000000000..4bd8bc6892 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigRegionPersistenceTest.java @@ -0,0 +1,104 @@ +package io.github.hectorvent.floci.services.appconfig; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.testing.StorageFactories; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AppConfigRegionPersistenceTest { + @TempDir + Path directory; + + @ParameterizedTest + @ValueSource(strings = {"persistent", "hybrid", "wal"}) + void deploymentsAndUnconsumedSessionsKeepTheirRegionAfterRestart(String mode) { + AtomicReference current = new AtomicReference<>("us-east-1"); + RegionResolver regions = mock(RegionResolver.class); + when(regions.getDefaultRegion()).thenReturn("us-east-1"); + when(regions.getRegion()).thenAnswer(invocation -> current.get()); + when(regions.getAccountId()).thenReturn("000000000000"); + Map selections = new LinkedHashMap<>(); + StorageFactory first = StorageFactories.open(directory, mode, "000000000000"); + try { + AppConfigService service = new AppConfigService(first, regions); + AppConfigDataService data = new AppConfigDataService(first, service, new ObjectMapper(), regions); + for (String region : List.of("us-east-1", "us-west-2", "cn-north-1")) { + current.set(region); + String app = service.createApplication(Map.of("Name", "same-application")).getId(); + String env = service.createEnvironment(app, Map.of("Name", "same-environment")).getId(); + String profile = service.createConfigurationProfile(app, + Map.of("Name", "same-profile", "LocationUri", "hosted")).getId(); + int version = service.createHostedConfigurationVersion( + app, profile, content(region), "text/plain", null).getVersionNumber(); + String strategy = service.createDeploymentStrategy(Map.of( + "Name", "same-strategy", "FinalBakeTimeInMinutes", 0)).getId(); + int deployment = service.startDeployment(app, env, Map.of( + "ConfigurationProfileId", profile, "ConfigurationVersion", String.valueOf(version), + "DeploymentStrategyId", strategy)).getDeploymentNumber(); + String token = data.startConfigurationSession(Map.of( + "ApplicationIdentifier", app, "EnvironmentIdentifier", env, + "ConfigurationProfileIdentifier", profile)); + selections.put(region, new Selection(app, env, profile, strategy, deployment, token)); + } + } finally { + first.shutdownAll(); + } + + StorageFactory restarted = StorageFactories.open(directory, mode, "000000000000"); + try { + AppConfigService service = new AppConfigService(restarted, regions); + AppConfigDataService data = new AppConfigDataService(restarted, service, new ObjectMapper(), regions); + selections.forEach((region, selection) -> { + current.set(region); + assertEquals(selection.app(), service.resolveApplication("same-application").getId()); + assertEquals(selection.env(), service.resolveEnvironment(selection.app(), "same-environment").getId()); + assertEquals(selection.profile(), + service.resolveConfigurationProfile(selection.app(), "same-profile").getId()); + assertEquals("same-strategy", service.getDeploymentStrategy(selection.strategy()).getName()); + assertEquals("COMPLETE", service.getDeployment( + selection.app(), selection.env(), selection.deployment()).getState()); + assertArrayEquals(content(region), data.getConfiguration( + "same-application", "same-environment", "same-profile", "client", null, null).content()); + selections.forEach((otherRegion, other) -> { + if (!region.equals(otherRegion)) { + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> service.getDeploymentStrategy(other.strategy())).getErrorCode()); + assertEquals("BadRequestException", assertThrows(AwsException.class, + () -> data.getLatestConfiguration(other.token())).getErrorCode()); + } + }); + assertArrayEquals(content(region), data.getLatestConfiguration(selection.token()).content()); + service.deleteEnvironment(selection.app(), selection.env()); + service.deleteConfigurationProfile(selection.app(), selection.profile()); + service.deleteDeploymentStrategy(selection.strategy()); + service.deleteApplication(selection.app()); + assertEquals(List.of(), service.listApplications()); + }); + } finally { + restarted.shutdownAll(); + } + } + + private static byte[] content(String region) { + return region.getBytes(StandardCharsets.UTF_8); + } + + private record Selection(String app, String env, String profile, String strategy, int deployment, String token) {} +} diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServicePersistenceTest.java index e1cee7e7c4..d9d68ba894 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServicePersistenceTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.appconfig; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.ServiceConfigAccess; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.appconfig.model.Deployment; @@ -57,7 +58,7 @@ class AppConfigServicePersistenceTest { "State": "COMPLETE", "Description": null}} """); - AppConfigService service = new AppConfigService(newFactory(), mock(EmulatorConfig.class)); + AppConfigService service = new AppConfigService(newFactory(), new RegionResolver("us-east-1", "000000000000")); Environment environment = service.getEnvironment("app1", "env1"); assertEquals("stored-env", environment.getName()); @@ -87,7 +88,7 @@ class AppConfigServicePersistenceTest { @Test void deploymentStrategySettingsSurviveARestart() { StorageFactory first = newFactory(); - AppConfigService service = new AppConfigService(first, mock(EmulatorConfig.class)); + AppConfigService service = new AppConfigService(first, new RegionResolver("us-east-1", "000000000000")); String appId = service.createApplication(Map.of("Name", "restarted-app")).getId(); String environmentId = service.createEnvironment(appId, Map.of("Name", "restarted-env")).getId(); String profileId = service.createConfigurationProfile(appId, Map.of( @@ -101,7 +102,7 @@ class AppConfigServicePersistenceTest { "DeploymentStrategyId", strategyId)); first.shutdownAll(); - AppConfigService restarted = new AppConfigService(newFactory(), mock(EmulatorConfig.class)); + AppConfigService restarted = new AppConfigService(newFactory(), new RegionResolver("us-east-1", "000000000000")); // With the strategy gone, the settings can only come from the stored deployment. restarted.deleteDeploymentStrategy(strategyId); @@ -115,7 +116,7 @@ class AppConfigServicePersistenceTest { @Test void monitorsAndDeploymentLocationsSurviveARestart() { StorageFactory first = newFactory(); - AppConfigService service = new AppConfigService(first, mock(EmulatorConfig.class)); + AppConfigService service = new AppConfigService(first, new RegionResolver("us-east-1", "000000000000")); String appId = service.createApplication(Map.of("Name", "restarted-app")).getId(); String monitoredId = service.createEnvironment(appId, Map.of("Name", "monitored", "Monitors", List.of( Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", ALARM_ROLE_ARN), @@ -129,7 +130,7 @@ class AppConfigServicePersistenceTest { "DeploymentStrategyId", "AppConfig.AllAtOnce")).getDeploymentNumber(); first.shutdownAll(); - AppConfigService restarted = new AppConfigService(newFactory(), mock(EmulatorConfig.class)); + AppConfigService restarted = new AppConfigService(newFactory(), new RegionResolver("us-east-1", "000000000000")); assertEquals(List.of(new Monitor(ALARM_ARN, ALARM_ROLE_ARN), new Monitor(SECOND_ALARM_ARN, null)), restarted.getEnvironment(appId, monitoredId).getMonitors()); diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java index 482a448cb3..eb96793545 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.appconfig; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.testing.InMemoryStorageFactory; @@ -33,7 +34,8 @@ class AppConfigServiceTaggingTest { @Test void anApplicationReadBeforeATagOrUntagKeepsItsPriorTags() { - AppConfigService service = new AppConfigService(new InMemoryStorageFactory(ACCOUNT), null); + AppConfigService service = new AppConfigService( + new InMemoryStorageFactory(ACCOUNT), new RegionResolver("us-east-1", ACCOUNT)); Application created = service.createApplication(Map.of("Name", "orders")); service.tagApplication(created.getId(), Map.of("team", "payments")); Application beforeTag = service.getApplication(created.getId()); @@ -90,6 +92,7 @@ class AppConfigServiceTaggingTest { /** An AppConfig service whose application store is {@code store} and every other store in memory. */ private static AppConfigService serviceWithApplicationStore(StorageBackend store) { - return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(APPLICATIONS, store), null); + return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(APPLICATIONS, store), + new RegionResolver("us-east-1", ACCOUNT)); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java index fb4d713bb3..e42018d51c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java @@ -1,8 +1,8 @@ package io.github.hectorvent.floci.services.appconfig; import com.fasterxml.jackson.core.type.TypeReference; -import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.appconfig.model.Application; @@ -51,6 +51,7 @@ class AppConfigServiceTest { private AccountAwareStorageBackend environmentStore; private AccountAwareStorageBackend deploymentStore; private AccountAwareStorageBackend activeConfigStore; + private RegionResolver regions; private AppConfigService service; @BeforeEach @@ -65,7 +66,8 @@ class AppConfigServiceTest { deployments.put(invocation.getArgument(0, String.class), invocation.getArgument(1, Deployment.class)); return null; }).when(deploymentStore).put(anyString(), any(Deployment.class)); - when(deploymentStore.scan(any())).thenAnswer(invocation -> List.copyOf(deployments.values())); + when(deploymentStore.get(anyString())).thenAnswer(invocation -> + Optional.ofNullable(deployments.get(invocation.getArgument(0, String.class)))); when(deploymentStore.keys()).thenAnswer(invocation -> Set.copyOf(deployments.keySet())); doAnswer(invocation -> deployments.remove(invocation.getArgument(0, String.class))) .when(deploymentStore).delete(anyString()); @@ -106,7 +108,11 @@ class AppConfigServiceTest { profile.setId("profile"); profile.setApplicationId("app"); when(profileStore.get("profile")).thenReturn(Optional.of(profile)); - service = new AppConfigService(storageFactory, mock(EmulatorConfig.class)); + regions = mock(RegionResolver.class); + when(regions.getDefaultRegion()).thenReturn("us-east-1"); + when(regions.getRegion()).thenReturn("us-east-1"); + when(regions.getAccountId()).thenReturn("000000000000"); + service = new AppConfigService(storageFactory, regions); } @Test @@ -115,7 +121,7 @@ class AppConfigServiceTest { assertEquals("orders-v2", updated.getName()); assertEquals("Order processing", updated.getDescription()); - verify(applicationStore).put("app", updated); + verify(applicationStore).put("us-east-1/app", updated); } @Test @@ -205,6 +211,37 @@ class AppConfigServiceTest { assertThrows(RuntimeException.class, () -> service.listDeployments("app", "env", 1, "unknown")); } + @Test + void listDeploymentsTokensCannotCrossRegionOrAccount() { + deploymentStore.put("app::env::1", deployment("app", "env", 1)); + deploymentStore.put("app::env::2", deployment("app", "env", 2)); + String token = service.listDeployments("app", "env", 1, null).nextToken(); + assertNotNull(token); + + // The same resource IDs exist in both regions, so rejection must come from token scope. + Application app = applicationStore.get("app").orElseThrow(); + Environment env = environmentStore.get("env").orElseThrow(); + when(applicationStore.get("us-west-2/app")).thenReturn(Optional.of(app)); + when(environmentStore.get("us-west-2/env")).thenReturn(Optional.of(env)); + when(regions.getRegion()).thenReturn("us-west-2"); + AwsException regionError = assertThrows(AwsException.class, + () -> service.listDeployments("app", "env", 1, token)); + assertEquals("BadRequestException", regionError.getErrorCode()); + assertEquals("Invalid next_token", regionError.getMessage()); + + when(regions.getRegion()).thenReturn("us-east-1"); + when(regions.getAccountId()).thenReturn("222222222222"); + AwsException accountError = assertThrows(AwsException.class, + () -> service.listDeployments("app", "env", 1, token)); + assertEquals("BadRequestException", accountError.getErrorCode()); + assertEquals("Invalid next_token", accountError.getMessage()); + + when(regions.getAccountId()).thenReturn("000000000000"); + AppConfigService.DeploymentPage secondPage = service.listDeployments("app", "env", 1, token); + assertEquals(List.of(1), secondPage.items().stream().map(DeploymentSummary::getDeploymentNumber).toList()); + assertNull(secondPage.nextToken()); + } + @Test void listDeploymentsStillReportsADeploymentWhoseStrategyWasDeleted() { String strategyId = createGradualStrategy().getId(); @@ -392,7 +429,7 @@ class AppConfigServiceTest { "DeploymentStrategyId", "AppConfig.AllAtOnce")); assertEquals(4, deployment.getDeploymentNumber()); - assertEquals(Set.of("app::env::1", "app::env::3", "app::env::4"), deploymentStore.keys()); + assertEquals(Set.of("app::env::1", "app::env::3", "us-east-1/app::env::4"), deploymentStore.keys()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateIsolationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateIsolationTest.java index 1a82746731..dbf5597ed5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateIsolationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateIsolationTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.appconfig; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile; import io.github.hectorvent.floci.services.appconfig.model.DeploymentStrategy; @@ -24,7 +25,8 @@ class AppConfigServiceUpdateIsolationTest { private static final String ALARM = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-errors"; - private final AppConfigService service = new AppConfigService(new InMemoryStorageFactory("000000000000"), null); + private final AppConfigService service = new AppConfigService(new InMemoryStorageFactory("000000000000"), + new RegionResolver("us-east-1", "000000000000")); @Test void aStrategyReadBeforeAnUpdateKeepsItsPriorSettings() { diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java index 02cdebc877..9f60ea1d62 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.appconfig; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile; @@ -129,6 +130,7 @@ class AppConfigServiceUpdateRaceTest { /** An AppConfig service whose {@code fileName} store is {@code store} and every other store in memory. */ private static AppConfigService serviceWithStore(String fileName, StorageBackend store) { - return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(fileName, store), null); + return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(fileName, store), + new RegionResolver("us-east-1", ACCOUNT)); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorageTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorageTest.java new file mode 100644 index 0000000000..0220925bc8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigStorageTest.java @@ -0,0 +1,71 @@ +package io.github.hectorvent.floci.services.appconfig; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.testing.StorageFactories; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AppConfigStorageTest { + @TempDir + Path directory; + + @ParameterizedTest + @ValueSource(strings = {"memory", "persistent", "hybrid", "wal"}) + void legacyKeysAndScopedKeysCannotEscapeTheirRegion(String mode) { + StorageFactory factory = StorageFactories.open(directory, mode, "000000000000"); + try { + StorageBackend legacy = factory.create( + "appconfig", "records.json", new TypeReference<>() {}); + legacy.put("existing", "legacy"); + AppConfigStorage east = store(factory, "us-east-1"); + AppConfigStorage west = store(factory, "us-west-2"); + assertEquals(Optional.of("legacy"), east.get("existing")); + assertEquals(Optional.empty(), west.get("existing")); + east.put("existing", "east"); + west.put("existing", "west"); + legacy.put("untouched", "older-record"); + + assertEquals(Optional.empty(), legacy.get("existing")); + assertEquals(Optional.of("east"), east.get("existing")); + assertEquals(Optional.of("west"), west.get("existing")); + assertEquals(Set.of("existing", "untouched"), east.keys()); + assertEquals(Set.of("existing"), west.keys()); + assertEquals(List.of("west"), west.scan(key -> key.equals("existing"))); + List selected = west.scan(key -> true); + selected.clear(); + assertEquals(List.of("west"), west.scan(key -> true)); + + assertEquals(Optional.empty(), east.get("us-west-2/existing")); + east.delete("us-west-2/existing"); + assertThrows(IllegalArgumentException.class, () -> east.put("us-west-2/existing", "wrong")); + assertEquals(Optional.of("west"), west.get("existing")); + east.clear(); + assertEquals(Set.of(), east.keys()); + assertEquals(Optional.of("west"), west.get("existing")); + } finally { + factory.shutdownAll(); + } + } + + private static AppConfigStorage store(StorageFactory factory, String region) { + RegionResolver regions = mock(RegionResolver.class); + when(regions.getDefaultRegion()).thenReturn("us-east-1"); + when(regions.getRegion()).thenReturn(region); + return new AppConfigStorage<>(factory, regions, "appconfig", "records.json", new TypeReference<>() {}); + } + +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAppConfigLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAppConfigLifecycleTest.java index 9d7f522e72..6b77133fc5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAppConfigLifecycleTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAppConfigLifecycleTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.appconfig.AppConfigService; import io.github.hectorvent.floci.services.appconfig.model.Application; @@ -53,7 +54,7 @@ class CloudFormationAppConfigLifecycleTest { @BeforeEach void setUp() { storage = new InMemoryStorageFactory(ACCOUNT); - appConfig = spy(new AppConfigService(storage, null)); + appConfig = spy(new AppConfigService(storage, new RegionResolver(REGION, ACCOUNT))); CfnResourceDispatcher dispatcher = new CfnResourceDispatcher(MAPPER, new CloudFormationResourceRegistry(List.of(new AppConfigCfnProvisioner(appConfig))), null, null); cfn = CloudFormationServiceFixture.create(dispatcher); @@ -80,6 +81,12 @@ class CloudFormationAppConfigLifecycleTest { "application/json", null); appConfig.startDeployment(oldApp, oldEnv, Map.of("ConfigurationProfileId", oldProfile, "ConfigurationVersion", "1", "DeploymentStrategyId", "AppConfig.AllAtOnce")); + StorageBackend deployments = store("appconfig-deployments.json", new TypeReference<>() {}); + StorageBackend versions = store( + "appconfig-versions.json", new TypeReference<>() {}); + String oldPrefix = REGION + "/" + oldApp + "::"; + assertEquals(1, deployments.scan(key -> key.startsWith(oldPrefix)).size()); + assertEquals(1, versions.scan(key -> key.startsWith(oldPrefix)).size()); deploy("moved", "UPDATE", "{\"Resources\":{\"AppB\":{\"Type\":\"AWS::AppConfig::Application\"," + "\"Properties\":{\"Name\":\"orders\"}}," + children.formatted("AppB") + "}}"); @@ -90,10 +97,8 @@ class CloudFormationAppConfigLifecycleTest { assertTrue(appConfig.listApplications().stream().noneMatch(app -> app.getId().equals(oldApp))); assertTrue(appConfig.findEnvironment(oldEnv).isEmpty(), "the displaced environment is deleted"); assertTrue(appConfig.findConfigurationProfile(oldProfile).isEmpty(), "the displaced profile is deleted"); - assertTrue(store("appconfig-deployments.json", new TypeReference>() {}) - .scan(key -> key.startsWith(oldApp + "::")).isEmpty(), "with its deployments"); - assertTrue(store("appconfig-versions.json", new TypeReference>() {}) - .scan(key -> key.startsWith(oldApp + "::")).isEmpty(), "and its hosted configuration versions"); + assertTrue(deployments.scan(key -> key.startsWith(oldPrefix)).isEmpty(), "with its deployments"); + assertTrue(versions.scan(key -> key.startsWith(oldPrefix)).isEmpty(), "and its hosted configuration versions"); } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AppConfigCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AppConfigCfnProvisionerTest.java index 2cd231ca8d..87e10b0755 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AppConfigCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AppConfigCfnProvisionerTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.appconfig.AppConfigService; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile; @@ -64,7 +65,7 @@ class AppConfigCfnProvisionerTest { @BeforeEach void setUp() { storage = new InMemoryStorageFactory(ACCOUNT); - service = spy(new AppConfigService(storage, null)); + service = spy(new AppConfigService(storage, new RegionResolver(REGION, ACCOUNT))); provisioner = new AppConfigCfnProvisioner(service); engine = CloudFormationTemplateEngine.standalone(ACCOUNT, REGION, STACK, MAPPER, value -> value); } @@ -576,6 +577,12 @@ class AppConfigCfnProvisionerTest { null); service.startDeployment(appId, env.getPhysicalId(), Map.of("ConfigurationProfileId", profile.getPhysicalId(), "ConfigurationVersion", "1", "DeploymentStrategyId", "AppConfig.AllAtOnce")); + String prefix = REGION + "/" + appId + "::"; + assertEquals(1, storage.create("appconfig", "appconfig-deployments.json", + new TypeReference>() {}).scan(key -> key.startsWith(prefix)).size()); + assertEquals(1, storage.create("appconfig", "appconfig-versions.json", + new TypeReference>() {}) + .scan(key -> key.startsWith(prefix)).size()); service.deleteApplication(appId); provisioner.delete(ENVIRONMENT, env.getPhysicalId(), REGION); @@ -584,10 +591,10 @@ class AppConfigCfnProvisionerTest { assertTrue(service.findEnvironment(env.getPhysicalId()).isEmpty()); assertTrue(service.findConfigurationProfile(profile.getPhysicalId()).isEmpty()); assertTrue(storage.create("appconfig", "appconfig-deployments.json", - new TypeReference>() {}).scan(key -> key.startsWith(appId + "::")).isEmpty()); + new TypeReference>() {}).scan(key -> key.startsWith(prefix)).isEmpty()); assertTrue(storage.create("appconfig", "appconfig-versions.json", new TypeReference>() {}) - .scan(key -> key.startsWith(appId + "::")).isEmpty()); + .scan(key -> key.startsWith(prefix)).isEmpty()); } @Test