Skip to content

Commit 32bfe19

Browse files
authored
RANGER-5543:Ranger KMS GCP always try to create master key irrespective of its existence (#910)
1 parent 8e81392 commit 32bfe19

4 files changed

Lines changed: 61 additions & 28 deletions

File tree

distro/src/main/assembly/kms.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@
166166
<include>com.google.android:annotations</include>
167167
<include>io.grpc:grpc-alts</include>
168168
<include>io.grpc:grpc-grpclb</include>
169+
<include>com.google.protobuf:protobuf-java:jar:${gcp.protobuf-java.version}</include>
169170
<include>com.google.protobuf:protobuf-java-util:jar:${gcp.protobuf-java.version}</include>
171+
<include>com.google.guava:guava:jar:${google.guava.version}</include>
172+
<include>com.google.guava:failureaccess:jar:${google.failureaccess.version}</include>
170173
<include>org.conscrypt:conscrypt-openjdk-uber</include>
171174
<include>org.threeten:threetenbp</include>
172175
<include>io.grpc:grpc-auth</include>

kms/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@
103103
<artifactId>jsr305</artifactId>
104104
<version>${jsr305.version}</version>
105105
</dependency>
106+
<dependency>
107+
<groupId>com.google.guava</groupId>
108+
<artifactId>failureaccess</artifactId>
109+
<version>${google.failureaccess.version}</version>
110+
</dependency>
111+
<dependency>
112+
<groupId>com.google.guava</groupId>
113+
<artifactId>guava</artifactId>
114+
<version>${google.guava.version}</version>
115+
</dependency>
106116
<dependency>
107117
<groupId>com.google.protobuf</groupId>
108118
<artifactId>protobuf-java</artifactId>

kms/src/main/java/org/apache/hadoop/crypto/key/RangerGoogleCloudHSMProvider.java

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.apache.hadoop.crypto.key;
1818

19-
import com.google.api.gax.rpc.AlreadyExistsException;
19+
import com.google.api.gax.rpc.NotFoundException;
2020
import com.google.cloud.kms.v1.CryptoKey;
2121
import com.google.cloud.kms.v1.CryptoKey.CryptoKeyPurpose;
2222
import com.google.cloud.kms.v1.CryptoKeyName;
@@ -68,37 +68,34 @@ public RangerGoogleCloudHSMProvider(Configuration conf) throws Exception {
6868

6969
@Override
7070
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) {
9088
throw new RuntimeCryptoException("Failed to create master key with name '" + this.gcpMasterKeyName + "', Error - " + e.getMessage());
9189
}
92-
}
9390

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+
}
9797
}
98-
99-
logger.info("Master Key Created Successfully On Google Cloud HSM : {}", this.gcpMasterKeyName);
100-
101-
return true;
98+
return isMKGenerated;
10299
}
103100

104101
@Override
@@ -221,4 +218,26 @@ private static void updateEnv(String name, String val) throws ReflectiveOperatio
221218

222219
writeAbleEnvMap.put(name, val);
223220
}
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+
}
224243
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999

100100
<!-- GCP HSM -->
101101
<google.cloud.kms>2.3.0</google.cloud.kms>
102+
<google.failureaccess.version>1.0.3</google.failureaccess.version>
102103
<google.guava.version>33.4.8-jre</google.guava.version>
103104
<google.re2j.version>1.2</google.re2j.version>
104105
<googlecode.log4jdbc.version>1.2</googlecode.log4jdbc.version>

0 commit comments

Comments
 (0)