fix(organizations): return OU and account paths (#6341)

* fix(organizations): return OU and account paths

* fix(organizations): stabilize list path rendering

Render selected resources from request-local parent IDs without leaf rereads. Preserve scoped ancestry errors and tolerate concurrent removal or movement. Reduce account collection scans for a full 20-row page from 21 to 1.

* refactor(organizations): share ancestry walk and path format

Keep live, CloudFormation, and cached list paths on one traversal with caller-supplied parent lookups. Preserve scoped fallback and stale-row handling, and cover root-to-leaf path parity at three depths.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-23 10:24:22 -04:00
1 parent 7c8daff08f
commit 99171dcbe0
5 files changed
+503 -16

No files matched your search

@@ -0,0 +1,224 @@
package io.github.hectorvent.floci.services.organizations;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.organizations.model.Organization;
import io.github.hectorvent.floci.services.organizations.model.OrganizationAccount;
import io.github.hectorvent.floci.services.organizations.model.OrganizationalUnit;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class OrganizationsPathRenderingTest {
private static final String OWNER = "100623600001";
private final ObjectMapper mapper = new ObjectMapper();
private AccountAwareStorageBackend<Organization> organizations;
private AccountAwareStorageBackend<OrganizationAccount> accounts;
private AccountAwareStorageBackend<OrganizationalUnit> units;
private OrganizationsService service;
private OrganizationsJsonHandler handler;
private Organization organization;
private String root;
@BeforeEach
void setUp() {
organizations = spy(AccountAwareStorageBackend.inMemory(OWNER));
accounts = spy(AccountAwareStorageBackend.inMemory(OWNER));
units = spy(AccountAwareStorageBackend.inMemory(OWNER));
service = spy(new OrganizationsService(mapper, new RegionResolver("us-east-1", OWNER),
organizations, accounts, units, AccountAwareStorageBackend.inMemory(OWNER),
AccountAwareStorageBackend.inMemory(OWNER), AccountAwareStorageBackend.inMemory(OWNER)));
handler = new OrganizationsJsonHandler(service, mapper);
organization = service.createOrganization(OWNER, "ALL");
root = organization.getRoot().getId();
}
@ParameterizedTest
@ValueSource(ints = {0, 1, 3})
void liveAndCachedPathsUseTheSameRootToLeafFormat(int depth) {
String parent = root;
String prefix = organization.getId() + "/" + root + "/";
assertEquals(prefix, service.organizationPath(OWNER, root));
for (int level = 0; level < depth; level++) {
OrganizationalUnit unit = service.createOrganizationalUnit(OWNER, parent, "Level" + level, null);
parent = unit.getId();
prefix += parent + "/";
assertEquals(prefix, service.organizationPath(OWNER, parent));
assertEquals(prefix, service.organizationPaths(OWNER).unitPath(unit));
}
String member = member("parity@example.test");
if (depth > 0) {
service.moveAccount(OWNER, member, root, parent);
}
String expected = prefix + member + "/";
assertEquals(expected, service.organizationPath(OWNER, member));
assertEquals(expected, service.organizationPaths(OWNER).accountPath(service.describeAccount(OWNER, member)));
}
@ParameterizedTest
@ValueSource(strings = {"ListAccounts", "ListAccountsForParent", "ListOrganizationalUnitsForParent"})
void selectedRowsStillRenderAfterTheirResourcesAndAncestorsAreRemoved(String action) {
OrganizationalUnit parent = service.createOrganizationalUnit(OWNER, root, "Parent", null);
OrganizationalUnit child = service.createOrganizationalUnit(OWNER, parent.getId(), "Child", null);
String member = member("removed@example.test");
service.moveAccount(OWNER, member, root, child.getId());
ObjectNode request = mapper.createObjectNode().put("ParentId",
action.equals("ListOrganizationalUnitsForParent") ? parent.getId() : child.getId());
if (action.equals("ListOrganizationalUnitsForParent")) {
doAnswer(invocation -> {
Object selected = invocation.callRealMethod();
removeHierarchy(member, child.getId(), parent.getId());
return selected;
}).when(service).listOrganizationalUnitsForParent(OWNER, parent.getId());
} else if (action.equals("ListAccountsForParent")) {
doAnswer(invocation -> {
Object selected = invocation.callRealMethod();
removeHierarchy(member, child.getId(), parent.getId());
return selected;
}).when(service).listAccountsForParent(OWNER, child.getId());
} else {
doAnswer(invocation -> {
Object selected = invocation.callRealMethod();
removeHierarchy(member, child.getId(), parent.getId());
return selected;
}).when(service).listAccounts(OWNER);
}
try (Response response = handler.handle(action, request, OWNER)) {
assertEquals(200, response.getStatus(), response.getEntity().toString());
JsonNode body = (JsonNode) response.getEntity();
String prefix = organization.getId() + "/" + root + "/" + parent.getId() + "/" + child.getId() + "/";
if (action.equals("ListOrganizationalUnitsForParent")) {
assertEquals(prefix, body.path("OrganizationalUnits").get(0).path("Path").asText());
} else {
JsonNode selected = body.path("Accounts").findParents("Id").stream()
.filter(account -> member.equals(account.path("Id").asText())).findFirst().orElseThrow();
assertEquals(prefix + member + "/", selected.path("Paths").get(0).asText());
}
}
}
@ParameterizedTest
@ValueSource(ints = {1, 19})
void accountPageRenderingDoesNotRepeatCollectionScansForEachRow(int members) {
OrganizationalUnit parent = service.createOrganizationalUnit(OWNER, root, "Parent", null);
for (int index = 0; index < members; index++) {
String member = member("member" + index + "@example.test");
service.moveAccount(OWNER, member, root, parent.getId());
}
clearInvocations(organizations, accounts, units);
try (Response response = handler.handle("ListAccounts", mapper.createObjectNode().put("MaxResults", 20), OWNER)) {
assertEquals(200, response.getStatus());
assertEquals(members + 1, ((JsonNode) response.getEntity()).path("Accounts").size());
}
verify(accounts, times(1)).scanAllAccounts();
verify(units, times(1)).scanAllAccounts();
verify(organizations, times(2)).scanAllAccounts();
}
@Test
void aParentCreatedAfterThePathMapIsCapturedCanStillBeResolved() {
String member = member("moved@example.test");
doAnswer(invocation -> {
OrganizationalUnit unit = service.createOrganizationalUnit(OWNER, root, "New", null);
service.moveAccount(OWNER, member, root, unit.getId());
return invocation.callRealMethod();
}).when(service).listAccounts(OWNER);
try (Response response = handler.handle("ListAccounts", mapper.createObjectNode(), OWNER)) {
assertEquals(200, response.getStatus(), response.getEntity().toString());
OrganizationAccount account = service.describeAccount(OWNER, member);
JsonNode selected = ((JsonNode) response.getEntity()).path("Accounts").findParents("Id").stream()
.filter(row -> member.equals(row.path("Id").asText())).findFirst().orElseThrow();
assertEquals(organization.getId() + "/" + root + "/" + account.getParentId() + "/" + member + "/",
selected.path("Paths").get(0).asText());
}
}
@Test
void aRemovedRowWithANewlyCreatedAndRemovedParentDoesNotFailThePage() {
String member = member("gone@example.test");
doAnswer(invocation -> {
OrganizationalUnit unit = service.createOrganizationalUnit(OWNER, root, "Temporary", null);
service.moveAccount(OWNER, member, root, unit.getId());
Object selected = invocation.callRealMethod();
service.removeAccountFromOrganization(OWNER, member);
service.deleteOrganizationalUnit(OWNER, unit.getId());
return selected;
}).when(service).listAccounts(OWNER);
try (Response response = handler.handle("ListAccounts", mapper.createObjectNode(), OWNER)) {
assertEquals(200, response.getStatus(), response.getEntity().toString());
JsonNode rows = ((JsonNode) response.getEntity()).path("Accounts");
assertEquals(1, rows.size());
assertEquals(OWNER, rows.get(0).path("Id").asText());
}
}
private String member(String email) {
return service.createAccount(OWNER, email, "Member", null, false).getAccountId();
}
@Test
void capturedPathsRemainBoundToTheirAuthorizedOrganization() {
String member = member("reader@example.test");
OrganizationsService.OrganizationPaths paths = service.organizationPaths(member);
assertEquals(organization.getId() + "/" + root + "/" + OWNER + "/",
paths.accountPath(service.describeAccount(OWNER, OWNER)));
AwsException outsider = assertThrows(AwsException.class, () -> service.organizationPaths("100623600009"));
assertEquals("AWSOrganizationsNotInUseException", outsider.getErrorCode());
service.createOrganization("100623600009", "ALL");
OrganizationAccount foreign = service.describeAccount("100623600009", "100623600009");
AwsException mismatch = assertThrows(AwsException.class, () -> paths.accountPath(foreign));
assertEquals("AccessDeniedException", mismatch.getErrorCode());
}
@Test
void anUnchangedTargetWithInvalidAncestryIsNotSilentlyOmitted() {
String member = member("invalid-parent@example.test");
OrganizationAccount account = service.describeAccount(OWNER, member);
account.setParentId("ou-abcd-abcdefgh");
OrganizationsService.OrganizationPaths paths = service.organizationPaths(OWNER);
AwsException invalid = assertThrows(AwsException.class, () -> paths.accountPath(account));
assertEquals("OrganizationalUnitNotFoundException", invalid.getErrorCode());
}
@Test
void aTargetMovedAwayFromAMissingNewParentCanBeOmitted() {
String member = member("moving@example.test");
OrganizationsService.OrganizationPaths paths = service.organizationPaths(OWNER);
OrganizationalUnit lateParent = service.createOrganizationalUnit(OWNER, root, "Late", null);
service.moveAccount(OWNER, member, root, lateParent.getId());
OrganizationAccount selected = service.describeAccount(OWNER, member);
AtomicBoolean armed = new AtomicBoolean(true);
doAnswer(invocation -> {
if (armed.compareAndSet(true, false)) {
service.moveAccount(OWNER, member, lateParent.getId(), root);
units.deleteForAccount(OWNER, lateParent.getId());
}
return invocation.callRealMethod();
}).when(units).scanAllAccounts();
assertNull(paths.accountPath(selected));
}
private void removeHierarchy(String member, String child, String parent) {
service.removeAccountFromOrganization(OWNER, member);
service.deleteOrganizationalUnit(OWNER, child);
service.deleteOrganizationalUnit(OWNER, parent);
}
}
@@ -0,0 +1,147 @@
package io.github.hectorvent.floci.services.organizations;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.organizations.OrganizationsClient;
import software.amazon.awssdk.services.organizations.model.Account;
import software.amazon.awssdk.services.organizations.model.OrganizationalUnit;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
class OrganizationsPathsIntegrationTest {
private static final AtomicLong ACCOUNTS = new AtomicLong(741623600001L);
private final String owner = String.valueOf(ACCOUNTS.getAndIncrement());
private OrganizationsClient client;
private String organizationId;
private String rootId;
private String parentId;
private String childId;
private String memberId;
private OrganizationalUnit createdChild;
@BeforeAll
static void configureContentTypes() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void createHierarchy() {
client = OrganizationsClient.builder().endpointOverride(URI.create("http://localhost:" + RestAssured.port))
.httpClientBuilder(UrlConnectionHttpClient.builder()).region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(owner, "test"))).build();
organizationId = client.createOrganization(request -> request.featureSet("ALL")).organization().id();
rootId = client.listRoots().roots().getFirst().id();
parentId = client.createOrganizationalUnit(request -> request.parentId(rootId).name("Parent"))
.organizationalUnit().id();
createdChild = client.createOrganizationalUnit(request -> request.parentId(parentId).name("Nested"))
.organizationalUnit();
childId = createdChild.id();
memberId = client.createAccount(request -> request.accountName("Member")
.email(UUID.randomUUID() + "@example.test")).createAccountStatus().accountId();
}
@AfterEach
void deleteHierarchy() {
if (client == null) {
return;
}
try {
if (memberId != null) {
client.removeAccountFromOrganization(request -> request.accountId(memberId));
}
if (childId != null) {
client.deleteOrganizationalUnit(request -> request.organizationalUnitId(childId));
}
if (parentId != null) {
client.deleteOrganizationalUnit(request -> request.organizationalUnitId(parentId));
}
if (organizationId != null) {
client.deleteOrganization();
}
} finally {
client.close();
}
}
@ParameterizedTest
@ValueSource(strings = {"CreateOrganizationalUnit", "UpdateOrganizationalUnit",
"DescribeOrganizationalUnit", "ListOrganizationalUnitsForParent"})
void sdkReturnsTheNestedOuPath(String action) {
OrganizationalUnit unit = switch (action) {
case "CreateOrganizationalUnit" -> createdChild;
case "UpdateOrganizationalUnit" -> client.updateOrganizationalUnit(request -> request
.organizationalUnitId(childId).name("Renamed")).organizationalUnit();
case "DescribeOrganizationalUnit" -> client.describeOrganizationalUnit(request -> request
.organizationalUnitId(childId)).organizationalUnit();
default -> client.listOrganizationalUnitsForParent(request -> request.parentId(parentId))
.organizationalUnits().getFirst();
};
assertEquals(childId, unit.id());
assertEquals(organizationId + "/" + rootId + "/" + parentId + "/" + childId + "/", unit.path());
}
@ParameterizedTest
@CsvSource({"DescribeAccount,false", "DescribeAccount,true", "ListAccounts,false", "ListAccounts,true",
"ListAccountsForParent,false", "ListAccountsForParent,true"})
void sdkReturnsTheCurrentAccountPath(String action, boolean nested) {
if (nested) {
client.moveAccount(request -> request.accountId(memberId).sourceParentId(rootId).destinationParentId(childId));
}
String parent = nested ? childId : rootId;
Account account = switch (action) {
case "DescribeAccount" -> client.describeAccount(request -> request.accountId(memberId)).account();
case "ListAccounts" -> {
List<Account> accounts = client.listAccounts().accounts();
assertEquals(List.of(organizationId + "/" + rootId + "/" + owner + "/"), accounts.stream()
.filter(candidate -> owner.equals(candidate.id())).findFirst().orElseThrow().paths());
yield accounts.stream().filter(candidate -> memberId.equals(candidate.id())).findFirst().orElseThrow();
}
default -> client.listAccountsForParent(request -> request.parentId(parent)).accounts().stream()
.filter(candidate -> memberId.equals(candidate.id())).findFirst().orElseThrow();
};
String ancestry = nested ? parentId + "/" + childId + "/" : "";
assertEquals(List.of(organizationId + "/" + rootId + "/" + ancestry + memberId + "/"), account.paths());
if (nested) {
client.moveAccount(request -> request.accountId(memberId).sourceParentId(childId).destinationParentId(rootId));
assertEquals(List.of(organizationId + "/" + rootId + "/" + memberId + "/"),
client.describeAccount(request -> request.accountId(memberId)).account().paths());
}
}
@Test
void delegatedAdministratorShapeDoesNotGainPaths() {
client.enableAWSServiceAccess(request -> request.servicePrincipal("config.amazonaws.com"));
client.registerDelegatedAdministrator(request -> request.accountId(memberId).servicePrincipal("config.amazonaws.com"));
given().contentType("application/x-amz-json-1.1")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + owner
+ "/20261023/us-east-1/organizations/aws4_request")
.header("X-Amz-Target", "AWSOrganizationsV20161128.ListDelegatedAdministrators")
.body("{\"ServicePrincipal\":\"config.amazonaws.com\"}").post("/").then().statusCode(200)
.body("DelegatedAdministrators[0].Id", equalTo(memberId))
.body("DelegatedAdministrators[0]", not(hasKey("Paths")));
}
}