Skip to content

Commit 62cb8b5

Browse files
committed
HADOOP-19639. SecretManager configuration at runtime
- fix style
1 parent b4c125b commit 62cb8b5

File tree

2 files changed

+144
-144
lines changed

2 files changed

+144
-144
lines changed

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

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,12 +21,12 @@
2121
import org.apache.hadoop.classification.InterfaceStability;
2222
import org.apache.hadoop.conf.Configuration;
2323
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
24+
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627

2728
import javax.crypto.KeyGenerator;
2829
import javax.crypto.Mac;
29-
import javax.crypto.SecretKey;
3030
import java.security.NoSuchAlgorithmException;
3131

3232
/**
@@ -42,88 +42,88 @@
4242
@InterfaceAudience.Public
4343
@InterfaceStability.Evolving
4444
public class SecretManagerConfig {
45-
private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class);
46-
private static String SELECTED_ALGORITHM;
47-
private static int SELECTED_LENGTH;
48-
private static boolean INITIALIZED;
45+
private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class);
46+
private static String SELECTED_ALGORITHM;
47+
private static int SELECTED_LENGTH;
48+
private static boolean INITIALIZED;
4949

50-
static {
51-
update(new Configuration());
52-
}
50+
static {
51+
update(new Configuration());
52+
}
5353

54-
/**
55-
* Updates the selected cryptographic algorithm and key length using the provided
56-
* Hadoop {@link Configuration}. This method reads the values for
57-
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and
58-
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set.
59-
*
60-
* @param conf the configuration object containing cryptographic settings
61-
*/
62-
public static synchronized void update(Configuration conf) {
63-
if (INITIALIZED) {
64-
LOG.warn("Keygen or Mac was already initialized with older configuration, those will not be updated");
65-
}
66-
SELECTED_ALGORITHM = conf.get(
67-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY,
68-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT);
69-
LOG.debug("Selected hash algorithm: {}", SELECTED_ALGORITHM);
70-
SELECTED_LENGTH = conf.getInt(
71-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY,
72-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT);
73-
LOG.debug("Selected hash key length: {}", SELECTED_LENGTH);
54+
/**
55+
* Updates the selected cryptographic algorithm and key length using the provided
56+
* Hadoop {@link Configuration}. This method reads the values for
57+
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and
58+
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set.
59+
*
60+
* @param conf the configuration object containing cryptographic settings
61+
*/
62+
public static synchronized void update(Configuration conf) {
63+
if (INITIALIZED) {
64+
LOG.warn(
65+
"Keygen or Mac was already initialized with older configuration, those will not be updated");
7466
}
67+
SELECTED_ALGORITHM = conf.get(
68+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY,
69+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT);
70+
LOG.debug("Selected hash algorithm: {}", SELECTED_ALGORITHM);
71+
SELECTED_LENGTH =
72+
conf.getInt(CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY,
73+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT);
74+
LOG.debug("Selected hash key length: {}", SELECTED_LENGTH);
75+
}
7576

76-
/**
77-
* Returns the currently selected cryptographic algorithm.
78-
*
79-
* @return the name of the selected algorithm
80-
*/
81-
public static synchronized String getSelectedAlgorithm() {
82-
return SELECTED_ALGORITHM;
83-
}
84-
85-
/**
86-
* Returns the currently selected key length in bits.
87-
*
88-
* @return the selected key length
89-
*/
90-
public static synchronized int getSelectedLength() {
91-
return SELECTED_LENGTH;
92-
}
77+
/**
78+
* Returns the currently selected cryptographic algorithm.
79+
*
80+
* @return the name of the selected algorithm
81+
*/
82+
public static synchronized String getSelectedAlgorithm() {
83+
return SELECTED_ALGORITHM;
84+
}
9385

86+
/**
87+
* Returns the currently selected key length in bits.
88+
*
89+
* @return the selected key length
90+
*/
91+
public static synchronized int getSelectedLength() {
92+
return SELECTED_LENGTH;
93+
}
9494

95-
/**
96-
* Creates a new {@link KeyGenerator} instance configured with the currently selected
97-
* algorithm and key length.
98-
*
99-
* @return a new {@code KeyGenerator} instance
100-
* @throws IllegalArgumentException if the specified algorithm is not available
101-
*/
102-
public static synchronized KeyGenerator createKeyGenerator() {
103-
LOG.debug("Creating key generator instance {}, {}", SELECTED_ALGORITHM, SELECTED_LENGTH);
104-
INITIALIZED = true;
105-
try {
106-
KeyGenerator keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM);
107-
keyGen.init(SELECTED_LENGTH);
108-
return keyGen;
109-
} catch (NoSuchAlgorithmException nsa) {
110-
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
111-
}
95+
/**
96+
* Creates a new {@link KeyGenerator} instance configured with the currently selected
97+
* algorithm and key length.
98+
*
99+
* @return a new {@code KeyGenerator} instance
100+
* @throws IllegalArgumentException if the specified algorithm is not available
101+
*/
102+
public static synchronized KeyGenerator createKeyGenerator() {
103+
LOG.debug("Creating key generator instance {}, {}", SELECTED_ALGORITHM, SELECTED_LENGTH);
104+
INITIALIZED = true;
105+
try {
106+
KeyGenerator keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM);
107+
keyGen.init(SELECTED_LENGTH);
108+
return keyGen;
109+
} catch (NoSuchAlgorithmException nsa) {
110+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
112111
}
112+
}
113113

