mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sts): reject unverified web identity tokens (#3151)
* fix(sts): reject unverified web identity tokens * test(sts): expect invalid web identity tokens to fail * fix(sts): make unknown issuer rejection opt in
This commit is contained in:
1 parent
dd33308e7e
commit
1de6b80035
6 files changed
+78
-29
No files matched your search
@@ -41,7 +41,7 @@ created through IAM with a real trust policy.
|
||||
|
||||
## Web Identity Validation (IRSA)
|
||||
|
||||
`AssumeRoleWithWebIdentity` validates tokens minted by an OIDC issuer Floci hosts - currently an EKS cluster's IRSA provider. This needs no configuration flag and is independent of `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED`.
|
||||
`AssumeRoleWithWebIdentity` validates tokens minted by an OIDC issuer Floci hosts - currently an EKS cluster's IRSA provider.
|
||||
|
||||
When the token's `iss` names a known Floci issuer, all of the following are enforced:
|
||||
|
||||
@@ -53,7 +53,7 @@ When the token's `iss` names a known Floci issuer, all of the following are enfo
|
||||
|
||||
The response carries the token's real claims in `SubjectFromWebIdentityToken`, `Provider`, and `Audience`. A bad token returns `InvalidIdentityToken` (400); a trust policy that does not permit the subject returns `AccessDenied` (403).
|
||||
|
||||
Tokens from an issuer Floci does not host are treated as opaque and accepted, since Floci cannot verify a third-party provider's signature. See [EKS](eks.md) for the full IRSA walkthrough and the token-minting endpoint.
|
||||
By default, tokens from an issuer Floci does not host, or tokens that are not parseable JWTs, are treated as opaque and accepted for compatibility with existing workflows. When `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true`, those tokens return `InvalidIdentityToken` because Floci cannot verify a third-party provider's signature. See [EKS](eks.md) for the full IRSA walkthrough and the token-minting endpoint.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
@@ -10,14 +10,13 @@ import java.util.Optional;
|
||||
* {@code services.iam}) can verify {@code sts:AssumeRoleWithWebIdentity} tokens minted for an
|
||||
* EKS cluster's IRSA issuer without {@code services.iam} depending on {@code services.eks}.
|
||||
*
|
||||
* <p>An empty result means the issuer is unknown to Floci. Callers treat that as "not a
|
||||
* Floci-issued token" and fall back to permissive handling rather than rejecting, so
|
||||
* third-party web-identity tokens keep working.
|
||||
* <p>An empty result means the issuer is unknown to Floci. Callers must reject tokens from
|
||||
* unknown issuers because they cannot be verified against a trusted key.
|
||||
*/
|
||||
public interface OidcIssuerKeyLookup {
|
||||
|
||||
/**
|
||||
* Returns the RSA public key for {@code issuer}, or {@link Optional#empty()} if no
|
||||
* Returns the RSA public key for {@code issuer}, or {@link Optional#empty()} if no trusted
|
||||
* resource known to Floci issues tokens under that issuer URL.
|
||||
*/
|
||||
Optional<RSAPublicKey> findVerificationKey(String issuer);
|
||||
|
||||
@@ -26,10 +26,10 @@ import jakarta.inject.Inject;
|
||||
* JWTs with plain {@code java.security} primitives, and this only needs the one algorithm EKS IRSA
|
||||
* uses.
|
||||
*
|
||||
* <p>Two-stage design, which is what keeps existing permissive behaviour intact. {@link #peekIssuer}
|
||||
* reads the {@code iss} claim <em>without</em> verifying anything, so a caller can ask "is this a
|
||||
* token Floci issued?" before deciding to enforce. Only when the issuer resolves to a known key does
|
||||
* {@link #verify} run and reject on any failure.
|
||||
* <p>Two-stage design lets the caller select a trusted key before verification. {@link #peekIssuer}
|
||||
* reads the {@code iss} claim <em>without</em> verifying anything. The caller must reject tokens
|
||||
* when the issuer does not resolve to a trusted key; {@link #verify} then rejects any verification
|
||||
* failure for a known issuer.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class WebIdentityTokenVerifier {
|
||||
@@ -55,7 +55,7 @@ public class WebIdentityTokenVerifier {
|
||||
|
||||
/**
|
||||
* Reads the {@code iss} claim without verifying the signature. Returns empty when {@code token}
|
||||
* is not a parseable JWT at all — the caller then treats it as an opaque third-party token.
|
||||
* is not a parseable JWT at all — the caller must reject it.
|
||||
*/
|
||||
public Optional<String> peekIssuer(String token) {
|
||||
return parseClaims(token).map(claims -> claims.path("iss").asText(null))
|
||||
@@ -169,8 +169,7 @@ public class WebIdentityTokenVerifier {
|
||||
return Optional.empty();
|
||||
}
|
||||
// Limit -1 for the same reason as verify(): without it an unsigned "header.payload." token
|
||||
// splits into two parts, so its issuer would go unseen and the caller would treat a token
|
||||
// that names a Floci issuer as opaque, skipping validation entirely.
|
||||
// splits into two parts, so its issuer would go unseen and validation could be skipped.
|
||||
String[] parts = token.split("\\.", -1);
|
||||
return parts.length == 3 ? decodeJson(parts[1]) : Optional.empty();
|
||||
}
|
||||
|
||||
@@ -199,9 +199,6 @@ public class StsQueryHandler {
|
||||
String callerAccountId = regionResolver.getAccountId();
|
||||
String accountId = AwsArnUtils.accountOrDefault(roleArn, callerAccountId);
|
||||
|
||||
// Only tokens naming an issuer Floci itself hosts (an EKS cluster's IRSA OIDC provider) are
|
||||
// verifiable, so only those are enforced. An opaque or third-party token keeps the historical
|
||||
// permissive behaviour rather than failing a workflow Floci cannot adjudicate.
|
||||
WebIdentityOutcome outcome = verifyWebIdentityToken(webIdentityToken, roleName, accountId, roleArn);
|
||||
if (outcome.denial() != null) {
|
||||
return outcome.denial();
|
||||
@@ -242,16 +239,11 @@ public class StsQueryHandler {
|
||||
/** The claims of a token Floci issued and verified, used to fill the response accurately. */
|
||||
private record VerifiedWebIdentity(String issuer, String subject, String audience) {}
|
||||
|
||||
/**
|
||||
* The result of inspecting a web identity token: at most one field is non-null. Both null means
|
||||
* the issuer is unknown to Floci, so the token is treated as opaque and accepted.
|
||||
*/
|
||||
private record WebIdentityOutcome(VerifiedWebIdentity verified, Response denial) {
|
||||
|
||||
static WebIdentityOutcome unverifiable() {
|
||||
return new WebIdentityOutcome(null, null);
|
||||
}
|
||||
|
||||
static WebIdentityOutcome allow(VerifiedWebIdentity verified) {
|
||||
return new WebIdentityOutcome(verified, null);
|
||||
}
|
||||
@@ -261,20 +253,22 @@ public class StsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inspects {@code token} and decides whether it may assume {@code roleArn}. Returns an outcome
|
||||
* carrying the verified claims, a denial response, or neither when the token's issuer is not one
|
||||
* Floci hosts (preserving the historical permissive behaviour for third-party providers).
|
||||
*/
|
||||
/** Inspects {@code token} and decides whether it may assume {@code roleArn}. */
|
||||
private WebIdentityOutcome verifyWebIdentityToken(String token, String roleName, String roleAccountId,
|
||||
String roleArn) {
|
||||
Optional<String> issuer = tokenVerifier.peekIssuer(token);
|
||||
if (issuer.isEmpty()) {
|
||||
return WebIdentityOutcome.unverifiable();
|
||||
return config.services().iam().enforcementEnabled()
|
||||
? WebIdentityOutcome.deny(AwsQueryResponse.error("InvalidIdentityToken",
|
||||
"The web identity token does not identify a trusted issuer.", AwsNamespaces.STS, 400))
|
||||
: WebIdentityOutcome.unverifiable();
|
||||
}
|
||||
Optional<RSAPublicKey> key = oidcIssuerKeys.findVerificationKey(issuer.get());
|
||||
if (key.isEmpty()) {
|
||||
return WebIdentityOutcome.unverifiable();
|
||||
return config.services().iam().enforcementEnabled()
|
||||
? WebIdentityOutcome.deny(AwsQueryResponse.error("InvalidIdentityToken",
|
||||
"The web identity token issuer is not trusted.", AwsNamespaces.STS, 400))
|
||||
: WebIdentityOutcome.unverifiable();
|
||||
}
|
||||
|
||||
WebIdentityToken claims;
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
* it via {@code sts:AssumeRoleWithWebIdentity}.
|
||||
*
|
||||
* <p>Also covers the negative cases that matter — wrong service account, tampered signature, wrong
|
||||
* audience — and the compatibility guarantee that an opaque third-party token is still accepted.
|
||||
* audience, and the compatibility guarantee that an opaque third-party token is still accepted.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(StsWebIdentityEnforcementIntegrationTest.EnforcementProfile.class)
|
||||
class StsWebIdentityEnforcementIntegrationTest {
|
||||
|
||||
@Test
|
||||
void rejectsOpaqueTokenWhenIamEnforcementIsEnabled() {
|
||||
assumeRole("dummy-token")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidIdentityToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownIssuerWhenIamEnforcementIsEnabled() {
|
||||
String header = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(
|
||||
"{\"alg\":\"none\"}".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
String payload = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(
|
||||
"{\"iss\":\"https://untrusted.example\",\"aud\":\"sts.amazonaws.com\"}"
|
||||
.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
|
||||
assumeRole(header + "." + payload + ".")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidIdentityToken"));
|
||||
}
|
||||
|
||||
private static io.restassured.response.Response assumeRole(String token) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRoleWithWebIdentity")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.formParam("RoleArn", "arn:aws:iam::000000000000:role/web-identity-role")
|
||||
.formParam("RoleSessionName", "enforcement-test-session")
|
||||
.formParam("WebIdentityToken", token)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
public static final class EnforcementProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.iam.enforcement-enabled", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user