mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(eks): resolve the token webhook's access keys from any account (#5003)
The EKS token webhook looked long-term keys up through findSecretKey, which is account-scoped. k3s calls the webhook with no account routing, so it saw only the default account's keys. A correctly signed `aws eks get-token` from a user in any other account was rejected: kubectl reported "the server has asked for the client to provide credentials". The webhook now uses a new IamService.findSecretKeyInAnyAccount. It tries the request's account first and scans the other accounts only on a miss. Inactive keys are still refused, and temporary (ASIA) keys never reach the scan. findSecretKey itself stays account-scoped: the ElastiCache, MemoryDB and RDS IAM-auth proxies verify through it and do not compare the key's account with the cluster's. Because a long-term key can now reach the webhook from any account, the legacy identity (system:masters) is limited to long-term keys owned by the cluster's account, or by the default account on unscoped webhook paths that predate per-account clusters. This also tightens one existing case: a default-account key used to get system:masters on clusters of every account, and now gets it only on the default account's clusters. Session credentials without an instance ID still map to the legacy identity on any cluster; that predates this change and is left alone. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
f8a721ce57
commit
7822b2c396
8 files changed
+243
-17
No files matched your search
@@ -167,7 +167,7 @@ class EksTokenValidator {
|
||||
if (iamService.isSeededDeployerAccessKey(accessKeyId)) {
|
||||
return null;
|
||||
}
|
||||
Optional<String> secretKey = iamService.findSecretKey(accessKeyId, sessionToken);
|
||||
Optional<String> secretKey = iamService.findSecretKeyInAnyAccount(accessKeyId, sessionToken);
|
||||
return secretKey.orElse(null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
@@ -22,13 +23,16 @@ class EksWorkerAuthentication {
|
||||
private final EksService eks;
|
||||
private final Ec2Service ec2;
|
||||
private final EksAccessEntryService entries;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@Inject
|
||||
EksWorkerAuthentication(IamService iam, EksService eks, Ec2Service ec2, EksAccessEntryService entries) {
|
||||
EksWorkerAuthentication(IamService iam, EksService eks, Ec2Service ec2, EksAccessEntryService entries,
|
||||
EmulatorConfig config) {
|
||||
this.iam = iam;
|
||||
this.eks = eks;
|
||||
this.ec2 = ec2;
|
||||
this.entries = entries;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
Optional<Map<String, Object>> authenticate(EksTokenValidator.VerifiedToken token, String name,
|
||||
@@ -37,7 +41,13 @@ class EksWorkerAuthentication {
|
||||
// Keep the existing non-worker compatibility path. Expired/revoked temporary sessions
|
||||
// must never fall through to it after signature validation.
|
||||
if (session.isEmpty()) {
|
||||
return IamService.isTemporaryAccessKey(token.accessKeyId()) ? Optional.empty() : legacyIdentity();
|
||||
if (IamService.isTemporaryAccessKey(token.accessKeyId())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
// Keys resolve from every account, and the legacy identity is system:masters.
|
||||
return iam.resolveAccountId(token.accessKeyId())
|
||||
.filter(owner -> owner.equals(clusterAccount(account)))
|
||||
.flatMap(owner -> legacyIdentity());
|
||||
}
|
||||
EksSessionIdentity identity = session.get();
|
||||
if (identity.instanceId() == null) {
|
||||
@@ -94,6 +104,11 @@ class EksWorkerAuthentication {
|
||||
.filter(profile -> List.of(roleName).equals(profile.getRoleNames())).isPresent();
|
||||
}
|
||||
|
||||
/** Unscoped webhook paths predate per-account clusters, so they serve the default account. */
|
||||
private String clusterAccount(String account) {
|
||||
return account != null ? account : config.defaultAccountId();
|
||||
}
|
||||
|
||||
private static Optional<Map<String, Object>> legacyIdentity() {
|
||||
return Optional.of(Map.of("username", "floci:aws-iam", "uid", "floci-aws-iam",
|
||||
"groups", List.of("system:masters")));
|
||||
|
||||
@@ -2947,6 +2947,28 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return DEFAULT_DEPLOYER_ACCESS_KEY_ID.equals(accessKeyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #findSecretKey(String, String)}, but finds a long-term key whichever account owns
|
||||
* it. Only for callers with no account in scope that check the owner themselves, such as the
|
||||
* EKS token webhook: everywhere else a key from another account must stay unknown.
|
||||
*/
|
||||
public Optional<String> findSecretKeyInAnyAccount(String accessKeyId, String sessionToken) {
|
||||
return findSecretKey(accessKeyId, sessionToken)
|
||||
.or(() -> activeAccessKeyInAnyAccount(accessKeyId).map(entry -> entry.value().getSecretAccessKey()));
|
||||
}
|
||||
|
||||
/** The active long-term key with this ID, from whichever account owns it. */
|
||||
private Optional<AccountAwareStorageBackend.AccountEntry<AccessKey>> activeAccessKeyInAnyAccount(
|
||||
String accessKeyId) {
|
||||
if (accessKeyId == null || isTemporaryAccessKey(accessKeyId)
|
||||
|| !(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return aware.scanAllAccountEntries(accessKeyId::equals).stream()
|
||||
.filter(entry -> "Active".equals(entry.value().getStatus()))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private Optional<String> activeAccessKeySecret(String accessKeyId) {
|
||||
return accessKeys.get(accessKeyId)
|
||||
.filter(accessKey -> "Active".equals(accessKey.getStatus()))
|
||||
@@ -3258,13 +3280,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
@Override
|
||||
public Optional<String> resolveAccountId(String accessKeyId) {
|
||||
if (!isTemporaryAccessKey(accessKeyId)) {
|
||||
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();
|
||||
return activeAccessKeyInAnyAccount(accessKeyId).map(AccountAwareStorageBackend.AccountEntry::accountId);
|
||||
}
|
||||
Optional<SessionCredential> sessionOpt = findSessionAnyAccount(accessKeyId);
|
||||
if (sessionOpt.isEmpty()) {
|
||||
|
||||
Reference in new issue
Block a user