This is an automated email from the ASF dual-hosted git repository.
bbende pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new dfa015ec33 NIFI-13599 Replaced Commons Codec Hex with HexFormat for
Encryptor (#9125)
dfa015ec33 is described below
commit dfa015ec337acaf72b0d5390d6d8b7c01619e15d
Author: David Handermann <[email protected]>
AuthorDate: Tue Jul 30 12:23:56 2024 -0500
NIFI-13599 Replaced Commons Codec Hex with HexFormat for Encryptor (#9125)
- Removed commons-codec dependency from nifi-property-encryptor
---
nifi-commons/nifi-property-encryptor/pom.xml | 4 ----
.../apache/nifi/encrypt/CipherPropertyEncryptor.java | 12 ++++++------
.../nifi/encrypt/KeyedCipherPropertyEncryptorTest.java | 17 ++++++++++++-----
3 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/nifi-commons/nifi-property-encryptor/pom.xml
b/nifi-commons/nifi-property-encryptor/pom.xml
index 3827317a86..cf316bd30c 100644
--- a/nifi-commons/nifi-property-encryptor/pom.xml
+++ b/nifi-commons/nifi-property-encryptor/pom.xml
@@ -31,9 +31,5 @@
<artifactId>nifi-security-crypto-key</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- </dependency>
</dependencies>
</project>
diff --git
a/nifi-commons/nifi-property-encryptor/src/main/java/org/apache/nifi/encrypt/CipherPropertyEncryptor.java
b/nifi-commons/nifi-property-encryptor/src/main/java/org/apache/nifi/encrypt/CipherPropertyEncryptor.java
index 2c5fa6c5eb..e5ecd0583d 100644
---
a/nifi-commons/nifi-property-encryptor/src/main/java/org/apache/nifi/encrypt/CipherPropertyEncryptor.java
+++
b/nifi-commons/nifi-property-encryptor/src/main/java/org/apache/nifi/encrypt/CipherPropertyEncryptor.java
@@ -16,14 +16,12 @@
*/
package org.apache.nifi.encrypt;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
-
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.HexFormat;
/**
* Cipher Property Encryptor provides hexadecimal encoding and decoding around
cipher operations
@@ -31,6 +29,8 @@ import java.nio.charset.StandardCharsets;
abstract class CipherPropertyEncryptor implements PropertyEncryptor {
private static final Charset PROPERTY_CHARSET = StandardCharsets.UTF_8;
+ private static final HexFormat HEX_FORMAT = HexFormat.of();
+
/**
* Encrypt property and encode as a hexadecimal string
*
@@ -45,7 +45,7 @@ abstract class CipherPropertyEncryptor implements
PropertyEncryptor {
final Cipher cipher = getEncryptionCipher(encodedParameters);
try {
final byte[] encrypted = cipher.doFinal(binary);
- return
Hex.encodeHexString(getConcatenatedBinary(encodedParameters, encrypted));
+ return
HEX_FORMAT.formatHex(getConcatenatedBinary(encodedParameters, encrypted));
} catch (final BadPaddingException | IllegalBlockSizeException e) {
final String message = String.format("Encryption Failed with
Algorithm [%s]", cipher.getAlgorithm());
throw new EncryptionException(message, e);
@@ -74,8 +74,8 @@ abstract class CipherPropertyEncryptor implements
PropertyEncryptor {
private byte[] getDecodedBinary(final String encryptedProperty) {
try {
- return Hex.decodeHex(encryptedProperty);
- } catch (final DecoderException e) {
+ return HEX_FORMAT.parseHex(encryptedProperty);
+ } catch (final IllegalArgumentException e) {
throw new EncryptionException("Hexadecimal decoding failed", e);
}
}
diff --git
a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java
b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java
index a0aa6d979f..9e6344fbcc 100644
---
a/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java
+++
b/nifi-commons/nifi-property-encryptor/src/test/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptorTest.java
@@ -16,8 +16,6 @@
*/
package org.apache.nifi.encrypt;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -25,8 +23,10 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.HexFormat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -60,18 +60,25 @@ public class KeyedCipherPropertyEncryptorTest {
}
@Test
- public void testEncryptHexadecimalEncoded() throws DecoderException {
+ public void testEncryptHexadecimalEncoded() {
final String encrypted = encryptor.encrypt(PROPERTY);
- final byte[] decoded = Hex.decodeHex(encrypted);
+ final byte[] decoded = HexFormat.of().parseHex(encrypted);
assertEquals(ENCRYPTED_BINARY_LENGTH, decoded.length);
}
@Test
public void testDecryptEncryptionException() {
- final String encodedProperty =
Hex.encodeHexString(PROPERTY.getBytes(DEFAULT_CHARSET));
+ final String encodedProperty =
HexFormat.of().formatHex(PROPERTY.getBytes(DEFAULT_CHARSET));
assertThrows(Exception.class, () ->
encryptor.decrypt(encodedProperty));
}
+ @Test
+ public void testDecryptHexadecimalInvalid() {
+ final String invalidProperty = String.class.getName();
+ final EncryptionException exception =
assertThrows(EncryptionException.class, () ->
encryptor.decrypt(invalidProperty));
+ assertInstanceOf(IllegalArgumentException.class, exception.getCause());
+ }
+
@Test
public void testGetCipherEncryptionException() {
encryptor = new KeyedCipherPropertyEncryptor(INVALID_SECRET_KEY);