Add JWT authenticator support for validating ID Tokens (#13242)

Expands the OIDC based auth in Druid by adding a JWT Authenticator that validates ID Tokens associated with a request. The existing pac4j authenticator works for authenticating web users while accessing the console, whereas this authenticator is for validating Druid API requests made by Direct clients. Services already supporting OIDC can attach their ID tokens to the Druid requests
under the Authorization request header.
This commit is contained in:
Atul Mohan 2023-03-25 06:11:40 -07:00 committed by GitHub
parent 598eaad7e1
commit 19db32d6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 384 additions and 3 deletions

View File

@ -25,14 +25,23 @@ title: "Druid pac4j based Security extension"
Apache Druid Extension to enable [OpenID Connect](https://openid.net/connect/) based Authentication for Druid Processes using [pac4j](https://github.com/pac4j/pac4j) as the underlying client library. Apache Druid Extension to enable [OpenID Connect](https://openid.net/connect/) based Authentication for Druid Processes using [pac4j](https://github.com/pac4j/pac4j) as the underlying client library.
This can be used with any authentication server that supports same e.g. [Okta](https://developer.okta.com/). This can be used with any authentication server that supports same e.g. [Okta](https://developer.okta.com/).
This extension should only be used at the router node to enable a group of users in existing authentication server to interact with Druid cluster, using the [web console](../../operations/web-console.md). This extension does not support JDBC client authentication. The pac4j authenticator should only be used at the router node to enable a group of users in existing authentication server to interact with Druid cluster, using the [web console](../../operations/web-console.md).
This extension also provides a JWT authenticator that validates [ID Tokens](https://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken) associated with a request. ID Tokens are attached to the request under the `Authorization` header with the bearer token prefix - `Bearer `. This authenticator is intended for services to talk to Druid by initially authenticating with an OIDC server to retrieve the ID Token which is then attached to every Druid request.
This extension does not support JDBC client authentication.
## Configuration ## Configuration
### Creating an Authenticator ### Creating an Authenticator
``` ```
#Create a pac4j web user authenticator
druid.auth.authenticatorChain=["pac4j"] druid.auth.authenticatorChain=["pac4j"]
druid.auth.authenticator.pac4j.type=pac4j druid.auth.authenticator.pac4j.type=pac4j
#Create a JWT token authenticator
druid.auth.authenticatorChain=["jwt"]
druid.auth.authenticator.jwt.type=jwt
``` ```
### Properties ### Properties
@ -44,3 +53,4 @@ druid.auth.authenticator.pac4j.type=pac4j
|`druid.auth.pac4j.oidc.clientID`|OAuth Client Application id.|none|Yes| |`druid.auth.pac4j.oidc.clientID`|OAuth Client Application id.|none|Yes|
|`druid.auth.pac4j.oidc.clientSecret`|OAuth Client Application secret. It can be provided as plaintext string or The [Password Provider](../../operations/password-provider.md).|none|Yes| |`druid.auth.pac4j.oidc.clientSecret`|OAuth Client Application secret. It can be provided as plaintext string or The [Password Provider](../../operations/password-provider.md).|none|Yes|
|`druid.auth.pac4j.oidc.discoveryURI`|discovery URI for fetching OP metadata [see this](http://openid.net/specs/openid-connect-discovery-1_0.html).|none|Yes| |`druid.auth.pac4j.oidc.discoveryURI`|discovery URI for fetching OP metadata [see this](http://openid.net/specs/openid-connect-discovery-1_0.html).|none|Yes|
|`druid.auth.pac4j.oidc.oidcClaim`|[claim](https://openid.net/specs/openid-connect-core-1_0.html#Claims) that will be extracted from the ID Token after validation.|name|No|

View File

@ -0,0 +1,129 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.druid.security.pac4j;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.proc.BadJOSEException;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.openid.connect.sdk.claims.IDTokenClaimsSet;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.server.security.AuthConfig;
import org.apache.druid.server.security.AuthenticationResult;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.oidc.profile.creator.TokenValidator;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Optional;
public class JwtAuthFilter implements Filter
{
private static final Logger LOG = new Logger(JwtAuthFilter.class);
private final String authorizerName;
private final String name;
private final OIDCConfig oidcConfig;
private final TokenValidator tokenValidator;
public JwtAuthFilter(String authorizerName, String name, OIDCConfig oidcConfig, TokenValidator tokenValidator)
{
this.authorizerName = authorizerName;
this.name = name;
this.oidcConfig = oidcConfig;
this.tokenValidator = tokenValidator;
}
@Override
public void init(FilterConfig filterConfig)
{
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException
{
// Skip this filter if the request has already been authenticated
if (servletRequest.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT) != null) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
Optional<String> idToken = extractBearerToken(httpServletRequest);
if (idToken.isPresent()) {
try {
// Parses the JWT and performs the ID Token validation specified in the OpenID spec: https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
IDTokenClaimsSet claims = tokenValidator.validate(JWTParser.parse(idToken.get()), null);
if (claims != null) {
Optional<String> claim = Optional.of(claims.getStringClaim(oidcConfig.getOidcClaim()));
if (claim.isPresent()) {
LOG.debug("Authentication successful for " + oidcConfig.getClientID());
AuthenticationResult authenticationResult = new AuthenticationResult(
claim.get(),
authorizerName,
name,
null
);
servletRequest.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, authenticationResult);
} else {
LOG.error(
"Authentication failed! Please ensure that the ID token is valid and it contains the configured claim.");
httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
}
}
catch (BadJOSEException | JOSEException | ParseException e) {
LOG.error(e, "Failed to parse JWT token");
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy()
{
}
private static Optional<String> extractBearerToken(HttpServletRequest request)
{
String header = request.getHeader(HttpConstants.AUTHORIZATION_HEADER);
if (header == null || !header.startsWith(HttpConstants.BEARER_HEADER_PREFIX)) {
LOG.debug("Request does not contain bearer authentication scheme");
return Optional.empty();
}
String headerWithoutPrefix = header.substring(HttpConstants.BEARER_HEADER_PREFIX.length());
return Optional.of(headerWithoutPrefix);
}
}

View File

@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.druid.security.pac4j;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.apache.druid.server.security.AuthenticationResult;
import org.apache.druid.server.security.Authenticator;
import org.pac4j.oidc.config.OidcConfiguration;
import org.pac4j.oidc.profile.creator.TokenValidator;
import javax.annotation.Nullable;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import java.util.EnumSet;
import java.util.Map;
@JsonTypeName("jwt")
public class JwtAuthenticator implements Authenticator
{
private final String authorizerName;
private final OIDCConfig oidcConfig;
private final Supplier<TokenValidator> tokenValidatorSupplier;
private final String name;
@JsonCreator
public JwtAuthenticator(
@JsonProperty("name") String name,
@JsonProperty("authorizerName") String authorizerName,
@JacksonInject OIDCConfig oidcConfig
)
{
this.name = name;
this.oidcConfig = oidcConfig;
this.authorizerName = authorizerName;
this.tokenValidatorSupplier = Suppliers.memoize(() -> createTokenValidator(oidcConfig));
}
@Override
public Filter getFilter()
{
return new JwtAuthFilter(authorizerName, name, oidcConfig, tokenValidatorSupplier.get());
}
@Override
public Class<? extends Filter> getFilterClass()
{
return JwtAuthFilter.class;
}
@Override
public Map<String, String> getInitParameters()
{
return null;
}
@Override
public String getPath()
{
return "/*";
}
@Nullable
@Override
public EnumSet<DispatcherType> getDispatcherType()
{
return null;
}
@Nullable
@Override
public String getAuthChallengeHeader()
{
return null;
}
@Nullable
@Override
public AuthenticationResult authenticateJDBCContext(Map<String, Object> context)
{
return null;
}
private TokenValidator createTokenValidator(OIDCConfig config)
{
OidcConfiguration oidcConfiguration = new OidcConfiguration();
oidcConfiguration.setClientId(config.getClientID());
oidcConfiguration.setSecret(config.getClientSecret().getPassword());
oidcConfiguration.setDiscoveryURI(config.getDiscoveryURI());
return new TokenValidator(oidcConfiguration);
}
}

View File

@ -26,6 +26,7 @@ import org.apache.druid.metadata.PasswordProvider;
public class OIDCConfig public class OIDCConfig
{ {
private final String DEFAULT_SCOPE = "name";
@JsonProperty @JsonProperty
private final String clientID; private final String clientID;
@ -35,16 +36,21 @@ public class OIDCConfig
@JsonProperty @JsonProperty
private final String discoveryURI; private final String discoveryURI;
@JsonProperty
private final String oidcClaim;
@JsonCreator @JsonCreator
public OIDCConfig( public OIDCConfig(
@JsonProperty("clientID") String clientID, @JsonProperty("clientID") String clientID,
@JsonProperty("clientSecret") PasswordProvider clientSecret, @JsonProperty("clientSecret") PasswordProvider clientSecret,
@JsonProperty("discoveryURI") String discoveryURI @JsonProperty("discoveryURI") String discoveryURI,
@JsonProperty("oidcClaim") String oidcClaim
) )
{ {
this.clientID = Preconditions.checkNotNull(clientID, "null clientID"); this.clientID = Preconditions.checkNotNull(clientID, "null clientID");
this.clientSecret = Preconditions.checkNotNull(clientSecret, "null clientSecret"); this.clientSecret = Preconditions.checkNotNull(clientSecret, "null clientSecret");
this.discoveryURI = Preconditions.checkNotNull(discoveryURI, "null discoveryURI"); this.discoveryURI = Preconditions.checkNotNull(discoveryURI, "null discoveryURI");
this.oidcClaim = oidcClaim == null ? DEFAULT_SCOPE : oidcClaim;
} }
@JsonProperty @JsonProperty
@ -64,4 +70,10 @@ public class OIDCConfig
{ {
return discoveryURI; return discoveryURI;
} }
@JsonProperty
public String getOidcClaim()
{
return oidcClaim;
}
} }

View File

@ -36,7 +36,8 @@ public class Pac4jDruidModule implements DruidModule
{ {
return ImmutableList.of( return ImmutableList.of(
new SimpleModule("Pac4jDruidSecurity").registerSubtypes( new SimpleModule("Pac4jDruidSecurity").registerSubtypes(
Pac4jAuthenticator.class Pac4jAuthenticator.class,
JwtAuthenticator.class
) )
); );
} }

View File

@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.druid.security.pac4j;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.server.security.AuthConfig;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.pac4j.oidc.profile.creator.TokenValidator;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class JwtAuthenticatorTest
{
@Test
public void testBearerToken()
throws IOException, ServletException
{
OIDCConfig configuration = EasyMock.createMock(OIDCConfig.class);
TokenValidator tokenValidator = EasyMock.createMock(TokenValidator.class);
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn(null);
EasyMock.expect(req.getHeader("Authorization")).andReturn("Nobearer");
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
JwtAuthenticator jwtAuthenticator = new JwtAuthenticator("jwt", "allowAll", configuration);
JwtAuthFilter authFilter = new JwtAuthFilter("allowAll", "jwt", configuration, tokenValidator);
authFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, filterChain);
Assert.assertEquals(jwtAuthenticator.getFilterClass(), JwtAuthFilter.class);
Assert.assertNull(jwtAuthenticator.getInitParameters());
Assert.assertNull(jwtAuthenticator.authenticateJDBCContext(ImmutableMap.of()));
}
@Test
public void testAuthenticatedRequest() throws ServletException, IOException
{
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn("AlreadyAuthenticated");
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
JwtAuthFilter authFilter = new JwtAuthFilter("allowAll", "jwt", null, null);
authFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, filterChain);
}
}

View File

@ -36,6 +36,28 @@ public class OIDCConfigTest
+ " \"discoveryURI\": \"testdiscoveryuri\"\n" + " \"discoveryURI\": \"testdiscoveryuri\"\n"
+ "}\n"; + "}\n";
OIDCConfig conf = jsonMapper.readValue(
jsonMapper.writeValueAsString(jsonMapper.readValue(jsonStr, OIDCConfig.class)),
OIDCConfig.class
);
Assert.assertEquals("testid", conf.getClientID());
Assert.assertEquals("testsecret", conf.getClientSecret().getPassword());
Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI());
Assert.assertEquals("name", conf.getOidcClaim());
}
@Test
public void testSerdeWithoutDefaults() throws Exception
{
ObjectMapper jsonMapper = new ObjectMapper();
String jsonStr = "{\n"
+ " \"clientID\": \"testid\",\n"
+ " \"clientSecret\": \"testsecret\",\n"
+ " \"discoveryURI\": \"testdiscoveryuri\",\n"
+ " \"oidcClaim\": \"email\"\n"
+ "}\n";
OIDCConfig conf = jsonMapper.readValue( OIDCConfig conf = jsonMapper.readValue(
jsonMapper.writeValueAsString(jsonMapper.readValue(jsonStr, OIDCConfig.class)), jsonMapper.writeValueAsString(jsonMapper.readValue(jsonStr, OIDCConfig.class)),
OIDCConfig.class OIDCConfig.class
@ -44,5 +66,6 @@ public class OIDCConfigTest
Assert.assertEquals("testid", conf.getClientID()); Assert.assertEquals("testid", conf.getClientID());
Assert.assertEquals("testsecret", conf.getClientSecret().getPassword()); Assert.assertEquals("testsecret", conf.getClientSecret().getPassword());
Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI()); Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI());
Assert.assertEquals("email", conf.getOidcClaim());
} }
} }

View File

@ -139,6 +139,7 @@ JSONPath
JSSE JSSE
JVM JVM
JVMs JVMs
JWT
Joda Joda
JsonProperty JsonProperty
Jupyter Jupyter
@ -170,6 +171,7 @@ Murmur3
MVCC MVCC
NFS NFS
OCF OCF
OIDC
OLAP OLAP
OOMs OOMs
OpenJDK OpenJDK