Skip to content

Commit ea7f701

Browse files
committed
YARN-11738. Modernize SecretManager config.
Make hash algorithm at SecretManager configurable. - hadoop.security.hmac-algorithm: The name of the hashing algorithm. Default: HmacSHA1 - hadoop.security.hmac-length: The length of the random keys to use. Default: 64 Change-Id: I735573c1d7b9f256e05722c98cd550cd8dd4acf0
1 parent 8c41fbc commit ea7f701

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,22 @@ 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+
/**
1009+
* @see
1010+
* <a href="{@docRoot}/../hadoop-project-dist/hadoop-common/core-default.xml">
1011+
* core-default.xml</a>
1012+
*/
1013+
public static final String HMAC_ALGORITHM = "hadoop.security.hmac-algorithm";
1014+
public static final String DEFAULT_HMAC_ALGORITHM = "HmacSHA1";
1015+
1016+
/**
1017+
* @see
1018+
* <a href="{@docRoot}/../hadoop-project-dist/hadoop-common/core-default.xml">
1019+
* core-default.xml</a>
1020+
*/
1021+
public static final String HMAC_LENGTH = "hadoop.security.hmac-length";
1022+
public static final int DEFAULT_HMAC_LENGTH = 64;
1023+
10081024
/**
10091025
* @see
10101026
* <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.HMAC_ALGORITHM,
125+
CommonConfigurationKeysPublic.DEFAULT_HMAC_ALGORITHM);
126+
LOG.info("Selected hash algorithm: {}", algorithm);
127+
SELECTED_ALGORITHM = algorithm;
128+
int length = conf.getInt(
129+
CommonConfigurationKeysPublic.HMAC_LENGTH,
130+
CommonConfigurationKeysPublic.DEFAULT_HMAC_LENGTH);
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 + " algorithm.");
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 + " algorithm.");
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,32 @@
10461046
</description>
10471047
</property>
10481048

1049+
<property>
1050+
<name>hadoop.security.hmac-algorithm</name>
1051+
<value>HmacSHA1</value>
1052+
<description>The configuration key specifying the hashing algorithm used for
1053+
HMAC (Hash-based Message Authentication Code) operations.
1054+
1055+
The HMAC algorithm is used in token management to compute secure
1056+
message digests. This configuration allows users to specify the
1057+
algorithm to be used for HMAC operations. The algorithm must be a
1058+
valid cryptographic hash algorithm supported by the Java Cryptography
1059+
Architecture (JCA). Common examples include "HmacSHA1", "HmacSHA256",
1060+
and "HmacSHA512".</description>
1061+
</property>
1062+
1063+
<property>
1064+
<name>hadoop.security.hmac-length</name>
1065+
<value>64</value>
1066+
<description>The configuration key specifying the key length for HMAC (Hash-based
1067+
Message Authentication Code) operations.
1068+
1069+
This property determines the size of the secret keys generated
1070+
for HMAC computations. The key length must be appropriate for the
1071+
selected HMAC algorithm. For example, longer keys are generally
1072+
more secure but may not be supported by all algorithms.</description>
1073+
</property>
1074+
10491075
<!-- file system properties -->
10501076

10511077
<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)