Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] PIP-257: Add AuthenticationProviderOpenID #19849

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
60df13a
[feat] PIP-257: Add AuthenticationProviderOpenID
michaeljmarshall Mar 17, 2023
c7474fa
Improve imports in pom.xml
michaeljmarshall Mar 17, 2023
af7e3ae
Refactor packaging: move under oidc
michaeljmarshall Mar 17, 2023
518aa08
Reorder imports
michaeljmarshall Mar 17, 2023
e93b903
Upgrade auth0 dependencies; improve claims validation
michaeljmarshall Mar 17, 2023
dcb6008
Remove some versions in child pom.xml
michaeljmarshall Mar 17, 2023
6f76bbd
Use async http client to get JWKS
michaeljmarshall Mar 20, 2023
c4bdd59
Require JWT has OIDC's required claims
michaeljmarshall Mar 20, 2023
83a512a
Remove audit logging; it belongs in framework
michaeljmarshall Mar 20, 2023
08d971a
Cleanup failure metrics
michaeljmarshall Mar 20, 2023
dd96e17
Cleanup pom.xml
michaeljmarshall Mar 20, 2023
1b46eb4
Cleanup Javadocs
michaeljmarshall Mar 20, 2023
6ea2331
Add Auth0 to license.bin.txt
michaeljmarshall Mar 20, 2023
64c8c15
Close the httpClient
michaeljmarshall Mar 28, 2023
fdf6f36
Normalize URL when getting openid-configuration
michaeljmarshall Mar 29, 2023
3d96294
Cover invalid audience claim in integration tests
michaeljmarshall Mar 29, 2023
941a9f5
Improve failure scenario coverage in integration tests
michaeljmarshall Mar 29, 2023
bc4e421
Add support for k8s Api Server
michaeljmarshall Mar 29, 2023
dadbf4c
Support a custom trust store for issuers http client
michaeljmarshall Mar 29, 2023
7340b0e
Improve config name for openIDRequireIssuersUseHttps
michaeljmarshall Mar 29, 2023
932dcd7
Add k8s discover mode to support EKS, AKS, GKE
michaeljmarshall Mar 29, 2023
5eccb86
Make discovery mode more abstract to enable future additions
michaeljmarshall Mar 29, 2023
ecb5566
Remove test dependency on hard coded port
michaeljmarshall Apr 7, 2023
fa93b4c
Refactor to support refreshAfterWrite
michaeljmarshall Apr 7, 2023
a94315b
Remove unused import
michaeljmarshall Apr 7, 2023
f4b18a0
Exclude io.prometheus:simpdleclient_httpserver from k8s client
michaeljmarshall Apr 8, 2023
511b27a
Remove trailing / from issuer per OIDC Discovery Section 4.1
michaeljmarshall Apr 10, 2023
860ceff
Require token has role claim
michaeljmarshall Apr 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Require token has role claim
  • Loading branch information
