27
27
import javax .crypto .SecretKey ;
28
28
import javax .crypto .spec .SecretKeySpec ;
29
29
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
32
+
30
33
import org .apache .hadoop .classification .InterfaceAudience ;
31
34
import org .apache .hadoop .classification .InterfaceStability ;
35
+ import org .apache .hadoop .conf .Configuration ;
36
+ import org .apache .hadoop .fs .CommonConfigurationKeysPublic ;
32
37
import org .apache .hadoop .ipc .RetriableException ;
33
38
import org .apache .hadoop .ipc .StandbyException ;
34
39
40
45
@ InterfaceAudience .Public
41
46
@ InterfaceStability .Evolving
42
47
public abstract class SecretManager <T extends TokenIdentifier > {
48
+
49
+ public static final Logger LOG = LoggerFactory .getLogger (SecretManager .class );
43
50
/**
44
51
* The token was invalid and the message explains why.
45
52
*/
@@ -107,16 +114,23 @@ public byte[] retriableRetrievePassword(T identifier)
107
114
public void checkAvailableForRead () throws StandbyException {
108
115
// Default to being available for read.
109
116
}
110
-
111
- /**
112
- * The name of the hashing algorithm.
113
- */
114
- private static final String DEFAULT_HMAC_ALGORITHM = "HmacSHA1" ;
115
117
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
+ }
120
134
121
135
/**
122
136
* A thread local store for the Macs.
@@ -126,10 +140,9 @@ public void checkAvailableForRead() throws StandbyException {
126
140
@ Override
127
141
protected Mac initialValue () {
128
142
try {
129
- return Mac .getInstance (DEFAULT_HMAC_ALGORITHM );
143
+ return Mac .getInstance (SELECTED_ALGORITHM );
130
144
} 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 );
133
146
}
134
147
}
135
148
};
@@ -140,11 +153,10 @@ protected Mac initialValue() {
140
153
private final KeyGenerator keyGen ;
141
154
{
142
155
try {
143
- keyGen = KeyGenerator .getInstance (DEFAULT_HMAC_ALGORITHM );
144
- keyGen .init (KEY_LENGTH );
156
+ keyGen = KeyGenerator .getInstance (SELECTED_ALGORITHM );
157
+ keyGen .init (SELECTED_LENGTH );
145
158
} 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 );
148
160
}
149
161
}
150
162
@@ -185,6 +197,6 @@ public static byte[] createPassword(byte[] identifier,
185
197
* @return the secret key
186
198
*/
187
199
protected static SecretKey createSecretKey (byte [] key ) {
188
- return new SecretKeySpec (key , DEFAULT_HMAC_ALGORITHM );
200
+ return new SecretKeySpec (key , SELECTED_ALGORITHM );
189
201
}
190
202
}
0 commit comments