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

svn commit: r1408571 - in /camel/trunk/components/camel-crypto/src: main/java/org/apache/camel/converter/crypto/ test/java/org/apache/camel/converter/crypto/

Author: ningjiang
Date: Tue Nov 13 03:27:44 2012
New Revision: 1408571

URL: http://svn.apache.org/viewvc?rev=1408571&view=rev
Log:
CAMEL-5788 support to configure the keyinformation dynamically in the PGPDataformat

Added:
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
Modified:
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java

Modified: camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java?rev=1408571&r1=1408570&r2=1408571&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java Tue Nov 13 03:27:44 2012
@@ -48,6 +48,9 @@ import org.bouncycastle.util.io.Streams;
  */
 public class PGPDataFormat implements DataFormat {
 
+    public static final String KEY_FILE_NAME = "CamelPGPDataFormatKeyFileName";
+    public static final String KEY_USERID = "CamelPGPDataFormatKeyUserid";
+    public static final String KEY_PASSWORD = "CamelPGPDataFormatKeyPassword";
     private String keyUserid;
     private String password;
     private String keyFileName;
@@ -59,9 +62,21 @@ public class PGPDataFormat implements Da
             Security.addProvider(new BouncyCastleProvider());
         }
     }
+    
+    protected String findKeyFileName(Exchange exchange) {
+        return exchange.getIn().getHeader(KEY_FILE_NAME, keyFileName, String.class);
+    }
+    
+    protected String findKeyUserid(Exchange exchange) {
+        return exchange.getIn().getHeader(KEY_USERID, keyUserid, String.class);
+    }
+    
+    protected String findKeyPassword(Exchange exchange) {
+        return exchange.getIn().getHeader(KEY_PASSWORD, password, String.class);
+    }
 
     public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
-        PGPPublicKey key = PGPDataFormatUtil.findPublicKey(exchange.getContext(), this.keyFileName, this.keyUserid);
+        PGPPublicKey key = PGPDataFormatUtil.findPublicKey(exchange.getContext(), findKeyFileName(exchange), findKeyUserid(exchange));
         if (key == null) {
             throw new IllegalArgumentException("Public key is null, cannot proceed");
         }
@@ -96,7 +111,7 @@ public class PGPDataFormat implements Da
             return null;
         }
 
-        PGPPrivateKey key = PGPDataFormatUtil.findPrivateKey(exchange.getContext(), keyFileName, encryptedStream, password);
+        PGPPrivateKey key = PGPDataFormatUtil.findPrivateKey(exchange.getContext(), findKeyFileName(exchange), encryptedStream, findKeyPassword(exchange));
         if (key == null) {
             throw new IllegalArgumentException("Private key is null, cannot proceed");
         }

Modified: camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java?rev=1408571&r1=1408570&r2=1408571&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java Tue Nov 13 03:27:44 2012
@@ -46,7 +46,7 @@ public final class PGPDataFormatUtil {
 
     private PGPDataFormatUtil() {
     }
-
+    
     public static PGPPublicKey findPublicKey(CamelContext context, String filename, String userid) throws IOException, PGPException,
             NoSuchProviderException {
 

Added: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java?rev=1408571&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java (added)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java Tue Nov 13 03:27:44 2012
@@ -0,0 +1,56 @@
+/**
+ * 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.converter.crypto;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class PGPDataFormatDynamicTest extends PGPDataFormatTest {
+    // setup a wrong userid
+    protected String getKeyUserId() {
+        return "wrong";
+    }
+    // setup a wrong password
+    protected String getKeyPassword() {
+        return "wrong";
+    }
+    
+    private Map<String, Object> getHeaders() {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(PGPDataFormat.KEY_USERID, "sdude@nowhere.net");
+        headers.put(PGPDataFormat.KEY_PASSWORD, "sdude");
+        return headers;
+    }
+    
+    @Test
+    public void testEncryption() throws Exception {
+        doRoundTripEncryptionTests("direct:inline", getHeaders());
+    }
+
+    @Test
+    public void testEncryption2() throws Exception {
+        doRoundTripEncryptionTests("direct:inline2", getHeaders());
+    }
+
+    @Test
+    public void testEncryptionArmor() throws Exception {
+        doRoundTripEncryptionTests("direct:inline-armor", getHeaders());
+    }
+
+}

Modified: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java?rev=1408571&r1=1408570&r2=1408571&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java (original)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java Tue Nov 13 03:27:44 2012
@@ -30,6 +30,14 @@ public class PGPDataFormatTest extends A
     protected String getKeyFileNameSec() {
         return "org/apache/camel/component/crypto/secring.gpg";
     }
+    
+    protected String getKeyUserId() {
+        return "sdude@nowhere.net";
+    }
+    
+    protected String getKeyPassword() {
+        return "sdude";
+    }
 
     @Test
     public void testEncryption() throws Exception {
@@ -55,9 +63,9 @@ public class PGPDataFormatTest extends A
                 // Private Key FileName
                 String keyFileNameSec = getKeyFileNameSec();
                 // Keyring Userid Used to Encrypt
-                String keyUserid = "sdude@nowhere.net";
+                String keyUserid = getKeyUserId();
                 // Private key password
-                String keyPassword = "sdude";
+                String keyPassword = getKeyPassword();
 
                 from("direct:inline")
                         .marshal().pgp(keyFileName, keyUserid)