mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(iam): route long-term access keys to owning accounts (#2319)
* feat(storage): expose account ownership when scanning resources * fix(storage): validate account prefixes when scanning * fix(iam): resolve long-term access key accounts * test(iam): cover long-term access key account routing * docs(auth): clarify account-routing trust model
This commit is contained in:
1 parent
be625023eb
commit
02898cf5e2
11 files changed
+168
-35
No files matched your search
@@ -17,8 +17,8 @@ import java.util.Optional;
|
||||
* downstream filters (e.g. IAM enforcement) can rely on the context being set.
|
||||
*
|
||||
* <p>Account resolution precedence: a 12-digit access key ID is used directly as
|
||||
* the account; otherwise temporary credentials (e.g. assumed-role {@code ASIA...}
|
||||
* keys) are looked up in the session store via {@link SessionAccountLookup}; if
|
||||
* 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.
|
||||
*/
|
||||
@Provider
|
||||
@@ -66,13 +66,13 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
/**
|
||||
* Applies the account-resolution precedence. A 12-digit AKID is already reflected in
|
||||
* {@code resolvedDefault} (the account or default returned by {@link AccountResolver});
|
||||
* for any other key shape, a live session lookup takes precedence before falling back.
|
||||
* for any other key shape, an IAM or live-session lookup takes precedence before falling back.
|
||||
*/
|
||||
private String resolveAccount(String akid, String resolvedDefault) {
|
||||
if (akid != null && !akid.matches("\\d{12}")) {
|
||||
Optional<String> sessionAccount = sessionAccountLookup.resolveAccountId(akid);
|
||||
if (sessionAccount.isPresent()) {
|
||||
return sessionAccount.get();
|
||||
Optional<String> credentialAccount = sessionAccountLookup.resolveAccountId(akid);
|
||||
if (credentialAccount.isPresent()) {
|
||||
return credentialAccount.get();
|
||||
}
|
||||
}
|
||||
return resolvedDefault;
|
||||
|
||||
@@ -3,8 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Port that maps an STS temporary access key ID (e.g. {@code ASIA...}) to the AWS
|
||||
* account the request should resolve to.
|
||||
* Port that maps an IAM or STS access key ID to the AWS account the request should resolve to.
|
||||
*
|
||||
* <p>Declared in {@code core.common} so {@link AccountContextFilter} can route
|
||||
* assumed-role and other temporary credentials to the correct account without
|
||||
@@ -14,8 +13,8 @@ import java.util.Optional;
|
||||
public interface SessionAccountLookup {
|
||||
|
||||
/**
|
||||
* Returns the account ID associated with the given temporary access key ID,
|
||||
* or {@link Optional#empty()} if it is not a known, live session.
|
||||
* Returns the account ID associated with the given access key ID, or
|
||||
* {@link Optional#empty()} if it is not a known, active credential.
|
||||
*/
|
||||
Optional<String> resolveAccountId(String accessKeyId);
|
||||
}
|
||||
@@ -31,6 +31,9 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class AccountAwareStorageBackend<V> implements StorageBackend<String, V> {
|
||||
|
||||
/** A stored value together with its owning AWS account and account-relative key. */
|
||||
public record AccountEntry<T>(String accountId, String key, T value) {}
|
||||
|
||||
private final StorageBackend<String, V> delegate;
|
||||
private final Instance<RequestContext> requestContextInstance;
|
||||
private final String defaultAccountId;
|
||||
@@ -129,6 +132,27 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns entries across every account while preserving the account that owns each logical key.
|
||||
* The filter receives the account-relative key. Keys without a 12-digit ASCII account prefix,
|
||||
* including legacy keys that contain slashes, are returned unchanged under the configured
|
||||
* default account. Unlike {@link #get}, this scan does not migrate legacy entries.
|
||||
*/
|
||||
public List<AccountEntry<V>> scanAllAccountEntries(Predicate<String> keyFilter) {
|
||||
List<AccountEntry<V>> result = new ArrayList<>();
|
||||
for (String rawKey : delegate.keys()) {
|
||||
boolean hasAccountPrefix = hasAccountPrefix(rawKey);
|
||||
String accountId = hasAccountPrefix ? rawKey.substring(0, 12) : defaultAccountId;
|
||||
String logicalKey = hasAccountPrefix ? rawKey.substring(13) : rawKey;
|
||||
if (!keyFilter.test(logicalKey)) {
|
||||
continue;
|
||||
}
|
||||
delegate.get(rawKey).ifPresent(value ->
|
||||
result.add(new AccountEntry<>(accountId, logicalKey, value)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all entries across every account as a map of logical-key (account prefix stripped)
|
||||
* to value. Entries without a slash-prefixed account segment are skipped.
|
||||
@@ -325,4 +349,17 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
private String prefixed(String key) {
|
||||
return prefix() + "/" + key;
|
||||
}
|
||||
|
||||
private static boolean hasAccountPrefix(String key) {
|
||||
if (key.length() < 13 || key.charAt(12) != '/') {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < 12; i++) {
|
||||
char character = key.charAt(i);
|
||||
if (character < '0' || character > '9') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1631,15 +1631,21 @@ public class IamService implements SessionAccountLookup {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the account a temporary access key belongs to: the account encoded in the
|
||||
* session's role (or federated-user) ARN when present, otherwise the caller account captured
|
||||
* at mint time. Returns empty for unknown or expired sessions so callers fall back to the
|
||||
* default account.
|
||||
* Resolves the account an IAM or temporary access key belongs to. Long-term IAM access keys
|
||||
* resolve from their owning account namespace. Temporary credentials resolve from the account
|
||||
* encoded in the session's role (or federated-user) ARN when present, otherwise the caller
|
||||
* account captured at mint time. Returns empty for unknown, inactive, or expired credentials.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> resolveAccountId(String accessKeyId) {
|
||||
if (!isTemporaryAccessKey(accessKeyId)) {
|
||||
return Optional.empty();
|
||||
if (accessKeyId == null || !(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return aware.scanAllAccountEntries(accessKeyId::equals).stream()
|
||||
.filter(entry -> "Active".equals(entry.value().getStatus()))
|
||||
.map(AccountAwareStorageBackend.AccountEntry::accountId)
|
||||
.findFirst();
|
||||
}
|
||||
Optional<SessionCredential> sessionOpt = findSessionAnyAccount(accessKeyId);
|
||||
if (sessionOpt.isEmpty()) {
|
||||
|
||||
Reference in new issue
Block a user