lianetm commented on code in PR #19622:
URL: https://github.com/apache/kafka/pull/19622#discussion_r2085329910


##########
clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/secured/DefaultJwtValidator.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.kafka.common.security.oauthbearer.internals.secured;
+
+import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken;
+import org.apache.kafka.common.utils.Utils;
+
+import org.jose4j.keys.resolvers.VerificationKeyResolver;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+import static 
org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_CLOCK_SKEW_SECONDS;
+import static 
org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_EXPECTED_AUDIENCE;
+import static 
org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_EXPECTED_ISSUER;
+import static 
org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_SCOPE_CLAIM_NAME;
+import static 
org.apache.kafka.common.config.SaslConfigs.SASL_OAUTHBEARER_SUB_CLAIM_NAME;
+
+/**
+ * This {@link JwtValidator} uses the delegation approach, instantiating and 
delegating calls to a
+ * more concrete implementation. The underlying implementation is determined 
by the presence/absence
+ * of the {@link VerificationKeyResolver}: if it's present, a {@link 
BrokerJwtValidator} is
+ * created, otherwise a {@link ClientJwtValidator} is created.
+ */
+public class DefaultJwtValidator implements JwtValidator {
+
+    private final Map<String, ?> configs;
+    private final String saslMechanism;
+    private final Optional<VerificationKeyResolver> verificationKeyResolver;
+
+    private JwtValidator delegate;
+
+    public DefaultJwtValidator(Map<String, ?> configs, String saslMechanism) {
+        this.configs = configs;
+        this.saslMechanism = saslMechanism;
+        this.verificationKeyResolver = Optional.empty();
+    }
+
+    public DefaultJwtValidator(Map<String, ?> configs,
+                               String saslMechanism,
+                               VerificationKeyResolver 
verificationKeyResolver) {
+        this.configs = configs;
+        this.saslMechanism = saslMechanism;
+        this.verificationKeyResolver = Optional.of(verificationKeyResolver);
+    }
+
+    @Override
+    public void init() throws IOException {
+        ConfigurationUtils cu = new ConfigurationUtils(configs, saslMechanism);
+
+        if (verificationKeyResolver.isPresent()) {
+            List<String> expectedAudiencesList = 
cu.get(SASL_OAUTHBEARER_EXPECTED_AUDIENCE);
+            Set<String> expectedAudiences = expectedAudiencesList != null ? 
Set.copyOf(expectedAudiencesList) : null;
+            Integer clockSkew = 
cu.validateInteger(SASL_OAUTHBEARER_CLOCK_SKEW_SECONDS, false);
+            String expectedIssuer = 
cu.validateString(SASL_OAUTHBEARER_EXPECTED_ISSUER, false);
+            String scopeClaimName = 
cu.validateString(SASL_OAUTHBEARER_SCOPE_CLAIM_NAME);
+            String subClaimName = 
cu.validateString(SASL_OAUTHBEARER_SUB_CLAIM_NAME);
+
+            delegate = new BrokerJwtValidator(clockSkew,
+                expectedAudiences,
+                expectedIssuer,
+                verificationKeyResolver.get(),
+                scopeClaimName,
+                subClaimName);
+        } else {
+            String scopeClaimName = cu.get(SASL_OAUTHBEARER_SCOPE_CLAIM_NAME);
+            String subClaimName = cu.get(SASL_OAUTHBEARER_SUB_CLAIM_NAME);
+            delegate = new ClientJwtValidator(scopeClaimName, subClaimName);
+        }
+
+        delegate.init();
+    }
+
+    @Override
+    public OAuthBearerToken validate(String accessToken) throws 
ValidateException {
+        return Objects.requireNonNull(delegate).validate(accessToken);

Review Comment:
   ditto



##########
clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/secured/DefaultJwtRetriever.java:
##########
@@ -86,6 +90,18 @@ public static AccessTokenRetriever create(Map<String, ?> 
configs,
                 cu.validateInteger(SASL_LOGIN_READ_TIMEOUT_MS, false),
                 urlencodeHeader);
         }
+
+        delegate.init();
+    }
+
+    @Override
+    public String retrieve() throws IOException {
+        return Objects.requireNonNull(delegate).retrieve();

Review Comment:
   the NPE on "delegate" here would be really obfuscated, should we add a 
message to clarify? it really means that the api user should call init() before 
calling retrieve()



##########
clients/src/test/java/org/apache/kafka/common/security/oauthbearer/OAuthBearerValidatorCallbackHandlerTest.java:
##########
@@ -81,9 +86,65 @@ public void testInvalidAccessToken() throws Exception {
         assertInvalidAccessTokenFails(createAccessKey("{}", "{}", "{}"), 
substring);
     }
 
+    @Test
+    public void testHandlerInitThrowsException() throws IOException {
+        AccessTokenBuilder builder = new AccessTokenBuilder()
+            .alg(AlgorithmIdentifiers.RSA_USING_SHA256);
+        CloseableVerificationKeyResolver verificationKeyResolver = 
createVerificationKeyResolver(builder);
+        JwtValidator jwtValidator = new JwtValidator() {
+            @Override
+            public void init() throws IOException {
+                throw new IOException("My init had an error!");
+            }
+
+            @Override
+            public OAuthBearerToken validate(String accessToken) throws 
ValidateException {
+                return null;
+            }
+        };
+
+        OAuthBearerValidatorCallbackHandler handler = new 
OAuthBearerValidatorCallbackHandler();
+
+        // An error initializing the JwtValidator should cause 
OAuthBearerValidatorCallbackHandler.init() to fail.
+        assertThrowsWithMessage(

Review Comment:
   would it be more robust to simply check that the getCause is the same 
exception we threw on ln 97? (the substring we're checking is really the same 
for several errors on init, not only the validator)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to