Search in sources :

Example 1 with AlreadyExistsException

use of com.google.api.gax.rpc.AlreadyExistsException in project ranger by apache.

the class RangerGoogleCloudHSMProvider method generateMasterKey.

@Override
public boolean generateMasterKey(String unused_password) throws Throwable {
    // The ENCRYPT_DECRYPT key purpose enables symmetric encryption.
    // All keys with key purpose ENCRYPT_DECRYPT use the GOOGLE_SYMMETRIC_ENCRYPTION algorithm.
    // No parameters are used with this algorithm.
    CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setProtectionLevel(ProtectionLevel.HSM).setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).build();
    // Create the key.
    CryptoKey createdKey = null;
    try {
        createdKey = client.createCryptoKey(this.keyRingName, this.gcpMasterKeyName, key);
    } catch (Exception e) {
        if (e instanceof AlreadyExistsException) {
            logger.info("MasterKey with the name '" + this.gcpMasterKeyName + "' already exist.");
            return true;
        } else {
            throw new RuntimeCryptoException("Failed to create master key with name '" + this.gcpMasterKeyName + "', Error - " + e.getMessage());
        }
    }
    if (createdKey == null) {
        logger.info("Failed to create master key : " + this.gcpMasterKeyName);
        return false;
    }
    logger.info("Master Key Created Successfully On Google Cloud HSM : " + this.gcpMasterKeyName);
    return true;
}
Also used : RuntimeCryptoException(org.bouncycastle.crypto.RuntimeCryptoException) AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) CryptoKey(com.google.cloud.kms.v1.CryptoKey) AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) RuntimeCryptoException(org.bouncycastle.crypto.RuntimeCryptoException)

Aggregations

AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 CryptoKey (com.google.cloud.kms.v1.CryptoKey)1 RuntimeCryptoException (org.bouncycastle.crypto.RuntimeCryptoException)1