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 17:06:46 UTC

svn commit: r992622 - in /camel/trunk/components: camel-core-xml/src/main/java/org/apache/camel/core/xml/ camel-jasypt/src/main/java/org/apache/camel/component/jasypt/ camel-jasypt/src/test/java/org/apache/camel/component/jasypt/ camel-jasypt/src/test/...

Author: davsclaus
Date: Sat Sep  4 15:06:45 2010
New Revision: 992622

URL: http://svn.apache.org/viewvc?rev=992622&view=rev
Log:
CAMEL-2987: using jasypt with properties component to have encrypted values in properties file.

Added:
    camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesTest.java
    camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.java
    camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.java
    camel/trunk/components/camel-jasypt/src/test/resources/org/
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml
    camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/myproperties.properties
Modified:
    camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
    camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.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/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java?rev=992622&r1=992621&r2=992622&view=diff
==============================================================================
--- camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java (original)
+++ camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java Sat Sep  4 15:06:45 2010
@@ -33,6 +33,7 @@ import org.apache.camel.ShutdownRunningT
 import org.apache.camel.builder.ErrorHandlerBuilderRef;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.component.properties.PropertiesParser;
 import org.apache.camel.component.properties.PropertiesResolver;
 import org.apache.camel.core.xml.scan.PatternBasedPackageScanFilter;
 import org.apache.camel.management.DefaultManagementAgent;
@@ -488,6 +489,13 @@ public abstract class AbstractCamelConte
                 pc.setPropertiesResolver(resolver);
             }
 
+            // if using a custom parser
+            if (ObjectHelper.isNotEmpty(def.getPropertiesParserRef())) {
+                PropertiesParser parser = CamelContextHelper.mandatoryLookup(getContext(), def.getPropertiesParserRef(),
+                                                                             PropertiesParser.class);
+                pc.setPropertiesParser(parser);
+            }
+
             // register the properties component
             getContext().addComponent("properties", pc);
         }

Modified: camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java?rev=992622&r1=992621&r2=992622&view=diff
==============================================================================
--- camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java (original)
+++ camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java Sat Sep  4 15:06:45 2010
@@ -38,6 +38,9 @@ public class CamelPropertyPlaceholderDef
     @XmlAttribute
     private String propertiesResolverRef;
 
+    @XmlAttribute
+    private String propertiesParserRef;
+
     public String getLocation() {
         return location;
     }
@@ -54,4 +57,11 @@ public class CamelPropertyPlaceholderDef
         this.propertiesResolverRef = propertiesResolverRef;
     }
 
+    public String getPropertiesParserRef() {
+        return propertiesParserRef;
+    }
+
+    public void setPropertiesParserRef(String propertiesParserRef) {
+        this.propertiesParserRef = propertiesParserRef;
+    }
 }

Modified: 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=992622&r1=992621&r2=992622&view=diff
==============================================================================
--- camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java (original)
+++ camel/trunk/components/camel-jasypt/src/main/java/org/apache/camel/component/jasypt/JasyptPropertiesParser.java Sat Sep  4 15:06:45 2010
@@ -43,7 +43,15 @@ public class JasyptPropertiesParser exte
     }
 
     public void setPassword(String password) {
+        // lookup password as either environment or JVM system property
+        if (password.startsWith("sysenv:")) {
+            password = System.getenv(ObjectHelper.after(password, "sysenv:"));
+        }
+        if (password.startsWith("sys:")) {
+            password = System.getProperty(ObjectHelper.after(password, "sys:"));
+        }
         this.password = password;
+
     }
 
     public String getAlgorithm() {
@@ -56,10 +64,13 @@ public class JasyptPropertiesParser exte
 
     public synchronized StandardPBEStringEncryptor getEncryptor() {
         if (encryptor == null) {
+            // password is mandatory
+            ObjectHelper.notEmpty("password", getPassword());
+
             encryptor = new StandardPBEStringEncryptor();
-            encryptor.setPassword(password);
+            encryptor.setPassword(getPassword());
             if (algorithm != null) {
-                encryptor.setAlgorithm(algorithm);
+                encryptor.setAlgorithm(getAlgorithm());
             }
         }
         return encryptor;

Modified: 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=992622&r1=992621&r2=992622&view=diff
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java (original)
+++ camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesParserTest.java Sat Sep  4 15:06:45 2010
@@ -31,4 +31,16 @@ public class JasyptPropertiesParserTest 
         assertEquals("tiger", parser.parsePropertyValue("ENC(bsW9uV37gQ0QHFu7KO03Ww==)"));
     }
 
+    public void testJasyptPropertiesParserSys() throws Exception {
+        System.setProperty("myfoo", "secret");
+
+        JasyptPropertiesParser parser = new JasyptPropertiesParser();
+        parser.setPassword("sys:myfoo");
+
+        assertEquals("foo", parser.parsePropertyValue("foo"));
+        assertEquals("tiger", parser.parsePropertyValue("ENC(bsW9uV37gQ0QHFu7KO03Ww==)"));
+
+        System.clearProperty("myfoo");
+    }
+
 }

