From aa9126e2bc323340acf8a1edae2fdead589ca2f8 Mon Sep 17 00:00:00 2001 From: saadsheralam Date: Sun, 17 Nov 2024 00:21:27 -0600 Subject: [PATCH] Fix NPE in KeyProviderCryptoExtension --- .../key/KeyProviderCryptoExtension.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyProviderCryptoExtension.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyProviderCryptoExtension.java index f1bb314582038..b7014a7e6e7da 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyProviderCryptoExtension.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyProviderCryptoExtension.java @@ -292,15 +292,20 @@ public EncryptedKeyVersion generateEncryptedKey(String encryptionKeyName) // Generate random bytes for new key and IV CryptoCodec cc = CryptoCodec.getInstance(keyProvider.getConf()); - try { - final byte[] newKey = new byte[encryptionKey.getMaterial().length]; - cc.generateSecureRandom(newKey); - final byte[] iv = new byte[cc.getCipherSuite().getAlgorithmBlockSize()]; - cc.generateSecureRandom(iv); - Encryptor encryptor = cc.createEncryptor(); - return generateEncryptedKey(encryptor, encryptionKey, newKey, iv); - } finally { - cc.close(); + + if (cc != null){ + try { + final byte[] newKey = new byte[encryptionKey.getMaterial().length]; + cc.generateSecureRandom(newKey); + final byte[] iv = new byte[cc.getCipherSuite().getAlgorithmBlockSize()]; + cc.generateSecureRandom(iv); + Encryptor encryptor = cc.createEncryptor(); + return generateEncryptedKey(encryptor, encryptionKey, newKey, iv); + } finally { + cc.close(); + } + } else { + throw new IOException("CyrptoCode object is null"); } }