cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1583131 - in /cayenne/main/trunk/cayenne-crypto/src: main/java/org/apache/cayenne/crypto/ main/java/org/apache/cayenne/crypto/transformer/value/ test/java/org/apache/cayenne/crypto/ test/java/org/apache/cayenne/crypto/key/ test/java/org/ap...
Date Sun, 30 Mar 2014 13:19:26 GMT
Author: aadamchik
Date: Sun Mar 30 13:19:25 2014
New Revision: 1583131

URL: http://svn.apache.org/r1583131
Log:
CAY-1916 cayenne-crypto module that enables data encryption for certain model attributes

more tests and builder options

Added:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactory.java
      - copied, changed from r1583130, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactoryTest.java
      - copied, changed from r1583130, cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
Removed:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/key/KeyStoreKeySourceTest.java

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java?rev=1583131&r1=1583130&r2=1583131&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
Sun Mar 30 13:19:25 2014
@@ -20,6 +20,7 @@ package org.apache.cayenne.crypto;
 
 import java.io.File;
 import java.net.MalformedURLException;
+import java.net.URL;
 
 import org.apache.cayenne.access.jdbc.reader.RowReaderFactory;
 import org.apache.cayenne.access.translator.batch.BatchTranslatorFactory;
@@ -29,6 +30,7 @@ import org.apache.cayenne.crypto.cipher.
 import org.apache.cayenne.crypto.key.KeySource;
 import org.apache.cayenne.crypto.key.KeyStoreKeySource;
 import org.apache.cayenne.crypto.map.ColumnMapper;
+import org.apache.cayenne.crypto.map.PatternColumnMapper;
 import org.apache.cayenne.crypto.reader.CryptoRowReaderFactoryDecorator;
 import org.apache.cayenne.crypto.transformer.DefaultTransformerFactory;
 import org.apache.cayenne.crypto.transformer.TransformerFactory;
