This is an automated email from the ASF dual-hosted git repository. wuzhiguo pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/ambari.git
The following commit(s) were added to refs/heads/trunk by this push: new 363cc260c0 AMBARI-25719: Fail to enable kerberos due to NullPointerException and HostNotFoundException (#3589) 363cc260c0 is described below commit 363cc260c02144dbb77277bc4e47fc591934c507 Author: Yubi Lee <eubn...@gmail.com> AuthorDate: Thu Dec 8 00:50:52 2022 +0900 AMBARI-25719: Fail to enable kerberos due to NullPointerException and HostNotFoundException (#3589) --- .../events/publishers/AgentCommandsPublisher.java | 12 +++- .../server/orm/dao/KerberosKeytabPrincipalDAO.java | 8 ++- .../stageutils/KerberosKeytabController.java | 10 ++- .../apache/ambari/server/state/ConfigHelper.java | 15 ++-- .../apache/ambari/server/utils/ThreadPools.java | 10 ++- .../orm/dao/KerberosKeytabPrincipalDAOTest.java | 81 ++++++++++++++++++++++ 6 files changed, 122 insertions(+), 14 deletions(-) diff --git a/ambari-server/src/main/java/org/apache/ambari/server/events/publishers/AgentCommandsPublisher.java b/ambari-server/src/main/java/org/apache/ambari/server/events/publishers/AgentCommandsPublisher.java index 0d82caad62..7249ad5646 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/events/publishers/AgentCommandsPublisher.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/events/publishers/AgentCommandsPublisher.java @@ -111,7 +111,9 @@ public class AgentCommandsPublisher { if (!clusterDesiredConfigs.containsKey(clusterId)) { clusterDesiredConfigs.put(clusterId, clusters.getCluster(clusterId).getDesiredConfigs()); } - } catch (NumberFormatException|AmbariException ignored) {} + } catch (NumberFormatException|AmbariException e) { + LOG.error("Exception on sendAgentCommand", e); + } } Map<String, DesiredConfig> desiredConfigs = (clusterId != null && clusterDesiredConfigs.containsKey(clusterId)) @@ -120,7 +122,9 @@ public class AgentCommandsPublisher { populateExecutionCommandsClusters(executionCommandsClusters, hostId, ac, desiredConfigs); }); }).get(); - } catch (InterruptedException|ExecutionException ignored) {} + } catch (InterruptedException|ExecutionException e) { + LOG.error("Exception on sendAgentCommand", e); + } try { threadPools.getAgentPublisherCommandsPool().submit(() -> { @@ -132,7 +136,9 @@ public class AgentCommandsPublisher { )); }); }).get(); - } catch (InterruptedException|ExecutionException ignored) {} + } catch (InterruptedException|ExecutionException e) { + LOG.error("Exception on sendAgentCommand", e); + } } } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAO.java index 1be9bfbc1f..7257acb26f 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAO.java @@ -88,11 +88,15 @@ public class KerberosKeytabPrincipalDAO { Long hostId = hostEntity == null ? null : hostEntity.getHostId(); // The DB requests should be avoided due to heavy impact on the performance - KerberosKeytabPrincipalEntity kkp = (keytabList == null) + KerberosKeytabPrincipalEntity kkp = (keytabList == null || keytabList.isEmpty()) ? findByNaturalKey(hostId, kerberosKeytabEntity.getKeytabPath(), kerberosPrincipalEntity.getPrincipalName()) : keytabList.stream() .filter(keytab -> - keytab.getHostId().equals(hostId) + keytab != null + && keytab.getHostId() != null + && keytab.getKeytabPath() != null + && keytab.getPrincipalName() != null + && keytab.getHostId().equals(hostId) && keytab.getKeytabPath().equals(kerberosKeytabEntity.getKeytabPath()) && keytab.getPrincipalName().equals(kerberosPrincipalEntity.getPrincipalName()) ) diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/stageutils/KerberosKeytabController.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/stageutils/KerberosKeytabController.java index 653cccb908..037c796643 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/stageutils/KerberosKeytabController.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/stageutils/KerberosKeytabController.java @@ -319,15 +319,19 @@ public class KerberosKeytabController { Set<String> hosts = new HashSet<>(hostMap.keySet()); Map<String, String> componentHosts = new HashMap<>(); - hosts.add(StageUtils.getHostName()); - - for(String hostName: hosts) { + for (String hostName: hosts) { hostConfigurations.put( hostName, kerberosHelper.calculateConfigurations(cluster, hostName, kerberosDescriptor, userDescriptor, false,false, componentHosts, desiredConfigs) ); } + + // ambari-agent may not be installed on ambari-server. Add ambari-server after calculating configurations. + // If not, HostNotFoundException will raise + String ambariServerHostname = StageUtils.getHostName(); + hosts.add(ambariServerHostname); + for (String service: services) { kerberosHelper.getActiveIdentities(clusterName,null, service, null,true, hostConfigurations,kerberosDescriptor, desiredConfigs).values().forEach(serviceIdentities::addAll); diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java index 5a58768a0d..096250b19b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java @@ -37,6 +37,7 @@ import javax.annotation.Nullable; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.AmbariRuntimeException; +import org.apache.ambari.server.HostNotFoundException; import org.apache.ambari.server.agent.stomp.AgentConfigsHolder; import org.apache.ambari.server.agent.stomp.MetadataHolder; import org.apache.ambari.server.agent.stomp.dto.ClusterConfigs; @@ -188,10 +189,15 @@ public class ConfigHelper { * @throws AmbariException */ public Map<String, Map<String, String>> getEffectiveDesiredTags(Cluster cluster, String hostName, - @Nullable Map<String, DesiredConfig> desiredConfigs) - throws AmbariException { - - Host host = (hostName == null) ? null : clusters.getHost(hostName); + @Nullable Map<String, DesiredConfig> desiredConfigs) throws AmbariException { + Host host = null; + if (hostName != null) { + try { + host = clusters.getHost(hostName); + } catch (HostNotFoundException e) { + LOG.error("Cannot get desired config for unknown host {}", hostName, e); + } + } Map<String, HostConfig> desiredHostConfigs = (host == null) ? null : host.getDesiredHostConfigs(cluster, desiredConfigs); @@ -2190,6 +2196,7 @@ public class ConfigHelper { * @param hostname a hostname * @param desiredConfigs desired configuration map * @return a map of the existing configurations + * @throws AmbariException */ public Map<String, Map<String, String>> calculateExistingConfigurations( AmbariManagementController ambariManagementController, Cluster cluster, String hostname, diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/ThreadPools.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/ThreadPools.java index ac148be4bb..93b6fcc92c 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/utils/ThreadPools.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/ThreadPools.java @@ -98,7 +98,10 @@ public class ThreadPools { agentPublisherCommandsPool = new ForkJoinPool( configuration.getAgentCommandPublisherThreadPoolSize(), createNamedFactory(AGENT_COMMAND_PUBLISHER_POOL_NAME), - null, + (t, e) -> { + LOG.error("Unexpected exception in thread: " + t, e); + throw new RuntimeException(e); + }, false ); } @@ -111,7 +114,10 @@ public class ThreadPools { defaultForkJoinPool = new ForkJoinPool( configuration.getDefaultForkJoinPoolSize(), createNamedFactory(DEFAULT_FORK_JOIN_POOL_NAME), - null, + (t, e) -> { + LOG.error("Unexpected exception in thread: " + t, e); + throw new RuntimeException(e); + }, false ); } diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAOTest.java new file mode 100644 index 0000000000..3fe38e2318 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/KerberosKeytabPrincipalDAOTest.java @@ -0,0 +1,81 @@ +/* + * 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.ambari.server.orm.dao; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.EntityManager; + +import org.apache.ambari.server.orm.entities.HostEntity; +import org.apache.ambari.server.orm.entities.KerberosKeytabEntity; +import org.apache.ambari.server.orm.entities.KerberosKeytabPrincipalEntity; +import org.apache.ambari.server.orm.entities.KerberosPrincipalEntity; +import org.easymock.EasyMockRule; +import org.easymock.Mock; +import org.easymock.MockType; +import org.easymock.TestSubject; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.inject.Provider; + +public class KerberosKeytabPrincipalDAOTest { + + @Rule + public EasyMockRule mocks = new EasyMockRule(this); + + @Mock(type = MockType.STRICT) + private Provider<EntityManager> entityManagerProvider; + + @Mock(type = MockType.STRICT) + private EntityManager entityManager; + + @TestSubject + private final KerberosKeytabPrincipalDAO kerberosKeytabPrincipalDAO = new KerberosKeytabPrincipalDAO(); + + @Before + public void before() { + reset(entityManagerProvider); + expect(entityManagerProvider.get()).andReturn(entityManager).atLeastOnce(); + replay(entityManagerProvider); + } + + @Test + public void testFindOrCreate() { + HostEntity hostEntity = new HostEntity(); + hostEntity.setHostName("h1"); + hostEntity.setHostId(1L); + + KerberosKeytabEntity kke = new KerberosKeytabEntity(); + kke.setKeytabPath("/some/path"); + + KerberosPrincipalEntity kpe = new KerberosPrincipalEntity(); + kpe.setPrincipalName("t...@example.com"); + + List<KerberosKeytabPrincipalEntity> keytabList = new ArrayList<>(); + keytabList.add(null); + + kerberosKeytabPrincipalDAO.findOrCreate(kke, hostEntity, kpe, keytabList); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@ambari.apache.org For additional commands, e-mail: commits-h...@ambari.apache.org