|
16 | 16 | */ |
17 | 17 | package org.apache.hadoop.crypto.key; |
18 | 18 |
|
19 | | -import com.google.api.gax.rpc.AlreadyExistsException; |
| 19 | +import com.google.api.gax.rpc.NotFoundException; |
20 | 20 | import com.google.cloud.kms.v1.CryptoKey; |
21 | 21 | import com.google.cloud.kms.v1.CryptoKey.CryptoKeyPurpose; |
22 | 22 | import com.google.cloud.kms.v1.CryptoKeyName; |
@@ -68,37 +68,34 @@ public RangerGoogleCloudHSMProvider(Configuration conf) throws Exception { |
68 | 68 |
|
69 | 69 | @Override |
70 | 70 | public boolean generateMasterKey(String unusedPassword) throws Throwable { |
71 | | - //The ENCRYPT_DECRYPT key purpose enables symmetric encryption. |
72 | | - //All keys with key purpose ENCRYPT_DECRYPT use the GOOGLE_SYMMETRIC_ENCRYPTION algorithm. |
73 | | - //No parameters are used with this algorithm. |
74 | | - CryptoKey key = CryptoKey.newBuilder() |
75 | | - .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT) |
76 | | - .setVersionTemplate(CryptoKeyVersionTemplate.newBuilder() |
77 | | - .setProtectionLevel(ProtectionLevel.HSM) |
78 | | - .setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)) |
79 | | - .build(); |
80 | | - |
81 | | - // Create the key. |
82 | | - CryptoKey createdKey = null; |
83 | | - try { |
84 | | - createdKey = client.createCryptoKey(this.keyRingName, this.gcpMasterKeyName, key); |
85 | | - } catch (Exception e) { |
86 | | - if (e instanceof AlreadyExistsException) { |
87 | | - logger.info("MasterKey with the name '{}' already exist.", this.gcpMasterKeyName); |
88 | | - return true; |
89 | | - } else { |
| 71 | + boolean isMKGenerated = false; |
| 72 | + if (!this.masterKeyExists()) { |
| 73 | + //The ENCRYPT_DECRYPT key purpose enables symmetric encryption. |
| 74 | + //All keys with key purpose ENCRYPT_DECRYPT use the GOOGLE_SYMMETRIC_ENCRYPTION algorithm. |
| 75 | + //No parameters are used with this algorithm. |
| 76 | + CryptoKey key = CryptoKey.newBuilder() |
| 77 | + .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT) |
| 78 | + .setVersionTemplate(CryptoKeyVersionTemplate.newBuilder() |
| 79 | + .setProtectionLevel(ProtectionLevel.HSM) |
| 80 | + .setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)) |
| 81 | + .build(); |
| 82 | + |
| 83 | + // Create the key. |
| 84 | + CryptoKey createdKey = null; |
| 85 | + try { |
| 86 | + createdKey = client.createCryptoKey(this.keyRingName, this.gcpMasterKeyName, key); |
| 87 | + } catch (Exception e) { |
90 | 88 | throw new RuntimeCryptoException("Failed to create master key with name '" + this.gcpMasterKeyName + "', Error - " + e.getMessage()); |
91 | 89 | } |
92 | | - } |
93 | 90 |
|
94 | | - if (createdKey == null) { |
95 | | - logger.info("Failed to create master key : {}", this.gcpMasterKeyName); |
96 | | - return false; |
| 91 | + if (createdKey != null) { |
| 92 | + logger.info("Master Key Created Successfully On Google Cloud HSM : {}", this.gcpMasterKeyName); |
| 93 | + isMKGenerated = true; |
| 94 | + } else { |
| 95 | + logger.info("Failed to create master key : {}", this.gcpMasterKeyName); |
| 96 | + } |
97 | 97 | } |
98 | | - |
99 | | - logger.info("Master Key Created Successfully On Google Cloud HSM : {}", this.gcpMasterKeyName); |
100 | | - |
101 | | - return true; |
| 98 | + return isMKGenerated; |
102 | 99 | } |
103 | 100 |
|
104 | 101 | @Override |
@@ -221,4 +218,26 @@ private static void updateEnv(String name, String val) throws ReflectiveOperatio |
221 | 218 |
|
222 | 219 | writeAbleEnvMap.put(name, val); |
223 | 220 | } |
| 221 | + |
| 222 | + private boolean masterKeyExists() throws Throwable { |
| 223 | + boolean exists = false; |
| 224 | + |
| 225 | + if (this.client == null) { |
| 226 | + throw new RuntimeCryptoException("Google Cloud KMS client is not initialized; call onInitialization() first."); |
| 227 | + } |
| 228 | + |
| 229 | + CryptoKeyName keyName = CryptoKeyName.of(this.gcpProjectId, this.gcpLocationId, this.gcpKeyRingId, this.gcpMasterKeyName); |
| 230 | + |
| 231 | + try { |
| 232 | + CryptoKey cryptoKey = this.client.getCryptoKey(keyName); |
| 233 | + logger.info("Ranger masterKey present with name: {}", cryptoKey.getName()); |
| 234 | + exists = true; |
| 235 | + } catch (NotFoundException e) { |
| 236 | + logger.info("Ranger masterKey not found with name: {}", keyName); |
| 237 | + } catch (Exception e) { |
| 238 | + logger.error("Error checking for masterkey: " + e.getMessage()); |
| 239 | + throw e; |
| 240 | + } |
| 241 | + return exists; |
| 242 | + } |
224 | 243 | } |
0 commit comments