bbende commented on a change in pull request #5277: URL: https://github.com/apache/nifi/pull/5277#discussion_r691319299
########## File path: nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/CustomKerberosLogin.java ########## @@ -0,0 +1,245 @@ +/* + * 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.nifi.processors.kafka.pubsub; + +import org.apache.kafka.common.config.SaslConfigs; +import org.apache.kafka.common.security.JaasContext; +import org.apache.kafka.common.security.JaasUtils; +import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler; +import org.apache.kafka.common.security.authenticator.AbstractLogin; +import org.apache.kafka.common.security.kerberos.KerberosLogin; +import org.apache.kafka.common.utils.KafkaThread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.security.auth.RefreshFailedException; +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosPrincipal; +import javax.security.auth.kerberos.KerberosTicket; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Customized version of {@link org.apache.kafka.common.security.kerberos.KerberosLogin} which improves the re-login logic + * to avoid making system calls to kinit when the ticket cache is being used, and to avoid exiting the refresh thread so that + * it may recover if the ticket cache is externally refreshed. + * + * The re-login thread follows a similar approach used by NiFi's KerberosUser which attempts to call tgt.refresh() + * and falls back to a logout/login. + * + * The Kafka client is configured to use this login by setting SaslConfigs.SASL_LOGIN_CLASS in {@link KafkaProcessorUtils} + * when the SASL mechanism is GSSAPI. + */ +public class CustomKerberosLogin extends AbstractLogin { + private static final Logger log = LoggerFactory.getLogger(CustomKerberosLogin.class); + + private Thread t; + private boolean isKrbTicket; + + private String principal; + + private double ticketRenewWindowFactor; + private long minTimeBeforeRelogin; + + private volatile Subject subject; + + private LoginContext loginContext; + private String serviceName; + + @Override + public void configure(Map<String, ?> configs, String contextName, Configuration configuration, + AuthenticateCallbackHandler callbackHandler) { + super.configure(configs, contextName, configuration, callbackHandler); + this.ticketRenewWindowFactor = (Double) configs.get(SaslConfigs.SASL_KERBEROS_TICKET_RENEW_WINDOW_FACTOR); + this.minTimeBeforeRelogin = (Long) configs.get(SaslConfigs.SASL_KERBEROS_MIN_TIME_BEFORE_RELOGIN); + this.serviceName = getServiceName(configs, contextName, configuration); + } + + /** + * Performs login for each login module specified for the login context of this instance and starts the thread used + * to periodically re-login to the Kerberos Ticket Granting Server. + */ + @Override + public LoginContext login() throws LoginException { + loginContext = super.login(); + subject = loginContext.getSubject(); + isKrbTicket = !subject.getPrivateCredentials(KerberosTicket.class).isEmpty(); + + AppConfigurationEntry[] entries = configuration().getAppConfigurationEntry(contextName()); + if (entries.length == 0) { + principal = null; + } else { + // there will only be a single entry + AppConfigurationEntry entry = entries[0]; + if (entry.getOptions().get("principal") != null) + principal = (String) entry.getOptions().get("principal"); + else + principal = null; + } + + if (!isKrbTicket) { + log.debug("[Principal={}]: It is not a Kerberos ticket", principal); + t = null; + // if no TGT, do not bother with ticket management. + return loginContext; + } + log.debug("[Principal={}]: It is a Kerberos ticket", principal); + + t = KafkaThread.daemon(String.format("kafka-kerberos-refresh-thread-%s", principal), () -> { Review comment: This class is a copy/paste with modifications from the code provided by Kafka: https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/security/kerberos/KerberosLogin.java Since we've actually been using the Kafka version this whole time, I think we are safe with login only being called once, although I agree it could protect against overwriting the thread. I thought about making bigger refactorings like the Runnable or ExecutorService, but I hesitated because I thought there might be some value in keeping a similar structure/approach to the original Kafka code for the case that we may want to compare future changes made to their version to see how it may apply to ours. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
