You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/09/04 15:37:34 UTC

svn commit: r992602 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/properties/ components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/ components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/

Author: davsclaus
Date: Sat Sep  4 13:37:33 2010
New Revision: 992602

URL: http://svn.apache.org/viewvc?rev=992602&view=rev
Log:
CAMEL-2987: Jasypt based PropertiesParser to plugin to the Properties component and have it be able to decrypt text on-the-fly.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java   (contents, props changed)
      - copied, changed from r992560, camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java
    camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
    camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java (from r992560, camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java&r1=992560&r2=992602&rev=992602&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java Sat Sep  4 13:37:33 2010
@@ -28,24 +28,11 @@ import org.apache.commons.logging.LogFac
  *
  * @version $Revision$
  */
-public final class PropertiesParser {
-    private static final transient Log LOG = LogFactory.getLog(PropertiesParser.class);
+public class DefaultPropertiesParser implements PropertiesParser {
+    protected final transient Log log = LogFactory.getLog(getClass());
     
-    private PropertiesParser() {
-    }
-
-    /**
-     * Parses the string and replaces the property placeholders with values from the given properties
-     *
-     * @param uri the uri
-     * @param properties the properties
-     * @param prefixToken the prefix token
-     * @param suffixToken the suffix token
-     * @return the uri with replaced placeholders
-     * @throws IllegalArgumentException if uri syntax is not valid or a property is not found
-     */
-    public static String parseUri(String uri, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException {
-        String answer = uri;
+    public String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException {
+        String answer = text;
         boolean done = false;
 
         // the placeholders can contain nested placeholders so we need to do recursive parsing
@@ -58,7 +45,7 @@ public final class PropertiesParser {
             // check the replaced with the visited to avoid circular reference
             for (String replace : replaced) {
                 if (visited.contains(replace)) {
-                    throw new IllegalArgumentException("Circular reference detected with key [" + replace + "] in uri " + uri);
+                    throw new IllegalArgumentException("Circular reference detected with key [" + replace + "] in uri " + text);
                 }
             }
             // okay all okay so add the replaced as visited
@@ -70,7 +57,11 @@ public final class PropertiesParser {
         return answer;
     }
 
-    private static String doParseUri(String uri, Properties properties, List<String> replaced, String prefixToken, String suffixToken) {
+    public String parsePropertyValue(String value) {
+        return value;
+    }
+
+    private String doParseUri(String uri, Properties properties, List<String> replaced, String prefixToken, String suffixToken) {
         StringBuilder sb = new StringBuilder();
 
         int pivot = 0;
@@ -102,23 +93,22 @@ public final class PropertiesParser {
         return sb.toString();
     }
 
-    private static String createConstantPart(String uri, int start, int end) {
-        return uri.substring(start, end);
+    private String createConstantPart(String uri, int start, int end) {
+        return parsePropertyValue(uri.substring(start, end));
     }
 
-    private static String createPlaceholderPart(String placeholderPart, Properties properties, List<String> replaced) {
-        String propertyValue;
-        
+    private String createPlaceholderPart(String placeholderPart, Properties properties, List<String> replaced) {
+        // keep track of which parts we have replaced
         replaced.add(placeholderPart);
         
-        propertyValue = System.getProperty(placeholderPart);
+        String propertyValue = System.getProperty(placeholderPart);
         if (propertyValue != null) {
-            LOG.info("Found a JVM system property: " + placeholderPart + ". Overriding property set via Property Location");
+            log.info("Found a JVM system property: " + placeholderPart + ". Overriding property set via Property Location");
         } else {
             propertyValue = properties.getProperty(placeholderPart);
         }
-        
-        return propertyValue;
+
+        return parsePropertyValue(propertyValue);
     }
 
 }

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java?rev=992602&r1=992601&r2=992602&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java Sat Sep  4 13:37:33 2010
@@ -39,6 +39,7 @@ public class PropertiesComponent extends
     private static final transient Log LOG = LogFactory.getLog(PropertiesComponent.class);
     private final Map<String[], Properties> cacheMap = new LRUCache<String[], Properties>(1000);
     private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver();
+    private PropertiesParser propertiesParser = new DefaultPropertiesParser();
     private String[] locations;
     private boolean cache = true;
 
@@ -89,7 +90,7 @@ public class PropertiesComponent extends
         if (LOG.isTraceEnabled()) {
             LOG.trace("Parsing uri " + uri + " with properties: " + prop);
         }
-        return PropertiesParser.parseUri(uri, prop, PREFIX_TOKEN, SUFFIX_TOKEN);
+        return propertiesParser.parseUri(uri, prop, PREFIX_TOKEN, SUFFIX_TOKEN);
     }
 
     public String[] getLocations() {
@@ -112,6 +113,14 @@ public class PropertiesComponent extends
         this.propertiesResolver = propertiesResolver;
     }
 
+    public PropertiesParser getPropertiesParser() {
+        return propertiesParser;
+    }
+
+    public void setPropertiesParser(PropertiesParser propertiesParser) {
+        this.propertiesParser = propertiesParser;
+    }
+
     public boolean isCache() {
         return cache;
     }

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java?rev=992602&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesParser.java Sat Sep  4 13:37:33 2010
@@ -0,0 +1,47 @@
+/**
+ * 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.properties;
+
+import java.util.Properties;
+
+/**
+ * A parser to parse properties for a given input
+ *
+ * @version $Revision$
+ */
+public interface PropertiesParser {
+
+    /**
+     * Parses the string and replaces the property placeholders with values from the given properties
+     *
+     * @param text        the text to be parsed
+     * @param properties  the properties resolved which values should be looked up
+     * @param prefixToken the prefix token
+     * @param suffixToken the suffix token
+     * @return the parsed text with replaced placeholders
+     * @throws IllegalArgumentException if uri syntax is not valid or a property is not found
+     */
+    String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException;
+
+    /**
+     * Parses the property value
+     *
+     * @param value the value
+     * @return the parsed value
+     */
+    String parsePropertyValue(String value);
+}

Added: camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java?rev=992602&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java (added)
+++ camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java Sat Sep  4 13:37:33 2010
@@ -0,0 +1,81 @@
+/**
+ * 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.apache.camel.component.properties.DefaultPropertiesParser;
+import org.apache.camel.util.ObjectHelper;
+import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
+
+/**
+ * A {@link org.apache.camel.component.properties.PropertiesParser} which is using Jasypt
+ * to decrypt any encrypted values.
+ * <p/>
+ * The values must be enclosed in the prefix and suffix token.
+ *
+ * @version $Revision$
+ */
+public class JasyptPropertiesParser extends DefaultPropertiesParser {
+
+    public static final String JASYPT_PREFIX_TOEKN = "ENC(";
+    public static final String JASYPT_SUFFIX_TOEKN = ")";
+
+    // TODO: A JasyptComponent we can leverage instead of directly from here
+    private StandardPBEStringEncryptor encryptor;
+    private String password;
+    private String algorithm;
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getAlgorithm() {
+        return algorithm;
+    }
+
+    public void setAlgorithm(String algorithm) {
+        this.algorithm = algorithm;
+    }
+
+    public synchronized StandardPBEStringEncryptor getEncryptor() {
+        if (encryptor == null) {
+            encryptor = new StandardPBEStringEncryptor();
+            encryptor.setPassword(password);
+            if (algorithm != null) {
+                encryptor.setAlgorithm(algorithm);
+            }
+        }
+        return encryptor;
+    }
+
+    @Override
+    public String parsePropertyValue(String value) {
+        // check if the value is using the tokens
+        String text = ObjectHelper.between(value, JASYPT_PREFIX_TOEKN, JASYPT_SUFFIX_TOEKN);
+        if (text == null) {
+            // not encrypted
+            return value;
+        } else {
+            // do not log the decrypted text as it could be sensitive information such as a password
+            return getEncryptor().decrypt(text);
+        }
+    }
+
+}

Added: camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java?rev=992602&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java (added)
+++ camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java Sat Sep  4 13:37:33 2010
@@ -0,0 +1,34 @@
+/**
+ * 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 junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class JasyptPropertiesParserTest extends TestCase {
+
+    public void testJasyptPropertiesParser() throws Exception {
+        JasyptPropertiesParser parser = new JasyptPropertiesParser();
+        parser.setPassword("secret");
+
+        assertEquals("foo", parser.parsePropertyValue("foo"));
+        assertEquals("tiger", parser.parsePropertyValue("ENC(bsW9uV37gQ0QHFu7KO03Ww==)"));
+    }
+
+}