-
Notifications
You must be signed in to change notification settings - Fork 9.1k
HADOOP-19639. SecretManager configuration at runtime #7827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
43d1e7a
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K c12d388
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K f9f28d6
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K 290f9d7
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K dc8b282
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K d7b4811
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K b4c125b
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K 62cb8b5
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K b02c202
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K e56c021
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K ef05d17
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K dc7eb8a
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K abbb7a3
HADOOP-19639. SecretManager configuration at runtime
K0K0V0K File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...ect/hadoop-common/src/main/java/org/apache/hadoop/security/token/SecretManagerConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.security.token; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience; | ||
import org.apache.hadoop.classification.InterfaceStability; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.CommonConfigurationKeysPublic; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.Mac; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* Provides configuration and utility methods for managing cryptographic key generation | ||
* and message authentication code (MAC) generation using specified algorithms and key lengths. | ||
* <p> | ||
* This class supports static access to the selected cryptographic algorithm and key length, | ||
* and provides methods to create configured {@link javax.crypto.KeyGenerator} | ||
* and {@link javax.crypto.Mac} instances. | ||
* The configuration is initialized statically from a provided {@link Configuration} object. | ||
* <p> | ||
* The {@link SecretManager} has some static method, so static configuration is required | ||
*/ | ||
@InterfaceAudience.Public | ||
@InterfaceStability.Evolving | ||
public class SecretManagerConfig { | ||
private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class); | ||
private static String selectedAlgorithm; | ||
private static int selectedLength; | ||
private static boolean initialized; | ||
|
||
static { | ||
update(new Configuration()); | ||
} | ||
|
||
private SecretManagerConfig() { | ||
} | ||
|
||
/** | ||
* Updates the selected cryptographic algorithm and key length using the provided | ||
* Hadoop {@link Configuration}. This method reads the values for | ||
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and | ||
* {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set. | ||
* | ||
* @param conf the configuration object containing cryptographic settings | ||
*/ | ||
public static synchronized void update(Configuration conf) { | ||
if (initialized) { | ||
LOG.warn( | ||
"Keygen or Mac was already initialized with older config, those will not be updated"); | ||
} | ||
selectedAlgorithm = conf.get( | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY, | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT); | ||
LOG.debug("Selected hash algorithm: {}", selectedAlgorithm); | ||
selectedLength = conf.getInt( | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY, | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT); | ||
LOG.debug("Selected hash key length: {}", selectedLength); | ||
} | ||
|
||
/** | ||
* Returns the currently selected cryptographic algorithm. | ||
* | ||
* @return the name of the selected algorithm | ||
*/ | ||
public static synchronized String getSelectedAlgorithm() { | ||
return selectedAlgorithm; | ||
} | ||
|
||
/** | ||
* Returns the currently selected key length in bits. | ||
* | ||
* @return the selected key length | ||
*/ | ||
public static synchronized int getSelectedLength() { | ||
return selectedLength; | ||
} | ||
|
||
/** | ||
* Creates a new {@link KeyGenerator} instance configured with the currently selected | ||
* algorithm and key length. | ||
* | ||
* @return a new {@code KeyGenerator} instance | ||
* @throws IllegalArgumentException if the specified algorithm is not available | ||
*/ | ||
public static synchronized KeyGenerator createKeyGenerator() { | ||
LOG.debug("Creating key generator instance {}, {}", selectedAlgorithm, selectedLength); | ||
initialized = true; | ||
try { | ||
KeyGenerator keyGen = KeyGenerator.getInstance(selectedAlgorithm); | ||
keyGen.init(selectedLength); | ||
return keyGen; | ||
} catch (NoSuchAlgorithmException nsa) { | ||
throw new IllegalArgumentException("Can't find " + selectedAlgorithm, nsa); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new {@link Mac} instance using the currently selected algorithm. | ||
* | ||
* @return a new {@code Mac} instance | ||
* @throws IllegalArgumentException if the specified algorithm is not available | ||
*/ | ||
public static synchronized Mac createMac() { | ||
LOG.debug("Creating mac instance {}", selectedAlgorithm); | ||
K0K0V0K marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
initialized = true; | ||
try { | ||
return Mac.getInstance(selectedAlgorithm); | ||
} catch (NoSuchAlgorithmException nsa) { | ||
throw new IllegalArgumentException("Can't find " + selectedAlgorithm, nsa); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...doop-common/src/test/java/org/apache/hadoop/security/token/TestSecurityManagerConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.security.token; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.CommonConfigurationKeysPublic; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.Mac; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class TestSecurityManagerConfig { | ||
|
||
private final String defaultAlgorithm = | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT; | ||
private final int defaultLength = | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT; | ||
private final String strongAlgorithm = "HmacSHA256"; | ||
private final int strongLength = 256; | ||
|
||
@Test | ||
public void testDefaults() { | ||
assertEquals(defaultAlgorithm, SecretManagerConfig.getSelectedAlgorithm()); | ||
assertEquals(defaultLength, SecretManagerConfig.getSelectedLength()); | ||
} | ||
|
||
@Test | ||
public void testUpdateByConfig() { | ||
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength)); | ||
assertEquals(strongAlgorithm, SecretManagerConfig.getSelectedAlgorithm()); | ||
assertEquals(strongLength, SecretManagerConfig.getSelectedLength()); | ||
} | ||
|
||
@Test | ||
public void testMacCreation() { | ||
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength)); | ||
Mac mac = SecretManagerConfig.createMac(); | ||
assertEquals(strongAlgorithm, mac.getAlgorithm()); | ||
} | ||
|
||
@Test | ||
public void testMacCreationUnknownAlgorithm() { | ||
SecretManagerConfig.update( | ||
createConfiguration("testMacCreationUnknownAlgorithm_NO_ALG", defaultLength)); | ||
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createMac); | ||
} | ||
|
||
@Test | ||
public void testKeygenCreation() { | ||
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength)); | ||
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator(); | ||
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm()); | ||
} | ||
|
||
@Test | ||
public void testKeygenCreationUnknownAlgorithm() { | ||
SecretManagerConfig.update( | ||
createConfiguration("testKeygenCreationUnknownAlgorithm_NO_ALG", defaultLength)); | ||
assertThrows(IllegalArgumentException.class, SecretManagerConfig::createKeyGenerator); | ||
} | ||
|
||
@Test | ||
public void testConfigUpdateAfterKeygenCreation() { | ||
SecretManagerConfig.update(createConfiguration(strongAlgorithm, strongLength)); | ||
KeyGenerator keyGenerator = SecretManagerConfig.createKeyGenerator(); | ||
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength)); | ||
assertEquals(strongAlgorithm, keyGenerator.getAlgorithm()); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
SecretManagerConfig.update(createConfiguration(defaultAlgorithm, defaultLength)); | ||
} | ||
|
||
private Configuration createConfiguration(String algorithm, int length) { | ||
Configuration conf = new Configuration(); | ||
conf.set( | ||
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY, | ||
algorithm); | ||
conf.setInt(CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY, | ||
length); | ||
return conf; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.