cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1578319 - in /cayenne/main/trunk/cayenne-crypto/src: main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/cayenne/ main/java/org/apache/cayenne/crypto/ main/java/org/apache/cayenne/crypto/cipher/ test/java/org/apache/...
Date Mon, 17 Mar 2014 11:48:20 GMT
Author: aadamchik
Date: Mon Mar 17 11:48:19 2014
New Revision: 1578319

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

CryptoModuleBuilder, CipherService

Added:
    cayenne/main/trunk/cayenne-crypto/src/main/
    cayenne/main/trunk/cayenne-crypto/src/main/java/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
Modified:
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Crypto_InRuntime_Test.java

Added: 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=1578319&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
(added)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
Mon Mar 17 11:48:19 2014
@@ -0,0 +1,57 @@
+/*****************************************************************
+ *   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 org.apache.cayenne.crypto.cipher.CipherService;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+
+/**
+ * A builder of a Cayenne DI module that will contain all extension to Cayenne
+ * runtime needed to enable encryption of certain data columns. Builder allows
+ * to specify custom ciphers, as well as a strategy for discovering which
+ * columns are encrypted.
+ * 
+ * @since 3.2
+ */
+public class CryptoModuleBuilder {
+
+    private Class<? extends CipherService> cipherServiceType;
+
+    public CryptoModuleBuilder cipherService(Class<? extends CipherService> cipherServiceType)
{
+        this.cipherServiceType = cipherServiceType;
+        return this;
+    }
+
+    public Module build() {
+
+        if (cipherServiceType == null) {
+            throw new IllegalStateException("'CipherService' is not initialized");
+        }
+
+        return new Module() {
+
+            @Override
+            public void configure(Binder binder) {
+                binder.bind(CipherService.class).to(cipherServiceType);
+            }
+        };
+    }
+
+}

Added: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java?rev=1578319&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
(added)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
Mon Mar 17 11:48:19 2014
@@ -0,0 +1,29 @@
+/*****************************************************************
+ *   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.cipher;
+
+/**
+ * @since 3.2
+ */
+public interface CipherService {
+
+    byte[] encrypt(byte[] plaintext);
+
+    byte[] decrypt(byte[] ciphertext);
+}

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Crypto_InRuntime_Test.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Crypto_InRuntime_Test.java?rev=1578319&r1=1578318&r2=1578319&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Crypto_InRuntime_Test.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Crypto_InRuntime_Test.java
Mon Mar 17 11:48:19 2014
@@ -26,9 +26,10 @@ import java.util.Map;
 import junit.framework.TestCase;
 
 import org.apache.cayenne.ObjectContext;
-import org.apache.cayenne.configuration.Rot13PasswordEncoder;
 import org.apache.cayenne.configuration.server.ServerRuntime;
 import org.apache.cayenne.crypto.db.Table1;
+import org.apache.cayenne.crypto.unit.Rot13CipherService;
+import org.apache.cayenne.di.Module;
 import org.apache.cayenne.test.jdbc.DBHelper;
 import org.apache.cayenne.test.jdbc.TableHelper;
 
@@ -38,13 +39,12 @@ public class Crypto_InRuntime_Test exten
 
     private TableHelper table1;
 
-    private Rot13PasswordEncoder rot13Cipher;
-
     @Override
     protected void setUp() throws Exception {
 
-        this.runtime = new ServerRuntime("cayenne-crypto.xml");
-        this.rot13Cipher = new Rot13PasswordEncoder();
+        Module crypto = new CryptoModuleBuilder().cipherService(Rot13CipherService.class).build();
+
+        this.runtime = new ServerRuntime("cayenne-crypto.xml", crypto);
 
         DBHelper dbHelper = new DBHelper(runtime.getDataSource(null));
 
@@ -64,10 +64,10 @@ public class Crypto_InRuntime_Test exten
 
         Object[] data = table1.select();
         assertEquals("plain_1", data[1]);
-        assertEquals(rot13Cipher.rotate("crypto_1"), data[2]);
+        assertEquals(Rot13CipherService.rotate("crypto_1"), data[2]);
     }
 
-    public void testInsert_Multiple() throws SQLException {
+    public void testInsert_MultipleObjects() throws SQLException {
 
         ObjectContext context = runtime.newContext();
 
@@ -89,8 +89,8 @@ public class Crypto_InRuntime_Test exten
             cipherByPlain.put(r[1], r[2]);
         }
 
-        assertEquals(rot13Cipher.rotate("crypto_1"), cipherByPlain.get("a"));
-        assertEquals(rot13Cipher.rotate("crypto_2"), cipherByPlain.get("b"));
+        assertEquals(Rot13CipherService.rotate("crypto_1"), cipherByPlain.get("a"));
+        assertEquals(Rot13CipherService.rotate("crypto_2"), cipherByPlain.get("b"));
     }
 
 }

Added: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java?rev=1578319&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
(added)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
Mon Mar 17 11:48:19 2014
@@ -0,0 +1,70 @@
+/*****************************************************************
+ *   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.unit;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.cayenne.crypto.cipher.CipherService;
+
+public class Rot13CipherService implements CipherService {
+
+    public static String rotate(String value) {
+        if (value == null) {
+            return null;
+        }
+
+        int length = value.length();
+        StringBuilder result = new StringBuilder();
+
+        for (int i = 0; i < length; i++) {
+            char c = value.charAt(i);
+
+            // If c is a letter, rotate it by 13. Numbers/symbols are untouched.
+            if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
+                c += 13; // The first half of the alphabet goes forward 13
+                         // letters
+            else if ((c >= 'n' && c <= 'z') || (c >= 'A' && c <=
'Z'))
+                c -= 13; // The last half of the alphabet goes backward 13
+                         // letters
+
+            result.append(c);
+        }
+
+        return result.toString();
+    }
+
+    public static byte[] rotate(byte[] value) {
+        try {
+            String valueString = new String(value, "UTF-8");
+            return rotate(valueString).getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalStateException("Can't convert between bytes and String", e);
+        }
+    }
+
+    @Override
+    public byte[] decrypt(byte[] ciphertext) {
+        return rotate(ciphertext);
+    }
+
+    @Override
+    public byte[] encrypt(byte[] plaintext) {
+        return rotate(plaintext);
+    }
+}



Mime
View raw message