Fix serialiation/deserialization for field level encryption. (#1622)

Closes #1621.
This commit is contained in:
Michael Reiche
2022-12-16 13:14:43 -08:00
committed by GitHub
parent 22eeaf5ec1
commit 1fcafab4d1
10 changed files with 85 additions and 68 deletions

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.couchbase.domain;
import com.couchbase.client.java.encryption.annotation.Encrypted;
import org.springframework.data.couchbase.core.mapping.Document;
@Document
@@ -26,6 +25,7 @@ public class Address extends ComparableEntity {
// for N1qlJoin
private String id;
private String parentId;
private ETurbulenceCategory turbulence;
public Address() {}
@@ -45,6 +45,13 @@ public class Address extends ComparableEntity {
this.city = city;
}
public ETurbulenceCategory getTurbulence() {
return turbulence;
}
public void setTurbulence(ETurbulenceCategory turbulence) {
this.turbulence = turbulence;
}
public String getParentId() {
return parentId;
}

View File

@@ -23,7 +23,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
* @author Michael Reiche
*/
public enum ETurbulenceCategory {
T10("10%"), T20("20%"), T30("30%");
T10("\"10%"), T20("\"20%"), T30("\"30%");
private final String code;

View File

@@ -137,6 +137,7 @@ public class UserEncrypted extends AbstractUser implements Serializable {
@Encrypted public UUID encUUID;
@Encrypted public DateTime encDateTime;
@Encrypted public ETurbulenceCategory turbulence;
@Encrypted public Address encAddress = new Address();
public Date plainDate;
@@ -228,8 +229,8 @@ public class UserEncrypted extends AbstractUser implements Serializable {
encCharacter = 'a';
encCharacters = new Character[] { 'a', 'b' };
encString = "myString";
encStrings = new String[] { "myString" };
encString = "myS\"tring";
encStrings = new String[] { "mySt\"ring" };
encDate = NOW_Date;
encDates = new Date[] { NOW_Date };
@@ -248,13 +249,12 @@ public class UserEncrypted extends AbstractUser implements Serializable {
//clazz = String.class;
encDateTime = NOW_DateTime;
encAddress = new Address();
plainDate = NOW_Date;
plainDateTime = NOW_DateTime;
nicknames = List.of("Happy", "Sleepy");
turbulence = ETurbulenceCategory.T10;
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.AddressWithEncStreet;
import org.springframework.data.couchbase.domain.ETurbulenceCategory;
import org.springframework.data.couchbase.domain.TestEncrypted;
import org.springframework.data.couchbase.domain.UserEncrypted;
import org.springframework.data.couchbase.domain.UserEncryptedRepository;
@@ -97,6 +98,7 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void saveAndFindByTestId() {
boolean cleanAfter = true;
TestEncrypted user = new TestEncrypted(UUID.randomUUID().toString());
user.initSimpleTypes();
couchbaseTemplate.save(user);
@@ -121,13 +123,20 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
writeSDKReadSpring.setId(user.getId());
assertEquals(user.toString(), writeSDKReadSpring.toString());
if (cleanAfter) {
try {
couchbaseTemplate.removeById(UserEncrypted.class).one(user.getId());
} catch (DataRetrievalFailureException iae) {
// ignore
}
}
}
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void writeSpring_readSpring() {
boolean cleanAfter = false;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSpring_readSpring", "l", "hello");
boolean cleanAfter = true;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSpring_readSpring", "l", "hel\"lo");
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setEncStreet("Olcott Street");
address.setCity("Santa Clara");
@@ -136,6 +145,7 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
Address encAddress = new Address(); // encrypted address with plaintext street.
encAddress.setStreet("Castro St");
encAddress.setCity("Mountain View");
encAddress.setTurbulence(ETurbulenceCategory.T10);
user.setEncAddress(encAddress);
user.initSimpleTypes();
@@ -159,8 +169,8 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void writeSpring_readSDK() {
boolean cleanAfter = false;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSpring_readSDK", "l", "hello");
boolean cleanAfter = true;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSpring_readSDK", "l", "hel\"lo");
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setEncStreet("Olcott Street");
address.setCity("Santa Clara");
@@ -195,8 +205,8 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void writeSDK_readSpring() {
boolean cleanAfter = false;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSDK_readSpring", "l", "hello");
boolean cleanAfter = true;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSDK_readSpring", "l", "hel\"lo");
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setEncStreet("Olcott Street");
address.setCity("Santa Clara");
@@ -232,8 +242,8 @@ public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends Clu
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void writeSDK_readSDK() {
boolean cleanAfter = false;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSDK_readSDK", "l", "hello");
boolean cleanAfter = true;
UserEncrypted user = new UserEncrypted(UUID.randomUUID().toString(), "writeSDK_readSDK", "l", "hel\"lo");
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setEncStreet("Olcott Street");
address.setCity("Santa Clara");