mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
ad549728df
commit
869a5fcb59
4 files changed
+163
-8
No files matched your search
+97
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user