kirktrue commented on a change in pull request #11284: URL: https://github.com/apache/kafka/pull/11284#discussion_r733850703
########## File path: clients/src/main/java/org/apache/kafka/common/security/oauthbearer/secured/ConfigurationUtils.java ########## @@ -0,0 +1,277 @@ +/* + * 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.secured; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.util.Locale; +import java.util.Map; +import javax.net.ssl.SSLSocketFactory; +import org.apache.kafka.common.config.AbstractConfig; +import org.apache.kafka.common.config.ConfigDef; +import org.apache.kafka.common.config.ConfigException; +import org.apache.kafka.common.network.ListenerName; +import org.apache.kafka.common.network.Mode; +import org.apache.kafka.common.security.ssl.DefaultSslEngineFactory; +import org.apache.kafka.common.security.ssl.SslFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * <code>ConfigurationUtils</code> is a utility class to perform basic configuration-related + * logic and is separated out here for easier, more direct testing. + */ + +public class ConfigurationUtils { + + private static final Logger log = LoggerFactory.getLogger(ConfigurationUtils.class); + + private final Map<String, ?> configs; + + private final String prefix; + + public ConfigurationUtils(Map<String, ?> configs) { + this(configs, null); + } + + public ConfigurationUtils(Map<String, ?> configs, String saslMechanism) { + this.configs = configs; + + if (saslMechanism != null && !saslMechanism.trim().isEmpty()) + this.prefix = ListenerName.saslMechanismPrefix(saslMechanism.trim()); + else + this.prefix = null; + } + + public Map<String, ?> getSslClientConfig(String uriConfigName) { + String urlConfigValue = get(uriConfigName); + + if (urlConfigValue == null || urlConfigValue.trim().isEmpty()) + throw new ConfigException(String.format("The OAuth configuration option %s is required", uriConfigName)); + + URL url; + + try { + url = new URL(urlConfigValue); + } catch (IOException e) { + throw new ConfigException(String.format("The OAuth configuration option %s was not a valid URL (%s)", uriConfigName, urlConfigValue)); + } + + if (!url.getProtocol().equalsIgnoreCase("https")) { + log.warn("Not creating SSL socket factory as URL for {} ({}) is not SSL-/TLS-based", uriConfigName, url); + return null; + } + + ConfigDef sslConfigDef = new ConfigDef(); + sslConfigDef.withClientSslSupport(); + AbstractConfig sslClientConfig = new AbstractConfig(sslConfigDef, configs); + return sslClientConfig.values(); + } + + public SSLSocketFactory createSSLSocketFactory(String uriConfigName) { + Map<String, ?> sslClientConfig = getSslClientConfig(uriConfigName); + + if (sslClientConfig == null) { + log.warn("Requesting SSL client socket factory but SSL configs were null"); Review comment: I refactored the code a bit, so this has been removed. -- 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