You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by ab...@apache.org on 2023/03/25 13:11:46 UTC

[druid] branch master updated: Add JWT authenticator support for validating ID Tokens (#13242)

This is an automated email from the ASF dual-hosted git repository.

abhishek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 19db32d6b4 Add JWT authenticator support for validating ID Tokens (#13242)
19db32d6b4 is described below

commit 19db32d6b4ba6986075acc5566f3ce9c97561211
Author: Atul Mohan <at...@gmail.com>
AuthorDate: Sat Mar 25 06:11:40 2023 -0700

    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.
---
 docs/development/extensions-core/druid-pac4j.md    |  12 +-
 .../apache/druid/security/pac4j/JwtAuthFilter.java | 129 +++++++++++++++++++++
 .../druid/security/pac4j/JwtAuthenticator.java     | 114 ++++++++++++++++++
 .../apache/druid/security/pac4j/OIDCConfig.java    |  14 ++-
 .../druid/security/pac4j/Pac4jDruidModule.java     |   3 +-
 .../druid/security/pac4j/JwtAuthenticatorTest.java |  90 ++++++++++++++
 .../druid/security/pac4j/OIDCConfigTest.java       |  23 ++++
 website/.spelling                                  |   2 +
 8 files changed, 384 insertions(+), 3 deletions(-)

diff --git a/docs/development/extensions-core/druid-pac4j.md b/docs/development/extensions-core/druid-pac4j.md
index 4ee7f38e2e..54833f7a64 100644
--- a/docs/development/extensions-core/druid-pac4j.md
+++ b/docs/development/extensions-core/druid-pac4j.md
@@ -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.
 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
 
 ### Creating an Authenticator
 ```
+#Create a pac4j web user authenticator
 druid.auth.authenticatorChain=["pac4j"]
 druid.auth.authenticator.pac4j.type=pac4j
+
+#Create a JWT token authenticator
+druid.auth.authenticatorChain=["jwt"]
+druid.auth.authenticator.jwt.type=jwt
 ```
 
 ### 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.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.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|
diff --git a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthFilter.java b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthFilter.java
new file mode 100644
index 0000000000..826e2a479e
--- /dev/null
+++ b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthFilter.java
@@ -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);
+  }
+}
diff --git a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthenticator.java b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthenticator.java
new file mode 100644
index 0000000000..96c82a491a
--- /dev/null
+++ b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/JwtAuthenticator.java
@@ -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);
+  }
+}
diff --git a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/OIDCConfig.java b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/OIDCConfig.java
index 1ddba0b7eb..0bc30fd910 100644
--- a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/OIDCConfig.java
+++ b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/OIDCConfig.java
@@ -26,6 +26,7 @@ import org.apache.druid.metadata.PasswordProvider;
 
 public class OIDCConfig
 {
+  private final String DEFAULT_SCOPE = "name";
   @JsonProperty
   private final String clientID;
 
@@ -35,16 +36,21 @@ public class OIDCConfig
   @JsonProperty
   private final String discoveryURI;
 
+  @JsonProperty
+  private final String oidcClaim;
+
   @JsonCreator
   public OIDCConfig(
       @JsonProperty("clientID") String clientID,
       @JsonProperty("clientSecret") PasswordProvider clientSecret,
-      @JsonProperty("discoveryURI") String discoveryURI
+      @JsonProperty("discoveryURI") String discoveryURI,
+      @JsonProperty("oidcClaim") String oidcClaim
   )
   {
     this.clientID = Preconditions.checkNotNull(clientID, "null clientID");
     this.clientSecret = Preconditions.checkNotNull(clientSecret, "null clientSecret");
     this.discoveryURI = Preconditions.checkNotNull(discoveryURI, "null discoveryURI");
+    this.oidcClaim = oidcClaim == null ? DEFAULT_SCOPE : oidcClaim;
   }
 
   @JsonProperty
@@ -64,4 +70,10 @@ public class OIDCConfig
   {
     return discoveryURI;
   }
+
+  @JsonProperty
+  public String getOidcClaim()
+  {
+    return oidcClaim;
+  }
 }
diff --git a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jDruidModule.java b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jDruidModule.java
index 5ca0b93fdc..f7113ab67a 100644
--- a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jDruidModule.java
+++ b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jDruidModule.java
@@ -36,7 +36,8 @@ public class Pac4jDruidModule implements DruidModule
   {
     return ImmutableList.of(
         new SimpleModule("Pac4jDruidSecurity").registerSubtypes(
-            Pac4jAuthenticator.class
+            Pac4jAuthenticator.class,
+            JwtAuthenticator.class
         )
     );
   }
diff --git a/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/JwtAuthenticatorTest.java b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/JwtAuthenticatorTest.java
new file mode 100644
index 0000000000..fee9913909
--- /dev/null
+++ b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/JwtAuthenticatorTest.java
@@ -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);
+  }
+}
diff --git a/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/OIDCConfigTest.java b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/OIDCConfigTest.java
index 409eb6c116..b5d4119c29 100644
--- a/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/OIDCConfigTest.java
+++ b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/OIDCConfigTest.java
@@ -36,6 +36,28 @@ public class OIDCConfigTest
                      + "  \"discoveryURI\": \"testdiscoveryuri\"\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(
         jsonMapper.writeValueAsString(jsonMapper.readValue(jsonStr, OIDCConfig.class)),
         OIDCConfig.class
@@ -44,5 +66,6 @@ public class OIDCConfigTest
     Assert.assertEquals("testid", conf.getClientID());
     Assert.assertEquals("testsecret", conf.getClientSecret().getPassword());
     Assert.assertEquals("testdiscoveryuri", conf.getDiscoveryURI());
+    Assert.assertEquals("email", conf.getOidcClaim());
   }
 }
diff --git a/website/.spelling b/website/.spelling
index b0321652fe..419f0fd26e 100644
--- a/website/.spelling
+++ b/website/.spelling
@@ -139,6 +139,7 @@ JSONPath
 JSSE
 JVM
 JVMs
+JWT
 Joda
 JsonProperty
 Jupyter
@@ -170,6 +171,7 @@ Murmur3
 MVCC
 NFS
 OCF
+OIDC
 OLAP
 OOMs
 OpenJDK


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org