michaeljmarshall committed Apr 10, 2023
commit 860ceffb4095c92fd02e8d48f03912d497f95e4e
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.util.Config;
import io.netty.handler.ssl.SslContext;
Expand Down Expand Up @@ -115,7 +116,8 @@ public class AuthenticationProviderOpenID implements AuthenticationProvider {

private long acceptedTimeLeewaySeconds;
private FallbackDiscoveryMode fallbackDiscoveryMode;
private String roleClaim;
private String roleClaim = ROLE_CLAIM_DEFAULT;
private boolean isRoleClaimNotSubject;

static final String ALLOWED_TOKEN_ISSUERS = "openIDAllowedTokenIssuers";
static final String ISSUER_TRUST_CERTS_FILE_PATH = "openIDTokenIssuerTrustCertsFilePath";
Expand Down Expand Up @@ -145,6 +147,7 @@ public class AuthenticationProviderOpenID implements AuthenticationProvider {
public void initialize(ServiceConfiguration config) throws IOException {
this.allowedAudiences = validateAllowedAudiences(getConfigValueAsSet(config, ALLOWED_AUDIENCES));
this.roleClaim = getConfigValueAsString(config, ROLE_CLAIM, ROLE_CLAIM_DEFAULT);
this.isRoleClaimNotSubject = !ROLE_CLAIM_DEFAULT.equals(roleClaim);
this.acceptedTimeLeewaySeconds = getConfigValueAsInt(config, ACCEPTED_TIME_LEEWAY_SECONDS,
ACCEPTED_TIME_LEEWAY_SECONDS_DEFAULT);
boolean requireHttps = getConfigValueAsBoolean(config, REQUIRE_HTTPS, REQUIRE_HTTPS_DEFAULT);
Expand Down Expand Up @@ -406,14 +409,19 @@ DecodedJWT verifyJWT(PublicKey publicKey,

// We verify issuer when retrieving the PublicKey, so it is not verified here.
// The claim presence requirements are based on https://openid.net/specs/openid-connect-basic-1_0.html#IDToken
JWTVerifier verifier = JWT.require(alg)
Verification verifierBuilder = JWT.require(alg)
.acceptLeeway(acceptedTimeLeewaySeconds)
.withAnyOfAudience(allowedAudiences)
.withClaimPresence(RegisteredClaims.ISSUED_AT)
.withClaimPresence(RegisteredClaims.EXPIRES_AT)
.withClaimPresence(RegisteredClaims.NOT_BEFORE)
.withClaimPresence(RegisteredClaims.SUBJECT)
.build();
.withClaimPresence(RegisteredClaims.SUBJECT);

if (isRoleClaimNotSubject) {
verifierBuilder = verifierBuilder.withClaimPresence(roleClaim);
}

JWTVerifier verifier = verifierBuilder.build();

try {
return verifier.verify(jwt);
Expand All @@ -432,7 +440,7 @@ DecodedJWT verifyJWT(PublicKey publicKey,
} catch (JWTDecodeException e) {
incrementFailureMetric(AuthenticationExceptionCode.ERROR_DECODING_JWT);
throw new AuthenticationException("Error while decoding JWT: " + e.getMessage());
} catch (JWTVerificationException e) {
} catch (JWTVerificationException | IllegalArgumentException e) {
incrementFailureMetric(AuthenticationExceptionCode.ERROR_VERIFYING_JWT);
throw new AuthenticationException("JWT verification failed: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -437,6 +438,49 @@ public void testAuthenticationStateOpenIDForTokenExpiration() throws Exception {
assertTrue(state.isExpired());
}

@Test
void ensureRoleClaimForNonSubClaimReturnsRole() throws Exception {
AuthenticationProviderOpenID provider = new AuthenticationProviderOpenID();
Properties props = new Properties();
props.setProperty(AuthenticationProviderOpenID.ALLOWED_TOKEN_ISSUERS, issuer);
props.setProperty(AuthenticationProviderOpenID.ALLOWED_AUDIENCES, "allowed-audience");
props.setProperty(AuthenticationProviderOpenID.ROLE_CLAIM, "test");
props.setProperty(AuthenticationProviderOpenID.REQUIRE_HTTPS, "false");
ServiceConfiguration config = new ServiceConfiguration();
config.setProperties(props);
provider.initialize(config);

// Build a JWT with a custom claim
HashMap<String, Object> claims = new HashMap();
claims.put("test", "my-role");
String token = generateToken(validJwk, issuer, "not-my-role", "allowed-audience", 0L,
0L, 10000L, claims);
assertEquals(provider.authenticateAsync(new AuthenticationDataCommand(token)).get(), "my-role");
}

@Test
void ensureRoleClaimForNonSubClaimFailsWhenClaimIsMissing() throws Exception {
AuthenticationProviderOpenID provider = new AuthenticationProviderOpenID();
Properties props = new Properties();
props.setProperty(AuthenticationProviderOpenID.ALLOWED_TOKEN_ISSUERS, issuer);
props.setProperty(AuthenticationProviderOpenID.ALLOWED_AUDIENCES, "allowed-audience");
props.setProperty(AuthenticationProviderOpenID.ROLE_CLAIM, "test");
props.setProperty(AuthenticationProviderOpenID.REQUIRE_HTTPS, "false");
ServiceConfiguration config = new ServiceConfiguration();
config.setProperties(props);
provider.initialize(config);

// Build a JWT without the "test" claim, which should cause the authentication to fail
String token = generateToken(validJwk, issuer, "not-my-role", "allowed-audience", 0L,
0L, 10000L);
try {
provider.authenticateAsync(new AuthenticationDataCommand(token)).get();
fail("Expected exception");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof AuthenticationException, "Found exception: " + e.getCause());
}
}

// This test is somewhat counterintuitive. We allow the state object to change roles, but then we fail it
// in the ServerCnx handling of the state object. As such, it is essential that the state object allow
// the role to change.
Expand All @@ -462,6 +506,11 @@ public void testAuthenticationStateOpenIDAllowsRoleChange() throws Exception {

private String generateToken(String kid, String issuer, String subject, String audience,
Long iatOffset, Long nbfOffset, Long expOffset) {
return generateToken(kid, issuer, subject, audience, iatOffset, nbfOffset, expOffset, new HashMap<>());
}

private String generateToken(String kid, String issuer, String subject, String audience,
Long iatOffset, Long nbfOffset, Long expOffset, HashMap<String, Object> extraClaims) {
long now = System.currentTimeMillis();
DefaultJwtBuilder defaultJwtBuilder = new DefaultJwtBuilder();
defaultJwtBuilder.setHeaderParam("kid", kid);
Expand All @@ -473,6 +522,7 @@ private String generateToken(String kid, String issuer, String subject, String a
defaultJwtBuilder.setIssuedAt(iatOffset != null ? new Date(now + iatOffset) : null);
defaultJwtBuilder.setNotBefore(nbfOffset != null ? new Date(now + nbfOffset) : null);
defaultJwtBuilder.setExpiration(expOffset != null ? new Date(now + expOffset) : null);
defaultJwtBuilder.addClaims(extraClaims);
defaultJwtBuilder.signWith(privateKey);
return defaultJwtBuilder.compact();
}
Expand Down