fix(opensearch): scope domains to the request's region (#6682)

* fix(opensearch): scope domains to the request's region

Domain records were keyed by the bare name, so a domain created in one
region could be described, updated and deleted from another, and the
same name conflicted across regions. Key the store by region and name,
resolve every operation inside the region the request is signed for,
and migrate pre-scoping records onto the scoped key taking the region
from their ARN. Region-scope the legacy host-path data directory per
account and region, keeping the bare directory for records that predate
the region marker while it still exists.

Closes #6081

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(opensearch): reject a tag ARN naming another account as not found

findByArn resolved the account named in the ARN, where every fixed
lookup only reached the caller's account through the account-aware
storage backend, so a caller could read and change another account's
domain tags under the default settings. An ARN naming a foreign account
now answers ResourceNotFoundException the way Lambda layers, EventBridge
and Redshift answer theirs, and the RegionScoping suite pins it for the
three tag operations.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-28 20:09:22 -04:00
1 parent fc1388da6f
commit 347e8f63a9
9 files changed
+884 -70

No files matched your search

@@ -21,6 +21,7 @@ import org.mockito.InOrder;
import org.mockito.MockMakers;
import org.mockito.MockedStatic;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@@ -578,6 +579,70 @@ class OpenSearchDomainManagerTest {
assertNull(domain.getHostPort());
}
/**
* Legacy host-path mode binds one directory per account and region, so two domains sharing
* a name across regions (or accounts) never share one data directory, which would corrupt
* both clusters.
*/
@ParameterizedTest
@ValueSource(strings = {"", "team-a"})
void hostPathStorageSeparatesAccountsAndRegions(String namespace, @TempDir Path hostRoot) {
when(storage.hostPersistentPath()).thenReturn(hostRoot.toString());
when(docker.resourceNamespace()).thenReturn(Optional.of(namespace));
List<Domain> domains = List.of(domain("000000000000", "us-east-1"),
domain("111111111111", "us-east-1"), domain("000000000000", "eu-west-1"));
for (Domain domain : domains) {
domain.setRegion(AwsArnUtils.parse(domain.getArn()).region());
}
for (Domain domain : domains) {
manager.startDomain(domain);
}
String prefix = namespace.isEmpty() ? "floci-aws-opensearch-" : "floci-aws-team-a-opensearch-";
Path serviceRoot = namespace.isEmpty()
? hostRoot.resolve("opensearch")
: hostRoot.resolve("team-a").resolve("opensearch");
List<String> expectedBinds = List.of(
serviceRoot.resolve(prefix + "000000000000-us-east-1-my-domain"),
serviceRoot.resolve(prefix + "111111111111-us-east-1-my-domain"),
serviceRoot.resolve(prefix + "000000000000-eu-west-1-my-domain"))
.stream().map(path -> path.toAbsolutePath().normalize().toString()).toList();
ArgumentCaptor<String> binds = ArgumentCaptor.forClass(String.class);
verify(builder, times(3)).withBind(binds.capture(), anyString());
assertEquals(expectedBinds, binds.getAllValues());
}
/**
* A record persisted before the region marker keeps the bare-name directory while that
* directory still exists, since that is where its data lives.
*/
@Test
void hostPathKeepsTheBareDirectoryForARecordPredatingTheRegionMarker(
@TempDir Path hostRoot) throws IOException {
when(storage.hostPersistentPath()).thenReturn(hostRoot.toString());
Path legacyDir = hostRoot.resolve("opensearch").resolve("my-domain");
Files.createDirectories(legacyDir);
Domain domain = domain("000000000000", "us-east-1");
manager.startDomain(domain);
verify(builder).withBind(legacyDir.toAbsolutePath().normalize().toString(), "/usr/share/opensearch/data");
}
/** With no bare-name directory on disk, even a marker-less record starts on the scoped one. */
@Test
void hostPathUsesTheScopedDirectoryWhenNoBareDirectoryExists(@TempDir Path hostRoot) {
when(storage.hostPersistentPath()).thenReturn(hostRoot.toString());
Domain domain = domain("000000000000", "us-east-1");
manager.startDomain(domain);
verify(builder).withBind(hostRoot.resolve("opensearch")
.resolve("floci-aws-opensearch-000000000000-us-east-1-my-domain")
.toAbsolutePath().normalize().toString(), "/usr/share/opensearch/data");
}
private Domain domain(String accountId, String region) {
Domain domain = new Domain();
domain.setDomainName("my-domain");
@@ -0,0 +1,316 @@
package io.github.hectorvent.floci.services.opensearch;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
/**
* OpenSearch domains are regional: a name is unique inside one account and region, and
* describe, update, delete and list resolve only in the region the request is signed for.
*/
@QuarkusTest
class OpenSearchRegionScopingIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/es/aws4_request";
private static final String OTHER_REGION_AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/eu-west-1/es/aws4_request";
private static final String SHARED_NAME = "shared-regional";
private static final String EAST_ONLY_NAME = "east-regional";
private static final String WEST_ONLY_NAME = "west-regional";
@AfterEach
void cleanup() {
for (String name : new String[]{SHARED_NAME, EAST_ONLY_NAME, WEST_ONLY_NAME}) {
given().header("Authorization", AUTH_HEADER)
.when().delete("/2021-01-01/opensearch/domain/" + name);
given().header("Authorization", OTHER_REGION_AUTH_HEADER)
.when().delete("/2021-01-01/opensearch/domain/" + name);
}
}
@Test
void theSameNameCoexistsInTwoRegionsAndEachArnCarriesItsRegion() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\",\"EngineVersion\":\"OpenSearch_2.11\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200)
.body("DomainStatus.ARN", equalTo("arn:aws:es:us-east-1:000000000000:domain/" + SHARED_NAME));
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\",\"EngineVersion\":\"OpenSearch_2.11\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200)
.body("DomainStatus.ARN", equalTo("arn:aws:es:eu-west-1:000000000000:domain/" + SHARED_NAME));
}
@Test
void createConflictsOnlyWithinTheSameRegion() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(409);
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
}
@Test
void describeAndDescribeConfigSeeOnlyTheRequestRegionsDomains() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + EAST_ONLY_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + EAST_ONLY_NAME)
.then()
.statusCode(409);
given()
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + EAST_ONLY_NAME + "/config")
.then()
.statusCode(409);
}
@Test
void listDomainNamesListsOnlyTheRequestRegionsDomains() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + EAST_ONLY_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + WEST_ONLY_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/2021-01-01/domain")
.then()
.statusCode(200)
.body("DomainNames.DomainName", hasItem(EAST_ONLY_NAME))
.body("DomainNames.DomainName", not(hasItem(WEST_ONLY_NAME)));
given()
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.get("/2021-01-01/domain")
.then()
.statusCode(200)
.body("DomainNames.DomainName", hasItem(WEST_ONLY_NAME))
.body("DomainNames.DomainName", not(hasItem(EAST_ONLY_NAME)));
}
@Test
void updateInOneRegionLeavesTheOtherRegionUntouched() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"ClusterConfig\":{\"InstanceCount\":5}}")
.when()
.post("/2021-01-01/opensearch/domain/" + SHARED_NAME + "/config")
.then()
.statusCode(200)
.body("DomainConfig.ClusterConfig.Options.InstanceCount", equalTo(5));
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + SHARED_NAME)
.then()
.statusCode(200)
.body("DomainStatus.ClusterConfig.InstanceCount", equalTo(5));
given()
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + SHARED_NAME)
.then()
.statusCode(200)
.body("DomainStatus.ClusterConfig.InstanceCount", equalTo(1));
}
@Test
void deleteInOneRegionLeavesTheOtherRegionIntact() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH_HEADER)
.when()
.delete("/2021-01-01/opensearch/domain/" + SHARED_NAME)
.then()
.statusCode(200)
.body("DomainStatus.Deleted", equalTo(true));
given()
.header("Authorization", AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + SHARED_NAME)
.then()
.statusCode(409);
given()
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.get("/2021-01-01/opensearch/domain/" + SHARED_NAME)
.then()
.statusCode(200)
.body("DomainStatus.ARN", equalTo("arn:aws:es:eu-west-1:000000000000:domain/" + SHARED_NAME));
}
@Test
void upgradeDomainResolvesOnlyInTheRequestRegion() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\",\"EngineVersion\":\"OpenSearch_2.11\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
given()
.contentType("application/json")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.body("{\"DomainName\":\"" + SHARED_NAME + "\",\"TargetVersion\":\"OpenSearch_2.13\"}")
.when()
.post("/2021-01-01/opensearch/upgradeDomain")
.then()
.statusCode(409);
}
@Test
void tagOperationsRejectAForeignAccountArnAsNotFound() {
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"DomainName\":\"" + EAST_ONLY_NAME + "\",\"EngineVersion\":\"OpenSearch_2.11\"}")
.when()
.post("/2021-01-01/opensearch/domain")
.then()
.statusCode(200);
String foreignArn = "arn:aws:es:us-east-1:387263792837:domain/" + EAST_ONLY_NAME;
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"ARN\":\"" + foreignArn + "\",\"TagList\":[{\"Key\":\"env\",\"Value\":\"test\"}]}")
.when()
.post("/2021-01-01/tags")
.then()
.statusCode(409)
.body("__type", equalTo("ResourceNotFoundException"));
given()
.header("Authorization", AUTH_HEADER)
.queryParam("arn", foreignArn)
.when()
.get("/2021-01-01/tags/")
.then()
.statusCode(409)
.body("__type", equalTo("ResourceNotFoundException"));
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"ARN\":\"" + foreignArn + "\",\"TagKeys\":[\"env\"]}")
.when()
.post("/2021-01-01/tags-removal")
.then()
.statusCode(409)
.body("__type", equalTo("ResourceNotFoundException"));
String localArn = "arn:aws:es:us-east-1:000000000000:domain/" + EAST_ONLY_NAME;
given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("{\"ARN\":\"" + localArn + "\",\"TagList\":[{\"Key\":\"env\",\"Value\":\"test\"}]}")
.when()
.post("/2021-01-01/tags")
.then()
.statusCode(200);
}
}
@@ -1,16 +1,22 @@
package io.github.hectorvent.floci.services.opensearch;
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.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.opensearch.model.ClusterConfig;
import io.github.hectorvent.floci.services.opensearch.model.Domain;
import io.github.hectorvent.floci.services.opensearch.model.VolumeOwnership;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeast;
@@ -21,10 +27,18 @@ import static org.mockito.Mockito.when;
/**
* A domain stays {@code Processing=true} until the readiness poller flips it. Mock mode and a
* Floci with no reachable Docker daemon report {@code Processing=false} from the first read.
*
* <p>Domains are scoped to an account and a region: the same name can exist in two regions,
* and describe, update, delete and list only ever see the caller's region.
*/
class OpenSearchServiceTest {
private static final String ACCOUNT = "000000000000";
private static final String US_EAST_1 = "us-east-1";
private static final String EU_WEST_1 = "eu-west-1";
private OpenSearchService service;
private AccountAwareStorageBackend<Domain> domainStore;
private OpenSearchDomainManager domainManager;
private EmulatorConfig.OpenSearchServiceConfig osConfig;
@@ -38,9 +52,9 @@ class OpenSearchServiceTest {
when(servicesConfig.opensearch()).thenReturn(osConfig);
when(osConfig.mock()).thenReturn(false);
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
service = new OpenSearchService(AccountAwareStorageBackend.inMemory("000000000000"),
config, regionResolver, domainManager);
RegionResolver regionResolver = new RegionResolver(US_EAST_1, ACCOUNT);
domainStore = AccountAwareStorageBackend.inMemory(ACCOUNT);
service = new OpenSearchService(domainStore, config, regionResolver, domainManager);
}
@Test
@@ -48,11 +62,11 @@ class OpenSearchServiceTest {
when(domainManager.tryStartDomain(any())).thenReturn(true);
Domain domain = service.createDomain("docker-domain", "OpenSearch_2.11",
null, null, null, "us-east-1");
null, null, null, US_EAST_1);
assertTrue(domain.isProcessing(),
"the readiness poller flips Processing once the container answers");
assertTrue(service.describeDomain("docker-domain").isProcessing());
assertTrue(service.describeDomain("docker-domain", US_EAST_1).isProcessing());
verify(domainManager).tryStartDomain(any());
}
@@ -66,7 +80,7 @@ class OpenSearchServiceTest {
when(domainManager.tryStartDomain(any())).thenReturn(false);
Domain domain = service.createDomain("stamped-domain", "OpenSearch_2.11",
null, null, null, "us-east-1");
null, null, null, US_EAST_1);
assertEquals(VolumeOwnership.FLOCI_MANAGED, domain.getVolumeOwnership());
assertTrue(domain.getDockerVolumeName().startsWith("floci-aws-opensearch-"),
@@ -81,11 +95,11 @@ class OpenSearchServiceTest {
when(domainManager.tryStartDomain(any())).thenReturn(false);
Domain domain = service.createDomain("no-docker-domain", "OpenSearch_2.11",
null, null, null, "us-east-1");
null, null, null, US_EAST_1);
assertFalse(domain.isProcessing());
assertEquals("no-docker-domain", service.describeDomain("no-docker-domain").getDomainName());
assertFalse(service.describeDomain("no-docker-domain").isProcessing());
assertEquals("no-docker-domain", service.describeDomain("no-docker-domain", US_EAST_1).getDomainName());
assertFalse(service.describeDomain("no-docker-domain", US_EAST_1).isProcessing());
}
@Test
@@ -93,7 +107,7 @@ class OpenSearchServiceTest {
when(osConfig.mock()).thenReturn(true);
Domain domain = service.createDomain("mock-domain", "OpenSearch_2.11",
null, null, null, "us-east-1");
null, null, null, US_EAST_1);
assertFalse(domain.isProcessing());
verify(domainManager, Mockito.never()).tryStartDomain(any());
@@ -107,13 +121,13 @@ class OpenSearchServiceTest {
.thenReturn(true);
service.createDomain("ready-recovery", "OpenSearch_2.11",
null, null, null, "us-east-1");
null, null, null, US_EAST_1);
service.pollReadiness();
assertTrue(service.describeDomain("ready-recovery").isProcessing());
assertTrue(service.describeDomain("ready-recovery", US_EAST_1).isProcessing());
service.pollReadiness();
assertFalse(service.describeDomain("ready-recovery").isProcessing());
assertFalse(service.describeDomain("ready-recovery", US_EAST_1).isProcessing());
verify(domainManager, atLeast(2)).isReady(any());
}
@@ -121,10 +135,144 @@ class OpenSearchServiceTest {
void shutdownLeavesDomainContainersRunningWhenRetentionIsEnabled() {
when(domainManager.tryStartDomain(any())).thenReturn(true);
when(osConfig.keepRunningOnShutdown()).thenReturn(true);
service.createDomain("kept-domain", "OpenSearch_2.11", null, null, null, "us-east-1");
service.createDomain("kept-domain", "OpenSearch_2.11", null, null, null, US_EAST_1);
service.shutdown();
verify(domainManager, Mockito.never()).stopDomain(any());
}
@Test
void sameNameInTwoRegionsCoexistsAndEachArnCarriesItsRegion() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
Domain east = service.createDomain("dual-region", "OpenSearch_2.11", null, null, null, US_EAST_1);
Domain west = service.createDomain("dual-region", "OpenSearch_2.11", null, null, null, EU_WEST_1);
assertEquals(AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/dual-region").toString(),
east.getArn());
assertEquals(AwsArnUtils.Arn.of("es", EU_WEST_1, ACCOUNT, "domain/dual-region").toString(),
west.getArn());
assertEquals(US_EAST_1, service.describeDomain("dual-region", US_EAST_1).getRegion());
assertEquals(EU_WEST_1, service.describeDomain("dual-region", EU_WEST_1).getRegion());
}
@Test
void sameNameInTheSameRegionStillConflicts() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
service.createDomain("occupied-name", "OpenSearch_2.11", null, null, null, US_EAST_1);
AwsException duplicate = assertThrows(AwsException.class, () -> service.createDomain(
"occupied-name", "OpenSearch_2.11", null, null, null, US_EAST_1));
assertEquals("ResourceAlreadyExistsException", duplicate.getErrorCode());
}
@Test
void describeFromAnotherRegionDoesNotSeeTheDomain() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
service.createDomain("east-only", "OpenSearch_2.11", null, null, null, US_EAST_1);
AwsException absent = assertThrows(AwsException.class,
() -> service.describeDomain("east-only", EU_WEST_1));
assertEquals("ResourceNotFoundException", absent.getErrorCode());
}
@Test
void updateInOneRegionLeavesTheOtherRegionUntouched() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
service.createDomain("split-update", "OpenSearch_2.11", null, null, null, US_EAST_1);
service.createDomain("split-update", "OpenSearch_2.11", null, null, null, EU_WEST_1);
ClusterConfig eastConfig = new ClusterConfig();
eastConfig.setInstanceCount(5);
service.updateDomainConfig("split-update", null, eastConfig, null, US_EAST_1);
assertEquals(5, service.describeDomain("split-update", US_EAST_1).getClusterConfig().getInstanceCount());
assertEquals(1, service.describeDomain("split-update", EU_WEST_1).getClusterConfig().getInstanceCount());
}
@Test
void deleteInOneRegionLeavesTheOtherRegionIntact() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
service.createDomain("split-delete", "OpenSearch_2.11", null, null, null, US_EAST_1);
service.createDomain("split-delete", "OpenSearch_2.11", null, null, null, EU_WEST_1);
Domain deleted = service.deleteDomain("split-delete", US_EAST_1);
assertTrue(deleted.isDeleted());
assertThrows(AwsException.class, () -> service.describeDomain("split-delete", US_EAST_1));
assertEquals(EU_WEST_1, service.describeDomain("split-delete", EU_WEST_1).getRegion());
}
@Test
void listDomainNamesReturnsOnlyTheRequestRegionsDomains() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
service.createDomain("east-listed", "OpenSearch_2.11", null, null, null, US_EAST_1);
service.createDomain("west-listed", "OpenSearch_2.11", null, null, null, EU_WEST_1);
List<Domain> east = service.listDomainNames(null, US_EAST_1);
List<Domain> west = service.listDomainNames(null, EU_WEST_1);
assertTrue(east.stream().anyMatch(d -> d.getDomainName().equals("east-listed")));
assertFalse(east.stream().anyMatch(d -> d.getDomainName().equals("west-listed")));
assertTrue(west.stream().anyMatch(d -> d.getDomainName().equals("west-listed")));
assertFalse(west.stream().anyMatch(d -> d.getDomainName().equals("east-listed")));
}
/**
* A record persisted before domains were keyed by region sits under the bare name; its ARN,
* which has always carried the region, decides which region may still see it, and the
* lookup moves it onto the region-scoped key.
*/
@Test
void preRegionScopingRecordResolvesByItsArnRegionAndMigrates() {
Domain legacy = new Domain();
legacy.setDomainName("legacy-record");
legacy.setAccountId(ACCOUNT);
legacy.setArn(AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/legacy-record").toString());
domainStore.putForAccount(ACCOUNT, "legacy-record", legacy);
Domain resolved = service.describeDomain("legacy-record", US_EAST_1);
assertEquals(US_EAST_1, AwsArnUtils.parse(resolved.getArn()).region());
assertFalse(domainStore.keysForAccount(ACCOUNT).contains("legacy-record"),
"the lookup migrates the bare-name key onto the region-scoped key");
assertTrue(domainStore.keysForAccount(ACCOUNT).contains(US_EAST_1 + "::legacy-record"));
assertThrows(AwsException.class, () -> service.describeDomain("legacy-record", EU_WEST_1));
}
/**
* A pre-scoping record whose ARN names another region is invisible from, and does not
* collide with, a same-named create in this region.
*/
@Test
void preRegionScopingRecordOfAnotherRegionDoesNotConflictWithACreateHere() {
when(domainManager.tryStartDomain(any())).thenReturn(false);
Domain legacy = new Domain();
legacy.setDomainName("legacy-elsewhere");
legacy.setAccountId(ACCOUNT);
legacy.setArn(AwsArnUtils.Arn.of("es", EU_WEST_1, ACCOUNT, "domain/legacy-elsewhere").toString());
domainStore.putForAccount(ACCOUNT, "legacy-elsewhere", legacy);
Domain created = service.createDomain("legacy-elsewhere", "OpenSearch_2.11",
null, null, null, US_EAST_1);
assertEquals(US_EAST_1, AwsArnUtils.parse(created.getArn()).region());
assertEquals(EU_WEST_1,
AwsArnUtils.parse(service.describeDomain("legacy-elsewhere", EU_WEST_1).getArn()).region());
}
@Test
void pollerFlipsReadinessUnderTheScopedKey() {
when(domainManager.tryStartDomain(any())).thenReturn(true);
when(domainManager.isReady(any())).thenReturn(true);
service.createDomain("poller-scoped", "OpenSearch_2.11", null, null, null, EU_WEST_1);
service.pollReadiness();
assertFalse(service.describeDomain("poller-scoped", EU_WEST_1).isProcessing());
assertTrue(domainStore.keysForAccount(ACCOUNT).contains(EU_WEST_1 + "::poller-scoped"));
}
}