Search in sources :

Example 1 with KeyEncryptionKey

use of org.apache.parquet.crypto.keytools.KeyToolkit.KeyEncryptionKey in project parquet-mr by apache.

the class FileKeyWrapper method getEncryptionKeyMetadata.

byte[] getEncryptionKeyMetadata(byte[] dataKey, String masterKeyID, boolean isFooterKey, String keyIdInFile) {
    if (null == kmsClient) {
        throw new ParquetCryptoRuntimeException("No KMS client available. See previous errors.");
    }
    String encodedKekID = null;
    String encodedWrappedKEK = null;
    String encodedWrappedDEK = null;
    if (!doubleWrapping) {
        encodedWrappedDEK = kmsClient.wrapKey(dataKey, masterKeyID);
    } else {
        // Find in cache, or generate KEK for Master Key ID
        KeyEncryptionKey keyEncryptionKey = KEKPerMasterKeyID.computeIfAbsent(masterKeyID, (k) -> createKeyEncryptionKey(masterKeyID));
        // Encrypt DEK with KEK
        byte[] AAD = keyEncryptionKey.getID();
        encodedWrappedDEK = KeyToolkit.encryptKeyLocally(dataKey, keyEncryptionKey.getBytes(), AAD);
        encodedKekID = keyEncryptionKey.getEncodedID();
        encodedWrappedKEK = keyEncryptionKey.getEncodedWrappedKEK();
    }
    boolean storeKeyMaterialInternally = (null == keyMaterialStore);
    String serializedKeyMaterial = KeyMaterial.createSerialized(isFooterKey, kmsInstanceID, kmsInstanceURL, masterKeyID, doubleWrapping, encodedKekID, encodedWrappedKEK, encodedWrappedDEK, storeKeyMaterialInternally);
    // Internal key material storage: key metadata and key material are the same
    if (storeKeyMaterialInternally) {
        return serializedKeyMaterial.getBytes(StandardCharsets.UTF_8);
    }
    // External key material storage: key metadata is a reference to a key in the material store
    if (null == keyIdInFile) {
        if (isFooterKey) {
            keyIdInFile = KeyMaterial.FOOTER_KEY_ID_IN_FILE;
        } else {
            keyIdInFile = KeyMaterial.COLUMN_KEY_ID_IN_FILE_PREFIX + keyCounter;
            keyCounter++;
        }
    }
    keyMaterialStore.addKeyMaterial(keyIdInFile, serializedKeyMaterial);
    String serializedKeyMetadata = KeyMetadata.createSerializedForExternalMaterial(keyIdInFile);
    return serializedKeyMetadata.getBytes(StandardCharsets.UTF_8);
}
Also used : ParquetCryptoRuntimeException(org.apache.parquet.crypto.ParquetCryptoRuntimeException) KeyEncryptionKey(org.apache.parquet.crypto.keytools.KeyToolkit.KeyEncryptionKey)

Aggregations

ParquetCryptoRuntimeException (org.apache.parquet.crypto.ParquetCryptoRuntimeException)1 KeyEncryptionKey (org.apache.parquet.crypto.keytools.KeyToolkit.KeyEncryptionKey)1