|
| 1 | +[[couchbase.fieldlevelencryption]] |
| 2 | += Couchbase Field Level Encrytpion |
| 3 | + |
| 4 | +Couchbase supports https://docs.couchbase.com/java-sdk/current/howtos/encrypting-using-sdk.html[Field Level Encryption]. This section documents how to use it with Spring Data Couchbase. |
| 5 | + |
| 6 | +== Requirements |
| 7 | + |
| 8 | + - Spring Data Couchbase 5.0.0-RC1 or above. |
| 9 | + |
| 10 | +== Overview |
| 11 | +Fields annotated with com.couchbase.client.java.encryption.annotation.Encrypted (@Encrypted) will be automatically encrypted on read and decrypted on write. Unencrypted fields can be migrated to encrypted by specifying @Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED). |
| 12 | + |
| 13 | + |
| 14 | +== Getting Started & Configuration |
| 15 | + |
| 16 | +=== Dependencies |
| 17 | + |
| 18 | +Field Level Encryption is available with the dependency ( see https://docs.couchbase.com/java-sdk/current/howtos/encrypting-using-sdk.html[Field Level Encryption] ) |
| 19 | +``` |
| 20 | + <groupId>com.couchbase.client</groupId> |
| 21 | + <artifactId>couchbase-encryption</artifactId> |
| 22 | +``` |
| 23 | +HashiCorp Vault Transit integration requires https://docs.spring.io/spring-vault/docs/current/reference/html[Spring Vault] |
| 24 | +``` |
| 25 | + <groupId>org.springframework.vault</groupId> |
| 26 | + <artifactId>spring-vault-core</artifactId> |
| 27 | +``` |
| 28 | +=== Providing a CryptoManager |
| 29 | + |
| 30 | +A CryptoManager needs to be provided by overriding the cryptoManager() method in AbstractCouchbaseConfiguration. This CryptoManager will be used by Spring Data Couchbase and also by Couchbase Java SDK direct calls made from a CouchbaseClientFactory. |
| 31 | + |
| 32 | +``` |
| 33 | +@Override |
| 34 | +protected CryptoManager cryptoManager() { |
| 35 | + KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType"); |
| 36 | + FileInputStream fis = new java.io.FileInputStream("keyStoreName"); |
| 37 | + char[] password = { 'a', 'b', 'c' }; |
| 38 | + javaKeyStore.load(fis, password); |
| 39 | + Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish"); |
| 40 | + |
| 41 | + // AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key. |
| 42 | + AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build(); |
| 43 | + |
| 44 | + CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter()) |
| 45 | + .defaultEncrypter(provider.encrypterForKey("myKey")).build(); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +=== Defining a Field as Encrypted. |
| 50 | + |
| 51 | +1. @Encrypted defines a field as encrypted. |
| 52 | +2. @Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED) defines a field that may or may not be encrypted when read. It will be encrypted when written. |
| 53 | +3. @Encrypted(encrypter = "<encrypterAlias>") specifies the alias of the encrypter to use for encryption. Note this is not the algorithm, but the name specified when adding the encrypter to the CryptoManager. |
| 54 | + |
| 55 | +=== Example |
| 56 | +.AbstractCouchbaseConfiguration |
| 57 | +==== |
| 58 | +[source,java] |
| 59 | +---- |
| 60 | +@Configuration |
| 61 | +@EnableCouchbaseRepositories("<parent-dir-of-repository-interfaces>") |
| 62 | +@EnableReactiveCouchbaseRepositories("<parent-dir-of-repository-interfaces>") |
| 63 | +static class Config extends AbstractCouchbaseConfiguration { |
| 64 | +
|
| 65 | + // Usual Setup |
| 66 | + @Override public String getConnectionString() { /* ... */ } |
| 67 | + @Override public String getUserName() { /* ... */ } |
| 68 | + @Override public String getPassword() { /* ... */ } |
| 69 | + @Override public String getBucketName() { /* ... */ } |
| 70 | +
|
| 71 | + /* provide a cryptoManager */ |
| 72 | + @Override |
| 73 | + protected CryptoManager cryptoManager() { |
| 74 | + KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType"); |
| 75 | + FileInputStream fis = new java.io.FileInputStream("keyStoreName"); |
| 76 | + char[] password = { 'a', 'b', 'c' }; |
| 77 | + javaKeyStore.load(fis, password); |
| 78 | + Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish"); |
| 79 | +
|
| 80 | + // AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key. |
| 81 | + AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build(); |
| 82 | +
|
| 83 | + CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter()) |
| 84 | + .defaultEncrypter(provider.encrypterForKey("myKey")).build(); |
| 85 | + } |
| 86 | +
|
| 87 | +} |
| 88 | +---- |
| 89 | +==== |
| 90 | +.The Annotation in the Document |
| 91 | +==== |
| 92 | +[source,java] |
| 93 | +---- |
| 94 | +@Document |
| 95 | +public class AddressWithEncStreet extends Address { |
| 96 | +
|
| 97 | + private @Encrypted String encStreet; |
| 98 | + . |
| 99 | + . |
| 100 | +---- |
| 101 | +==== |
| 102 | +.Usage in Code |
| 103 | +==== |
| 104 | +[source,java] |
| 105 | +---- |
| 106 | +AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street |
| 107 | +address.setCity("Santa Clara"); |
| 108 | +address.setEncStreet("Olcott Street"); |
| 109 | +addressEncryptedRepository.save(address); |
| 110 | +---- |
| 111 | +==== |
| 112 | +.Resulting Document |
| 113 | +==== |
| 114 | +[source,json] |
| 115 | +---- |
| 116 | +{ |
| 117 | + "_class": "AddressWithEncStreet", |
| 118 | + "city": "Santa Clara", |
| 119 | + "encrypted$encStreet": { |
| 120 | + "alg": "AEAD_AES_256_CBC_HMAC_SHA512", |
| 121 | + "ciphertext": "A/tJALmtixTxqj77ZUcUgMklIt3372DKD7l5FvbCzHNJMplbgQEv0RgSbxIfiRNr+uW2H7cokkcCW/F5YnQoXA==", |
| 122 | + "kid": "myKey" |
| 123 | + } |
| 124 | +} |
| 125 | +---- |
| 126 | +==== |
| 127 | + |
0 commit comments