Skip to content

Commit fe7517b

Browse files
apache license, checkstyle, pr feedback
1 parent 5d2c591 commit fe7517b

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SaslPropertiesResolver.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.hadoop.util.ReflectionUtils;
3232
import org.apache.hadoop.util.StringUtils;
3333

34+
import static java.util.Objects.requireNonNull;
35+
3436
/**
3537
* Provides SaslProperties to be used for a connection.
3638
* The default implementation is to read the values from configuration.
@@ -39,7 +41,7 @@
3941
*
4042
*/
4143
public class SaslPropertiesResolver implements Configurable{
42-
private SortedMap<String,String> properties;
44+
private SortedMap<String, String> properties;
4345
Configuration conf;
4446

4547
/**
@@ -83,7 +85,7 @@ public Configuration getConf() {
8385
* @return sasl Properties
8486
*/
8587
public Map<String,String> getDefaultProperties() {
86-
return new TreeMap<>(properties);
88+
return cloneProperties();
8789
}
8890

8991
/**
@@ -92,7 +94,7 @@ public Map<String,String> getDefaultProperties() {
9294
* @return the sasl properties to be used for the connection.
9395
*/
9496
public Map<String, String> getServerProperties(InetAddress clientAddress){
95-
return new TreeMap<>(properties);
97+
return cloneProperties();
9698
}
9799

98100
/**
@@ -112,7 +114,7 @@ public Map<String, String> getServerProperties(InetAddress clientAddress,
112114
* @return the sasl properties to be used for the connection.
113115
*/
114116
public Map<String, String> getClientProperties(InetAddress serverAddress){
115-
return new TreeMap<>(properties);
117+
return cloneProperties();
116118
}
117119

118120
/**
@@ -126,6 +128,12 @@ public Map<String, String> getClientProperties(InetAddress serverAddress,
126128
return getClientProperties(serverAddress);
127129
}
128130

131+
private Map<String, String> cloneProperties() {
132+
requireNonNull(properties,
133+
"properties map in SaslPropertiesResolver was null, did you call setConf()?");
134+
return new TreeMap<>(properties);
135+
}
136+
129137
/**
130138
* A util function to retrieve specific additional sasl property from config.
131139
* Used by subclasses to read sasl properties used by themselves.
Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
118
package org.apache.hadoop.security;
219

320
import java.net.InetAddress;
421
import java.net.InetSocketAddress;
22+
import java.util.Map;
523
import javax.security.sasl.Sasl;
624

725
import org.junit.Before;
826
import org.junit.Test;
927

1028
import org.apache.hadoop.conf.Configuration;
29+
import org.apache.hadoop.test.AbstractHadoopTestBase;
1130

1231
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_RPC_PROTECTION;
1332
import static org.junit.Assert.assertEquals;
1433

15-
public class TestSaslPropertiesResolver {
34+
public class TestSaslPropertiesResolver extends AbstractHadoopTestBase {
1635

17-
private static final SaslRpcServer.QualityOfProtection PRIVACY_QOP = SaslRpcServer.QualityOfProtection.PRIVACY;
18-
private static final SaslRpcServer.QualityOfProtection AUTHENTICATION_QOP = SaslRpcServer.QualityOfProtection.AUTHENTICATION;
1936
private static final InetAddress LOCALHOST = new InetSocketAddress("127.0.0.1", 1).getAddress();
2037

2138
private SaslPropertiesResolver resolver;
@@ -30,20 +47,29 @@ public void setup() {
3047

3148
@Test
3249
public void testResolverDoesNotMutate() {
33-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getDefaultProperties().get(Sasl.QOP));
34-
resolver.getDefaultProperties().put(Sasl.QOP, AUTHENTICATION_QOP.getSaslQop());
35-
// Even after changing the map returned by SaslPropertiesResolver, it does not change its future responses
36-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getDefaultProperties().get(Sasl.QOP));
37-
38-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getClientProperties(LOCALHOST).get(Sasl.QOP));
39-
resolver.getDefaultProperties().put(Sasl.QOP, AUTHENTICATION_QOP.getSaslQop());
40-
// Even after changing the map returned by SaslPropertiesResolver, it does not change its future responses
41-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getClientProperties(LOCALHOST).get(Sasl.QOP));
42-
43-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getServerProperties(LOCALHOST).get(Sasl.QOP));
44-
resolver.getDefaultProperties().put(Sasl.QOP, AUTHENTICATION_QOP.getSaslQop());
45-
// Even after changing the map returned by SaslPropertiesResolver, it does not change its future responses
46-
assertEquals(PRIVACY_QOP.getSaslQop(), resolver.getServerProperties(LOCALHOST).get(Sasl.QOP));
50+
assertPrivacyQop(resolver.getDefaultProperties());
51+
setAuthenticationQop(resolver.getDefaultProperties());
52+
// After changing the map returned by SaslPropertiesResolver, future maps are not affected
53+
assertPrivacyQop(resolver.getDefaultProperties());
54+
55+
assertPrivacyQop(resolver.getClientProperties(LOCALHOST));
56+
setAuthenticationQop(resolver.getClientProperties(LOCALHOST));
57+
// After changing the map returned by SaslPropertiesResolver, future maps are not affected
58+
assertPrivacyQop(resolver.getClientProperties(LOCALHOST));
59+
60+
assertPrivacyQop(resolver.getServerProperties(LOCALHOST));
61+
setAuthenticationQop(resolver.getServerProperties(LOCALHOST));
62+
// After changing the map returned by SaslPropertiesResolver, future maps are not affected
63+
assertPrivacyQop(resolver.getServerProperties(LOCALHOST));
64+
}
65+
66+
private static void setAuthenticationQop(Map<String, String> saslProperties) {
67+
saslProperties.put(Sasl.QOP, SaslRpcServer.QualityOfProtection.AUTHENTICATION.getSaslQop());
68+
}
69+
70+
private static void assertPrivacyQop(Map<String, String> saslProperties) {
71+
assertEquals(SaslRpcServer.QualityOfProtection.PRIVACY.getSaslQop(),
72+
saslProperties.get(Sasl.QOP));
4773
}
4874

4975
}

0 commit comments

Comments
 (0)