cayenne-commits mailing list archives

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

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

integration with BatchTranslator API
introducing ColumnMapper

Added:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/CryptoBatchTranslatorFactoryDecorator.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/map/
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/map/ColumnMapper.java
      - copied, changed from r1578319, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
Modified:
    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/CipherService.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.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=1578320&r1=1578319&r2=1578320&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
Mon Mar 17 11:48:32 2014
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.crypto;
 
+import org.apache.cayenne.access.translator.batch.BatchTranslatorFactory;
+import org.apache.cayenne.crypto.batch.CryptoBatchTranslatorFactoryDecorator;
 import org.apache.cayenne.crypto.cipher.CipherService;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
@@ -50,6 +52,7 @@ public class CryptoModuleBuilder {
             @Override
             public void configure(Binder binder) {
                 binder.bind(CipherService.class).to(cipherServiceType);
+                binder.decorate(BatchTranslatorFactory.class).after(CryptoBatchTranslatorFactoryDecorator.class);
             }
         };
     }

Added: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/CryptoBatchTranslatorFactoryDecorator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/CryptoBatchTranslatorFactoryDecorator.java?rev=1578320&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/CryptoBatchTranslatorFactoryDecorator.java
(added)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/batch/CryptoBatchTranslatorFactoryDecorator.java
Mon Mar 17 11:48:32 2014
@@ -0,0 +1,82 @@
+/*****************************************************************
+ *   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.batch;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.cayenne.access.translator.batch.BatchParameterBinding;
+import org.apache.cayenne.access.translator.batch.BatchTranslator;
+import org.apache.cayenne.access.translator.batch.BatchTranslatorFactory;
+import org.apache.cayenne.crypto.cipher.CipherService;
+import org.apache.cayenne.crypto.map.ColumnMapper;
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.query.BatchQuery;
+import org.apache.cayenne.query.BatchQueryRow;
+
+/**
+ * @since 3.2
+ */
+public class CryptoBatchTranslatorFactoryDecorator implements BatchTranslatorFactory {
+
+    private CipherService cipherService;
+    private ColumnMapper columnMapper;
+    private BatchTranslatorFactory delegate;
+
+    public CryptoBatchTranslatorFactoryDecorator(@Inject BatchTranslatorFactory delegate,
+            @Inject CipherService cipherService, @Inject ColumnMapper columnMapper) {
+
+        this.columnMapper = columnMapper;
+        this.cipherService = cipherService;
+        this.delegate = delegate;
+    }
+
+    @Override
+    public BatchTranslator translator(BatchQuery query, DbAdapter adapter, String trimFunction)
{
+        final BatchTranslator delegateTranslator = delegate.translator(query, adapter, trimFunction);
+
+        return new BatchTranslator() {
+
+            @Override
+            public String getTrimFunction() {
+                return delegateTranslator.getTrimFunction();
+            }
+
+            @Override
+            public String createSqlString() throws IOException {
+                return delegateTranslator.createSqlString();
+            }
+
+            @Override
+            public List<BatchParameterBinding> createBindings(BatchQueryRow row) {
+                List<BatchParameterBinding> bindings = delegateTranslator.createBindings(row);
+
+                for (BatchParameterBinding b : bindings) {
+                    if (columnMapper.isEncrypted(b.getAttribute())) {
+                        Object encrypted = cipherService.encrypt(b.getValue(), b.getAttribute().getType());
+                        b.setValue(encrypted);
+                    }
+                }
+
+                return bindings;
+            }
+        };
+    }
+}

Modified: 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=1578320&r1=1578319&r2=1578320&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
Mon Mar 17 11:48:32 2014
@@ -23,7 +23,7 @@ package org.apache.cayenne.crypto.cipher
  */
 public interface CipherService {
 
-    byte[] encrypt(byte[] plaintext);
+    Object encrypt(Object plaintext, int jdbcType);
 
-    byte[] decrypt(byte[] ciphertext);
+    Object decrypt(Object ciphertext, int jdbcType);
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/map/ColumnMapper.java
(from r1578319, 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/map/ColumnMapper.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/map/ColumnMapper.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java&r1=1578319&r2=1578320&rev=1578320&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/cipher/CipherService.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/map/ColumnMapper.java
Mon Mar 17 11:48:32 2014
@@ -16,14 +16,17 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.crypto.cipher;
+package org.apache.cayenne.crypto.map;
+
+import org.apache.cayenne.map.DbAttribute;
 
 /**
  * @since 3.2
  */
-public interface CipherService {
-
-    byte[] encrypt(byte[] plaintext);
+public interface ColumnMapper {
 
-    byte[] decrypt(byte[] ciphertext);
+    /**
+     * Returns true if a given DbAttribute is encrypted.
+     */
+    boolean isEncrypted(DbAttribute column);
 }

Modified: 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=1578320&r1=1578319&r2=1578320&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/Rot13CipherService.java
Mon Mar 17 11:48:32 2014
@@ -59,12 +59,13 @@ public class Rot13CipherService implemen
     }
 
     @Override
-    public byte[] decrypt(byte[] ciphertext) {
-        return rotate(ciphertext);
+    public Object decrypt(Object ciphertext, int jdbcType) {
+        return rotate(ciphertext.toString());
     }
 
     @Override
-    public byte[] encrypt(byte[] plaintext) {
-        return rotate(plaintext);
+    public Object encrypt(Object plaintext, int jdbcType) {
+        return rotate(plaintext.toString().getBytes());
     }
+
 }



Mime
View raw message