Couchbase Field Level 加密

Couchbase 支持字段级加密。本节记录了如何将其与 Spring Data Couchbase 一起使用。spring-doc.cn

要求

  • Spring Data Couchbase 5.0.0-RC1 或更高版本。spring-doc.cn

概述

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

入门和配置

依赖

字段级加密可通过依赖项使用(请参阅字段级加密 )spring-doc.cn

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

HashiCorp Vault Transit 集成需要 Spring Vaultspring-doc.cn

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

提供 CryptoManager

需要通过覆盖 AbstractCouchbaseConfiguration 中的 cryptoManager() 方法来提供 CryptoManager。Spring Data Couchbase 以及从 CouchbaseClientFactory 进行的 Couchbase Java SDK 直接调用都将使用此CryptoManager。spring-doc.cn

@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;
}

将字段定义为 Encrypted。

  1. @Encrypted 将字段定义为 encrypted。spring-doc.cn

  2. @Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED) 定义一个字段,该字段在读取时可能会加密,也可能不会加密。写入时将被加密。spring-doc.cn

  3. @Encrypted(encrypter = “<encrypterAlias>”) 指定要用于加密的加密器的别名。请注意,这不是算法,而是在将加密程序添加到 CryptoManager 时指定的名称。spring-doc.cn

示例 1.AbstractCouchbase配置
@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;
  }

}
示例 2.文档中的批注
@Document
public class AddressWithEncStreet extends Address {

    private @Encrypted String encStreet;
    .
    .
例 3.在代码中的用法
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setCity("Santa Clara");
address.setEncStreet("Olcott Street");
addressEncryptedRepository.save(address);
示例 4.生成的文档
{
  "_class": "AddressWithEncStreet",
   "city": "Santa Clara",
   "encrypted$encStreet": {
     "alg": "AEAD_AES_256_CBC_HMAC_SHA512",
     "ciphertext": "A/tJALmtixTxqj77ZUcUgMklIt3372DKD7l5FvbCzHNJMplbgQEv0RgSbxIfiRNr+uW2H7cokkcCW/F5YnQoXA==",
     "kid": "myKey"
   }
}