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


##########
clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/secured/VerificationKeyResolverFactory.java:
##########
@@ -37,7 +37,7 @@
 public class VerificationKeyResolverFactory {
 
     /**
-     * Create an {@link AccessTokenRetriever} from the given
+     * Create an {@link JwtRetriever} from the given

Review Comment:
   ```suggestion
        * Create a {@link JwtRetriever} from the given
   ```



##########
clients/src/test/java/org/apache/kafka/common/security/oauthbearer/OAuthBearerValidatorCallbackHandlerTest.java:
##########
@@ -81,6 +84,52 @@ public void testInvalidAccessToken() throws Exception {
         assertInvalidAccessTokenFails(createAccessKey("{}", "{}", "{}"), 
substring);
     }
 
+    @Test
+    public void testConfigureThrowsExceptionOnJwtValidatorInit() throws 
IOException {
+        OAuthBearerLoginCallbackHandler handler = new 
OAuthBearerLoginCallbackHandler();
+        JwtRetriever jwtRetriever = new JwtRetriever() {
+            @Override
+            public void init() throws IOException {
+                throw new IOException("My init had an error!");
+            }
+
+            @Override
+            public String retrieve() {
+                return "dummy";
+            }
+        };
+
+        Map<String, ?> configs = getSaslConfigs();
+
+        try (JwtValidator jwtValidator = new DefaultJwtValidator(configs, 
OAuthBearerLoginModule.OAUTHBEARER_MECHANISM)) {
+            assertThrowsWithMessage(
+                KafkaException.class, () -> handler.init(jwtRetriever, 
jwtValidator), "encountered an error when initializing");
+        }
+    }
+
+    @Test
+    public void testConfigureThrowsExceptionOnJwtValidatorClose() throws 
IOException {
+        OAuthBearerLoginCallbackHandler handler = new 
OAuthBearerLoginCallbackHandler();
+        JwtRetriever jwtRetriever = new JwtRetriever() {
+            @Override
+            public void close() throws IOException {
+                throw new IOException("My close had an error!");
+            }
+            @Override
+            public String retrieve() {
+                return "dummy";
+            }
+        };
+
+        Map<String, ?> configs = getSaslConfigs();
+        try (JwtValidator jwtValidator = new DefaultJwtValidator(configs, 
OAuthBearerLoginModule.OAUTHBEARER_MECHANISM)) {
+            handler.init(jwtRetriever, jwtValidator);
+
+            // Basically asserting this doesn't throw an exception :(
+            handler.close();

Review Comment:
   How about use the `assertDoesNotThrow`
   ```suggestion
               assertDoesNotThrow(handler::close);
   ```



##########
clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/secured/DefaultJwtValidator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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()) {
+            Set<String> expectedAudiences = null;
+            List<String> l = cu.get(SASL_OAUTHBEARER_EXPECTED_AUDIENCE);
+
+            if (l != null)
+                expectedAudiences = Set.copyOf(l);

Review Comment:
   Perhaps we could update the variable name `l` to something more descriptive 
for better readability.



-- 
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