Skip to content

Commit 93fdd42

Browse files
K0K0V0Ksteveloughran
authored andcommitted
CDPD-75832. YARN-11738. Modernize SecretManager config (apache#7144)
Change-Id: Ibad7f66a6ffd0fa68746526132d8fa660278e9a5
1 parent ff94e11 commit 93fdd42

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/CommonConfigurationKeysPublic.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,15 @@ public class CommonConfigurationKeysPublic {
10051005
public static final String HADOOP_SECURITY_CREDENTIAL_PASSWORD_FILE_KEY =
10061006
"hadoop.security.credstore.java-keystore-provider.password-file";
10071007

1008+
public static final String HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY =
1009+
"hadoop.security.secret-manager.key-generator.algorithm";
1010+
public static final String HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT =
1011+
"HmacSHA1";
1012+
1013+
public static final String HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY =
1014+
"hadoop.security.secret-manager.key-length";
1015+
public static final int HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT = 64;
1016+
10081017
/**
10091018
* @see
10101019
* <a href="{@docRoot}/../hadoop-project-dist/hadoop-common/core-default.xml">

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727
import javax.crypto.SecretKey;
2828
import javax.crypto.spec.SecretKeySpec;
2929

30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
3033
import org.apache.hadoop.classification.InterfaceAudience;
3134
import org.apache.hadoop.classification.InterfaceStability;
35+
import org.apache.hadoop.conf.Configuration;
36+
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
3237
import org.apache.hadoop.ipc.RetriableException;
3338
import org.apache.hadoop.ipc.StandbyException;
3439

@@ -40,6 +45,8 @@
4045
@InterfaceAudience.Public
4146
@InterfaceStability.Evolving
4247
public abstract class SecretManager<T extends TokenIdentifier> {
48+
49+
public static final Logger LOG = LoggerFactory.getLogger(SecretManager.class);
4350
/**
4451
* The token was invalid and the message explains why.
4552
*/
@@ -107,16 +114,23 @@ public byte[] retriableRetrievePassword(T identifier)
107114
public void checkAvailableForRead() throws StandbyException {
108115
// Default to being available for read.
109116
}
110-
111-
/**
112-
* The name of the hashing algorithm.
113-
*/
114-
private static final String DEFAULT_HMAC_ALGORITHM = "HmacSHA1";
115117

116-
/**
117-
* The length of the random keys to use.
118-
*/
119-
private static final int KEY_LENGTH = 64;
118+
private static final String SELECTED_ALGORITHM;
119+
private static final int SELECTED_LENGTH;
120+
121+
static {
122+
Configuration conf = new Configuration();
123+
String algorithm = conf.get(
124+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY,
125+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT);
126+
LOG.info("Selected hash algorithm: {}", algorithm);
127+
SELECTED_ALGORITHM = algorithm;
128+
int length = conf.getInt(
129+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY,
130+
CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT);
131+
LOG.info("Selected hash key length:{}", length);
132+
SELECTED_LENGTH = length;
133+
}
120134

121135
/**
122136
* A thread local store for the Macs.
@@ -126,10 +140,9 @@ public void checkAvailableForRead() throws StandbyException {
126140
@Override
127141
protected Mac initialValue() {
128142
try {
129-
return Mac.getInstance(DEFAULT_HMAC_ALGORITHM);
143+
return Mac.getInstance(SELECTED_ALGORITHM);
130144
} catch (NoSuchAlgorithmException nsa) {
131-
throw new IllegalArgumentException("Can't find " + DEFAULT_HMAC_ALGORITHM +
132-
" algorithm.");
145+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
133146
}
134147
}
135148
};
@@ -140,11 +153,10 @@ protected Mac initialValue() {
140153
private final KeyGenerator keyGen;
141154
{
142155
try {
143-
keyGen = KeyGenerator.getInstance(DEFAULT_HMAC_ALGORITHM);
144-
keyGen.init(KEY_LENGTH);
156+
keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM);
157+
keyGen.init(SELECTED_LENGTH);
145158
} catch (NoSuchAlgorithmException nsa) {
146-
throw new IllegalArgumentException("Can't find " + DEFAULT_HMAC_ALGORITHM +
147-
" algorithm.");
159+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa);
148160
}
149161
}
150162

@@ -185,6 +197,6 @@ public static byte[] createPassword(byte[] identifier,
185197
* @return the secret key
186198
*/
187199
protected static SecretKey createSecretKey(byte[] key) {
188-
return new SecretKeySpec(key, DEFAULT_HMAC_ALGORITHM);
200+
return new SecretKeySpec(key, SELECTED_ALGORITHM);
189201
}
190202
}

hadoop-common-project/hadoop-common/src/main/resources/core-default.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,28 @@
10451045
</description>
10461046
</property>
10471047

1048+
<property>
1049+
<name>hadoop.security.secret-manager.key-generator.algorithm</name>
1050+
<value>HmacSHA1</value>
1051+
<description>
1052+
The configuration key specifying the KeyGenerator algorithm used in SecretManager
1053+
for generating secret keys. The algorithm must be a KeyGenerator algorithm supported by
1054+
the Java Cryptography Architecture (JCA). Common examples include "HmacSHA1",
1055+
"HmacSHA256", and "HmacSHA512".
1056+
</description>
1057+
</property>
1058+
1059+
<property>
1060+
<name>hadoop.security.secret-manager.key-length</name>
1061+
<value>64</value>
1062+
<description>
1063+
The configuration key specifying the key length of the generated secret keys
1064+
in SecretManager. The key length must be appropriate for the algorithm.
1065+
For example, longer keys are generally more secure but may not be supported
1066+
by all algorithms.
1067+
</description>
1068+
</property>
1069+
10481070
<!-- file system properties -->
10491071

10501072
<property>

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/security/TestNMTokenSecretManagerInNM.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public void testRecovery() throws IOException {
6060
secretMgr.setNodeId(nodeId);
6161
MasterKey currentKey = keygen.generateKey();
6262
secretMgr.setMasterKey(currentKey);
63+
// check key is 64 bit long (8 byte)
64+
assertEquals(8, currentKey.getBytes().array().length);
6365
NMTokenIdentifier attemptToken1 =
6466
getNMTokenId(secretMgr.createNMToken(attempt1, nodeId, "user1"));
6567
NMTokenIdentifier attemptToken2 =

0 commit comments

Comments
 (0)