@@ -53,6 +55,7 @@ public class CryptoModuleBuilder {
 
     private Class<? extends ValueTransformerFactory> valueTransformerFactoryType;
 
+    private String columnMapperPattern;
     private ColumnMapper columnMapper;
     private Class<? extends ColumnMapper> columnMapperType;
 
@@ -61,7 +64,8 @@ public class CryptoModuleBuilder {
     private String cipherPadding;
     private Class<? extends CipherFactory> cipherFactoryType;
 
-    private String keyStoreUrl;
+    private URL keyStoreUrl;
+    private String keyStoreUrlString;
     private File keyStoreFile;
     private Class<? extends KeySource> keySourceType;
 
@@ -77,6 +81,8 @@ public class CryptoModuleBuilder {
 
         this.cipherFactoryType = DefaultCipherFactory.class;
         this.keySourceType = KeyStoreKeySource.class;
+
+        this.columnMapperPattern = "^CRYPTO_";
     }
 
     public CryptoModuleBuilder cipherAlgorithm(String algorithm) {
@@ -100,17 +106,26 @@ public class CryptoModuleBuilder {
     }
 
     public CryptoModuleBuilder columnMapper(Class<? extends ColumnMapper> columnMapperType)
{
+        this.columnMapperPattern = null;
         this.columnMapperType = columnMapperType;
         this.columnMapper = null;
         return this;
     }
 
     public CryptoModuleBuilder columnMapper(ColumnMapper columnMapper) {
+        this.columnMapperPattern = null;
         this.columnMapperType = null;
         this.columnMapper = columnMapper;
         return this;
     }
 
+    public CryptoModuleBuilder columnMapper(String pattern) {
+        this.columnMapperPattern = pattern;
+        this.columnMapperType = null;
+        this.columnMapper = null;
+        return this;
+    }
+
     /**
      * Sets a password used that unlocks a secret key.
      */
@@ -121,13 +136,23 @@ public class CryptoModuleBuilder {
 
     public CryptoModuleBuilder keyStore(File file) {
         this.keyStoreUrl = null;
+        this.keyStoreUrlString = null;
         this.keyStoreFile = file;
 
         return this;
     }
 
     public CryptoModuleBuilder keyStore(String url) {
+        this.keyStoreUrl = null;
+        this.keyStoreUrlString = url;
+        this.keyStoreFile = null;
+
+        return this;
+    }
+
+    public CryptoModuleBuilder keyStore(URL url) {
         this.keyStoreUrl = url;
+        this.keyStoreUrlString = null;
         this.keyStoreFile = null;
 
         return this;
@@ -147,7 +172,7 @@ public class CryptoModuleBuilder {
             throw new IllegalStateException("'ValueTransformerFactory' is not initialized");
         }
 
-        if (columnMapperType == null && columnMapper == null) {
+        if (columnMapperType == null && columnMapper == null && columnMapperPattern
== null) {
             throw new IllegalStateException("'ColumnMapper' is not initialized");
         }
 
@@ -162,7 +187,9 @@ public class CryptoModuleBuilder {
 
                 String keyStoreUrl = null;
                 if (CryptoModuleBuilder.this.keyStoreUrl != null) {
-                    keyStoreUrl = CryptoModuleBuilder.this.keyStoreUrl;
+                    keyStoreUrl = CryptoModuleBuilder.this.keyStoreUrl.toExternalForm();
+                } else if (CryptoModuleBuilder.this.keyStoreUrlString != null) {
+                    keyStoreUrl = CryptoModuleBuilder.this.keyStoreUrlString;
                 } else if (keyStoreFile != null) {
                     try {
                         keyStoreUrl = keyStoreFile.toURI().toURL().toExternalForm();
@@ -194,7 +221,9 @@ public class CryptoModuleBuilder {
                 binder.bind(ValueTransformerFactory.class).to(valueTransformerFactoryType);
                 binder.bind(KeySource.class).to(keySourceType);
 
-                if (columnMapperType != null) {
+                if (columnMapperPattern != null) {
+                    binder.bind(ColumnMapper.class).toInstance(new PatternColumnMapper(columnMapperPattern));
+                } else if (columnMapperType != null) {
                     binder.bind(ColumnMapper.class).to(columnMapperType);
                 } else {
                     binder.bind(ColumnMapper.class).toInstance(columnMapper);

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactory.java
(from r1583130, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactory.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactory.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java&r1=1583130&r2=1583131&rev=1583131&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactory.java
Sun Mar 30 13:19:25 2014
@@ -39,7 +39,7 @@ import org.apache.cayenne.map.ObjEntity;
  * 
  * @since 3.2
  */
-public class JceTransformerFactory implements ValueTransformerFactory {
+public class JceValueTransformerFactory implements ValueTransformerFactory {
 
     private Map<String, BytesConverter> objectToBytes;
     private Map<Integer, BytesConverter> dbToBytes;
@@ -50,7 +50,7 @@ public class JceTransformerFactory imple
     private ConcurrentMap<DbAttribute, ValueTransformer> encryptors;
     private ConcurrentMap<DbAttribute, ValueTransformer> decryptors;
 
-    public JceTransformerFactory() {
+    public JceValueTransformerFactory() {
         this.encryptors = new ConcurrentHashMap<DbAttribute, ValueTransformer>();
         this.decryptors = new ConcurrentHashMap<DbAttribute, ValueTransformer>();
 

Added: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java?rev=1583131&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
(added)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
Sun Mar 30 13:19:25 2014
@@ -0,0 +1,50 @@
+/*****************************************************************
+ *   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.cayenne.crypto;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.security.Key;
+
+import org.apache.cayenne.crypto.key.KeySource;
+import org.apache.cayenne.crypto.key.KeyStoreKeySourceTest;
+import org.apache.cayenne.crypto.transformer.value.JceValueTransformerFactory;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.di.Module;
+import org.junit.Test;
+
+public class CryptoModuleBuilderTest {
+
+    @Test
+    public void testBuild_KeySource() {
+        Module m = new CryptoModuleBuilder().keyPassword(KeyStoreKeySourceTest.TEST_KEY_PASS)
+                .keyStore(KeyStoreKeySourceTest.class.getResource(KeyStoreKeySourceTest.KS1_JCEKS))
+                .valueTransformer(JceValueTransformerFactory.class).build();
+
+        Injector injector = DIBootstrap.createInjector(m);
+
+        KeySource ks = injector.getInstance(KeySource.class);
+        Key k1 = ks.getKey("k1");
+        assertNotNull(k1);
+        assertEquals("DES", k1.getAlgorithm());
+    }
+
+}

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/key/KeyStoreKeySourceTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/key/KeyStoreKeySourceTest.java?rev=1583131&r1=1583130&r2=1583131&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/key/KeyStoreKeySourceTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/key/KeyStoreKeySourceTest.java
Sun Mar 30 13:19:25 2014
@@ -33,7 +33,8 @@ import org.junit.Test;
 
 public class KeyStoreKeySourceTest {
 
-    private static final char[] TEST_KEY_PASS = "testkeypass".toCharArray();
+    public static final char[] TEST_KEY_PASS = "testkeypass".toCharArray();
+    public static final String KS1_JCEKS = "ks1.jceks";
 
     @Test(expected = CayenneCryptoException.class)
     public void testConstructor_NoUrl() {
@@ -45,12 +46,12 @@ public class KeyStoreKeySourceTest {
     @Test
     public void testGetKey_JCEKS_DES() {
 
-        URL url = getClass().getResource("ks1.jceks");
+        URL url = getClass().getResource(KS1_JCEKS);
         assertNotNull(url);
 
         Map<String, String> props = new HashMap<String, String>();
         props.put(CryptoConstants.KEYSTORE_URL, url.toExternalForm());
-        
+
         Map<String, char[]> creds = new HashMap<String, char[]>();
         creds.put(CryptoConstants.KEY_PASSWORD, TEST_KEY_PASS);
 
@@ -65,7 +66,7 @@ public class KeyStoreKeySourceTest {
         Key k2 = ks.getKey("k2");
         assertNotNull(k2);
         assertEquals("DES", k2.getAlgorithm());
-        
+
         Key k3 = ks.getKey("k3");
         assertNotNull(k3);
         assertEquals("AES", k3.getAlgorithm());

Copied: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactoryTest.java
(from r1583130, cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactoryTest.java?p2=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactoryTest.java&p1=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java&r1=1583130&r2=1583131&rev=1583131&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueTransformerFactoryTest.java
Sun Mar 30 13:19:25 2014
@@ -33,7 +33,7 @@ import org.apache.cayenne.map.DbEntity;
 import org.junit.Before;
 import org.junit.Test;
 
-public class JceTransformerFactoryTest {
+public class JceValueTransformerFactoryTest {
 
     private DbEntity t1;
     private DbEntity t2;
@@ -50,7 +50,7 @@ public class JceTransformerFactoryTest {
     @Test
     public void testGetJavaType() {
 
-        JceTransformerFactory f = new JceTransformerFactory();
+        JceValueTransformerFactory f = new JceValueTransformerFactory();
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
         assertEquals("java.lang.String", f.getJavaType(t1_ct));
@@ -78,7 +78,7 @@ public class JceTransformerFactoryTest {
 
     @Test
     public void testCreateEncryptor() {
-        JceTransformerFactory f = new JceTransformerFactory();
+        JceValueTransformerFactory f = new JceValueTransformerFactory();
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
 
@@ -99,7 +99,7 @@ public class JceTransformerFactoryTest {
 
     @Test
     public void testCreateDecryptor() {
-        JceTransformerFactory f = new JceTransformerFactory();
+        JceValueTransformerFactory f = new JceValueTransformerFactory();
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
 
@@ -128,7 +128,7 @@ public class JceTransformerFactoryTest {
 
     @Test
     public void testEncryptor() {
-        JceTransformerFactory f = new JceTransformerFactory();
+        JceValueTransformerFactory f = new JceValueTransformerFactory();
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
 
@@ -147,7 +147,7 @@ public class JceTransformerFactoryTest {
     
     @Test
     public void testDecryptor() {
-        JceTransformerFactory f = new JceTransformerFactory();
+        JceValueTransformerFactory f = new JceValueTransformerFactory();
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
 



Mime
View raw message