mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sts): dispatch AssumeRoot and authorize centralized root access (#6251)
Floci dispatched the five operations that turn centralized root access on and off and nothing that uses it, so the feature was half emulated: AssumeRoot, the operation they exist for, answered UnsupportedOperation. The five took no caller check of their own either, so any account could enable or disable organization wide root features, and the record they wrote was scoped to the calling account, which left the management account's toggle invisible to a delegated administrator. All five answer for one organization now, so all five check the caller, and each refusal is a shape the reference models on them and on nothing else: no organization is OrganizationNotFound, one without All features is OrganizationNotInAllFeaturesMode, one without trusted access for IAM is ServiceAccessNotEnabled, and a caller that is neither the management account nor a delegated administrator is AccountNotManagementOrDelegatedAdministrator. The two enable operations add CallerIsNotManagementAccount, modeled on them alone, so a delegated administrator may read the features and turn them off but not on. The feature set belongs to the organization, so it is read and written in the management account's scope whoever asks, and the OrganizationId the model declares beside EnabledFeatures is answered now that the organization is known. Organizations answers all of this through a provider interface in the iam package, the way ScpProvider already does, so IAM still does not depend on it. AssumeRoot opens the session those features allow. It acts as the member account's root user, which no identity policy grants or denies, scoped by the TaskPolicyArn the request names: the five AWS managed policies the model names, under the root-task path, each denying everything it does not name, so the policy is the whole of the bound and is read beside the root's own allow-all rather than as a session policy with nothing to allow. A customer- managed policy can carry that path too and is refused, being the caller's own document rather than one of AWS's. The caller must be able to manage root access and have RootSessions enabled, the target must be a member account other than the management account, and the audit reads those policies allow name the member account's root user when they carry no UserName, which is what their own arn:aws:iam::*:root asks for. What is chosen rather than read is documented in docs/services/sts.md: the operation models only RegionDisabled and ExpiredToken, so every caller, target and feature refusal is AccessDenied 403 and a task policy outside the root-task path is a ValidationError, the shape MinimumSessionTokenSize also answers outside the 0 to 4096 the model declares for it, the size asked for being the size issued. The guide's refusal of root user credentials covers both credentials this emulator reads as the account root, one that resolves to a root ARN and the bare account-id key the enforcement filter calls floci's account-root principal, so a privileged session is opened by an IAM user or role of the management account as it is on AWS. The global endpoint is refused when the request arrives on that host, which is the only way a request says so.
This commit is contained in:
1 parent
812c95499d
commit
c094ce3973
16 files changed
+1173
-25
No files matched your search
@@ -0,0 +1,160 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The caller checks the reference models on the five centralized root features. Each refusal is a
|
||||
* shape botocore declares on those operations and no other, and the order they are asked in is
|
||||
* the order below: whether there is an organization, what it has enabled, then who is asking.
|
||||
*/
|
||||
class CentralRootAccessTest {
|
||||
|
||||
private static final String ORGANIZATION = "o-rootaccess1";
|
||||
private static final String MANAGEMENT = "100000000001";
|
||||
private static final String DELEGATED_ADMIN = "100000000002";
|
||||
private static final String MEMBER = "100000000003";
|
||||
|
||||
private IamService iamService;
|
||||
private RootAccessOrganization organizations;
|
||||
private CentralRootAccess rootAccess;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
iamService = mock(IamService.class);
|
||||
organizations = mock(RootAccessOrganization.class);
|
||||
Instance<RootAccessOrganization> instance = mock(Instance.class);
|
||||
when(instance.isResolvable()).thenReturn(true);
|
||||
when(instance.get()).thenReturn(organizations);
|
||||
rootAccess = new CentralRootAccess(iamService, instance);
|
||||
}
|
||||
|
||||
private void organizationOf(String caller, boolean allFeatures, boolean trustedAccess) {
|
||||
when(organizations.membershipOf(caller)).thenReturn(Optional.of(
|
||||
new RootAccessOrganization.Membership(ORGANIZATION, MANAGEMENT, allFeatures, trustedAccess,
|
||||
Set.of(DELEGATED_ADMIN), Set.of(MANAGEMENT, DELEGATED_ADMIN, MEMBER))));
|
||||
}
|
||||
|
||||
private static String codeOf(Executable call) {
|
||||
return assertThrows(AwsException.class, call).getErrorCode();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAccountInNoOrganizationIsRefused() {
|
||||
when(organizations.membershipOf(MANAGEMENT)).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals("OrganizationNotFoundException", codeOf(() -> rootAccess.list(MANAGEMENT)));
|
||||
verify(iamService, never()).listOrganizationsFeatures();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOrganizationWithoutAllFeaturesIsRefused() {
|
||||
organizationOf(MANAGEMENT, false, true);
|
||||
|
||||
assertEquals("OrganizationNotInAllFeaturesModeException", codeOf(() -> rootAccess.list(MANAGEMENT)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamNeedsTrustedAccessInOrganizations() {
|
||||
organizationOf(MANAGEMENT, true, false);
|
||||
|
||||
assertEquals("ServiceAccessNotEnabledException", codeOf(() -> rootAccess.list(MANAGEMENT)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOrdinaryMemberAccountCannotSeeOrMoveTheFeatures() {
|
||||
organizationOf(MEMBER, true, true);
|
||||
|
||||
assertEquals("AccountNotManagementOrDelegatedAdministratorException",
|
||||
codeOf(() -> rootAccess.list(MEMBER)));
|
||||
assertEquals("AccountNotManagementOrDelegatedAdministratorException",
|
||||
codeOf(() -> rootAccess.enableRootSessions(MEMBER)));
|
||||
verify(iamService, never()).enableOrganizationsRootSessions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code CallerIsNotManagementAccount} is modeled on the two enable operations and on neither
|
||||
* disable one, so a delegated administrator may read the features and turn them off but not on.
|
||||
*/
|
||||
@Test
|
||||
void aDelegatedAdministratorMayListAndDisableButNotEnable() {
|
||||
organizationOf(DELEGATED_ADMIN, true, true);
|
||||
when(iamService.listOrganizationsFeatures()).thenReturn(List.of(IamService.FEATURE_ROOT_SESSIONS));
|
||||
when(iamService.disableOrganizationsRootSessions()).thenReturn(List.of());
|
||||
|
||||
assertEquals(List.of(IamService.FEATURE_ROOT_SESSIONS), rootAccess.list(DELEGATED_ADMIN).enabledFeatures());
|
||||
assertEquals(List.of(), rootAccess.disableRootSessions(DELEGATED_ADMIN).enabledFeatures());
|
||||
assertEquals("CallerIsNotManagementAccountException",
|
||||
codeOf(() -> rootAccess.enableRootSessions(DELEGATED_ADMIN)));
|
||||
assertEquals("CallerIsNotManagementAccountException",
|
||||
codeOf(() -> rootAccess.enableRootCredentialsManagement(DELEGATED_ADMIN)));
|
||||
verify(iamService, never()).enableOrganizationsRootSessions();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFeatureSetIsAnsweredWithTheOrganizationItBelongsTo() {
|
||||
organizationOf(MANAGEMENT, true, true);
|
||||
when(iamService.enableOrganizationsRootSessions())
|
||||
.thenReturn(List.of(IamService.FEATURE_ROOT_SESSIONS));
|
||||
|
||||
CentralRootAccess.Features features = rootAccess.enableRootSessions(MANAGEMENT);
|
||||
|
||||
assertEquals(ORGANIZATION, features.organizationId());
|
||||
assertEquals(List.of(IamService.FEATURE_ROOT_SESSIONS), features.enabledFeatures());
|
||||
}
|
||||
|
||||
/**
|
||||
* A privileged session needs the feature the guide asks for: "You have enabled centralized
|
||||
* root access in your organization", which is {@code EnableOrganizationsRootSessions}.
|
||||
*/
|
||||
@Test
|
||||
void aPrivilegedSessionNeedsRootSessionsEnabled() {
|
||||
organizationOf(MANAGEMENT, true, true);
|
||||
when(iamService.listOrganizationsFeatures())
|
||||
.thenReturn(List.of(IamService.FEATURE_ROOT_CREDENTIALS));
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> rootAccess.privilegedSessionOrganization(MANAGEMENT));
|
||||
|
||||
assertEquals("AccessDenied", refused.getErrorCode());
|
||||
assertEquals(403, refused.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPrivilegedSessionAnswersTheOrganizationOnceItIsEnabled() {
|
||||
organizationOf(DELEGATED_ADMIN, true, true);
|
||||
when(iamService.listOrganizationsFeatures()).thenReturn(List.of(IamService.FEATURE_ROOT_SESSIONS));
|
||||
|
||||
RootAccessOrganization.Membership organization =
|
||||
rootAccess.privilegedSessionOrganization(DELEGATED_ADMIN);
|
||||
|
||||
assertEquals(ORGANIZATION, organization.organizationId());
|
||||
assertEquals(MANAGEMENT, organization.managementAccountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAccountOutsideTheOrganizationCannotOpenAPrivilegedSession() {
|
||||
when(organizations.membershipOf(MEMBER)).thenReturn(Optional.empty());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> rootAccess.privilegedSessionOrganization(MEMBER));
|
||||
|
||||
assertEquals("AccessDenied", refused.getErrorCode());
|
||||
assertEquals(403, refused.getHttpStatus());
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ class IamQueryHandlerTest {
|
||||
"fabricated-key", null, Map.of())).thenReturn(certificate);
|
||||
when(service.getServerCertificate(CERTIFICATE_NAME)).thenReturn(certificate);
|
||||
when(service.listServerCertificates(null)).thenReturn(List.of(certificate));
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null, null, null);
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("ServerCertificateName", CERTIFICATE_NAME);
|
||||
@@ -90,7 +90,7 @@ class IamQueryHandlerTest {
|
||||
IamService service = mock(IamService.class);
|
||||
when(service.createRole(ROLE_NAME, "/", "{}", null, 3600, Map.of(), null)).thenReturn(role);
|
||||
when(service.getValidatedRole(ROLE_NAME)).thenReturn(role);
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null, null, null);
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleName", ROLE_NAME);
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.OrganizationsRequests;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.github.hectorvent.floci.testing.S3IamEnforcementProfile;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.ExtractableResponse;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* What a privileged session may do, under IAM enforcement. The session is the member account's
|
||||
* root user, which no identity policy grants or denies, so the whole of the bound is the task
|
||||
* policy the request named: "you must define TaskPolicyArn to scope the session to the privileged
|
||||
* action to be performed during the session".
|
||||
*
|
||||
* <p>{@code IAMAuditRootUserCredentials} is the one used here. It denies every action it does not
|
||||
* name and then denies the ones it does on any resource but {@code arn:aws:iam::*:root}, so one
|
||||
* policy shows both halves of the scope: the audit reads it allows on the root user, and
|
||||
* everything else refused.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(S3IamEnforcementProfile.class)
|
||||
class StsAssumeRootEnforcementIntegrationTest {
|
||||
|
||||
private static final String MANAGEMENT = "777777777771";
|
||||
private static final String TASK_POLICY = "arn:aws:iam::aws:policy/root-task/IAMAuditRootUserCredentials";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPrivilegedSessionMayAuditTheRootUserAndNothingElse() {
|
||||
String member = memberAccountWithRootSessions();
|
||||
ExtractableResponse<Response> session = privilegedSession(member, callerWithAssumeRoot());
|
||||
String accessKeyId = session.path("AssumeRootResponse.AssumeRootResult.Credentials.AccessKeyId");
|
||||
String sessionToken = session.path("AssumeRootResponse.AssumeRootResult.Credentials.SessionToken");
|
||||
|
||||
// Allowed: the policy's first statement denies every action it does not name, and the
|
||||
// account summary is one it names, on no particular resource.
|
||||
privileged(accessKeyId, sessionToken, "GetAccountSummary")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
// Denied on any resource but the root user, which is the policy's second statement. The
|
||||
// audit reads it allows with no UserName name that root user here, since a root caller's
|
||||
// self-resource is its root ARN, but Floci keeps no user record for the root user, so
|
||||
// those calls answer NoSuchEntity rather than the credentials report AWS gives.
|
||||
privileged(accessKeyId, sessionToken, "GetUser")
|
||||
.formParam("UserName", "someone-else")
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
// And denied outright for an action the policy does not name, which is its first.
|
||||
privileged(accessKeyId, sessionToken, "CreateUser")
|
||||
.formParam("UserName", "made-by-a-privileged-session")
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
}
|
||||
|
||||
/** An organization with one member account and {@code RootSessions} enabled. */
|
||||
private String memberAccountWithRootSessions() {
|
||||
OrganizationsRequests.request(MANAGEMENT, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
String member = OrganizationsRequests.request(MANAGEMENT, "CreateAccount",
|
||||
"{\"Email\":\"audit@example.com\",\"AccountName\":\"Audit\"}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getString("CreateAccountStatus.AccountId");
|
||||
OrganizationsRequests.request(MANAGEMENT, "EnableAWSServiceAccess",
|
||||
"{\"ServicePrincipal\":\"iam.amazonaws.com\"}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
given().header("Authorization", EnforcementFixtures.auth(MANAGEMENT, "iam"))
|
||||
.formParam("Action", "EnableOrganizationsRootSessions")
|
||||
.formParam("Version", "2010-05-08")
|
||||
.when().post("/").then().statusCode(200);
|
||||
return member;
|
||||
}
|
||||
|
||||
/**
|
||||
* An IAM user of the management account allowed {@code sts:AssumeRoot}, which is the caller the
|
||||
* guide's prerequisites ask for. The account root cannot be it, AssumeRoot refusing root
|
||||
* credentials, so under enforcement the permission is granted to somebody rather than
|
||||
* inherited from the account's own allow-all.
|
||||
*/
|
||||
private String callerWithAssumeRoot() {
|
||||
String userName = "root-session-caller-" + UUID.randomUUID();
|
||||
String authorization = EnforcementFixtures.auth(MANAGEMENT, "iam");
|
||||
String accessKeyId = EnforcementFixtures
|
||||
.createUser(cleanup, baseUri.getPort(), authorization, userName).accessKeyId();
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), authorization, userName, "assume-root",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"sts:AssumeRoot\",\"Resource\":\"*\"}]}");
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
private ExtractableResponse<Response> privilegedSession(String member, String callerKey) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(callerKey, "sts"))
|
||||
.formParam("Action", "AssumeRoot")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.formParam("TargetPrincipal", member)
|
||||
.formParam("TaskPolicyArn.arn", TASK_POLICY)
|
||||
.when().post("/").then().statusCode(200).extract();
|
||||
}
|
||||
|
||||
private static RequestSpecification privileged(String accessKeyId, String sessionToken, String action) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accessKeyId, "iam"))
|
||||
.header("X-Amz-Security-Token", sessionToken)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2010-05-08");
|
||||
}
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.OrganizationsRequests;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* Centralized root access end to end: the management account turns the feature on, a delegated
|
||||
* administrator sees the same organization-wide answer, and a privileged session acts as a member
|
||||
* account's root user scoped to the task policy it names.
|
||||
*
|
||||
* <p>Floci resolves a 12-digit access key id straight to an account id, so each request carries a
|
||||
* SigV4-shaped {@code Authorization} header naming the calling account. The accounts here are
|
||||
* their own, so this class never contends with another over one organization.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class StsAssumeRootIntegrationTest {
|
||||
|
||||
private static final String MANAGEMENT = "666666666661";
|
||||
private static final String OUTSIDER = "666666666669";
|
||||
private static final String UNRELATED_ACCOUNT = "666666666668";
|
||||
private static final String TASK_POLICY = "arn:aws:iam::aws:policy/root-task/S3UnlockBucketPolicy";
|
||||
|
||||
private String organizationId;
|
||||
private String memberAccount;
|
||||
private String delegatedAdmin;
|
||||
private String managementKey;
|
||||
private String adminKey;
|
||||
private String outsiderKey;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* An IAM user of {@code accountId} holding one access key. AssumeRoot refuses the account root,
|
||||
* both the key that resolves to its ARN and the bare account-id key floci reads as that
|
||||
* principal, so every caller here is a user of the account rather than the account itself.
|
||||
*/
|
||||
private static String accessKeyFor(String accountId, String userName) {
|
||||
iam(accountId, "CreateUser").formParam("UserName", userName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
return iam(accountId, "CreateAccessKey").formParam("UserName", userName)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
|
||||
}
|
||||
|
||||
private static RequestSpecification iam(String accountId, String action) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accountId, "iam"))
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2010-05-08");
|
||||
}
|
||||
|
||||
private static RequestSpecification assumeRoot(String accessKeyId, String targetPrincipal,
|
||||
String taskPolicyArn) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accessKeyId, "sts"))
|
||||
.formParam("Action", "AssumeRoot")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.formParam("TargetPrincipal", targetPrincipal)
|
||||
.formParam("TaskPolicyArn.arn", taskPolicyArn);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void anOrganizationWithAMemberAndADelegatedAdministrator() {
|
||||
organizationId = OrganizationsRequests.request(MANAGEMENT, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getString("Organization.Id");
|
||||
|
||||
memberAccount = OrganizationsRequests.request(MANAGEMENT, "CreateAccount",
|
||||
"{\"Email\":\"member@example.com\",\"AccountName\":\"Member\"}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getString("CreateAccountStatus.AccountId");
|
||||
delegatedAdmin = OrganizationsRequests.request(MANAGEMENT, "CreateAccount",
|
||||
"{\"Email\":\"iam-admin@example.com\",\"AccountName\":\"IamAdmin\"}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getString("CreateAccountStatus.AccountId");
|
||||
|
||||
OrganizationsRequests.request(MANAGEMENT, "EnableAWSServiceAccess",
|
||||
"{\"ServicePrincipal\":\"iam.amazonaws.com\"}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
OrganizationsRequests.request(MANAGEMENT, "RegisterDelegatedAdministrator",
|
||||
"{\"AccountId\":\"" + delegatedAdmin + "\",\"ServicePrincipal\":\"iam.amazonaws.com\"}")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
managementKey = accessKeyFor(MANAGEMENT, "root-session-caller");
|
||||
adminKey = accessKeyFor(delegatedAdmin, "delegated-session-caller");
|
||||
outsiderKey = accessKeyFor(OUTSIDER, "outsider-session-caller");
|
||||
}
|
||||
|
||||
/** "You have enabled centralized root access in your organization" is a prerequisite, not a default. */
|
||||
@Test
|
||||
@Order(2)
|
||||
void aPrivilegedSessionIsRefusedBeforeRootSessionsIsEnabled() {
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void theManagementAccountEnablesRootSessions() {
|
||||
iam(MANAGEMENT, "EnableOrganizationsRootSessions")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("EnableOrganizationsRootSessionsResponse.EnableOrganizationsRootSessionsResult"
|
||||
+ ".OrganizationId", equalTo(organizationId))
|
||||
.body("EnableOrganizationsRootSessionsResponse.EnableOrganizationsRootSessionsResult"
|
||||
+ ".EnabledFeatures.member", equalTo("RootSessions"));
|
||||
}
|
||||
|
||||
/** The feature set is the organization's, so the delegated administrator reads what was enabled. */
|
||||
@Test
|
||||
@Order(4)
|
||||
void aDelegatedAdministratorSeesTheOrganizationsOwnFeatureSet() {
|
||||
iam(delegatedAdmin, "ListOrganizationsFeatures")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("ListOrganizationsFeaturesResponse.ListOrganizationsFeaturesResult.OrganizationId",
|
||||
equalTo(organizationId))
|
||||
.body("ListOrganizationsFeaturesResponse.ListOrganizationsFeaturesResult"
|
||||
+ ".EnabledFeatures.member", equalTo("RootSessions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void onlyTheRightCallerMayMoveTheFeatures() {
|
||||
iam(delegatedAdmin, "EnableOrganizationsRootCredentialsManagement")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("CallerIsNotManagementAccountException"));
|
||||
iam(memberAccount, "ListOrganizationsFeatures")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code",
|
||||
equalTo("AccountNotManagementOrDelegatedAdministratorException"));
|
||||
iam(OUTSIDER, "ListOrganizationsFeatures")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("OrganizationNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void aPrivilegedSessionActsAsTheMemberAccountRoot() {
|
||||
String accessKeyId = assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("DurationSeconds", "900")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("AssumeRootResponse.AssumeRootResult.Credentials.AccessKeyId", startsWith("ASIA"))
|
||||
.body("AssumeRootResponse.AssumeRootResult.Credentials.Expiration", notNullValue())
|
||||
.body("AssumeRootResponse.AssumeRootResult.SessionTokenSize", notNullValue())
|
||||
.extract().path("AssumeRootResponse.AssumeRootResult.Credentials.AccessKeyId");
|
||||
String sessionToken = assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("AssumeRootResponse.AssumeRootResult.Credentials.SessionToken");
|
||||
|
||||
given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accessKeyId, "sts"))
|
||||
.header("X-Amz-Security-Token", sessionToken)
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Account", equalTo(memberAccount))
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn",
|
||||
equalTo("arn:aws:iam::" + memberAccount + ":root"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void aDelegatedAdministratorMayOpenOneToo() {
|
||||
assumeRoot(adminKey, memberAccount, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("AssumeRootResponse.AssumeRootResult.Credentials.AccessKeyId", startsWith("ASIA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void theCallerTheTargetAndTheTaskPolicyAreAllChecked() {
|
||||
assumeRoot(outsiderKey, memberAccount, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
assumeRoot(managementKey, UNRELATED_ACCOUNT, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
// Privileged tasks are what the management account performs on members, not on itself.
|
||||
assumeRoot(managementKey, MANAGEMENT, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
assumeRoot(managementKey, memberAccount, "arn:aws:iam::aws:policy/AdministratorAccess")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("DurationSeconds", "901")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
given()
|
||||
.header("Authorization", EnforcementFixtures.auth(managementKey, "sts"))
|
||||
.formParam("Action", "AssumeRoot")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.formParam("TaskPolicyArn.arn", TASK_POLICY)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
/**
|
||||
* "The global endpoint is not supported for AssumeRoot. You must send this request to a
|
||||
* Regional AWS STS endpoint." Floci can only see that when the request names the global host,
|
||||
* which is why the refusal is on the host rather than on the signing region.
|
||||
*/
|
||||
@Test
|
||||
@Order(9)
|
||||
void theGlobalEndpointIsRefusedWhenTheRequestArrivesOnIt() {
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.header("Host", "sts.amazonaws.com")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
/** The five task policies the guide names all live under the one path the check reads. */
|
||||
@Test
|
||||
@Order(10)
|
||||
void everyTaskPolicyTheGuideNamesIsUsable() {
|
||||
for (String policy : new String[] {"IAMAuditRootUserCredentials", "IAMCreateRootUserPassword",
|
||||
"IAMDeleteRootUserCredentials", "S3UnlockBucketPolicy", "SQSUnlockQueuePolicy"}) {
|
||||
assumeRoot(managementKey, memberAccount, "arn:aws:iam::aws:policy/root-task/" + policy)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("AssumeRootResponse.AssumeRootResult.Credentials.SessionToken", notNullValue());
|
||||
}
|
||||
iam(MANAGEMENT, "ListOrganizationsFeatures")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("ListOrganizationsFeaturesResponse.ListOrganizationsFeaturesResult"
|
||||
+ ".EnabledFeatures.member", equalTo("RootSessions"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A customer-managed policy can carry the {@code /root-task/} path as easily as AWS's own, so
|
||||
* whose policy it is decides and not only where it sits: the model names five AWS managed
|
||||
* policies, and a document of the caller's own would scope the session to whatever it allows.
|
||||
*/
|
||||
@Test
|
||||
@Order(11)
|
||||
void onlyAwsOwnRootTaskPoliciesCanScopeASession() {
|
||||
String ownPolicy = iam(MANAGEMENT, "CreatePolicy")
|
||||
.formParam("PolicyName", "LooksLikeATaskPolicy")
|
||||
.formParam("Path", "/root-task/")
|
||||
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\""
|
||||
+ ":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
|
||||
assumeRoot(managementKey, memberAccount, ownPolicy)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code MinimumSessionTokenSize} is 0 to 4096 in the model, "The value must not exceed 4,096
|
||||
* bytes", and the size it asks for is the size issued, so it is held to that bound rather than
|
||||
* clamped to it.
|
||||
*/
|
||||
@Test
|
||||
@Order(12)
|
||||
void theMinimumSessionTokenSizeIsHeldToTheBoundTheModelDeclares() {
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("MinimumSessionTokenSize", "4097")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("MinimumSessionTokenSize", "-1")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("MinimumSessionTokenSize", "two thousand")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
|
||||
assumeRoot(managementKey, memberAccount, TASK_POLICY)
|
||||
.formParam("MinimumSessionTokenSize", "1024")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("AssumeRootResponse.AssumeRootResult.SessionTokenSize", equalTo("1024"));
|
||||
}
|
||||
|
||||
/**
|
||||
* "You cannot use root user credentials to call sts:AssumeRoot." The bare account-id key is
|
||||
* floci's own account-root principal, which {@code IamEnforcementFilter} says in as many words, so it
|
||||
* is refused here as the credential it stands for even though it resolves to no ARN.
|
||||
*/
|
||||
@Test
|
||||
@Order(13)
|
||||
void theAccountRootCannotOpenAPrivilegedSession() {
|
||||
assumeRoot(MANAGEMENT, memberAccount, TASK_POLICY)
|
||||
.when().post("/").then().statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"))
|
||||
.body("ErrorResponse.Error.Message", startsWith("Root user credentials cannot call"));
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,7 @@ class StsQueryHandlerTest {
|
||||
mock(SAMLProviderService.class),
|
||||
mock(SAMLTrustPolicyEvaluator.class),
|
||||
delegation,
|
||||
mock(CentralRootAccess.class),
|
||||
clock);
|
||||
}
|
||||
|
||||
@@ -661,7 +662,7 @@ class StsQueryHandlerTest {
|
||||
StsQueryHandler handler = new StsQueryHandler(iamService, mock(AccountResolver.class),
|
||||
new RegionResolver(REGION, "000000000000"), config, mock(AssumeRolePolicyEvaluator.class),
|
||||
trust, verifier, keys, mock(SAMLProviderService.class), mock(SAMLTrustPolicyEvaluator.class),
|
||||
mock(DelegationRequestService.class));
|
||||
mock(DelegationRequestService.class), mock(CentralRootAccess.class));
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", requestedRoleArn);
|
||||
params.putSingle("RoleSessionName", "app");
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
/**
|
||||
* Organizations calls over its JSON 1.1 wire, signed as the calling account the way
|
||||
* {@link EnforcementFixtures#auth} signs every other fixture here.
|
||||
*/
|
||||
public final class OrganizationsRequests {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String TARGET_PREFIX = "AWSOrganizationsV20161128.";
|
||||
|
||||
private OrganizationsRequests() {
|
||||
}
|
||||
|
||||
/** One call, named by its action, carrying {@code body} as its payload. */
|
||||
public static RequestSpecification request(String accountId, String action, String body) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accountId, "organizations"))
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user