Added: camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesTest.java?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesTest.java (added)
+++ camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/JasyptPropertiesTest.java Sat Sep  4 15:06:45 2010
@@ -0,0 +1,72 @@
+/**
+ * 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.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class JasyptPropertiesTest extends CamelTestSupport {
+
+    @Test
+    public void testJasyptProperties() throws Exception {
+        getMockEndpoint("mock:tiger").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        // START SNIPPET: e1
+        // create the jasypt properties parser
+        JasyptPropertiesParser jasypt = new JasyptPropertiesParser();
+        // and set the master password
+        jasypt.setPassword("secret");
+
+        // create the properties component
+        PropertiesComponent pc = new PropertiesComponent();
+        pc.setLocation("classpath:org/apache/camel/component/jasypt/myproperties.properties");
+        // and use the jasypt properties parser so we can decrypt values
+        pc.setPropertiesParser(jasypt);
+
+        // add properties component to camel context
+        context.addComponent("properties", pc);
+        // END SNIPPET: e1
+
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("{{cool.result}}");
+            }
+        };
+    }
+
+}

Added: camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.java?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.java (added)
+++ camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.java Sat Sep  4 15:06:45 2010
@@ -0,0 +1,43 @@
+/**
+ * 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.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringJasyptProperties2Test extends CamelSpringTestSupport {
+
+    @Test
+    public void testJasyptProperties() throws Exception {
+        getMockEndpoint("mock:tiger").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml");
+    }
+
+}

Added: camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.java?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.java (added)
+++ camel/trunk/components/camel-jasypt/src/test/java/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.java Sat Sep  4 15:06:45 2010
@@ -0,0 +1,43 @@
+/**
+ * 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.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringJasyptPropertiesTest extends CamelSpringTestSupport {
+
+    @Test
+    public void testJasyptProperties() throws Exception {
+        getMockEndpoint("mock:tiger").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml");
+    }
+
+}

Added: camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml (added)
+++ camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptProperties2Test.xml Sat Sep  4 15:06:45 2010
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <!-- START SNIPPET: e1 -->
+    <!-- define the jasypt properties parser with the given password to be used -->
+    <bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser">
+        <!-- password is mandatory, you can prefix it with sysenv: or sys: to indicate it should use
+             an OS environment or JVM system property value, so you dont have the master password defined here -->
+        <property name="password" value="secret"/>
+    </bean>
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <!-- define the camel properties placeholder, and let it leverage jasypt -->
+        <propertyPlaceholder id="properties"
+                             location="classpath:org/apache/camel/component/jasypt/myproperties.properties"
+                             propertiesParserRef="jasypt"/>
+        <route>
+            <from uri="direct:start"/>
+            <to uri="{{cool.result}}"/>
+        </route>
+    </camelContext>
+    <!-- END SNIPPET: e1 -->
+
+</beans>

Added: camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml (added)
+++ camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/SpringJasyptPropertiesTest.xml Sat Sep  4 15:06:45 2010
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <!-- START SNIPPET: e1 -->
+    <!-- define the jasypt properties parser with the given password to be used -->
+    <bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser">
+        <property name="password" value="secret"/>
+    </bean>
+
+    <!-- define the camel properties component -->
+    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
+        <!-- the properties file is in the classpath -->
+        <property name="location" value="classpath:org/apache/camel/component/jasypt/myproperties.properties"/>
+        <!-- and let it leverage the jasypt parser -->
+        <property name="propertiesParser" ref="jasypt"/>
+    </bean>
+    <!-- END SNIPPET: e1 -->
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="direct:start"/>
+            <to uri="{{cool.result}}"/>
+        </route>
+    </camelContext>
+
+</beans>

Added: camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/myproperties.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/myproperties.properties?rev=992622&view=auto
==============================================================================
--- camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/myproperties.properties (added)
+++ camel/trunk/components/camel-jasypt/src/test/resources/org/apache/camel/component/jasypt/myproperties.properties Sat Sep  4 15:06:45 2010
@@ -0,0 +1,24 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# START SNIPPET: e1
+# refer to a mock endpoint name by that encrypted password
+cool.result=mock:{{cool.password}}
+
+# here is a password which is encrypted
+cool.password=ENC(bsW9uV37gQ0QHFu7KO03Ww==)
+# END SNIPPET: e1