fix(account): refuse the management account as an AccountId target (#8276)

* fix(account): refuse the management account as an AccountId target
This commit is contained in:
Yang Hao authored and GitHub committed 2026-11-14 09:31:36 -04:00
1 parent ad549728df
commit 869a5fcb59
4 files changed
+163 -8

No files matched your search

+2
View File
@@ -26,6 +26,8 @@ Alternate contacts and primary contact information are isolated by caller accoun
`PutContactInformation` and `GetContactInformation` requests are checked by `RequestConstraintCheck` against the member types, required members, lengths and patterns generated from botocore's Account model, such as the `PhoneNumber` pattern (a `+` followed by digits, spaces, parentheses, or hyphens). `tools/aws/regen_account_shapes.py` vendors them; `make aws-data-sync` regenerates them and `make aws-data-check` checks for drift. A member of the wrong JSON type fails with `SerializationException` and any other violation with `ValidationException`. `PutContactInformation` also requires `StateOrRegion` for the countries the API reference documents as requiring it (`US`, `CA`, `GB`, `DE`, `JP`, `IN`, `BR`), a rule the model does not carry. `GetContactInformation` returns `ResourceNotFoundException` when no contact information has been stored. Both operations apply the same target account ID rules as the alternate-contact operations.
A request that names an `AccountId` runs in the organizations context: the caller must be the organization's management account or its delegated administrator for Account Management, the organization must have all features and trusted access for Account Management enabled, and the `AccountId` must name a member account of that organization. Naming the management account returns `AccessDeniedException`, including when the management account names itself; its own contacts are reached by leaving `AccountId` out.
Three behaviors are Floci's own. An account has no primary contact until `PutContactInformation` stores one, because Floci has no sign-up contact to return. `CountryCode` is checked for its two-character length only, not against the ISO 3166-1 list. `GetContactInformation` always reports `VerificationStatus` as `NOT_SUPPORTED`, because Floci does not verify phone numbers.
AWS models provider-side `InternalServerException` and `TooManyRequestsException`; Floci does not synthesize those failures without an actual triggering condition.
@@ -150,8 +150,9 @@ public class AccountService implements Resettable, StoredResourceArns {
throw new AwsException("AccessDeniedException",
"The calling account is not the management account or a delegated administrator.", 403);
}
if (managementAccount && requestedAccountId.equals(callerAccountId)) {
throw validation("The management account cannot specify its own AccountId.");
if (requestedAccountId.equals(organization.getMasterAccountId())) {
throw new AwsException("AccessDeniedException",
"AccountId must name a member account, not the management account of the organization.", 403);
}
return requestedAccountId;
}
@@ -0,0 +1,97 @@
package io.floci.aws.services.account;
import io.floci.aws.testing.EnforcementFixtures;
import io.floci.aws.testing.OrganizationsRequests;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.services.account.ContactInformationRequests.contactInformationRequest;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
/** Requests that name an {@code AccountId}, sent by the accounts of one organization. */
@QuarkusTest
class AccountOrganizationsContextIntegrationTest {
/** The management account of an organization of this class's own, which no other test uses. */
private static final String MANAGEMENT = "731000826800";
private static String delegatedAdministrator;
private static String member;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
/** Once for the class, from a test's own setup: the port is not known before the first test. */
@BeforeEach
void createOrganizationWithADelegatedAdministrator() {
if (delegatedAdministrator != null) {
return;
}
organizations("CreateOrganization", "{\"FeatureSet\":\"ALL\"}");
member = createAccount("account-member");
String administrator = createAccount("account-delegated-admin");
organizations("EnableAWSServiceAccess", "{\"ServicePrincipal\":\"account.amazonaws.com\"}");
organizations("RegisterDelegatedAdministrator", "{\"AccountId\":\"" + administrator
+ "\",\"ServicePrincipal\":\"account.amazonaws.com\"}");
delegatedAdministrator = administrator;
}
@Test
void delegatedAdministratorReachesAMemberAccount() {
account(delegatedAdministrator, "/putAlternateContact", alternateContact(member)).statusCode(200);
account(member, "/getAlternateContact", "{\"AlternateContactType\":\"SECURITY\"}")
.statusCode(200).body("AlternateContact.EmailAddress", equalTo("security@example.com"));
}
@Test
void delegatedAdministratorCannotNameTheManagementAccount() {
String target = "{\"AccountId\":\"" + MANAGEMENT + "\",\"AlternateContactType\":\"SECURITY\"}";
refused(account(delegatedAdministrator, "/putAlternateContact", alternateContact(MANAGEMENT)));
refused(account(delegatedAdministrator, "/getAlternateContact", target));
refused(account(delegatedAdministrator, "/deleteAlternateContact", target));
refused(account(delegatedAdministrator, "/putContactInformation",
contactInformationRequest().put("AccountId", MANAGEMENT).toString()));
refused(account(delegatedAdministrator, "/getContactInformation", "{\"AccountId\":\"" + MANAGEMENT + "\"}"));
}
@Test
void managementAccountCannotNameItself() {
refused(account(MANAGEMENT, "/putAlternateContact", alternateContact(MANAGEMENT)));
}
private static void refused(ValidatableResponse response) {
response.statusCode(403).body("__type", equalTo("AccessDeniedException"))
.body("message", containsString("management account of the organization"));
}
private static String alternateContact(String accountId) {
return "{\"AccountId\":\"" + accountId + "\",\"AlternateContactType\":\"SECURITY\",\"Name\":\"Security\","
+ "\"Title\":\"Owner\",\"EmailAddress\":\"security@example.com\",\"PhoneNumber\":\"+12025550123\"}";
}
private static String createAccount(String name) {
return organizations("CreateAccount",
"{\"Email\":\"" + name + "@example.com\",\"AccountName\":\"" + name + "\"}")
.body("CreateAccountStatus.State", equalTo("SUCCEEDED"))
.extract().path("CreateAccountStatus.AccountId");
}
private static ValidatableResponse organizations(String action, String body) {
return OrganizationsRequests.request(MANAGEMENT, action, body).when().post("/").then().statusCode(200);
}
private static ValidatableResponse account(String caller, String path, String body) {
return given().contentType("application/json")
.header("Authorization", EnforcementFixtures.auth(caller, "account"))
.body(body).post(path).then();
}
}
@@ -14,6 +14,7 @@ import io.floci.aws.services.organizations.model.Organization;
import io.floci.aws.services.organizations.model.OrganizationAccount;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
@@ -36,6 +37,7 @@ import static org.mockito.Mockito.when;
class AccountServiceTest {
private static final String ACCOUNT_ID = "123456789012";
private static final String MANAGEMENT_ACCOUNT_ID = "444455556666";
private final ObjectMapper mapper = new ObjectMapper();
private AccountService service;
private OrganizationsService organizations;
@@ -318,12 +320,7 @@ class AccountServiceTest {
@Test
void managementAccountPutsContactInformationForAMemberAccount() {
String member = "111122223333";
Organization organization = new Organization();
organization.setFeatureSet("ALL");
organization.setMasterAccountId(ACCOUNT_ID);
organization.setEnabledServicePrincipals(Map.of(ServicePrincipals.of("account"), Instant.EPOCH));
when(organizations.describeOrganization(ACCOUNT_ID)).thenReturn(organization);
when(organizations.describeAccount(ACCOUNT_ID, ACCOUNT_ID)).thenReturn(new OrganizationAccount());
callerIsInAnOrganizationManagedBy(ACCOUNT_ID, new OrganizationAccount());
ObjectNode request = contactInformationRequest();
request.put("AccountId", member);
@@ -338,6 +335,42 @@ class AccountServiceTest {
assertEquals("ResourceNotFoundException", error.getErrorCode());
}
@Test
void delegatedAdministratorCannotNameTheManagementAccount() {
callerIsInAnOrganizationManagedBy(MANAGEMENT_ACCOUNT_ID, delegatedAdministrator());
ObjectNode alternateContact = contactRequest();
alternateContact.put("AccountId", MANAGEMENT_ACCOUNT_ID);
ObjectNode contactInformation = contactInformationRequest();
contactInformation.put("AccountId", MANAGEMENT_ACCOUNT_ID);
assertManagementAccountRefused(() -> service.putAlternateContact(ACCOUNT_ID, alternateContact));
assertManagementAccountRefused(() -> service.getAlternateContact(ACCOUNT_ID, alternateContact));
assertManagementAccountRefused(() -> service.deleteAlternateContact(ACCOUNT_ID, alternateContact));
assertManagementAccountRefused(() -> service.putContactInformation(ACCOUNT_ID, contactInformation));
assertManagementAccountRefused(() -> service.getContactInformation(ACCOUNT_ID, contactInformation));
}
@Test
void managementAccountCannotNameItself() {
callerIsInAnOrganizationManagedBy(ACCOUNT_ID, new OrganizationAccount());
ObjectNode request = contactRequest();
request.put("AccountId", ACCOUNT_ID);
assertManagementAccountRefused(() -> service.putAlternateContact(ACCOUNT_ID, request));
}
@Test
void delegatedAdministratorCanNameItsOwnAccount() {
callerIsInAnOrganizationManagedBy(MANAGEMENT_ACCOUNT_ID, delegatedAdministrator());
ObjectNode request = contactRequest();
request.put("AccountId", ACCOUNT_ID);
service.putAlternateContact(ACCOUNT_ID, request);
assertEquals("security@example.com",
service.getAlternateContact(ACCOUNT_ID, contactRequest()).getEmailAddress());
}
@Test
void clearRemovesContactInformation() {
service.putContactInformation(ACCOUNT_ID, contactInformationRequest());
@@ -357,6 +390,28 @@ class AccountServiceTest {
assertTrue(error.getMessage().contains(member), error.getMessage());
}
private void callerIsInAnOrganizationManagedBy(String managementAccountId, OrganizationAccount caller) {
Organization organization = new Organization();
organization.setFeatureSet("ALL");
organization.setMasterAccountId(managementAccountId);
organization.setEnabledServicePrincipals(Map.of(ServicePrincipals.of("account"), Instant.EPOCH));
when(organizations.describeOrganization(ACCOUNT_ID)).thenReturn(organization);
when(organizations.describeAccount(ACCOUNT_ID, ACCOUNT_ID)).thenReturn(caller);
}
private static OrganizationAccount delegatedAdministrator() {
OrganizationAccount account = new OrganizationAccount();
account.setDelegatedServices(Map.of(ServicePrincipals.of("account"), Instant.EPOCH));
return account;
}
private static void assertManagementAccountRefused(Executable call) {
AwsException error = assertThrows(AwsException.class, call);
assertEquals("AccessDeniedException", error.getErrorCode());
assertEquals(403, error.getHttpStatus());
assertTrue(error.getMessage().contains("management account of the organization"), error.getMessage());
}
private ObjectNode contactRequest() {
ObjectNode request = mapper.createObjectNode();
request.put("AlternateContactType", "SECURITY");