|
| 1 | + |
| 2 | +package org.apache.hadoop.security.token; |
| 3 | + |
| 4 | +import org.apache.hadoop.conf.Configuration; |
| 5 | +import org.apache.hadoop.fs.CommonConfigurationKeysPublic; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import javax.crypto.KeyGenerator; |
| 10 | +import javax.crypto.Mac; |
| 11 | +import javax.crypto.SecretKey; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides configuration and utility methods for managing cryptographic key generation |
| 16 | + * and message authentication code (MAC) generation using specified algorithms and key lengths. |
| 17 | + * <p> |
| 18 | + * This class supports static access to the selected cryptographic algorithm and key length, |
| 19 | + * and provides methods to create configured {@link javax.crypto.KeyGenerator} and {@link javax.crypto.Mac} instances. |
| 20 | + * The configuration is initialized statically from a provided {@link Configuration} object. |
| 21 | + * <p> |
| 22 | + * The {@link SecretManager} has some static method, so static configuration is required |
| 23 | + */ |
| 24 | +public class SecretManagerConfig { |
| 25 | + private static final Logger LOG = LoggerFactory.getLogger(SecretManagerConfig.class); |
| 26 | + private static String SELECTED_ALGORITHM; |
| 27 | + private static int SELECTED_LENGTH; |
| 28 | + |
| 29 | + static { |
| 30 | + update(new Configuration()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Updates the selected cryptographic algorithm and key length using the provided |
| 35 | + * Hadoop {@link Configuration}. This method reads the values for |
| 36 | + * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and |
| 37 | + * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default values if not set. |
| 38 | + * |
| 39 | + * @param conf the configuration object containing cryptographic settings |
| 40 | + */ |
| 41 | + public static void update(Configuration conf) { |
| 42 | + String algorithm = conf.get( |
| 43 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY, |
| 44 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_DEFAULT); |
| 45 | + LOG.debug("Selected hash algorithm: {}", algorithm); |
| 46 | + SELECTED_ALGORITHM = algorithm; |
| 47 | + int length = conf.getInt( |
| 48 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY, |
| 49 | + CommonConfigurationKeysPublic.HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_DEFAULT); |
| 50 | + LOG.debug("Selected hash key length: {}", length); |
| 51 | + SELECTED_LENGTH = length; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the currently selected cryptographic algorithm. |
| 56 | + * |
| 57 | + * @return the name of the selected algorithm |
| 58 | + */ |
| 59 | + public static String getSelectedAlgorithm() { |
| 60 | + return SELECTED_ALGORITHM; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the currently selected key length in bits. |
| 65 | + * |
| 66 | + * @return the selected key length |
| 67 | + */ |
| 68 | + public static int getSelectedLength() { |
| 69 | + return SELECTED_LENGTH; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Sets the cryptographic algorithm to use. |
| 74 | + * |
| 75 | + * @param algorithm the algorithm name (e.g., "HmacSHA256", "AES") |
| 76 | + */ |
| 77 | + public static void setSelectedAlgorithm(String algorithm) { |
| 78 | + SELECTED_ALGORITHM = algorithm; |
| 79 | + LOG.debug("Selected hash algorithm set to {}", algorithm); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the cryptographic key length to use (in bits). |
| 85 | + * |
| 86 | + * @param length the key length |
| 87 | + */ |
| 88 | + public static void setSelectedLength(int length) { |
| 89 | + SELECTED_LENGTH = length; |
| 90 | + LOG.debug("Selected hash key length set to{}", length); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Creates a new {@link KeyGenerator} instance configured with the currently selected |
| 96 | + * algorithm and key length. |
| 97 | + * |
| 98 | + * @return a new {@code KeyGenerator} instance |
| 99 | + * @throws IllegalArgumentException if the specified algorithm is not available |
| 100 | + */ |
| 101 | + public static KeyGenerator createKeyGenerator() { |
| 102 | + LOG.debug("Creating key generator instance {}, {}", SELECTED_ALGORITHM, SELECTED_LENGTH); |
| 103 | + try { |
| 104 | + KeyGenerator keyGen = KeyGenerator.getInstance(SELECTED_ALGORITHM); |
| 105 | + keyGen.init(SELECTED_LENGTH); |
| 106 | + return keyGen; |
| 107 | + } catch (NoSuchAlgorithmException nsa) { |
| 108 | + throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a new {@link Mac} instance using the currently selected algorithm. |
| 114 | + * |
| 115 | + * @return a new {@code Mac} instance |
| 116 | + * @throws IllegalArgumentException if the specified algorithm is not available |
| 117 | + */ |
| 118 | + public static Mac createMac() { |
| 119 | + LOG.debug("Creating mac instance {}", SELECTED_ALGORITHM); |
| 120 | + try { |
| 121 | + return Mac.getInstance(SELECTED_ALGORITHM); |
| 122 | + } catch (NoSuchAlgorithmException nsa) { |
| 123 | + throw new IllegalArgumentException("Can't find " + SELECTED_ALGORITHM, nsa); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments