2828import javax .crypto .KeyGenerator ;
2929import javax .crypto .Mac ;
3030import java .security .NoSuchAlgorithmException ;
31+ import java .util .Map ;
32+ import java .util .WeakHashMap ;
3133
3234/**
3335 * Provides configuration and utility methods for managing cryptographic key generation
4244 */
4345@ InterfaceAudience .Public
4446@ InterfaceStability .Evolving
45- public class SecretManagerConfig {
47+ public final class SecretManagerConfig {
4648 private static final Logger LOG = LoggerFactory .getLogger (SecretManagerConfig .class );
4749 private static String selectedAlgorithm ;
4850 private static int selectedLength ;
49- private static boolean initialized ;
51+
52+ private static final Map <Thread , KeyGenerator > KEYGENS = new WeakHashMap <>();
53+ public static final Map <Thread , Mac > MACS = new WeakHashMap <>();
5054
5155 static {
5256 update (new Configuration ());
@@ -64,9 +68,13 @@ private SecretManagerConfig() {
6468 * @param conf the configuration object containing cryptographic settings
6569 */
6670 public static synchronized void update (Configuration conf ) {
67- if (initialized ) {
68- LOG .warn (
69- "Keygen or Mac was already initialized with older config, those will not be updated" );
71+ if (!KEYGENS .isEmpty ()) {
72+ LOG .warn ("Keygen was already initialized with older config, those will not be updated." +
73+ "Hint: If you turn on debug log you can see when it happened. Keygens: {}" , KEYGENS );
74+ }
75+ if (!MACS .isEmpty ()) {
76+ LOG .warn ("Mac was already initialized with older config, those will not be updated." +
77+ "Hint: If you turn on debug log you can see when it happened. Macs: {}" , MACS );
7078 }
7179 selectedAlgorithm = conf .get (
7280 CommonConfigurationKeysPublic .HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY ,
@@ -104,11 +112,12 @@ public static synchronized int getSelectedLength() {
104112 * @throws IllegalArgumentException if the specified algorithm is not available
105113 */
106114 public static synchronized KeyGenerator createKeyGenerator () {
107- LOG .debug ("Creating key generator instance {}, {}" , selectedAlgorithm , selectedLength );
108- initialized = true ;
115+ LOG .debug ("Creating key generator instance {} - {} bit with thread {}" ,
116+ selectedAlgorithm , selectedLength , Thread . currentThread ()) ;
109117 try {
110118 KeyGenerator keyGen = KeyGenerator .getInstance (selectedAlgorithm );
111119 keyGen .init (selectedLength );
120+ KEYGENS .put (Thread .currentThread (), keyGen );
112121 return keyGen ;
113122 } catch (NoSuchAlgorithmException nsa ) {
114123 throw new IllegalArgumentException ("Can't find " + selectedAlgorithm , nsa );
@@ -122,10 +131,11 @@ public static synchronized KeyGenerator createKeyGenerator() {
122131 * @throws IllegalArgumentException if the specified algorithm is not available
123132 */
124133 public static synchronized Mac createMac () {
125- LOG .debug ("Creating mac instance {}" , selectedAlgorithm );
126- initialized = true ;
134+ LOG .debug ("Creating mac instance {} with thread {}" , selectedAlgorithm , Thread .currentThread ());
127135 try {
128- return Mac .getInstance (selectedAlgorithm );
136+ Mac mac = Mac .getInstance (selectedAlgorithm );
137+ MACS .put (Thread .currentThread (), mac );
138+ return mac ;
129139 } catch (NoSuchAlgorithmException nsa ) {
130140 throw new IllegalArgumentException ("Can't find " + selectedAlgorithm , nsa );
131141 }
0 commit comments