Skip to content

Commit 3495662

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 3495662

File tree

2 files changed

+29
-9
lines changed
  • hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/security

2 files changed

+29
-9
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
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;
3236
import org.apache.hadoop.ipc.RetriableException;
3337
import org.apache.hadoop.ipc.StandbyException;
3438

@@ -40,6 +44,8 @@
4044
@InterfaceAudience.Public
4145
@InterfaceStability.Evolving
4246
public abstract class SecretManager<T extends TokenIdentifier> {
47+
48+
public static final Logger LOG = LoggerFactory.getLogger(SecretManager.class);
4349
/**
4450
* The token was invalid and the message explains why.
4551
*/
@@ -111,12 +117,26 @@ public void checkAvailableForRead() throws StandbyException {
111117
/**
112118
* The name of the hashing algorithm.
113119
*/
120+
private static final String HMAC_ALGORITHM = "hadoop.security.hmac-algorithm";
114121
private static final String DEFAULT_HMAC_ALGORITHM = "HmacSHA1";
122+
private static final String SELECTED_ALGORITHM;
115123

116124
/**
117125
* The length of the random keys to use.
118126
*/
119-
private static final int KEY_LENGTH = 64;
127+
private static final String HMAC_LENGTH = "hadoop.security.hmac-length";
128+
private static final int DEFAULT_HMAC_LENGTH = 64;
129+
private static final int SELECTED_LENGTH;
130+
131+
static {
132+
Configuration conf = new Configuration();
133+
String algorithm = conf.get(HMAC_ALGORITHM, DEFAULT_HMAC_ALGORITHM);
134+
LOG.info("Selected hash algorithm: {}", algorithm);
135+
SELECTED_ALGORITHM = algorithm;
136+
int length = conf.getInt(HMAC_LENGTH, DEFAULT_HMAC_LENGTH);
137+
LOG.info("Selected hash key length:{}", length);
138+
SELECTED_LENGTH = length;
139+
}
120140

121141
/**
122142
* A thread local store for the Macs.
@@ -126,10 +146,9 @@ public void checkAvailableForRead() throws StandbyException {
126146
@Override
127147
protected Mac initialValue() {
128148
try {
129-
return Mac.getInstance(DEFAULT_HMAC_ALGORITHM);
149+
return Mac.getInstance(SELECTED_ALGORITHM);
130150
} catch (NoSuchAlgorithmException nsa) {
131-
throw new IllegalArgumentException("Can't find " + DEFAULT_HMAC_ALGORITHM +
132-
" algorithm.");
151+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM + " algorithm.");
133152
}
134153
}
135154
};
@@ -140,11 +159,10 @@ protected Mac initialValue() {
140159
private final KeyGenerator keyGen;
141160
{
142161
try {
143-
keyGen = KeyGenerator.getInstance(DEFAULT_HMAC_ALGORITHM);
144-
keyGen.init(KEY_LENGTH);
162+
keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM);
163+
keyGen.init(SELECTED_LENGTH);
145164
} catch (NoSuchAlgorithmException nsa) {
146-
throw new IllegalArgumentException("Can't find " + DEFAULT_HMAC_ALGORITHM +
147-
" algorithm.");
165+
throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM + " algorithm.");
148166
}
149167
}
150168

@@ -185,6 +203,6 @@ public static byte[] createPassword(byte[] identifier,
185203
* @return the secret key
186204
*/
187205
protected static SecretKey createSecretKey(byte[] key) {
188-
return new SecretKeySpec(key, DEFAULT_HMAC_ALGORITHM);
206+
return new SecretKeySpec(key, SELECTED_ALGORITHM);
189207
}
190208
}

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)