mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(organizations): close accounts and refuse their requests (#9331)
This commit is contained in:
1 parent
ee51e7b993
commit
85a63055be
21 files changed
+749
-29
No files matched your search
@@ -5,6 +5,7 @@ import io.floci.aws.core.common.auth.SigV4aSignature;
|
||||
import io.floci.aws.services.s3.PreSignedUrlFilter;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Priorities;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
@@ -29,6 +30,8 @@ import java.util.Set;
|
||||
* the account; otherwise IAM and temporary credentials (e.g. assumed-role {@code ASIA...}
|
||||
* keys) are looked up via {@link SessionAccountLookup}; if
|
||||
* neither matches, the configured default account applies.
|
||||
*
|
||||
* <p>A request whose credentials resolve to a closed account (see {@link ClosedAccountLookup}) is refused.
|
||||
*/
|
||||
@Provider
|
||||
@ApplicationScoped
|
||||
@@ -60,6 +63,8 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
private final RegionResolver regionResolver;
|
||||
private final RequestContext requestContext;
|
||||
private final SessionAccountLookup sessionAccountLookup;
|
||||
// Read lazily, as IamService reads the organization ports: absent when Organizations is not deployed.
|
||||
private final Instance<ClosedAccountLookup> closedAccountLookup;
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist
|
||||
// (the AwsProtocolClaimFilter pattern). jakarta.inject.Provider is qualified inline because
|
||||
// this class also carries the JAX-RS @Provider annotation.
|
||||
@@ -71,12 +76,14 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
RegionResolver regionResolver,
|
||||
RequestContext requestContext,
|
||||
SessionAccountLookup sessionAccountLookup,
|
||||
Instance<ClosedAccountLookup> closedAccountLookup,
|
||||
jakarta.inject.Provider<EmulatorConfig> configProvider,
|
||||
jakarta.inject.Provider<ResolvedServiceCatalog> catalogProvider) {
|
||||
this.accountResolver = accountResolver;
|
||||
this.regionResolver = regionResolver;
|
||||
this.requestContext = requestContext;
|
||||
this.sessionAccountLookup = sessionAccountLookup;
|
||||
this.closedAccountLookup = closedAccountLookup;
|
||||
this.config = new MemoizedSupplier<>(configProvider::get);
|
||||
this.catalog = new MemoizedSupplier<>(catalogProvider::get);
|
||||
}
|
||||
@@ -106,9 +113,11 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
if (rejectAmbiguousRegion(ctx, auth, query)) {
|
||||
return;
|
||||
}
|
||||
String akid = resourceInfo != null && catalog().byResourceClass(resourceInfo.getResourceClass())
|
||||
.map(ServiceDescriptor::acceptsLegacyAuthorization).orElse(false)
|
||||
&& !config().services().iam().enforcementEnabled()
|
||||
boolean legacyS3Authorization = resourceInfo != null
|
||||
&& catalog().byResourceClass(resourceInfo.getResourceClass())
|
||||
.map(ServiceDescriptor::acceptsLegacyAuthorization).orElse(false)
|
||||
&& !config().services().iam().enforcementEnabled();
|
||||
String akid = legacyS3Authorization
|
||||
? accountResolver.extractS3AccessKeyId(auth) : accountResolver.extractAccessKeyId(auth);
|
||||
if (akid != null) {
|
||||
requestContext.setAccountId(resolveAccount(akid, accountResolver.resolve(auth)));
|
||||
@@ -125,6 +134,12 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth));
|
||||
return;
|
||||
}
|
||||
if (accountClosed()) {
|
||||
// A SigV2 header names no service, and only S3 accepts one.
|
||||
rejectClosedAccount(ctx, SigV4CredentialScope.serviceName(auth)
|
||||
.orElse(legacyS3Authorization ? "s3" : null));
|
||||
return;
|
||||
}
|
||||
if (config().partitions().strict()) {
|
||||
rejectPartitionAbsentService(ctx, SigV4CredentialScope.serviceName(auth).orElse(null));
|
||||
}
|
||||
@@ -152,6 +167,10 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential));
|
||||
return;
|
||||
}
|
||||
if (accountClosed()) {
|
||||
rejectClosedAccount(ctx, SigV4CredentialScope.serviceNameFromCredential(credential).orElse(null));
|
||||
return;
|
||||
}
|
||||
if (config().partitions().strict()) {
|
||||
rejectPartitionAbsentService(ctx,
|
||||
SigV4CredentialScope.serviceNameFromCredential(credential).orElse(null));
|
||||
@@ -258,6 +277,22 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
ctx.abortWith(AwsProtocolClaimFilter.errorResponse(400, "InvalidSignatureException", message));
|
||||
}
|
||||
|
||||
private boolean accountClosed() {
|
||||
return closedAccountLookup.isResolvable() && closedAccountLookup.get().isClosed(requestContext.getAccountId());
|
||||
}
|
||||
|
||||
/**
|
||||
* A closed account's credentials and sessions stop working on AWS, so its requests are refused here, before
|
||||
* signature checks and whether or not IAM enforcement is on. AWS documents only that the calls fail, not an
|
||||
* error code, so the refusal is the one IAM enforcement gives a credential it does not recognize.
|
||||
*/
|
||||
private void rejectClosedAccount(ContainerRequestContext ctx, String signingName) {
|
||||
LOG.debugv("Refusing request from closed account {0}: {1} {2}",
|
||||
requestContext.getAccountId(), ctx.getMethod(), ctx.getUriInfo().getPath());
|
||||
String credentialScope = signingName == null ? null : catalog().canonicalCredentialScope(signingName);
|
||||
ctx.abortWith(IamEnforcementFilter.unrecognizedClientResponse(credentialScope, ctx));
|
||||
}
|
||||
|
||||
private static boolean isFormEncoded(MediaType mediaType) {
|
||||
return mediaType != null
|
||||
&& "application".equalsIgnoreCase(mediaType.getType())
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
/**
|
||||
* Port that tells {@link AccountContextFilter} whether a request's account has been closed, without
|
||||
* {@code core} depending on the {@code services.organizations} package that closes accounts.
|
||||
*/
|
||||
public interface ClosedAccountLookup {
|
||||
|
||||
/** Whether {@code accountId} is closed, so that AWS no longer accepts its credentials or sessions. */
|
||||
boolean isClosed(String accountId);
|
||||
}
|
||||
@@ -2121,7 +2121,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
}
|
||||
|
||||
/** {@link #unrecognizedClientResponse} for a caller that renders the error itself. */
|
||||
private static AwsException unrecognizedClientException(String credentialScope) {
|
||||
public static AwsException unrecognizedClientException(String credentialScope) {
|
||||
return "s3".equals(credentialScope)
|
||||
? new AwsException("InvalidAccessKeyId", S3_INVALID_ACCESS_KEY_ID, 403)
|
||||
: new AwsException("UnrecognizedClientException", INVALID_SECURITY_TOKEN, 403);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class AccountService implements Resettable, StoredResourceArns, AccountPl
|
||||
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 static final String ORGANIZATIONS_STATE_PENDING_CLOSURE = "PENDING_CLOSURE";
|
||||
|
||||
private final AccountAwareStorageBackend<AlternateContact> contacts;
|
||||
private final AccountAwareStorageBackend<ContactInformation> contactInformation;
|
||||
@@ -154,10 +154,11 @@ public class AccountService implements Resettable, StoredResourceArns, AccountPl
|
||||
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 Management has no PENDING_CLOSURE, which only an account closed by an earlier Floci still has.
|
||||
String organizationsState = OrganizationsService.accountState(account);
|
||||
String state = ORGANIZATIONS_STATE_PENDING_CLOSURE.equals(organizationsState)
|
||||
? ACCOUNT_STATE_ACTIVE
|
||||
: account.getStatus();
|
||||
: organizationsState;
|
||||
return new AccountInformation(account.getId(), account.getName(), created, state);
|
||||
}
|
||||
|
||||
|
||||
+1
-5
@@ -186,11 +186,7 @@ public class OrganizationsCfnProvisioner implements CfnResourceProvisioner {
|
||||
r.getAttributes().put("AccountId", account.getId());
|
||||
r.getAttributes().put("Arn", account.getArn());
|
||||
r.getAttributes().put("Status", account.getStatus());
|
||||
// State supersedes Status in the Organizations API, which retires Status on 2026-09-09.
|
||||
// Floci only ever puts an account in ACTIVE or PENDING_CLOSURE, and both are AccountState
|
||||
// values too, so the two agree; the phases State adds — PENDING_ACTIVATION and CLOSED —
|
||||
// are ones the emulator does not model.
|
||||
r.getAttributes().put("State", account.getStatus());
|
||||
r.getAttributes().put("State", OrganizationsService.accountState(account));
|
||||
r.getAttributes().put("JoinedMethod", account.getJoinedMethod());
|
||||
if (account.getJoinedTimestamp() != null) {
|
||||
r.getAttributes().put("JoinedTimestamp", account.getJoinedTimestamp().toString());
|
||||
|
||||
@@ -711,7 +711,7 @@ public class OrganizationsJsonHandler {
|
||||
node.put("Arn", account.getArn());
|
||||
node.put("Email", account.getEmail());
|
||||
node.put("Name", account.getName());
|
||||
node.put("State", account.getStatus());
|
||||
node.put("State", OrganizationsService.accountState(account));
|
||||
node.put("Status", account.getStatus());
|
||||
node.put("JoinedMethod", account.getJoinedMethod());
|
||||
putTimestamp(node, "JoinedTimestamp", account.getJoinedTimestamp());
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.ClosedAccountLookup;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.ServicePrincipals;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
@@ -62,7 +63,7 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class OrganizationsService implements ScpProvider, RootAccessOrganization,
|
||||
PrincipalOrganization {
|
||||
PrincipalOrganization, ClosedAccountLookup {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(OrganizationsService.class);
|
||||
|
||||
@@ -129,7 +130,8 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
|
||||
private static final String FEATURE_SET_CONSOLIDATED_BILLING = "CONSOLIDATED_BILLING";
|
||||
|
||||
private static final String STATUS_ACTIVE = "ACTIVE";
|
||||
private static final String STATUS_PENDING_CLOSURE = "PENDING_CLOSURE";
|
||||
private static final String STATUS_SUSPENDED = "SUSPENDED";
|
||||
private static final String STATE_CLOSED = "CLOSED";
|
||||
|
||||
/** CreateAccount's RoleName default, the role AWS preconfigures in every account it creates. */
|
||||
private static final String DEFAULT_ACCOUNT_ACCESS_ROLE_NAME = "OrganizationAccountAccessRole";
|
||||
@@ -764,9 +766,7 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
|
||||
throw new AwsException("MasterCannotLeaveOrganizationException",
|
||||
"The management account can't be removed from the organization.", 400);
|
||||
}
|
||||
detachAllPoliciesFrom(organization, account.getId());
|
||||
accounts.deleteForAccount(organization.getMasterAccountId(), account.getId());
|
||||
placementChanged(organization, account.getId());
|
||||
removeMember(organization, account);
|
||||
}
|
||||
|
||||
public void leaveOrganization(String callerAccountId) {
|
||||
@@ -775,7 +775,16 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
|
||||
throw new AwsException("MasterCannotLeaveOrganizationException",
|
||||
"The management account can't leave the organization.", 400);
|
||||
}
|
||||
OrganizationAccount account = requireAccount(organization, callerAccountId);
|
||||
removeMember(organization, requireAccount(organization, callerAccountId));
|
||||
}
|
||||
|
||||
/** As on AWS, a closed account stays in its organization until its post-closure period ends. */
|
||||
private void removeMember(Organization organization, OrganizationAccount account) {
|
||||
if (isClosed(account)) {
|
||||
throw new AwsException("ConstraintViolationException",
|
||||
"You can't remove a closed account from the organization until its post-closure period ends.",
|
||||
400);
|
||||
}
|
||||
detachAllPoliciesFrom(organization, account.getId());
|
||||
accounts.deleteForAccount(organization.getMasterAccountId(), account.getId());
|
||||
placementChanged(organization, account.getId());
|
||||
@@ -788,11 +797,33 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
|
||||
throw new AwsException("ConstraintViolationException",
|
||||
"The management account can't be closed.", 400);
|
||||
}
|
||||
account.setStatus(STATUS_PENDING_CLOSURE);
|
||||
if (isClosed(account)) {
|
||||
throw new AwsException("AccountAlreadyClosedException",
|
||||
"You attempted to close an account that is already closed.", 400);
|
||||
}
|
||||
// AWS holds the account in PENDING_CLOSURE for the few minutes closing takes, which Floci does not wait out.
|
||||
account.setStatus(STATUS_SUSPENDED);
|
||||
accounts.putForAccount(organization.getMasterAccountId(), accountId, account);
|
||||
return account;
|
||||
}
|
||||
|
||||
/**
|
||||
* The State of an account, the field that supersedes Status. The two differ only for a closed account: its
|
||||
* State is CLOSED while its Status is SUSPENDED, because State keeps SUSPENDED for an account AWS suspended.
|
||||
*/
|
||||
public static String accountState(OrganizationAccount account) {
|
||||
return isClosed(account) ? STATE_CLOSED : account.getStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed(String accountId) {
|
||||
return findAccount(accountId).filter(OrganizationsService::isClosed).isPresent();
|
||||
}
|
||||
|
||||
private static boolean isClosed(OrganizationAccount account) {
|
||||
return STATUS_SUSPENDED.equals(account.getStatus());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Policies ────────────────────────────
|
||||
|
||||
public OrganizationPolicy createPolicy(String callerAccountId, String content, String description,
|
||||
@@ -1262,6 +1293,10 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
|
||||
throw new AwsException("ConstraintViolationException",
|
||||
"The management account can't be registered as a delegated administrator.", 400);
|
||||
}
|
||||
if (isClosed(account)) {
|
||||
throw new AwsException("ConstraintViolationException",
|
||||
"You cannot register a suspended account as a delegated administrator.", 400);
|
||||
}
|
||||
if (account.getDelegatedServices().containsKey(servicePrincipal)) {
|
||||
throw new AwsException("AccountAlreadyRegisteredException",
|
||||
"The account " + accountId + " is already a delegated administrator for "
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.floci.aws.core.common.AwsCanonicalUserId;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsNamespaces;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.ClosedAccountLookup;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.IamEnforcementFilter;
|
||||
import io.floci.aws.core.common.MultipartFormParser;
|
||||
@@ -48,6 +49,7 @@ import io.floci.aws.services.s3.model.S3Object;
|
||||
import io.floci.aws.services.s3.model.TopicNotification;
|
||||
import io.floci.aws.services.s3.model.WebsiteConfiguration;
|
||||
import io.floci.aws.services.sns.SnsQueryHandler;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
@@ -147,6 +149,7 @@ public class S3Controller {
|
||||
private final RequestContext requestContext;
|
||||
private final IamService iamService;
|
||||
private final IamEnforcementFilter iamEnforcementFilter;
|
||||
private final Instance<ClosedAccountLookup> closedAccountLookup;
|
||||
private final EmulatorConfig config;
|
||||
private final S3VirtualHostFilter virtualHostFilter;
|
||||
private final Clock clock;
|
||||
@@ -162,6 +165,7 @@ public class S3Controller {
|
||||
RequestContext requestContext,
|
||||
IamService iamService,
|
||||
IamEnforcementFilter iamEnforcementFilter,
|
||||
Instance<ClosedAccountLookup> closedAccountLookup,
|
||||
EmulatorConfig config,
|
||||
S3VirtualHostFilter virtualHostFilter,
|
||||
Clock clock) {
|
||||
@@ -176,6 +180,7 @@ public class S3Controller {
|
||||
this.requestContext = requestContext;
|
||||
this.iamService = iamService;
|
||||
this.iamEnforcementFilter = iamEnforcementFilter;
|
||||
this.closedAccountLookup = closedAccountLookup;
|
||||
this.config = config;
|
||||
this.virtualHostFilter = virtualHostFilter;
|
||||
this.clock = clock;
|
||||
@@ -4173,6 +4178,7 @@ public class S3Controller {
|
||||
*/
|
||||
String credential = lcFields.get("x-amz-credential");
|
||||
rejectUnknownPostRegion(credential);
|
||||
rejectClosedPostAccount(credential);
|
||||
if (credential != null && !credential.isEmpty()) {
|
||||
iamEnforcementFilter.authorizePostObject(AccountResolver.SIGV4_SCHEME + " Credential=" + credential,
|
||||
S3ResourceArns.object(s3Service.bucketPartition(bucket), bucket, key),
|
||||
@@ -4259,6 +4265,21 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A presigned POST carries its credential in the form, which AccountContextFilter never sees, so a closed
|
||||
* account's POST is refused here as the filter refuses its other requests.
|
||||
*/
|
||||
private void rejectClosedPostAccount(String credential) {
|
||||
if (credential == null || credential.isEmpty() || !closedAccountLookup.isResolvable()) {
|
||||
return;
|
||||
}
|
||||
String akid = accountResolver.extractPresignedAccessKeyId(credential);
|
||||
String accountId = AccountResolver.isAccountId(akid) ? akid : iamService.resolveAccountId(akid).orElse(null);
|
||||
if (closedAccountLookup.get().isClosed(accountId)) {
|
||||
throw IamEnforcementFilter.unrecognizedClientException("s3");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a browser POST carries any SigV4 form field. Under {@code floci.auth.validate-signatures}
|
||||
* such a POST must verify, while one with none of them is anonymous and is left to
|
||||
|
||||
Reference in new issue
Block a user