I want to integrate GCP key management service.I am using java-11 for my spring boot application.Project dependency:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-kms</artifactId>
<version>2.32.0</version>
</dependency>
application.properties:
spring.cloud.gcp.project-id=noottverk
spring.cloud.gcp.credentials.location=classpath:noottverk-0fc5045048a2.json
spring.cloud.gcp.kms.enabled=true
my service:
package org.noottverk.backend.services;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.protobuf.ByteString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class AesGcpKmsService {
private final String projectId;
private final String locationId;
private final String keyRingId;
private final String cryptoKeyId;
// Inject values from application.properties or any other configuration method
public AesGcpKmsService(@Value("${gcp.kms.projectId}") String projectId,
@Value("${gcp.kms.locationId}") String locationId,
@Value("${gcp.kms.keyRingId}") String keyRingId,
@Value("${gcp.kms.cryptoKeyId}") String cryptoKeyId) {
this.projectId = projectId;
this.locationId = locationId;
this.keyRingId = keyRingId;
this.cryptoKeyId = cryptoKeyId;
}
public byte[] encrypt(byte[] plaintext) throws Exception {
System.out.println(projectId+" "+locationId+" "+keyRingId+" "+cryptoKeyId+" "+ plaintext.toString());
// Create the KeyManagementServiceClient using try-with-resources to close it automatically
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// The CryptoKeyName represents the full path of the key
CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, cryptoKeyId);
System.out.println("cryptoKeyName: "+cryptoKeyName+ "plaintext: "+plaintext);
// Encrypt the plaintext
ByteString encrypted = client.encrypt(cryptoKeyName, ByteString.copyFrom(plaintext)).getCiphertext();
return encrypted.toByteArray();
}catch (Exception e){
System.out.println(e.getMessage());
throw new RuntimeException(e.getMessage());
}
}
public byte[] decrypt(byte[] ciphertext) throws Exception {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, cryptoKeyId);
// Decrypt the ciphertext
ByteString decrypted = client.decrypt(cryptoKeyName, ByteString.copyFrom(ciphertext)).getPlaintext();
return decrypted.toByteArray();
}
}
}
When i submit a form it creates error like nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.google.cloud.kms.v1.stub.GrpcKeyManagementServiceStub