From f06f1e7fd842890929cec9fe9602ab60ecae13d1 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 27 Nov 2026 16:16:58 -0600 Subject: [PATCH] fix(cloudmap): answer a resource by id only in the region it was created in (#9622) Cloud Map lists were filtered by region, but every operation that names a namespace, service or operation by id or ARN fetched it by the bare id, so a request signed for one region read, changed or deleted another region's resource. AWS keeps Cloud Map resources per account and region. GetNamespace, DeleteNamespace, CreateService, GetService, DeleteService, the instance operations, GetOperation and the tag operations now answer a record only in the region recorded for it, with the not-found error each operation declares (NamespaceNotFound, ServiceNotFound, OperationNotFound, ResourceNotFoundException). A record stored without a region is answered in every region, as it was. Signed-off-by: Hector Ventura --- docs/services/cloudmap.md | 8 ++ .../services/cloudmap/CloudMapHandler.java | 60 ++++---- .../services/cloudmap/CloudMapService.java | 129 ++++++++++++++---- .../proxy/CloudMapInstanceResolverTest.java | 4 +- .../cloudmap/CloudMapDnsResolutionTest.java | 6 +- .../cloudmap/CloudMapIntegrationTest.java | 36 +++++ .../cloudmap/CloudMapServiceTest.java | 2 +- 7 files changed, 185 insertions(+), 60 deletions(-) diff --git a/docs/services/cloudmap.md b/docs/services/cloudmap.md index 0233b0aa29..b1a6b00026 100644 --- a/docs/services/cloudmap.md +++ b/docs/services/cloudmap.md @@ -21,6 +21,14 @@ and report its id as `HostedZoneId`, so `route53 get-hosted-zone` and namespace; if you added records of your own to the zone, the delete fails with `HostedZoneNotEmpty` and the namespace is kept. HTTP namespaces have no zone. +Namespaces, services and operations are kept per account and region, as AWS +keeps them. An operation that names one by id or ARN (`GetNamespace`, +`GetService`, `GetOperation`, `DeleteService`, `RegisterInstance`, the tag +operations and the rest) answers it only in the region it was created in, and +returns `NamespaceNotFound`, `ServiceNotFound`, `OperationNotFound` or +`ResourceNotFoundException` for one created in another region. A record stored +before Floci recorded its region is answered in every region. + `ListNamespaces`, `ListServices`, and `ListOperations` accept `MaxResults` from 1 to 100 (default 100) and an opaque `NextToken`. Final and empty collections omit the token. Tokens are scoped to the action, account, region, and effective diff --git a/src/main/java/io/floci/aws/services/cloudmap/CloudMapHandler.java b/src/main/java/io/floci/aws/services/cloudmap/CloudMapHandler.java index 36c9ab2aa5..1f9afb6874 100644 --- a/src/main/java/io/floci/aws/services/cloudmap/CloudMapHandler.java +++ b/src/main/java/io/floci/aws/services/cloudmap/CloudMapHandler.java @@ -54,28 +54,28 @@ public class CloudMapHandler { case "CreatePrivateDnsNamespace" -> opResponse(service.createPrivateDnsNamespace( text(request, "Name"), text(request, "Vpc"), text(request, "CreatorRequestId"), text(request, "Description"), parseTags(request), region)); - case "GetNamespace" -> handleGetNamespace(request); + case "GetNamespace" -> handleGetNamespace(request, region); case "ListNamespaces" -> handleListNamespaces(request, region); case "DeleteNamespace" -> opResponse(service.deleteNamespace(text(request, "Id"), region)); - case "GetOperation" -> handleGetOperation(request); + case "GetOperation" -> handleGetOperation(request, region); case "ListOperations" -> handleListOperations(request, region); case "CreateService" -> handleCreateService(request, region); - case "GetService" -> handleGetService(request); + case "GetService" -> handleGetService(request, region); case "ListServices" -> handleListServices(request, region); - case "DeleteService" -> handleDeleteService(request); + case "DeleteService" -> handleDeleteService(request, region); case "RegisterInstance" -> opResponse(service.registerInstance( text(request, "ServiceId"), text(request, "InstanceId"), text(request, "CreatorRequestId"), parseAttributes(request.path("Attributes")), region)); case "DeregisterInstance" -> opResponse(service.deregisterInstance( text(request, "ServiceId"), text(request, "InstanceId"), region)); - case "GetInstance" -> handleGetInstance(request); - case "ListInstances" -> handleListInstances(request); - case "GetInstancesHealthStatus" -> handleGetInstancesHealthStatus(request); + case "GetInstance" -> handleGetInstance(request, region); + case "ListInstances" -> handleListInstances(request, region); + case "GetInstancesHealthStatus" -> handleGetInstancesHealthStatus(request, region); case "DiscoverInstances" -> handleDiscoverInstances(request, region); case "DiscoverInstancesRevision" -> handleDiscoverInstancesRevision(request, region); - case "TagResource" -> handleTagResource(request); - case "UntagResource" -> handleUntagResource(request); - case "ListTagsForResource" -> handleListTagsForResource(request); + case "TagResource" -> handleTagResource(request, region); + case "UntagResource" -> handleUntagResource(request, region); + case "ListTagsForResource" -> handleListTagsForResource(request, region); default -> Response.status(400) .entity(new AwsErrorResponse("UnknownOperationException", "Operation " + action + " is not supported.")) @@ -95,8 +95,8 @@ public class CloudMapHandler { // ──────────────────────────── Namespaces ──────────────────────────── - private Response handleGetNamespace(JsonNode request) { - Namespace ns = service.getNamespace(text(request, "Id")); + private Response handleGetNamespace(JsonNode request, String region) { + Namespace ns = service.getNamespace(text(request, "Id"), region); ObjectNode response = objectMapper.createObjectNode(); response.set("Namespace", buildNamespaceNode(ns)); return Response.ok(response).build(); @@ -119,8 +119,8 @@ public class CloudMapHandler { // ──────────────────────────── Operations ──────────────────────────── - private Response handleGetOperation(JsonNode request) { - Operation op = service.getOperation(text(request, "OperationId")); + private Response handleGetOperation(JsonNode request, String region) { + Operation op = service.getOperation(text(request, "OperationId"), region); ObjectNode response = objectMapper.createObjectNode(); response.set("Operation", buildOperationNode(op)); return Response.ok(response).build(); @@ -158,8 +158,8 @@ public class CloudMapHandler { return Response.ok(response).build(); } - private Response handleGetService(JsonNode request) { - Service s = service.getService(text(request, "Id")); + private Response handleGetService(JsonNode request, String region) { + Service s = service.getService(text(request, "Id"), region); ObjectNode response = objectMapper.createObjectNode(); response.set("Service", buildServiceNode(s)); return Response.ok(response).build(); @@ -183,15 +183,15 @@ public class CloudMapHandler { return Response.ok(response).build(); } - private Response handleDeleteService(JsonNode request) { - service.deleteService(text(request, "Id")); + private Response handleDeleteService(JsonNode request, String region) { + service.deleteService(text(request, "Id"), region); return Response.ok(objectMapper.createObjectNode()).build(); } // ──────────────────────────── Instances ──────────────────────────── - private Response handleGetInstance(JsonNode request) { - Instance instance = service.getInstance(text(request, "ServiceId"), text(request, "InstanceId")); + private Response handleGetInstance(JsonNode request, String region) { + Instance instance = service.getInstance(text(request, "ServiceId"), text(request, "InstanceId"), region); ObjectNode response = objectMapper.createObjectNode(); ObjectNode node = response.putObject("Instance"); node.put("Id", instance.getInstanceId()); @@ -202,8 +202,8 @@ public class CloudMapHandler { return Response.ok(response).build(); } - private Response handleListInstances(JsonNode request) { - List instances = service.listInstances(text(request, "ServiceId")); + private Response handleListInstances(JsonNode request, String region) { + List instances = service.listInstances(text(request, "ServiceId"), region); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Instances"); for (Instance i : instances) { @@ -215,10 +215,10 @@ public class CloudMapHandler { return Response.ok(response).build(); } - private Response handleGetInstancesHealthStatus(JsonNode request) { + private Response handleGetInstancesHealthStatus(JsonNode request, String region) { List ids = new ArrayList<>(); request.path("Instances").forEach(n -> ids.add(n.asText())); - Map status = service.getInstancesHealthStatus(text(request, "ServiceId"), ids); + Map status = service.getInstancesHealthStatus(text(request, "ServiceId"), ids, region); ObjectNode response = objectMapper.createObjectNode(); ObjectNode statusNode = response.putObject("Status"); status.forEach(statusNode::put); @@ -258,20 +258,20 @@ public class CloudMapHandler { // ──────────────────────────── Tags ──────────────────────────── - private Response handleTagResource(JsonNode request) { - service.tagResource(text(request, "ResourceARN"), parseTags(request)); + private Response handleTagResource(JsonNode request, String region) { + service.tagResource(text(request, "ResourceARN"), parseTags(request), region); return Response.ok(objectMapper.createObjectNode()).build(); } - private Response handleUntagResource(JsonNode request) { + private Response handleUntagResource(JsonNode request, String region) { List keys = new ArrayList<>(); request.path("TagKeys").forEach(k -> keys.add(k.asText())); - service.untagResource(text(request, "ResourceARN"), keys); + service.untagResource(text(request, "ResourceARN"), keys, region); return Response.ok(objectMapper.createObjectNode()).build(); } - private Response handleListTagsForResource(JsonNode request) { - Map tags = service.listTagsForResource(text(request, "ResourceARN")); + private Response handleListTagsForResource(JsonNode request, String region) { + Map tags = service.listTagsForResource(text(request, "ResourceARN"), region); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Tags"); tags.forEach((k, v) -> { diff --git a/src/main/java/io/floci/aws/services/cloudmap/CloudMapService.java b/src/main/java/io/floci/aws/services/cloudmap/CloudMapService.java index 91d8411d81..92469a2ac2 100644 --- a/src/main/java/io/floci/aws/services/cloudmap/CloudMapService.java +++ b/src/main/java/io/floci/aws/services/cloudmap/CloudMapService.java @@ -51,6 +51,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import java.util.function.Predicate; /** * Cloud Map (AWS service discovery) business logic. Mirrors the EventBridge service @@ -175,6 +176,11 @@ public class CloudMapService implements StoredResourceArns { return requireNamespace(id); } + /** The namespace of the id as GetNamespace answers it in the Region: only one created there. */ + public Namespace getNamespace(String id, String region) { + return requireNamespace(id, region); + } + /** Whether a namespace is a DNS one, public or private, whose service names are DNS labels. */ public static boolean isDnsNamespace(Namespace namespace) { return "DNS_PRIVATE".equals(namespace.getType()) || "DNS_PUBLIC".equals(namespace.getType()); @@ -202,7 +208,7 @@ public class CloudMapService implements StoredResourceArns { } public Operation deleteNamespace(String id, String region) { - Namespace ns = requireNamespace(id); + Namespace ns = requireNamespace(id, region); boolean hasServices = scan(serviceStore).stream() .anyMatch(s -> ns.getId().equals(s.getNamespaceId())); if (hasServices) { @@ -243,7 +249,7 @@ public class CloudMapService implements StoredResourceArns { resolvedNamespaceId = dnsConfigNode.path("NamespaceId").textValue(); } if (resolvedNamespaceId != null) { - requireNamespace(resolvedNamespaceId); + requireNamespace(resolvedNamespaceId, region); } final String nsId = resolvedNamespaceId; if (findServiceByName(name, nsId, region).isPresent()) { @@ -282,12 +288,18 @@ public class CloudMapService implements StoredResourceArns { return requireService(id); } + /** The service of the id as GetService answers it in the Region: only one created there. */ + public Service getService(String id, String region) { + return requireService(id, region); + } + /** * The service named {@code name} in the namespace {@code namespaceId} of {@code region}, of * which there is at most one. A DNS namespace compares names without case, as DNS labels are. */ public Optional findServiceByName(String name, String namespaceId, String region) { - Namespace namespace = namespaceId == null ? null : namespaceStore.get(namespaceId).orElse(null); + Namespace namespace = namespaceId == null ? null : namespaceStore.get(namespaceId) + .filter(n -> answeredIn(n.getRegion(), region)).orElse(null); boolean dnsNamespace = namespace != null && isDnsNamespace(namespace); return scan(serviceStore).stream() .filter(s -> region.equals(s.getRegion()) && Objects.equals(namespaceId, s.getNamespaceId()) @@ -340,15 +352,24 @@ public class CloudMapService implements StoredResourceArns { } public void deleteService(String id) { - Service service = requireService(id); + deleteService(requireService(id)); + } + + /** Deletes the service of the id as DeleteService does in the Region: only one created there. */ + public void deleteService(String id, String region) { + deleteService(requireService(id, region)); + } + + private void deleteService(Service service) { boolean hasInstances = !scanInstances(service.getId()).isEmpty(); if (hasInstances) { throw new AwsException("ResourceInUse", "The service contains registered instances and cannot be deleted.", 400); } - serviceStore.delete(id); + serviceStore.delete(service.getId()); if (service.getNamespaceId() != null) { - namespaceStore.get(service.getNamespaceId()).ifPresent(n -> { + namespaceStore.get(service.getNamespaceId()) + .filter(n -> answeredIn(n.getRegion(), service.getRegion())).ifPresent(n -> { n.setServiceCount(Math.max(0, n.getServiceCount() - 1)); namespaceStore.put(n.getId(), n); }); @@ -359,7 +380,7 @@ public class CloudMapService implements StoredResourceArns { public Operation registerInstance(String serviceId, String instanceId, String creatorRequestId, Map attributes, String region) { - Service service = requireService(serviceId); + Service service = requireService(serviceId, region); if (instanceId == null || instanceId.isBlank()) { throw new AwsException("InvalidInput", "InstanceId is required.", 400); } @@ -393,7 +414,7 @@ public class CloudMapService implements StoredResourceArns { } public Operation deregisterInstance(String serviceId, String instanceId, String region) { - Service service = requireService(serviceId); + Service service = requireService(serviceId, region); if (instanceStore.get(instanceKey(serviceId, instanceId)).isEmpty()) { throw new AwsException("InstanceNotFound", "Instance not found: " + instanceId, 404); } @@ -404,8 +425,8 @@ public class CloudMapService implements StoredResourceArns { return submitOperation("DEREGISTER_INSTANCE", "INSTANCE", instanceId, region); } - public Instance getInstance(String serviceId, String instanceId) { - requireService(serviceId); + public Instance getInstance(String serviceId, String instanceId, String region) { + requireService(serviceId, region); return instanceStore.get(instanceKey(serviceId, instanceId)) .orElseThrow(() -> new AwsException("InstanceNotFound", "Instance not found: " + instanceId, 404)); } @@ -415,8 +436,14 @@ public class CloudMapService implements StoredResourceArns { return scanInstances(serviceId); } - public Map getInstancesHealthStatus(String serviceId, List instanceIds) { - requireService(serviceId); + /** The instances of the service of the id as ListInstances answers them in the Region: only one created there. */ + public List listInstances(String serviceId, String region) { + requireService(serviceId, region); + return scanInstances(serviceId); + } + + public Map getInstancesHealthStatus(String serviceId, List instanceIds, String region) { + requireService(serviceId, region); Map status = new LinkedHashMap<>(); for (Instance i : scanInstances(serviceId)) { if (instanceIds == null || instanceIds.isEmpty() || instanceIds.contains(i.getInstanceId())) { @@ -796,8 +823,9 @@ public class CloudMapService implements StoredResourceArns { // ──────────────────────────── Operations ──────────────────────────── - public Operation getOperation(String id) { + public Operation getOperation(String id, String region) { return operationStore.get(id) + .filter(op -> answeredIn(op.getRegion(), region)) .orElseThrow(() -> new AwsException("OperationNotFound", "Operation not found: " + id, 404)); } @@ -892,7 +920,12 @@ public class CloudMapService implements StoredResourceArns { // ──────────────────────────── Tags ──────────────────────────── public Map listTagsForResource(String resourceArn) { - return taggable(resourceArn).getTags(); + return taggable(taggableRaw(resourceArn)).getTags(); + } + + /** The tags of the namespace or the service of the ARN as ListTagsForResource answers them in the Region. */ + public Map listTagsForResource(String resourceArn, String region) { + return taggable(taggableRaw(resourceArn, region)).getTags(); } /** @@ -900,7 +933,15 @@ public class CloudMapService implements StoredResourceArns { * two of them never lose one's tags, and a lookup reads one whole set. */ public synchronized void tagResource(String resourceArn, Map tags) { - Object resource = taggableRaw(resourceArn); + addTags(taggableRaw(resourceArn), tags); + } + + /** Tags the namespace or the service of the ARN as TagResource does in the Region: only one created there. */ + public synchronized void tagResource(String resourceArn, Map tags, String region) { + addTags(taggableRaw(resourceArn, region), tags); + } + + private void addTags(Object resource, Map tags) { if (resource instanceof Namespace ns) { Map merged = new LinkedHashMap<>(ns.getTags()); merged.putAll(tags); @@ -916,7 +957,15 @@ public class CloudMapService implements StoredResourceArns { /** Gives the namespace or the service a new map without {@code tagKeys}, one tag call at a time. */ public synchronized void untagResource(String resourceArn, List tagKeys) { - Object resource = taggableRaw(resourceArn); + removeTags(taggableRaw(resourceArn), tagKeys); + } + + /** Untags the namespace or the service of the ARN as UntagResource does in the Region: only one created there. */ + public synchronized void untagResource(String resourceArn, List tagKeys, String region) { + removeTags(taggableRaw(resourceArn, region), tagKeys); + } + + private void removeTags(Object resource, List tagKeys) { if (resource instanceof Namespace ns) { Map remaining = new LinkedHashMap<>(ns.getTags()); tagKeys.forEach(remaining::remove); @@ -944,8 +993,8 @@ public class CloudMapService implements StoredResourceArns { /** * The tags of the namespace or the service whose ARN is {@code arn}, in the request's account, where the tag * handlers read: an ARN of another account names none. Each is read in place by the id its ARN ends in, one stored - * before storage was account-aware included. That is the id the handlers find it by, in every Region, so an ARN - * naming another Region than the resource's reads its tags too, as a request sent there acts on it. A tag without + * before storage was account-aware included. That is the id the handlers find it by, so an ARN naming another + * Region than the resource's reads its tags too, although a request sent there finds no resource. A tag without * a value reads as an empty one. Tag calls give the resource a new map, so the read is of one whole set. */ @Override @@ -974,8 +1023,7 @@ public class CloudMapService implements StoredResourceArns { private interface Taggable { Map getTags(); } - private Taggable taggable(String resourceArn) { - Object raw = taggableRaw(resourceArn); + private static Taggable taggable(Object raw) { if (raw instanceof Namespace ns) { return ns::getTags; } @@ -993,28 +1041,61 @@ public class CloudMapService implements StoredResourceArns { if (ns.isPresent()) { return ns.get(); } - return serviceStore.get(id).orElseThrow(() -> - new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 404)); + return serviceStore.get(id).orElseThrow(() -> resourceNotFound(resourceArn)); + } + + private Object taggableRaw(String resourceArn, String region) { + Object raw = taggableRaw(resourceArn); + String createdIn = raw instanceof Namespace ns ? ns.getRegion() : ((Service) raw).getRegion(); + if (!answeredIn(createdIn, region)) { + throw resourceNotFound(resourceArn); + } + return raw; + } + + private static AwsException resourceNotFound(String resourceArn) { + return new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 404); } // ──────────────────────────── Helpers ──────────────────────────── private Namespace requireNamespace(String id) { + return requireNamespace(id, namespace -> true); + } + + private Namespace requireNamespace(String id, String region) { + return requireNamespace(id, namespace -> answeredIn(namespace.getRegion(), region)); + } + + private Namespace requireNamespace(String id, Predicate answered) { if (id == null) { throw new AwsException("InvalidInput", "Namespace id is required.", 400); } - return namespaceStore.get(id) + return namespaceStore.get(id).filter(answered) .orElseThrow(() -> new AwsException("NamespaceNotFound", "Namespace not found: " + id, 404)); } private Service requireService(String id) { + return requireService(id, service -> true); + } + + private Service requireService(String id, String region) { + return requireService(id, service -> answeredIn(service.getRegion(), region)); + } + + private Service requireService(String id, Predicate answered) { if (id == null) { throw new AwsException("InvalidInput", "Service id is required.", 400); } - return serviceStore.get(id) + return serviceStore.get(id).filter(answered) .orElseThrow(() -> new AwsException("ServiceNotFound", "Service not found: " + id, 404)); } + /** Whether Cloud Map answers a record created in {@code createdIn} in the Region: created there, or unrecorded. */ + private static boolean answeredIn(String createdIn, String region) { + return createdIn == null || createdIn.equals(region); + } + private List scanInstances(String serviceId) { List result = new ArrayList<>(); for (Instance i : scan(instanceStore)) { diff --git a/src/test/java/io/floci/aws/services/apigatewayv2/proxy/CloudMapInstanceResolverTest.java b/src/test/java/io/floci/aws/services/apigatewayv2/proxy/CloudMapInstanceResolverTest.java index 68fe760461..d9a2bb28f5 100644 --- a/src/test/java/io/floci/aws/services/apigatewayv2/proxy/CloudMapInstanceResolverTest.java +++ b/src/test/java/io/floci/aws/services/apigatewayv2/proxy/CloudMapInstanceResolverTest.java @@ -50,9 +50,9 @@ class CloudMapInstanceResolverTest { // Floci has no API that marks an instance UNHEALTHY, so the test flips the stored instance directly private void markUnhealthy(String serviceId, String instanceId) { - Instance instance = cloudMap.getInstance(serviceId, instanceId); + Instance instance = cloudMap.getInstance(serviceId, instanceId, REGION); instance.setHealthStatus("UNHEALTHY"); - assertEquals("UNHEALTHY", cloudMap.getInstance(serviceId, instanceId).getHealthStatus()); + assertEquals("UNHEALTHY", cloudMap.getInstance(serviceId, instanceId, REGION).getHealthStatus()); } private static Map address(String ip, String port) { diff --git a/src/test/java/io/floci/aws/services/cloudmap/CloudMapDnsResolutionTest.java b/src/test/java/io/floci/aws/services/cloudmap/CloudMapDnsResolutionTest.java index e408e40082..aab34c6787 100644 --- a/src/test/java/io/floci/aws/services/cloudmap/CloudMapDnsResolutionTest.java +++ b/src/test/java/io/floci/aws/services/cloudmap/CloudMapDnsResolutionTest.java @@ -361,7 +361,7 @@ class CloudMapDnsResolutionTest { cloudMapService.registerInstance(service.getId(), "task-" + i, null, Map.of("AWS_INSTANCE_IPV6", "2001:db8::" + i), REGION); } - cloudMapService.getInstance(service.getId(), "task-1").setHealthStatus("UNHEALTHY"); + cloudMapService.getInstance(service.getId(), "task-1", REGION).setHealthStatus("UNHEALTHY"); Set sampled = new HashSet<>(); for (int i = 0; i < 100; i++) { DnsAnswer answer = cloudMapService.resolveDnsNameIfOwned("weighted." + namespace, 28).orElseThrow(); @@ -370,8 +370,8 @@ class CloudMapDnsResolutionTest { sampled.addAll(answer.records()); } assertEquals(2, sampled.size()); - cloudMapService.getInstance(service.getId(), "task-2").setHealthStatus("UNHEALTHY"); - cloudMapService.getInstance(service.getId(), "task-3").setHealthStatus("UNHEALTHY"); + cloudMapService.getInstance(service.getId(), "task-2", REGION).setHealthStatus("UNHEALTHY"); + cloudMapService.getInstance(service.getId(), "task-3", REGION).setHealthStatus("UNHEALTHY"); assertEquals(1, cloudMapService.resolveDnsNameIfOwned("weighted." + namespace, 28).orElseThrow().records().size()); } diff --git a/src/test/java/io/floci/aws/services/cloudmap/CloudMapIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudmap/CloudMapIntegrationTest.java index 6762c65fce..2aa4e53941 100644 --- a/src/test/java/io/floci/aws/services/cloudmap/CloudMapIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudmap/CloudMapIntegrationTest.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import static io.floci.aws.testing.EnforcementFixtures.auth; import static io.restassured.RestAssured.given; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -287,6 +288,41 @@ class CloudMapIntegrationTest { call("GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200); } + @Test + void byIdOperationsAnswerOnlyInTheRegionOfTheResource() { + String operationId = call("CreateHttpNamespace", "{\"Name\":\"cm-region-ns\"}") + .then().statusCode(200).extract().jsonPath().getString("OperationId"); + String nsId = call("GetOperation", "{\"OperationId\":\"" + operationId + "\"}") + .then().statusCode(200).extract().jsonPath().getString("Operation.Targets.NAMESPACE"); + String svcId = call("CreateService", "{\"Name\":\"cm-region-svc\",\"NamespaceId\":\"" + nsId + "\"}") + .then().statusCode(200).extract().jsonPath().getString("Service.Id"); + + callIn("eu-west-1", "GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(404) + .body("__type", equalTo("NamespaceNotFound")); + callIn("eu-west-1", "GetService", "{\"Id\":\"" + svcId + "\"}").then().statusCode(404) + .body("__type", equalTo("ServiceNotFound")); + callIn("eu-west-1", "GetOperation", "{\"OperationId\":\"" + operationId + "\"}").then().statusCode(404) + .body("__type", equalTo("OperationNotFound")); + callIn("eu-west-1", "DeleteService", "{\"Id\":\"" + svcId + "\"}").then().statusCode(404) + .body("__type", equalTo("ServiceNotFound")); + + callIn("us-east-1", "GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200) + .body("Namespace.Id", equalTo(nsId)); + callIn("us-east-1", "GetService", "{\"Id\":\"" + svcId + "\"}").then().statusCode(200) + .body("Service.Id", equalTo(svcId)); + callIn("us-east-1", "GetOperation", "{\"OperationId\":\"" + operationId + "\"}").then().statusCode(200) + .body("Operation.Id", equalTo(operationId)); + + callIn("us-east-1", "DeleteService", "{\"Id\":\"" + svcId + "\"}").then().statusCode(200); + callIn("us-east-1", "DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200); + } + + private static Response callIn(String region, String action, String body) { + return given().contentType(CT).header("X-Amz-Target", PREFIX + action) + .header("Authorization", auth("test", region, "servicediscovery")) + .body(body).when().post("/"); + } + private static String createDnsNamespace(String action, String body) { String operationId = call(action, body).then().statusCode(200) .extract().jsonPath().getString("OperationId"); diff --git a/src/test/java/io/floci/aws/services/cloudmap/CloudMapServiceTest.java b/src/test/java/io/floci/aws/services/cloudmap/CloudMapServiceTest.java index a610ced9f2..2821c3c3bb 100644 --- a/src/test/java/io/floci/aws/services/cloudmap/CloudMapServiceTest.java +++ b/src/test/java/io/floci/aws/services/cloudmap/CloudMapServiceTest.java @@ -120,7 +120,7 @@ class CloudMapServiceTest { Service service = createService(privateDnsNamespace(), "health", null, null); register(service, "up", Map.of("AWS_INSTANCE_IPV4", "10.0.0.1")); register(service, "down", Map.of("AWS_INSTANCE_IPV4", "10.0.0.2")); - cloudMapService.getInstance(service.getId(), "down").setHealthStatus("UNHEALTHY"); + cloudMapService.getInstance(service.getId(), "down", REGION).setHealthStatus("UNHEALTHY"); assertEquals(List.of("up"), instanceIds(cloudMapService.filterInstances(service.getId(), "HEALTHY", Map.of()))); assertEquals(List.of("down"), instanceIds(cloudMapService.filterInstances(service.getId(), "UNHEALTHY", Map.of())));