114-
/**
115-
* Creates a new {@link Mac} instance using the currently selected algorithm.
116-
*
117-
* @return a new {@code Mac} instance
118-
* @throws IllegalArgumentException if the specified algorithm is not available
119-
*/
120-
public static synchronized Mac createMac() {
121-
LOG.debug("Creating mac instance {}", SELECTED_ALGORITHM);
122-
INITIALIZED = true;
123-
try {
124-
return Mac.getInstance(SELECTED_ALGORITHM);
125-
} catch (NoSuchAlgorithmException nsa) {
126-
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
127-
}
114+
/**
115+
* Creates a new {@link Mac} instance using the currently selected algorithm.
116+
*
117+
* @return a new {@code Mac} instance
118+
* @throws IllegalArgumentException if the specified algorithm is not available
119+
*/
120+
public static synchronized Mac createMac() {
121+
LOG.debug("Creating mac instance {}", SELECTED_ALGORITHM);
122+
INITIALIZED = true;
123+
try {
124+
return Mac.getInstance(SELECTED_ALGORITHM);
125+
} catch (NoSuchAlgorithmException nsa) {
126+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
128127
}
128+
}
129129
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestSecurityManagerConfig.java

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,7 @@
1919

2020
import org.apache.hadoop.conf.Configuration;
2121
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
22+
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.Test;
2425

@@ -30,75 +31,74 @@
3031

3132
public class TestSecurityManagerConfig {
3233

33-
private final String defaultAlgorithm =
34-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT;
35-
private final int defaultLength =
36-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT;
37-
private final String strongAlgorithm = "HmacSHA256";
38-
private final int strongLength = 256;
34+
private final String defaultAlgorithm =
35+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT;
36+
private final int defaultLength =
37+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT;
38+
private final String strongAlgorithm = "HmacSHA256";
39+
private final int strongLength = 256;
3940

40-
@Test
41-
public void testDefaults() {
42-
assertEquals(defaultAlgorithm, SecretManagerConfig.getSelectedAlgorithm());
43-
assertEquals(defaultLength, SecretManagerConfig.getSelectedLength());
44-
}
41+
@Test
42+
public void testDefaults() {
43+
assertEquals(defaultAlgorithm, SecretManagerConfig.getSelectedAlgorithm());
44+
assertEquals(defaultLength, SecretManagerConfig.getSelectedLength());
45+
}
4546

46-
@Test
47-
public void testUpdateByConfig() {
48-
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
49-
assertEquals(strongAlgorithm, SecretManagerConfig.getSelectedAlgorithm());
50-
assertEquals(strongLength, SecretManagerConfig.getSelectedLength());
51-
}
47+
@Test
48+
public void testUpdateByConfig() {
49+
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
50+
assertEquals(strongAlgorithm, SecretManagerConfig.getSelectedAlgorithm());
51+
assertEquals(strongLength, SecretManagerConfig.getSelectedLength());
52+
}
5253

53-
@Test
54-
public void testMacCreation() {
55-
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
56-
Mac mac = SecretManagerConfig.createMac();
57-
assertEquals(strongAlgorithm, mac.getAlgorithm());
58-
}
54+
@Test
55+
public void testMacCreation() {
56+
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
57+
Mac mac = SecretManagerConfig.createMac();
58+
assertEquals(strongAlgorithm, mac.getAlgorithm());
59+
}
5960

60-
@Test
61-
public void testMacCreationUnknownAlgorithm() {
62-
SecretManagerConfig.update(
63-
createConfiguration("testMacCreationUnknownAlgorithm_NO_ALG", defaultLength));
64-
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createMac);
65-
}
61+
@Test
62+
public void testMacCreationUnknownAlgorithm() {
63+
SecretManagerConfig.update(
64+
createConfiguration("testMacCreationUnknownAlgorithm_NO_ALG", defaultLength));
65+
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createMac);
66+
}
6667

67-
@Test
68-
public void testKeygenCreation() {
69-
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
70-
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator();
71-
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm());
72-
}
68+
@Test
69+
public void testKeygenCreation() {
70+
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
71+
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator();
72+
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm());
73+
}
7374

74-
@Test
75-
public void testKeygenCreationUnknownAlgorithm() {
76-
SecretManagerConfig.update(
77-
createConfiguration("testKeygenCreationUnknownAlgorithm_NO_ALG", defaultLength));
78-
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createKeyGenerator);
79-
}
75+
@Test
76+
public void testKeygenCreationUnknownAlgorithm() {
77+
SecretManagerConfig.update(
78+
createConfiguration("testKeygenCreationUnknownAlgorithm_NO_ALG", defaultLength));
79+
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createKeyGenerator);
80+
}
8081

81-
@Test
82-
public void testConfigUpdateAfterKeygenCreation() {
83-
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
84-
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator();
85-
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength));
86-
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm());
87-
}
82+
@Test
83+
public void testConfigUpdateAfterKeygenCreation() {
84+
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength));
85+
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator();
86+
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength));
87+
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm());
88+
}
8889

89-
@AfterEach
90-
public void tearDown() {
91-
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength));
92-
}
90+
@AfterEach
91+
public void tearDown() {
92+
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength));
93+
}
9394

94-
private Configuration createConfiguration(String algorithm, int length) {
95-
Configuration conf = new Configuration();
96-
conf.set(
97-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY,
98-
algorithm);
99-
conf.setInt(
100-
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY,
101-
length);
102-
return conf;
103-
}
95+
private Configuration createConfiguration(String algorithm, int length) {
96+
Configuration conf = new Configuration();
97+
conf.set(
98+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY,
99+
algorithm);
100+
conf.setInt(CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY,
101+
length);
102+
return conf;
103+
}
104104
}

0 commit comments

Comments
 (0)