Couchbase Field Level Encrytpion

Couchbase 支持 Field Level Encryption。本部分介绍如何配合 Spring Data Couchbase 使用。

Requirements

  • Spring Data Couchbase 5.0.0-RC1 或更高版本。

Overview

使用 com.couchbase.client.java.encryption.annotation.Encrypted (@Encrypted) 注释的字段将在写入时自动加密,并在读取时自动解密。未加密的字段可以通过指定 @Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED) 迁移到加密。

Getting Started & Configuration

Dependencies

场级加密可通过依赖项获取 (参见 Field Level Encryption)

    <groupId>com.couchbase.client</groupId>
    <artifactId>couchbase-encryption</artifactId>

HashiCorp Vault Transit 集成需要 Spring Vault

    <groupId>org.springframework.vault</groupId>
    <artifactId>spring-vault-core</artifactId>

Providing a CryptoManager

必须通过覆盖 AbstractCouchbaseConfiguration 中的 cryptoManager() 方法来提供一个 CryptoManager。Spring Data Couchbase 和由 CouchbaseClientFactory 发出的 Couchbase Java SDK 直接调用将使用此 CryptoManager

@Override
protected CryptoManager cryptoManager() {
  KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType");
  FileInputStream fis = new java.io.FileInputStream("keyStoreName");
  char[] password = { 'a', 'b', 'c' };
  javaKeyStore.load(fis, password);
  Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish");

  // AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key.
  AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build();

  CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter())
    .defaultEncrypter(provider.encrypterForKey("myKey")).build();
  return cryptoManager;
}

Defining a Field as Encrypted.

  1. @Encrypted 将字段定义为加密字段。

  2. @Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED) 定义读取时可能加密或不加密的字段。写入时将加密。

  3. @Encrypted(encrypter = "<encrypterAlias>") 指定用于加密的加密器的别名。请注意,这不是算法,而是添加到 CryptoManager 时指定的名称。

Example

Example 1. AbstractCouchbaseConfiguration
@Configuration
@EnableCouchbaseRepositories("<parent-dir-of-repository-interfaces>")
@EnableReactiveCouchbaseRepositories("<parent-dir-of-repository-interfaces>")
static class Config extends AbstractCouchbaseConfiguration {

  // Usual Setup
  @Override public String getConnectionString() { /* ... */ }
  @Override public String getUserName() { /* ... */ }
  @Override public String getPassword() { /* ... */ }
  @Override public String getBucketName() { /* ... */ }

  /* provide a cryptoManager */
  @Override
  protected CryptoManager cryptoManager() {
    KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType");
    FileInputStream fis = new java.io.FileInputStream("keyStoreName");
    char[] password = { 'a', 'b', 'c' };
    javaKeyStore.load(fis, password);
    Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish");

    // AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key.
    AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build();

    CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter())
      .defaultEncrypter(provider.encrypterForKey("myKey")).build();
    return cryptoManager;
  }

}
Example 2. The Annotation in the Document
@Document
public class AddressWithEncStreet extends Address {

    private @Encrypted String encStreet;
    .
    .
Example 3. Usage in Code
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setCity("Santa Clara");
address.setEncStreet("Olcott Street");
addressEncryptedRepository.save(address);
Example 4. Resulting Document
{
  "_class": "AddressWithEncStreet",
   "city": "Santa Clara",
   "encrypted$encStreet": {
     "alg": "AEAD_AES_256_CBC_HMAC_SHA512",
     "ciphertext": "A/tJALmtixTxqj77ZUcUgMklIt3372DKD7l5FvbCzHNJMplbgQEv0RgSbxIfiRNr+uW2H7cokkcCW/F5YnQoXA==",
     "kid": "myKey"
   }
}