From ab3fb28d47e8fa260d2de7490aea59404d571c2e Mon Sep 17 00:00:00 2001 From: Wei-Chiu Chuang Date: Thu, 18 Oct 2018 17:25:57 -0700 Subject: [PATCH] HADOOP-15418. Hadoop KMSAuthenticationFilter needs to use getPropsByPrefix instead of iterator to avoid ConcurrentModificationException. Contributed by lqjack and Suma Shivaprasad (cherry picked from commit cd2158456db8c89eeea64b72654a736ea8607e23) --- .../kms/server/KMSAuthenticationFilter.java | 23 +++++---- .../server/TestKMSAuthenticationFilter.java | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSAuthenticationFilter.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSAuthenticationFilter.java index 3e98a252320..da542ffb191 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSAuthenticationFilter.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSAuthenticationFilter.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.crypto.key.kms.server; +import com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.crypto.key.kms.KMSDelegationToken; @@ -54,16 +55,22 @@ public class KMSAuthenticationFilter @Override protected Properties getConfiguration(String configPrefix, FilterConfig filterConfig) { - Properties props = new Properties(); + Configuration conf = KMSWebApp.getConfiguration(); - for (Map.Entry entry : conf) { - String name = entry.getKey(); - if (name.startsWith(CONFIG_PREFIX)) { - String value = conf.get(name); - name = name.substring(CONFIG_PREFIX.length()); - props.setProperty(name, value); - } + return getKMSConfiguration(conf); + } + + @VisibleForTesting + Properties getKMSConfiguration(Configuration conf) { + Properties props = new Properties(); + + Map propsWithPrefixMap = conf.getPropsWithPrefix( + CONFIG_PREFIX); + + for (Map.Entry entry : propsWithPrefixMap.entrySet()) { + props.setProperty(entry.getKey(), entry.getValue()); } + String authType = props.getProperty(AUTH_TYPE); if (authType.equals(PseudoAuthenticationHandler.TYPE)) { props.setProperty(AUTH_TYPE, diff --git a/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java b/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java new file mode 100644 index 00000000000..da3913b0c91 --- /dev/null +++ b/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java @@ -0,0 +1,48 @@ +/** + * 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.hadoop.crypto.key.kms.server; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.crypto.key.kms.KMSDelegationToken; +import org.apache.hadoop.security.token.delegation.web + .DelegationTokenAuthenticationHandler; +import org.apache.hadoop.security.token.delegation.web + .PseudoDelegationTokenAuthenticationHandler; +import org.junit.Test; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +/** + * Test KMS Authentication Filter. + */ +public class TestKMSAuthenticationFilter { + + @Test public void testConfiguration() throws Exception { + Configuration conf = new Configuration(); + conf.set("hadoop.kms.authentication.type", "simple"); + + Properties prop = new KMSAuthenticationFilter().getKMSConfiguration(conf); + assertEquals(prop.getProperty(KMSAuthenticationFilter.AUTH_TYPE), + PseudoDelegationTokenAuthenticationHandler.class.getName()); + assertEquals( + prop.getProperty(DelegationTokenAuthenticationHandler.TOKEN_KIND), + KMSDelegationToken.TOKEN_KIND_STR); + } +} +