bbende commented on a change in pull request #5277:
URL: https://github.com/apache/nifi/pull/5277#discussion_r691263263



##########
File path: 
nifi-commons/nifi-security-kerberos/src/test/java/org/apache/nifi/security/krb/TestKerberosTicketCacheUser.java
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.security.krb;
+
+import org.junit.Test;
+
+import javax.security.auth.login.AppConfigurationEntry;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TestKerberosTicketCacheUser {
+
+    @Test
+    public void testGetConfigurationEntry() {
+        final String principal = "f...@nifi.com";
+
+        final KerberosUser kerberosUser = new 
KerberosTicketCacheUser(principal);
+        assertEquals(principal, kerberosUser.getPrincipal());
+
+        final AppConfigurationEntry entry = 
kerberosUser.getConfigurationEntry();
+        assertNotNull(entry);
+        assertEquals(ConfigurationUtil.SUN_KRB5_LOGIN_MODULE, 
entry.getLoginModuleName());
+        assertEquals(AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, 
entry.getControlFlag());
+        assertEquals(principal, entry.getOptions().get("principal"));
+        assertEquals("true", entry.getOptions().get("useTicketCache"));
+        assertNull(entry.getOptions().get("ticketCache"));
+    }
+
+    @Test
+    public void testGetConfigurationEntryWithSpecificTicketCache() {
+        final String principal = "f...@nifi.com";
+        final String ticketCache = "/tmp/cache";

Review comment:
       It is just a placeholder, the test is just confirming that the produced 
Configuration has the correct values that were provided, but nothing is being 
done with it.

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

##########
File path: 
nifi-nar-bundles/nifi-extension-utils/nifi-hadoop-utils/src/main/java/org/apache/nifi/processors/hadoop/AbstractHadoopProcessor.java
##########
@@ -450,6 +476,36 @@ HdfsResources resetHDFSResources(final List<String> 
resourceLocations, ProcessCo
         return new HdfsResources(config, fs, ugi, kerberosUser);
     }
 
+    private KerberosUser getKerberosUser(final ProcessContext context) throws 
IOException {
+        // Check Kerberos User Service first, if present then get the 
KerberosUser from the service
+        // The customValidate method ensures that KerberosUserService can't be 
set at the same time as the credentials service or explicit properties
+        final KerberosUserService kerberosUserService = 
context.getProperty(KERBEROS_USER_SERVICE).asControllerService(KerberosUserService.class);
+        if (kerberosUserService != null) {
+            return kerberosUserService.createKerberosUser();
+        }
+
+        // Kerberos User Service wasn't set, so create KerberosUser based on 
credentials service or explicit properties...
+        String principal = 
context.getProperty(kerberosProperties.getKerberosPrincipal()).evaluateAttributeExpressions().getValue();
+        String keyTab = 
context.getProperty(kerberosProperties.getKerberosKeytab()).evaluateAttributeExpressions().getValue();
+        String password = 
context.getProperty(kerberosProperties.getKerberosPassword()).getValue();
+
+        // If the Kerberos Credentials Service is specified, we need to use 
its configuration, not the explicit properties for principal/keytab.
+        // The customValidate method ensures that only one can be set, so we 
know that the principal & keytab above are null.
+        final KerberosCredentialsService credentialsService = 
context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
+        if (credentialsService != null) {
+            principal = credentialsService.getPrincipal();
+            keyTab = credentialsService.getKeytab();
+        }
+
+        if (keyTab != null) {
+            return new KerberosKeytabUser(principal, keyTab);
+        } else if (password != null) {
+            return new KerberosPasswordUser(principal, password);
+        } else {
+            throw new IOException("Unable to authenticate with Kerberos, no 
keytab or password was provided");

Review comment:
       At first I was hesitant to change this due to being unsure about 
existing exception handling, but it seems like the code path originates from 
the onScheduled method, so I think it is fair to change it. I'm fine with 
IllegalArgumentException, or would IllegalStateException make sense since it is 
really an unexpected situation to end up in this state? There should really be 
no way for this exception to happen with all of the validation that exists.




-- 
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: issues-unsubscr...@nifi.apache.org

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


Reply via email to