feat(account): implement GetAccountInformation and PutAccountName (#8725)

This commit is contained in:
Yang Hao authored and GitHub committed 2026-11-17 06:58:24 -04:00
1 parent 3e7ce122e7
commit 93c1d39348
21 files changed
+698 -50

No files matched your search

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.JsonErrorResponseUtils;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.services.account.model.AccountInformation;
import io.floci.aws.services.account.model.AlternateContact;
import io.floci.aws.services.account.model.ContactInformation;
import jakarta.inject.Inject;
@@ -17,6 +18,8 @@ import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.time.temporal.ChronoUnit;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@@ -76,6 +79,31 @@ public class AccountController {
return Response.ok(response).build();
}
@POST
@Path("/getAccountInformation")
public Response getAccountInformation(String body) {
AccountInformation information = accountService.getAccountInformation(
requestContext.getAccountId(), readTree(body));
ObjectNode response = objectMapper.createObjectNode();
response.put("AccountId", information.accountId());
if (information.accountName() != null) {
response.put("AccountName", information.accountName());
}
if (information.accountCreatedDate() != null) {
response.put("AccountCreatedDate",
information.accountCreatedDate().truncatedTo(ChronoUnit.SECONDS).toString());
}
response.put("AccountState", information.accountState());
return Response.ok(response).build();
}
@POST
@Path("/putAccountName")
public Response putAccountName(String body) {
accountService.putAccountName(requestContext.getAccountId(), readTree(body));
return Response.ok().build();
}
private JsonNode readTree(String body) {
JsonNode request;
try {
@@ -9,14 +9,17 @@ import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.account.model.AccountInformation;
import io.floci.aws.services.account.model.AlternateContact;
import io.floci.aws.services.account.model.ContactInformation;
import io.floci.aws.services.organizations.AccountPlacementListener;
import io.floci.aws.services.organizations.OrganizationsService;
import io.floci.aws.services.organizations.model.Organization;
import io.floci.aws.services.organizations.model.OrganizationAccount;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -24,14 +27,19 @@ import java.util.Optional;
import java.util.Set;
@ApplicationScoped
public class AccountService implements Resettable, StoredResourceArns {
public class AccountService implements Resettable, StoredResourceArns, AccountPlacementListener {
private static final String ACCOUNT_MANAGEMENT_SERVICE_PRINCIPAL = ServicePrincipals.of("account");
private static final Set<String> COUNTRIES_REQUIRING_STATE_OR_REGION =
Set.of("US", "CA", "GB", "DE", "JP", "IN", "BR");
private static final String PRIMARY_CONTACT_KEY = "primary";
private static final String ACCOUNT_NAME_KEY = "name";
private static final String ACCOUNT_STATE_ACTIVE = "ACTIVE";
private static final String JOINED_METHOD_CREATED = "CREATED";
private static final String ORGANIZATIONS_STATUS_PENDING_CLOSURE = "PENDING_CLOSURE";
private final AccountAwareStorageBackend<AlternateContact> contacts;
private final AccountAwareStorageBackend<ContactInformation> contactInformation;
private final AccountAwareStorageBackend<String> accountNames;
private final OrganizationsService organizationsService;
@Inject
@@ -40,6 +48,8 @@ public class AccountService implements Resettable, StoredResourceArns {
new TypeReference<Map<String, AlternateContact>>() {});
this.contactInformation = storageFactory.create("account", "account-contact-information.json",
new TypeReference<Map<String, ContactInformation>>() {});
this.accountNames = storageFactory.create("account", "account-names.json",
new TypeReference<Map<String, String>>() {});
this.organizationsService = organizationsService;
}
@@ -107,6 +117,50 @@ public class AccountService implements Resettable, StoredResourceArns {
.orElseThrow(AccountService::contactInformationNotFound);
}
public AccountInformation getAccountInformation(String callerAccountId, JsonNode request) {
AccountRequestShapes.check("GetAccountInformation", request);
String targetAccountId = resolveTargetAccount(callerAccountId, request);
return organizationsService.findAccount(targetAccountId)
.map(AccountService::informationOf)
.orElseGet(() -> new AccountInformation(targetAccountId,
accountNames.getForAccount(targetAccountId, ACCOUNT_NAME_KEY).orElse(null),
null, ACCOUNT_STATE_ACTIVE));
}
public void putAccountName(String callerAccountId, JsonNode request) {
AccountRequestShapes.check("PutAccountName", request);
String targetAccountId = resolveTargetAccount(callerAccountId, request);
String name = text(request, "AccountName");
// Kept for the account as well, so the name outlives its organization.
accountNames.putForAccount(targetAccountId, ACCOUNT_NAME_KEY, name);
organizationsService.renameAccount(targetAccountId, name);
}
/**
* Keeps the Organizations name of an account created in or invited into an organization, which
* {@code GetAccountInformation} answers once the account leaves it. A name the account already has stays.
*/
@Override
public void accountPlacementChanged(String managementAccountId, String accountId) {
if (accountNames.getForAccount(accountId, ACCOUNT_NAME_KEY).isPresent()) {
return;
}
organizationsService.findAccount(accountId)
.ifPresent(account -> accountNames.putForAccount(accountId, ACCOUNT_NAME_KEY, account.getName()));
}
private static AccountInformation informationOf(OrganizationAccount account) {
// An invited account existed before it joined, so its JoinedTimestamp is not its creation date.
Instant created = JOINED_METHOD_CREATED.equals(account.getJoinedMethod())
? account.getJoinedTimestamp()
: null;
// Account Management has no PENDING_CLOSURE, and such an account is still usable.
String state = ORGANIZATIONS_STATUS_PENDING_CLOSURE.equals(account.getStatus())
? ACCOUNT_STATE_ACTIVE
: account.getStatus();
return new AccountInformation(account.getId(), account.getName(), created, state);
}
private String resolveTargetAccount(String callerAccountId, JsonNode request) {
String requestedAccountId = text(request, "AccountId");
if (requestedAccountId == null) {
@@ -151,6 +205,7 @@ public class AccountService implements Resettable, StoredResourceArns {
public void clear() {
contacts.clear();
contactInformation.clear();
accountNames.clear();
}
/** The namespace of the resource types this service finds, as the request-resources table names it. */
@@ -0,0 +1,8 @@
package io.floci.aws.services.account.model;
import java.time.Instant;
/** What {@code GetAccountInformation} answers for an account; a member Floci does not know is null. */
public record AccountInformation(String accountId, String accountName, Instant accountCreatedDate,
String accountState) {
}
@@ -694,7 +694,21 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
return requireAccount(requireOrganizationForCaller(callerAccountId), accountId);
}
public Optional<OrganizationAccount> findAccountForPortal(String accountId) {
/**
* Renames an account in the organization that holds it, as Account Management's {@code PutAccountName} does,
* so {@code DescribeAccount} and {@code ListAccounts} answer the new name. Does nothing for an account in no
* organization.
*/
public void renameAccount(String accountId, String name) {
findOrganizationForAccount(accountId).ifPresent(organization -> accountIn(organization, accountId)
.ifPresent(account -> {
account.setName(name);
accounts.putForAccount(organization.getMasterAccountId(), accountId, account);
}));
}
/** The record of an account in the organization that holds it, empty for an account in no organization. */
public Optional<OrganizationAccount> findAccount(String accountId) {
if (accountId == null || !ACCOUNT_ID_PATTERN.matcher(accountId).matches()) {
return Optional.empty();
}
@@ -1593,6 +1607,12 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
.toList();
}
private Optional<OrganizationAccount> accountIn(Organization organization, String accountId) {
return accountsIn(organization).stream()
.filter(account -> accountId.equals(account.getId()))
.findFirst();
}
private List<OrganizationalUnit> organizationalUnitsIn(Organization organization) {
return organizationalUnits.scanAllAccounts().stream()
.filter(ou -> organization.getId().equals(ou.getOrganizationId()))
@@ -1623,9 +1643,7 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
if (accountId == null || !ACCOUNT_ID_PATTERN.matcher(accountId).matches()) {
throw invalidInput("Account id must be 12 digits.");
}
return accountsIn(organization).stream()
.filter(account -> accountId.equals(account.getId()))
.findFirst()
return accountIn(organization, accountId)
.orElseThrow(() -> new AwsException("AccountNotFoundException",
"We can't find an AWS account with the AccountId " + accountId + ".", 400));
}
@@ -54,7 +54,7 @@ public class SsoPortalService {
if (accounts.containsKey(assignment.accountId())) {
return;
}
PortalAccountInfo info = organizationsService.findAccountForPortal(assignment.accountId())
PortalAccountInfo info = organizationsService.findAccount(assignment.accountId())
.map(account -> new PortalAccountInfo(account.getId(), account.getName(), account.getEmail()))
.orElseGet(() -> new PortalAccountInfo(assignment.accountId(), null, null));
accounts.put(assignment.accountId(), info);