Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(appconfig): reject ambiguous configuration names (#6783)

Andre Kurait5c20d54646f9
diff --git a/compatibility-tests/sdk-test-python/tests/test_appconfig_ambiguous_names.py b/compatibility-tests/sdk-test-python/tests/test_appconfig_ambiguous_names.py
new file mode 100644
index 0000000000..c68e511410
--- /dev/null
+++ b/compatibility-tests/sdk-test-python/tests/test_appconfig_ambiguous_names.py
@@ -0,0 +1,82 @@
+"""Name ambiguity must not choose another application's configuration."""
+
+from contextlib import ExitStack
+
+import boto3
+import pytest
+from botocore.exceptions import ClientError
+
+
[email protected]("resource", ["application", "environment", "profile"])
+def test_ambiguous_names_are_refused_and_exact_ids_still_deliver(
+ aws_config, client_config, unique_name, resource):
+ with ExitStack() as cleanup:
+ client = boto3.client("appconfig", config=client_config, **aws_config)
+ data = boto3.client("appconfigdata", config=client_config, **aws_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 = b'{"scope":"owned"}'
+ 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)
+ env = client.create_environment(ApplicationId=app, Name="local")["Id"]
+ cleanup.callback(client.delete_environment, ApplicationId=app, EnvironmentId=env)
+ client.start_deployment(
+ ApplicationId=app, EnvironmentId=env, ConfigurationProfileId=profile,
+ ConfigurationVersion=str(version), DeploymentStrategyId="AppConfig.AllAtOnce")
+
+ def duplicate(name):
+ if resource == "application":
+ added = client.create_application(Name=name)["Id"]
+ cleanup.callback(client.delete_application, ApplicationId=added)
+ elif resource == "environment":
+ added = client.create_environment(ApplicationId=app, Name=name)["Id"]
+ cleanup.callback(client.delete_environment, ApplicationId=app, EnvironmentId=added)
+ else:
+ added = client.create_configuration_profile(
+ ApplicationId=app, Name=name, LocationUri="hosted", Type="AWS.Freeform")["Id"]
+ cleanup.callback(
+ client.delete_configuration_profile, ApplicationId=app, ConfigurationProfileId=added)
+
+ names = {"application": unique_name, "environment": "local", "profile": "settings"}
+ ids = {"application": app, "environment": env, "profile": profile}
+ duplicate(names[resource])
+ duplicate(ids[resource])
+ selected = dict(ids)
+ selected[resource] = names[resource]
+ with pytest.raises(ClientError) as error:
+ client.get_configuration(
+ Application=selected["application"], Environment=selected["environment"],
+ Configuration=selected["profile"], ClientId=unique_name)
+ assert error.value.response["Error"]["Code"] == "BadRequestException"
+ assert error.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+ with pytest.raises(ClientError) as error:
+ data.start_configuration_session(
+ ApplicationIdentifier=selected["application"],
+ EnvironmentIdentifier=selected["environment"],
+ ConfigurationProfileIdentifier=selected["profile"])
+ assert error.value.response["Error"]["Code"] == "BadRequestException"
+ assert error.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+
+ response = client.get_configuration(
+ Application=app, Environment=env, Configuration=profile, ClientId=unique_name)
+ with response["Content"] as stream:
+ assert stream.read() == content
+ assert response["ConfigurationVersion"] == str(version)
+ token = data.start_configuration_session(
+ ApplicationIdentifier=app, EnvironmentIdentifier=env,
+ ConfigurationProfileIdentifier=profile)["InitialConfigurationToken"]
+ response = data.get_latest_configuration(ConfigurationToken=token)
+ with response["Configuration"] as stream:
+ assert stream.read() == content
diff --git a/docs/services/appconfig.md b/docs/services/appconfig.md
index 81561c1472..7d4375655b 100644
--- a/docs/services/appconfig.md
+++ b/docs/services/appconfig.md
@@ -35,6 +35,9 @@ The management plane allows you to create and manage applications, environments,
`GetConfiguration` accepts application, environment and configuration profile names or IDs, and requires
`client_id`. It returns the deployed version's content with its `Content-Type` and `Configuration-Version`;
feature flags are resolved as on the [data plane](#data-plane), including Ion for a caller that accepts it.
+An exact resource ID takes precedence over another resource's name. If multiple resources in the
+selected scope share a name, name lookup returns `BadRequestException`; use the intended resource's ID.
+This also applies to the management operations and `StartConfigurationSession` that use these lookups.
A `client_configuration_version` equal to the deployed version returns HTTP 204 with an empty body
and `Configuration-Version`, without `Content-Type` or `Content-Length`. An empty configuration
that the caller has not received still returns HTTP 200 and its stored content type.
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 e9f108d2a1..78e1c3df3e 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
@@ -75,7 +75,8 @@ public class AppConfigService {
/** The application whose ID or name is {@code idOrName}. */
public Application resolveApplication(String idOrName) {
return applicationStore.get(idOrName)
- .or(() -> listApplications().stream().filter(a -> idOrName.equals(a.getName())).findFirst())
+ .or(() -> uniqueNamed(listApplications().stream()
+ .filter(a -> idOrName.equals(a.getName())).toList(), "Application"))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Application not found", 404));
}
@@ -129,7 +130,8 @@ public class AppConfigService {
// An exact ID always wins over another environment's name, and a direct lookup skips the scan.
return environmentStore.get(idOrName)
.filter(e -> appId.equals(e.getApplicationId()))
- .or(() -> listEnvironments(appId).stream().filter(e -> idOrName.equals(e.getName())).findFirst())
+ .or(() -> uniqueNamed(listEnvironments(appId).stream()
+ .filter(e -> idOrName.equals(e.getName())).toList(), "Environment"))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Environment not found", 404));
}
@@ -190,10 +192,19 @@ public class AppConfigService {
public ConfigurationProfile resolveConfigurationProfile(String appId, String idOrName) {
return profileStore.get(idOrName)
.filter(p -> appId.equals(p.getApplicationId()))
- .or(() -> listConfigurationProfiles(appId).stream().filter(p -> idOrName.equals(p.getName())).findFirst())
+ .or(() -> uniqueNamed(listConfigurationProfiles(appId).stream()
+ .filter(p -> idOrName.equals(p.getName())).toList(), "Configuration profile"))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Configuration profile not found", 404));
}
+ private static <T> Optional<T> uniqueNamed(List<T> matches, String resourceType) {
+ if (matches.size() > 1) {
+ throw new AwsException("BadRequestException",
+ resourceType + " name is ambiguous; use its ID.", 400);
+ }
+ return matches.stream().findFirst();
+ }
+
public List<ConfigurationProfile> listConfigurationProfiles(String applicationIdentifier) {
String appId = resolveApplication(applicationIdentifier).getId();
return profileStore.scan(k -> true).stream()