emiliosetiadarma commented on code in PR #7238:
URL: https://github.com/apache/nifi/pull/7238#discussion_r1194421748


##########
nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderTest.java:
##########
@@ -14,311 +14,298 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.nifi.security.util.crypto
+package org.apache.nifi.security.util.crypto;
 
-import org.apache.commons.codec.binary.Hex
-import org.apache.nifi.security.util.EncryptionMethod
-import org.bouncycastle.jce.provider.BouncyCastleProvider
-import org.junit.jupiter.api.BeforeAll
-import org.junit.jupiter.api.Test
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.nifi.security.util.EncryptionMethod;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 
-import javax.crypto.Cipher
-import javax.crypto.SecretKey
-import javax.crypto.spec.SecretKeySpec
-import java.security.SecureRandom
-import java.security.Security
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
 
-import static org.junit.jupiter.api.Assertions.assertEquals
-import static org.junit.jupiter.api.Assertions.assertFalse
-import static org.junit.jupiter.api.Assertions.assertThrows
-import static org.junit.jupiter.api.Assertions.assertTrue
-import static org.junit.jupiter.api.Assumptions.assumeTrue
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
-class AESKeyedCipherProviderGroovyTest {
-    private static final Logger logger = 
LoggerFactory.getLogger(AESKeyedCipherProviderGroovyTest.class)
+public class AESKeyedCipherProviderTest {
+    private static final String KEY_HEX = "0123456789ABCDEFFEDCBA9876543210";
 
-    private static final String KEY_HEX = "0123456789ABCDEFFEDCBA9876543210"
+    private static final String PLAINTEXT = "ExactBlockSizeRequiredForProcess";
 
-    private static final String PLAINTEXT = "ExactBlockSizeRequiredForProcess"
+    private static final List<EncryptionMethod> keyedEncryptionMethods = 
Arrays.stream(EncryptionMethod.values())
+            .filter(EncryptionMethod::isKeyedCipher)
+            .collect(Collectors.toList());
 
-    private static final List<EncryptionMethod> keyedEncryptionMethods = 
EncryptionMethod.values().findAll { it.keyedCipher }
+    private static SecretKey key;
 
-    private static final SecretKey key = new 
SecretKeySpec(Hex.decodeHex(KEY_HEX as char[]), "AES")
 
     @BeforeAll
     static void setUpOnce() throws Exception {
-        Security.addProvider(new BouncyCastleProvider())
+        Security.addProvider(new BouncyCastleProvider());
 
-        logger.metaClass.methodMissing = { String name, args ->
-            logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
+        try {
+            key = new SecretKeySpec(Hex.decodeHex(KEY_HEX.toCharArray()), 
"AES");
+        } catch (final DecoderException e) {
+            throw new RuntimeException(e);
         }
     }
 
     private static boolean isUnlimitedStrengthCryptoAvailable() {
-        Cipher.getMaxAllowedKeyLength("AES") > 128
+        try {
+            return Cipher.getMaxAllowedKeyLength("AES") > 128;
+        } catch (final NoSuchAlgorithmException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Test
     void testGetCipherShouldBeInternallyConsistent() throws Exception {
         // Arrange
-        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider()
+        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider();
 
         // Act
         for (EncryptionMethod em : keyedEncryptionMethods) {
-            logger.info("Using algorithm: ${em.getAlgorithm()}")
-
             // Initialize a cipher for encryption
-            Cipher cipher = cipherProvider.getCipher(em, key, true)
-            byte[] iv = cipher.getIV()
-            logger.info("IV: ${Hex.encodeHexString(iv)}")
+            Cipher cipher = cipherProvider.getCipher(em, key, true);
+            byte[] iv = cipher.getIV();
 
-            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"))
-            logger.info("Cipher text: ${Hex.encodeHexString(cipherBytes)} 
${cipherBytes.length}")
+            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"));
 
-            cipher = cipherProvider.getCipher(em, key, iv, false)
-            byte[] recoveredBytes = cipher.doFinal(cipherBytes)
-            String recovered = new String(recoveredBytes, "UTF-8")
-            logger.info("Recovered: ${recovered}")
+            cipher = cipherProvider.getCipher(em, key, iv, false);
+            byte[] recoveredBytes = cipher.doFinal(cipherBytes);
+            String recovered = new String(recoveredBytes, "UTF-8");
 
             // Assert
-            assertEquals(PLAINTEXT, recovered)
+            assertEquals(PLAINTEXT, recovered);
         }
     }
 
     @Test
     void testGetCipherWithExternalIVShouldBeInternallyConsistent() throws 
Exception {
         // Arrange
-        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider()
+        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider();
 
         // Act
-        keyedEncryptionMethods.each { EncryptionMethod em ->
-            logger.info("Using algorithm: ${em.getAlgorithm()}")
-            byte[] iv = cipherProvider.generateIV()
-            logger.info("IV: ${Hex.encodeHexString(iv)}")
+        for (final EncryptionMethod em : keyedEncryptionMethods) {
+            byte[] iv = cipherProvider.generateIV();
 
             // Initialize a cipher for encryption
-            Cipher cipher = cipherProvider.getCipher(em, key, iv, true)
+            Cipher cipher = cipherProvider.getCipher(em, key, iv, true);
 
-            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"))
-            logger.info("Cipher text: ${Hex.encodeHexString(cipherBytes)} 
${cipherBytes.length}")
+            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"));
 
-            cipher = cipherProvider.getCipher(em, key, iv, false)
-            byte[] recoveredBytes = cipher.doFinal(cipherBytes)
-            String recovered = new String(recoveredBytes, "UTF-8")
-            logger.info("Recovered: ${recovered}")
+            cipher = cipherProvider.getCipher(em, key, iv, false);
+            byte[] recoveredBytes = cipher.doFinal(cipherBytes);
+            String recovered = new String(recoveredBytes, "UTF-8");
 
             // Assert
-            assertEquals(PLAINTEXT, recovered)
+            assertEquals(PLAINTEXT, recovered);
         }
     }
 
     @Test
     void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() 
throws Exception {
         // Arrange
-        assumeTrue(isUnlimitedStrengthCryptoAvailable(), "Test is being 
skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy 
file.")
+        assumeTrue(isUnlimitedStrengthCryptoAvailable(), "Test is being 
skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy 
file.");

Review Comment:
   Remove it



##########
nifi-commons/nifi-security-utils/src/test/java/org/apache/nifi/security/util/crypto/AESKeyedCipherProviderTest.java:
##########
@@ -14,311 +14,298 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.nifi.security.util.crypto
+package org.apache.nifi.security.util.crypto;
 
-import org.apache.commons.codec.binary.Hex
-import org.apache.nifi.security.util.EncryptionMethod
-import org.bouncycastle.jce.provider.BouncyCastleProvider
-import org.junit.jupiter.api.BeforeAll
-import org.junit.jupiter.api.Test
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.nifi.security.util.EncryptionMethod;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 
-import javax.crypto.Cipher
-import javax.crypto.SecretKey
-import javax.crypto.spec.SecretKeySpec
-import java.security.SecureRandom
-import java.security.Security
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
 
-import static org.junit.jupiter.api.Assertions.assertEquals
-import static org.junit.jupiter.api.Assertions.assertFalse
-import static org.junit.jupiter.api.Assertions.assertThrows
-import static org.junit.jupiter.api.Assertions.assertTrue
-import static org.junit.jupiter.api.Assumptions.assumeTrue
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
-class AESKeyedCipherProviderGroovyTest {
-    private static final Logger logger = 
LoggerFactory.getLogger(AESKeyedCipherProviderGroovyTest.class)
+public class AESKeyedCipherProviderTest {
+    private static final String KEY_HEX = "0123456789ABCDEFFEDCBA9876543210";
 
-    private static final String KEY_HEX = "0123456789ABCDEFFEDCBA9876543210"
+    private static final String PLAINTEXT = "ExactBlockSizeRequiredForProcess";
 
-    private static final String PLAINTEXT = "ExactBlockSizeRequiredForProcess"
+    private static final List<EncryptionMethod> keyedEncryptionMethods = 
Arrays.stream(EncryptionMethod.values())
+            .filter(EncryptionMethod::isKeyedCipher)
+            .collect(Collectors.toList());
 
-    private static final List<EncryptionMethod> keyedEncryptionMethods = 
EncryptionMethod.values().findAll { it.keyedCipher }
+    private static SecretKey key;
 
-    private static final SecretKey key = new 
SecretKeySpec(Hex.decodeHex(KEY_HEX as char[]), "AES")
 
     @BeforeAll
     static void setUpOnce() throws Exception {
-        Security.addProvider(new BouncyCastleProvider())
+        Security.addProvider(new BouncyCastleProvider());
 
-        logger.metaClass.methodMissing = { String name, args ->
-            logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
+        try {
+            key = new SecretKeySpec(Hex.decodeHex(KEY_HEX.toCharArray()), 
"AES");
+        } catch (final DecoderException e) {
+            throw new RuntimeException(e);
         }
     }
 
     private static boolean isUnlimitedStrengthCryptoAvailable() {
-        Cipher.getMaxAllowedKeyLength("AES") > 128
+        try {
+            return Cipher.getMaxAllowedKeyLength("AES") > 128;
+        } catch (final NoSuchAlgorithmException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Test
     void testGetCipherShouldBeInternallyConsistent() throws Exception {
         // Arrange
-        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider()
+        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider();
 
         // Act
         for (EncryptionMethod em : keyedEncryptionMethods) {
-            logger.info("Using algorithm: ${em.getAlgorithm()}")
-
             // Initialize a cipher for encryption
-            Cipher cipher = cipherProvider.getCipher(em, key, true)
-            byte[] iv = cipher.getIV()
-            logger.info("IV: ${Hex.encodeHexString(iv)}")
+            Cipher cipher = cipherProvider.getCipher(em, key, true);
+            byte[] iv = cipher.getIV();
 
-            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"))
-            logger.info("Cipher text: ${Hex.encodeHexString(cipherBytes)} 
${cipherBytes.length}")
+            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"));
 
-            cipher = cipherProvider.getCipher(em, key, iv, false)
-            byte[] recoveredBytes = cipher.doFinal(cipherBytes)
-            String recovered = new String(recoveredBytes, "UTF-8")
-            logger.info("Recovered: ${recovered}")
+            cipher = cipherProvider.getCipher(em, key, iv, false);
+            byte[] recoveredBytes = cipher.doFinal(cipherBytes);
+            String recovered = new String(recoveredBytes, "UTF-8");
 
             // Assert
-            assertEquals(PLAINTEXT, recovered)
+            assertEquals(PLAINTEXT, recovered);
         }
     }
 
     @Test
     void testGetCipherWithExternalIVShouldBeInternallyConsistent() throws 
Exception {
         // Arrange
-        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider()
+        KeyedCipherProvider cipherProvider = new AESKeyedCipherProvider();
 
         // Act
-        keyedEncryptionMethods.each { EncryptionMethod em ->
-            logger.info("Using algorithm: ${em.getAlgorithm()}")
-            byte[] iv = cipherProvider.generateIV()
-            logger.info("IV: ${Hex.encodeHexString(iv)}")
+        for (final EncryptionMethod em : keyedEncryptionMethods) {
+            byte[] iv = cipherProvider.generateIV();
 
             // Initialize a cipher for encryption
-            Cipher cipher = cipherProvider.getCipher(em, key, iv, true)
+            Cipher cipher = cipherProvider.getCipher(em, key, iv, true);
 
-            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"))
-            logger.info("Cipher text: ${Hex.encodeHexString(cipherBytes)} 
${cipherBytes.length}")
+            byte[] cipherBytes = cipher.doFinal(PLAINTEXT.getBytes("UTF-8"));
 
-            cipher = cipherProvider.getCipher(em, key, iv, false)
-            byte[] recoveredBytes = cipher.doFinal(cipherBytes)
-            String recovered = new String(recoveredBytes, "UTF-8")
-            logger.info("Recovered: ${recovered}")
+            cipher = cipherProvider.getCipher(em, key, iv, false);
+            byte[] recoveredBytes = cipher.doFinal(cipherBytes);
+            String recovered = new String(recoveredBytes, "UTF-8");
 
             // Assert
-            assertEquals(PLAINTEXT, recovered)
+            assertEquals(PLAINTEXT, recovered);
         }
     }
 
     @Test
     void testGetCipherWithUnlimitedStrengthShouldBeInternallyConsistent() 
throws Exception {
         // Arrange
-        assumeTrue(isUnlimitedStrengthCryptoAvailable(), "Test is being 
skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy 
file.")
+        assumeTrue(isUnlimitedStrengthCryptoAvailable(), "Test is being 
skipped due to this JVM lacking JCE Unlimited Strength Jurisdiction Policy 
file.");

Review Comment:
   Removed it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to