You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by fm...@apache.org on 2023/12/13 13:27:27 UTC

(camel) branch main updated (096329ddfa8 -> 9d66b01c1c3)

This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


    from 096329ddfa8 CAMEL-20232: camel-core - Kamelets with Enrich and PollEnrich dynamic endpoints with template parameters (#12443)
     new 805c3829630 CAMEL-20231: make generators configurable
     new 9d66b01c1c3 Update jasypt docs

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 components/camel-jasypt/src/main/docs/jasypt.adoc  | 16 +++++++++++++
 .../component/jasypt/JasyptPropertiesParser.java   | 21 +++++++++++++++++
 .../org/apache/camel/component/jasypt/Main.java    | 22 ++++++++++++++++++
 .../jasypt/JasyptPropertiesParserTest.java         | 21 ++++++++---------
 .../jasypt/JasytPropertiesParserCustomAlgTest.java | 26 ++++++++++++++++------
 5 files changed, 89 insertions(+), 17 deletions(-)
 copy core/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithHeaderAnnotation.java => components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java (50%)


(camel) 01/02: CAMEL-20231: make generators configurable

Posted by fm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 805c3829630a5630d6f82a137fd08a8ebb2cc456
Author: Croway <fe...@gmail.com>
AuthorDate: Wed Dec 13 12:07:07 2023 +0100

    CAMEL-20231: make generators configurable
---
 .../component/jasypt/JasyptPropertiesParser.java   | 21 ++++++++++++
 .../org/apache/camel/component/jasypt/Main.java    | 28 +++++++++++++++
 .../jasypt/JasyptPropertiesParserTest.java         | 21 ++++++------
 .../jasypt/JasytPropertiesParserCustomAlgTest.java | 40 ++++++++++++++++++++++
 4 files changed, 100 insertions(+), 10 deletions(-)

diff --git a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
index 87152432528..0dc7ddbec66 100644
--- a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
+++ b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
@@ -25,6 +25,8 @@ import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.jasypt.encryption.StringEncryptor;
 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
 
 /**
  * A {@link org.apache.camel.component.properties.PropertiesParser} which is using
@@ -44,6 +46,8 @@ public class JasyptPropertiesParser extends DefaultPropertiesParser {
     private StringEncryptor encryptor;
     private String password;
     private String algorithm;
+    private String randomSaltGeneratorAlgorithm;
+    private String randomIvGeneratorAlgorithm;
 
     public JasyptPropertiesParser() {
     }
@@ -69,6 +73,7 @@ public class JasyptPropertiesParser extends DefaultPropertiesParser {
         if (encryptor == null) {
             StringHelper.notEmpty("password", password);
             StandardPBEStringEncryptor pbeStringEncryptor = new StandardPBEStringEncryptor();
+
             pbeStringEncryptor.setPassword(password);
             if (algorithm != null) {
                 pbeStringEncryptor.setAlgorithm(algorithm);
@@ -76,6 +81,14 @@ public class JasyptPropertiesParser extends DefaultPropertiesParser {
             } else {
                 log.debug("Initialized encryptor using default algorithm and provided password");
             }
+
+            if (randomSaltGeneratorAlgorithm != null) {
+                pbeStringEncryptor.setSaltGenerator(new RandomSaltGenerator(randomSaltGeneratorAlgorithm));
+            }
+            if (randomIvGeneratorAlgorithm != null) {
+                pbeStringEncryptor.setIvGenerator(new RandomIvGenerator(randomIvGeneratorAlgorithm));
+            }
+
             encryptor = pbeStringEncryptor;
         }
     }
@@ -88,6 +101,14 @@ public class JasyptPropertiesParser extends DefaultPropertiesParser {
         this.algorithm = algorithm;
     }
 
+    public void setRandomSaltGeneratorAlgorithm(String randomSaltGeneratorAlgorithm) {
+        this.randomSaltGeneratorAlgorithm = randomSaltGeneratorAlgorithm;
+    }
+
+    public void setRandomIvGeneratorAlgorithm(String randomIvGeneratorAlgorithm) {
+        this.randomIvGeneratorAlgorithm = randomIvGeneratorAlgorithm;
+    }
+
     public void setPassword(String password) {
         // lookup password as either environment or JVM system property
         if (password.startsWith("sysenv:")) {
diff --git a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
index e165e4d5dc1..15a89d414ac 100644
--- a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
+++ b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
@@ -22,6 +22,8 @@ import java.util.LinkedList;
 import java.util.List;
 
 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
 
 public class Main {
 
@@ -31,6 +33,8 @@ public class Main {
     private String password;
     private String input;
     private String algorithm;
+    private String randomSaltGeneratorAlgorithm;
+    private String randomIvGeneratorAlgorithm;
 
     private abstract class Option {
         private String abbreviation;
@@ -134,6 +138,24 @@ public class Main {
                 algorithm = parameter;
             }
         });
+
+        addOption(new ParameterOption("rsga", "salt", "Optional random salt generator algorithm to use", "salt") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                randomSaltGeneratorAlgorithm = parameter;
+            }
+        });
+
+        addOption(new ParameterOption("riga", "iv", "Optional random iv generator algorithm to use", "iv") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                randomIvGeneratorAlgorithm = parameter;
+            }
+        });
+
+        addOption(new ParameterOption("a", "algorithm", "Optional algorithm to use", "algorithm") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                algorithm = parameter;
+            }
+        });
     }
 
     private void addOption(Option option) {
@@ -204,6 +226,12 @@ public class Main {
         if (algorithm != null) {
             encryptor.setAlgorithm(algorithm);
         }
+        if (randomSaltGeneratorAlgorithm != null) {
+            encryptor.setSaltGenerator(new RandomSaltGenerator(randomSaltGeneratorAlgorithm));
+        }
+        if (randomIvGeneratorAlgorithm != null) {
+            encryptor.setIvGenerator(new RandomIvGenerator(randomIvGeneratorAlgorithm));
+        }
         if ("encrypt".equals(command)) {
             System.out.println("Encrypted text: " + encryptor.encrypt(input));
         } else {
diff --git a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
index 1f2fe6a7060..c212eba7b00 100644
--- a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
+++ b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
@@ -31,16 +31,17 @@ public class JasyptPropertiesParserTest {
 
     private static final String KEY = "somekey";
 
-    private static final String KNOWN_PASSWORD = "secret";
-    private static final String KNOWN_ENCRYPTED = "ENC(bsW9uV37gQ0QHFu7KO03Ww==)";
-    private static final String KNOW_DECRYPTED = "tiger";
+    protected String knownPassword = "secret";
+    protected String knownEncrypted = "ENC(bsW9uV37gQ0QHFu7KO03Ww==)";
+    protected String knowDecrypted = "tiger";
 
-    private JasyptPropertiesParser jasyptPropertiesParser = new JasyptPropertiesParser();
-    private StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
+    protected JasyptPropertiesParser jasyptPropertiesParser = new JasyptPropertiesParser();
+    protected StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
 
     @BeforeEach
     public void before() {
-        encryptor.setPassword(KNOWN_PASSWORD);
+        encryptor.setPassword(knownPassword);
+
         jasyptPropertiesParser.setEncryptor(encryptor);
     }
 
@@ -95,19 +96,19 @@ public class JasyptPropertiesParserTest {
     @Test
     public void testUsesProvidedPasswordIfEncryptorIsNotSet() {
         jasyptPropertiesParser.setEncryptor(null);
-        jasyptPropertiesParser.setPassword(KNOWN_PASSWORD);
+        jasyptPropertiesParser.setPassword(knownPassword);
 
-        assertEquals(KNOW_DECRYPTED, jasyptPropertiesParser.parseProperty(KEY, KNOWN_ENCRYPTED, null));
+        assertEquals(knowDecrypted, jasyptPropertiesParser.parseProperty(KEY, knownEncrypted, null));
     }
 
     @Test
     public void testUsesProvidedPasswordFromSystemPropertyIfEncryptorIsNotSet() {
-        System.setProperty("myfoo", KNOWN_PASSWORD);
+        System.setProperty("myfoo", knownPassword);
 
         jasyptPropertiesParser.setEncryptor(null);
         jasyptPropertiesParser.setPassword("sys:myfoo");
 
-        assertEquals(KNOW_DECRYPTED, jasyptPropertiesParser.parseProperty(KEY, KNOWN_ENCRYPTED, null));
+        assertEquals(knowDecrypted, jasyptPropertiesParser.parseProperty(KEY, knownEncrypted, null));
 
         System.clearProperty("myfoo");
     }
diff --git a/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java
new file mode 100644
index 00000000000..aa050eb0f54
--- /dev/null
+++ b/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasytPropertiesParserCustomAlgTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jasypt;
+
+import org.jasypt.iv.RandomIvGenerator;
+import org.jasypt.salt.RandomSaltGenerator;
+import org.junit.jupiter.api.BeforeEach;
+
+public class JasytPropertiesParserCustomAlgTest extends JasyptPropertiesParserTest {
+
+	@BeforeEach
+	public void before() {
+		knowDecrypted = "tigertigertiger";
+		knownEncrypted = "ENC(LuCBTHaY1G6XHRwp63teshi/LbFRzpPtq5j8SNpJgv1yn9D25py+xHNGjXEMnf/J)";
+
+        encryptor.setAlgorithm("PBEWithHmacSHA256AndAES_256");
+		encryptor.setSaltGenerator(new RandomSaltGenerator("SHA1PRNG"));
+        encryptor.setIvGenerator(new RandomIvGenerator("SHA1PRNG"));
+		encryptor.setPassword(knownPassword);
+
+        jasyptPropertiesParser.setAlgorithm("PBEWithHmacSHA256AndAES_256");
+		jasyptPropertiesParser.setRandomSaltGeneratorAlgorithm("SHA1PRNG");
+        jasyptPropertiesParser.setRandomIvGeneratorAlgorithm("SHA1PRNG");
+		jasyptPropertiesParser.setEncryptor(encryptor);
+	}
+}


(camel) 02/02: Update jasypt docs

Posted by fm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9d66b01c1c3f94cccbaaca123af543c4cc378d2b
Author: Croway <fe...@gmail.com>
AuthorDate: Wed Dec 13 13:44:25 2023 +0100

    Update jasypt docs
---
 components/camel-jasypt/src/main/docs/jasypt.adoc        | 16 ++++++++++++++++
 .../java/org/apache/camel/component/jasypt/Main.java     |  6 ------
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/components/camel-jasypt/src/main/docs/jasypt.adoc b/components/camel-jasypt/src/main/docs/jasypt.adoc
index 508c57f47c6..bb51a37dfe9 100644
--- a/components/camel-jasypt/src/main/docs/jasypt.adoc
+++ b/components/camel-jasypt/src/main/docs/jasypt.adoc
@@ -62,6 +62,8 @@ Apache Camel Jasypt takes the following options
   -p or -password <password> = Password to use
   -i or -input <input> = Text to encrypt or decrypt
   -a or -algorithm <algorithm> = Optional algorithm to use
+  -rsga or -algorithm <algorithm> = Optional random salt generator algorithm to use
+  -riga or -algorithm <algorithm> = Optional random iv generator algorithm to use
 --------------------------------------------------------------
 
 For example to encrypt the value `tiger` you run with the following
@@ -171,6 +173,20 @@ The properties file `myproperties.properties` then contain the encrypted
 value, such as shown below. Notice how the password value is encrypted
 and the value has the tokens surrounding `ENC(value here)`
 
+[TIP]
+====
+It is possible to configure custom algorithms on the JasyptPropertiesParser like this
+
+[source,java]
+-----------------------------------------------------------------------------------------------------------
+JasyptPropertiesParser jasyptPropertiesParser = new JasyptPropertiesParser();
+
+jasyptPropertiesParser.setAlgorithm("PBEWithHmacSHA256AndAES_256");
+jasyptPropertiesParser.setRandomSaltGeneratorAlgorithm("PKCS11");
+jasyptPropertiesParser.setRandomIvGeneratorAlgorithm("PKCS11");
+-----------------------------------------------------------------------------------------------------------
+====
+
 == Example with Spring XML
 
 In Spring XML you need to configure the `JasyptPropertiesParser` which
diff --git a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
index 15a89d414ac..14c8a07a41f 100644
--- a/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
+++ b/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/Main.java
@@ -150,12 +150,6 @@ public class Main {
                 randomIvGeneratorAlgorithm = parameter;
             }
         });
-
-        addOption(new ParameterOption("a", "algorithm", "Optional algorithm to use", "algorithm") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                algorithm = parameter;
-            }
-        });
     }
 
     private void addOption(Option option) {