|
6 | 6 | * to you under the Apache License, Version 2.0 (the |
7 | 7 | * "License"); you may not use this file except in compliance |
8 | 8 | * with the License. You may obtain a copy of the License at |
9 | | - * |
10 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | - * |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
12 | 12 | * Unless required by applicable law or agreed to in writing, software |
13 | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
21 | 21 | import org.apache.hadoop.classification.InterfaceStability; |
22 | 22 | import org.apache.hadoop.conf.Configuration; |
23 | 23 | import org.apache.hadoop.fs.CommonConfigurationKeysPublic; |
| 24 | + |
24 | 25 | import org.slf4j.Logger; |
25 | 26 | import org.slf4j.LoggerFactory; |
26 | 27 |
|
27 | 28 | import javax.crypto.KeyGenerator; |
28 | 29 | import javax.crypto.Mac; |
29 | | -import javax.crypto.SecretKey; |
30 | 30 | import java.security.NoSuchAlgorithmException; |
31 | 31 |
|
32 | 32 | /** |
|
42 | 42 | @InterfaceAudience.Public |
43 | 43 | @InterfaceStability.Evolving |
44 | 44 | public class SecretManagerConfig { |
45 | | - private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class); |
46 | | - private static String SELECTED_ALGORITHM; |
47 | | - private static int SELECTED_LENGTH; |
48 | | - private static boolean INITIALIZED; |
| 45 | + private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class); |
| 46 | + private static String SELECTED_ALGORITHM; |
| 47 | + private static int SELECTED_LENGTH; |
| 48 | + private static boolean INITIALIZED; |
49 | 49 |
|
50 | | - static { |
51 | | - update(new Configuration()); |
52 | | - } |
| 50 | + static { |
| 51 | + update(new Configuration()); |
| 52 | + } |
53 | 53 |
|
54 | | - /** |
55 | | - * Updates the selected cryptographic algorithm and key length using the provided |
56 | | - * Hadoop {@link Configuration}. This method reads the values for |
57 | | - * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and |
58 | | - * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set. |
59 | | - * |
60 | | - * @param conf the configuration object containing cryptographic settings |
61 | | - */ |
62 | | - public static synchronized void update(Configuration conf) { |
63 | | - if (INITIALIZED) { |
64 | | - LOG.warn("Keygen or Mac was already initialized with older configuration, those will not be updated"); |
65 | | - } |
66 | | - SELECTED_ALGORITHM = conf.get( |
67 | | - CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY, |
68 | | - CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT); |
69 | | - LOG.debug("Selected hash algorithm: {}", SELECTED_ALGORITHM); |
70 | | - SELECTED_LENGTH = conf.getInt( |
71 | | - CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY, |
72 | | - CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT); |
73 | | - LOG.debug("Selected hash key length: {}", SELECTED_LENGTH); |
| 54 | + /** |
| 55 | + * Updates the selected cryptographic algorithm and key length using the provided |
| 56 | + * Hadoop {@link Configuration}. This method reads the values for |
| 57 | + * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and |
| 58 | + * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set. |
| 59 | + * |
| 60 | + * @param conf the configuration object containing cryptographic settings |
| 61 | + */ |
| 62 | + public static synchronized void update(Configuration conf) { |
| 63 | + if (INITIALIZED) { |
| 64 | + LOG.warn( |
| 65 | + "Keygen or Mac was already initialized with older configuration, those will not be updated"); |
74 | 66 | } |
| 67 | + SELECTED_ALGORITHM = conf.get( |
| 68 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY, |
| 69 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT); |
| 70 | + LOG.debug("Selected hash algorithm: {}", SELECTED_ALGORITHM); |
| 71 | + SELECTED_LENGTH = |
| 72 | + conf.getInt(CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY, |
| 73 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT); |
| 74 | + LOG.debug("Selected hash key length: {}", SELECTED_LENGTH); |
| 75 | + } |
75 | 76 |
|
76 | | - /** |
77 | | - * Returns the currently selected cryptographic algorithm. |
78 | | - * |
79 | | - * @return the name of the selected algorithm |
80 | | - */ |
81 | | - public static synchronized String getSelectedAlgorithm() { |
82 | | - return SELECTED_ALGORITHM; |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * Returns the currently selected key length in bits. |
87 | | - * |
88 | | - * @return the selected key length |
89 | | - */ |
90 | | - public static synchronized int getSelectedLength() { |
91 | | - return SELECTED_LENGTH; |
92 | | - } |
| 77 | + /** |
| 78 | + * Returns the currently selected cryptographic algorithm. |
| 79 | + * |
| 80 | + * @return the name of the selected algorithm |
| 81 | + */ |
| 82 | + public static synchronized String getSelectedAlgorithm() { |
| 83 | + return SELECTED_ALGORITHM; |
| 84 | + } |
93 | 85 |
|
| 86 | + /** |
| 87 | + * Returns the currently selected key length in bits. |
| 88 | + * |
| 89 | + * @return the selected key length |
| 90 | + */ |
| 91 | + public static synchronized int getSelectedLength() { |
| 92 | + return SELECTED_LENGTH; |
| 93 | + } |
94 | 94 |
|
95 | | - /** |
96 | | - * Creates a new {@link KeyGenerator} instance configured with the currently selected |
97 | | - * algorithm and key length. |
98 | | - * |
99 | | - * @return a new {@code KeyGenerator} instance |
100 | | - * @throws IllegalArgumentException if the specified algorithm is not available |
101 | | - */ |
102 | | - public static synchronized KeyGenerator createKeyGenerator() { |
103 | | - LOG.debug("Creating key generator instance {}, {}", SELECTED_ALGORITHM, SELECTED_LENGTH); |
104 | | - INITIALIZED = true; |
105 | | - try { |
106 | | - KeyGenerator keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM); |
107 | | - keyGen.init(SELECTED_LENGTH); |
108 | | - return keyGen; |
109 | | - } catch (NoSuchAlgorithmException nsa) { |
110 | | - throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
111 | | - } |
| 95 | + /** |
| 96 | + * Creates a new {@link KeyGenerator} instance configured with the currently selected |
| 97 | + * algorithm and key length. |
| 98 | + * |
| 99 | + * @return a new {@code KeyGenerator} instance |
| 100 | + * @throws IllegalArgumentException if the specified algorithm is not available |
| 101 | + */ |
| 102 | + public static synchronized KeyGenerator createKeyGenerator() { |
| 103 | + LOG.debug("Creating key generator instance {}, {}", SELECTED_ALGORITHM, SELECTED_LENGTH); |
| 104 | + INITIALIZED = true; |
| 105 | + try { |
| 106 | + KeyGenerator keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM); |
| 107 | + keyGen.init(SELECTED_LENGTH); |
| 108 | + return keyGen; |
| 109 | + } catch (NoSuchAlgorithmException nsa) { |
| 110 | + throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
112 | 111 | } |
| 112 | + } |
113 | 113 |
|
114 | | - /** |
115 | | - * Creates a new {@link Mac} instance using the currently selected algorithm. |
116 | | - * |
117 | | - * @return a new {@code Mac} instance |
118 | | - * @throws IllegalArgumentException if the specified algorithm is not available |
119 | | - */ |
120 | | - public static synchronized Mac createMac() { |
121 | | - LOG.debug("Creating mac instance {}", SELECTED_ALGORITHM); |
122 | | - INITIALIZED = true; |
123 | | - try { |
124 | | - return Mac.getInstance(SELECTED_ALGORITHM); |
125 | | - } catch (NoSuchAlgorithmException nsa) { |
126 | | - throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
127 | | - } |
| 114 | + /** |
| 115 | + * Creates a new {@link Mac} instance using the currently selected algorithm. |
| 116 | + * |
| 117 | + * @return a new {@code Mac} instance |
| 118 | + * @throws IllegalArgumentException if the specified algorithm is not available |
| 119 | + */ |
| 120 | + public static synchronized Mac createMac() { |
| 121 | + LOG.debug("Creating mac instance {}", SELECTED_ALGORITHM); |
| 122 | + INITIALIZED = true; |
| 123 | + try { |
| 124 | + return Mac.getInstance(SELECTED_ALGORITHM); |
| 125 | + } catch (NoSuchAlgorithmException nsa) { |
| 126 | + throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
128 | 127 | } |
| 128 | + } |
129 | 129 | } |
0 commit comments