mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 19:18:01 -05:00
refactor(eks): resolve the token webhook's key and its owner in one lookup (#5055)
Follow-up to #5003. The webhook verified a long-term key through findSecretKeyInAnyAccount, and EksWorkerAuthentication then called resolveAccountId to learn which account owns it. On a key from another account both scanned every account's keys, so one token review scanned twice. findSecretKeyInAnyAccount now returns the secret with the account that owns it, and EksTokenValidator carries that account on the VerifiedToken. EksWorkerAuthentication compares it with the cluster's account instead of looking the key up again. The owner is the request's account when the key is found there, the scanned account otherwise, and null for a temporary key, whose session already records its account. The same keys are accepted, and system:masters still goes only to long-term keys of the cluster's account (or of the default account on unscoped webhook paths). One case changes, in both directions: the owner is now always the account whose stored secret verified the signature. Before, a key ID stored in two accounts could verify against one and be attributed to the other, because resolveAccountId took the first match in scan order, so such a key could be admitted for the wrong account or denied for the right one. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
728e000187
commit
60c368e939
5 files changed
+90
-28
No files matched your search
@@ -55,7 +55,8 @@ class EksTokenValidator {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
record VerifiedToken(String accessKeyId, String region) {}
|
||||
/** {@code keyAccountId} owns a long-term key, from the lookup that verified it; null for a temporary key. */
|
||||
record VerifiedToken(String accessKeyId, String region, String keyAccountId) {}
|
||||
|
||||
boolean validate(String token, String clusterName) {
|
||||
return verify(token, clusterName).isPresent();
|
||||
@@ -84,10 +85,11 @@ class EksTokenValidator {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String secretKey = secretKey(scope.accessKeyId(), parameters.get("X-Amz-Security-Token"));
|
||||
if (secretKey == null) {
|
||||
IamService.OwnedSecretKey key = secretKey(scope.accessKeyId(), parameters.get("X-Amz-Security-Token"));
|
||||
if (key == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String secretKey = key.secretAccessKey();
|
||||
|
||||
String canonicalRequest = canonicalRequest(request, parameters, clusterName);
|
||||
String stringToSign = ALGORITHM + "\n"
|
||||
@@ -99,7 +101,8 @@ class EksTokenValidator {
|
||||
boolean valid = MessageDigest.isEqual(
|
||||
expectedSignature.getBytes(StandardCharsets.UTF_8),
|
||||
parameters.get("X-Amz-Signature").getBytes(StandardCharsets.UTF_8));
|
||||
return valid ? Optional.of(new VerifiedToken(scope.accessKeyId(), scope.region())) : Optional.empty();
|
||||
return valid ? Optional.of(new VerifiedToken(scope.accessKeyId(), scope.region(), key.accountId()))
|
||||
: Optional.empty();
|
||||
} catch (Exception exception) {
|
||||
LOG.debugv("EKS IAM token validation rejected a malformed token: {0}", exception.getMessage());
|
||||
return Optional.empty();
|
||||
@@ -163,12 +166,11 @@ class EksTokenValidator {
|
||||
return !signedAt.isAfter(now.plus(MAX_FUTURE_SKEW)) && !now.isAfter(signedAt.plus(TOKEN_LIFETIME));
|
||||
}
|
||||
|
||||
private String secretKey(String accessKeyId, String sessionToken) {
|
||||
private IamService.OwnedSecretKey secretKey(String accessKeyId, String sessionToken) {
|
||||
if (iamService.isSeededDeployerAccessKey(accessKeyId)) {
|
||||
return null;
|
||||
}
|
||||
Optional<String> secretKey = iamService.findSecretKeyInAnyAccount(accessKeyId, sessionToken);
|
||||
return secretKey.orElse(null);
|
||||
return iamService.findSecretKeyInAnyAccount(accessKeyId, sessionToken).orElse(null);
|
||||
}
|
||||
|
||||
private static CredentialScope parseCredentialScope(String encodedCredential) {
|
||||
|
||||
@@ -44,8 +44,8 @@ class EksWorkerAuthentication {
|
||||
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())
|
||||
// The legacy identity is system:masters, so only a key of the cluster's own account gets it.
|
||||
return Optional.ofNullable(token.keyAccountId())
|
||||
.filter(owner -> owner.equals(clusterAccount(account)))
|
||||
.flatMap(owner -> legacyIdentity());
|
||||
}
|
||||
|
||||
@@ -3212,11 +3212,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* 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()));
|
||||
public Optional<OwnedSecretKey> findSecretKeyInAnyAccount(String accessKeyId, String sessionToken) {
|
||||
Optional<OwnedSecretKey> own = activeAccessKeySecret(accessKeyId)
|
||||
.map(secret -> new OwnedSecretKey(secret, accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware
|
||||
? aware.accountId() : null));
|
||||
return own
|
||||
.or(() -> currentSession(accessKeyId)
|
||||
.filter(session -> hasMatchingSessionToken(session, sessionToken))
|
||||
.map(session -> new OwnedSecretKey(session.getSecretAccessKey(), null)))
|
||||
.or(() -> activeAccessKeyInAnyAccount(accessKeyId)
|
||||
.map(entry -> new OwnedSecretKey(entry.value().getSecretAccessKey(), entry.accountId())));
|
||||
}
|
||||
|
||||
/**
|
||||
* A secret access key with the account that owns it, so a caller that checks the owner needs no
|
||||
* second lookup. The account is null for a temporary key, whose session records its own.
|
||||
*/
|
||||
public record OwnedSecretKey(String secretAccessKey, String accountId) {}
|
||||
|
||||
/** The active long-term key with this ID, from whichever account owns it. */
|
||||
private Optional<AccountAwareStorageBackend.AccountEntry<AccessKey>> activeAccessKeyInAnyAccount(
|
||||
String accessKeyId) {
|
||||
|
||||
Reference in new issue
Block a user