类:Mongo::Crypt::ExplicitEncrypter Private

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
定义于:
lib/ Mongo/crypt/explicit_encrypter.rb

Overview

此类是私有 API 的一部分。 应尽可能避免使用此类,因为它将来可能会被删除或更改。

ExplicitEncrypter 是执行显式加密操作并处理所有关联选项和实例变量的对象。

实例方法摘要折叠

构造函数详情

#initialize (key_vault_client, key_vault_namespace, kms_providers, kms_tls_options, timeout_ms = nil) ⇒ ExplicitEncrypter

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

创建一个新的 ExplicitEncrypter 对象。

参数:

  • key_vault_client ( Mongo::Client )

    用于连接到密钥保管库集合的 Mongo::Client实例。

  • key_vault_namespace ( string )

    密钥保管库集合的命名空间,格式为“db_name.collection_name”。

  • kms_providers ( Crypt:: KMS::Credentials )

    KMS 配置信息的哈希值。

  • kms_tls_options (哈希)

    用于连接到 KMS 提供商的 TLS 选项。 哈希键应为 KSM 提供程序名称;值应该是 TLS 连接选项的哈希值。 这些选项相当于 Mongo::Client 的 TLS 连接选项。

  • timeout_ms ( Integer | nil ) (默认为: nil

    在此对象上执行的每个操作的超时时间。



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 40

def 初始化(key_vault_client, key_vault_namespace, kms_providers, kms_tls_options, timeout_ms = nil)
  Crypt.validate_ffi!
  @crypt_handle = 句柄.new(
    kms_providers,
    kms_tls_options,
    explicit_encryption_only: true
  )
  @encryption_io = EncryptionIO.new(
    key_vault_client: key_vault_client,
    metadata_client: nil,
    key_vault_namespace: key_vault_namespace
  )
  @timeout_ms = timeout_ms
end

实例方法详细信息

# add_key_alt_name ( ID , key_alt_name) ⇒ BSON::Document | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

在具有给定 ID 的密钥保管库集合中为密钥添加 key_alt_name。

参数:

  • id ( BSON::Binary )

    用于添加新密钥替代名称的密钥 ID。

  • key_alt_name ( string )

    要添加的新密钥替代名称。

返回:

  • ( BSON::Document | nil )

    在添加密钥替代名称之前描述已识别密钥的文档,如果没有此类密钥,则为 nil。



203
204
205
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 203

def add_key_alt_name(id, key_alt_name)
  @encryption_io.add_key_alt_name(id, key_alt_name, timeout_ms: @timeout_ms)
end

# create_and_insert_data_key (master_key_document, key_alt_names, key_material = nil) ⇒ BSON::Binary

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

生成用于加密/解密的数据密钥,并将该密钥存储在 KMS 集合中。 生成的密钥使用 KMS 主密钥进行加密。

参数:

  • master_key_document ( Mongo::Crypt::KMS::MasterKeyDocument )

    包含主加密密钥参数的主密钥文档。

  • key_alt_names ( Array<String> | nil )

    一个可选的字符串数组,用于指定新数据键的备用名称。

  • key_material ( string | nil ) (默认为: nil

    可选的96字节用作正在创建的数据密钥的自定义密钥材料。 如果给出了 key_material 选项,则使用自定义密钥材料加密和解密数据。

返回:

  • ( BSON::Binary )

    新数据密钥的16字节 UUID,为BSON::Binary对象,类型为 :uuid。



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 70

def create_and_insert_data_key(master_key_document, key_alt_names, key_material = nil)
  data_key_document = Crypt::DataKeyContext.new(
    @crypt_handle,
    @encryption_io,
    master_key_document,
    key_alt_names,
    key_material
  ).run_state_machine(timeout_holder)

  @encryption_io.insert_data_key(
    data_key_document, timeout_ms: timeout_holder.剩余超时毫秒!
  ).Inserted_id
end

#解密(值) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

对已加密的值进行解密

参数:

  • ( BSON::Binary )

    将要解密的子类型为6 (密文)的 BSON 二进制对象

返回:

  • ( Object )

    解密后的值



188
189
190
191
192
193
194
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 188

def 解密()
  Crypt::ExplicitDecryptionContext.new(
    @crypt_handle,
    @encryption_io,
    { v:  }
  ).run_state_machine(timeout_holder)[' v ']
end

# delete_key ( ID ) = "Operation::Result"

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

从密钥保管库集合中删除具有给定 ID 的密钥。

参数:

  • id ( BSON::Binary )

    要删除的密钥的 ID。

返回:



213
214
215
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 213

def delete_key(id)
  @encryption_io.delete_key(id, timeout_ms: @timeout_ms)
end

# encrypt (value, options) ⇒ BSON::Binary

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

注意:

:key_id 和 :key_alt_name 选项是互斥的。 只需一次即可执行显式加密。

使用指定的加密密钥和算法加密值

如果加密算法设立为“Indexed”。 应设立查询类型

only if encryption algorithm is set to "Indexed". The only allowed
value is "equality".

参数:

  • ( Object )

    要加密的值

  • 选项 哈希

选项哈希 ( options ):

  • :key_id ( BSON::Binary )

    :uuid 类型的BSON::Binary对象,表示存储在密钥保管库集合中的加密密钥的 UUID。

  • :key_alt_name string

    加密密钥的备用名称。

  • :algorithm string

    用于加密值的算法。 有效算法为“AEAD_AES_ 256 _CBC_HMAC_SHA_ 512 -Deterministic”、“AEAD_AES_ 256 _CBC_HMAC_SHA_ 512 -Random”、“Indexed”、“Unindexed”。

  • :contention_factor ( Integer | nil )

    加密算法设置为“Indexed”时要应用的争用因子。 如果未提供,则默认值为0 。 仅当加密算法设置为“Indexed”时,才应设置争用因子。

  • query_type ( string | nil )

    要应用的查询类型

返回:

  • ( BSON::Binary )

    表示加密值的子类型为6 (密文)的 BSON 二进制对象

引发:

  • ( ArgumentError )

    如果设置了contention_factor或query_type,并且算法不是“已索引”。



113
114
115
116
117
118
119
120
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 113

def 加密(, 选项)
  Crypt::ExplicitEncryptionContext.new(
    @crypt_handle,
    @encryption_io,
    { v:  },
    选项
  ).run_state_machine(timeout_holder)[' v ']
end

# encrypt_expression (表达式, options) ⇒ BSON::Binary

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

注意:

范围算法仅处于实验阶段。 它不是

注意:

:key_id 和 :key_alt_name 选项是互斥的。 只需一次即可执行显式加密。

加密匹配表达式或聚合表达式以查询范围索引。

仅当 queryType 为“范围”且算法为“ Range ”时才支持。 @note:Range算法仅处于实验阶段。 这不是为了

for public use. It is subject to breaking changes.

# @param [ Hash ] 供公众使用的选项。

例子:

加密匹配表达式。

encryption.encrypt_expression(
  {'$and' =>  [{'field' => {'$gt' => 10}}, {'field' =>  {'$lt' => 20 }}]}
)

加密聚合表达式。

encryption.encrypt_expression(
  {'$and' =>  [{'$gt' => ['$field', 10]}, {'$lt' => ['$field', 20]}}
)
{$and: [{$gt: [<fieldpath>, <value1>]}, {$lt: [<fieldpath>, <value2>]}]

参数:

  • 表达式(expression) (哈希)

    要加密的表达式。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( options ):

  • :key_id ( BSON::Binary )

    :uuid 类型的BSON::Binary对象,表示存储在密钥保管库集合中的加密密钥的 UUID。

  • :key_alt_name string

    加密密钥的备用名称。

  • :algorithm string

    用于加密表达式的算法。 唯一允许的值为“Range”(范围)

  • :contention_factor ( Integer | nil )

    要应用的争用系数 如果未提供,则默认值为0 。

  • query_type ( string | nil )

    要应用的查询类型。 唯一允许的值为“范围”。

  • :range_opts 哈希 | nil

    为支持“范围”查询的Queryable Encryption字段指定索引选项。 允许的选项包括:

    • :min

    • :max

    • :trim_factor

    • :sparsity

    • :Precision

    min、max、trim_factor、稀疏性和精度必须与目标集合的encryptedFields中设立的值匹配。 对于 double 和十进制128,最小/最大/精度必须全部设立,或者全部取消设置。

返回:

  • ( BSON::Binary )

    子类型为6 (密文)的BSON二进制对象,表示加密的表达式。

引发:

  • ( ArgumentError )

    如果在选项中设立了不允许的值。



173
174
175
176
177
178
179
180
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 173

def encrypt_expression(表达式(expression), 选项)
  Crypt::ExplicitEncryptionExpressionContext.new(
    @crypt_handle,
    @encryption_io,
    { v: 表达式(expression) },
    选项
  ).run_state_machine(timeout_holder)[' v ']
end

#get_key(id) ⇒ BSON::Document | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

查找具有给定 ID 的单个键。

参数:

  • id ( BSON::Binary )

    要获取的密钥的 ID。

返回:

  • ( BSON::Document | nil )

    找到的密钥文档,如果未找到,则返回 nil。



223
224
225
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 223

def get_key(id)
  @encryption_io.get_key(id, timeout_ms: @timeout_ms)
end

# get_key_by_alt_name (key_alt_name) ⇒ BSON::Document | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回密钥保管库集合中具有给定 key_alt_name 的密钥。

参数:

  • key_alt_name ( string )

    用于查找密钥的密钥替代名称。

返回:

  • ( BSON::Document | nil )

    找到的密钥文档,如果未找到,则返回 nil。



233
234
235
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 233

def get_key_by_alt_name(key_alt_name)
  @encryption_io.get_key_by_alt_name(key_alt_name, timeout_ms: @timeout_ms)
end

# get_keys Collection::View

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回密钥保管库集合中的所有密钥。

rubocop:disable Naming/AccessorMethodName 此方法的名称在 FLE 规范中定义

返回:



242
243
244
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 242

def get_keys
  @encryption_io.get_keys(timeout_ms: @timeout_ms)
end

# remove_key_alt_name ( ID , key_alt_name) ⇒ BSON::Document | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

从密钥保管库集合中具有给定 ID 的密钥中删除 key_alt_name。

参数:

  • id ( BSON::Binary )

    要删除密钥替代名称的密钥的 ID。

  • key_alt_name ( string )

    要删除的密钥替代名称。

返回:

  • ( BSON::Document | nil )

    在删除密钥替代名称之前描述已识别密钥的文档,如果没有此类密钥,则为 nil。



254
255
256
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 254

def remove_key_alt_name(id, key_alt_name)
  @encryption_io.remove_key_alt_name(id, key_alt_name, timeout_ms: @timeout_ms)
end

# rewrap_many_data_key (过滤, opts = {}) ⇒ Crypt::RewrapManyDataKeyResult

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

解密多个数据密钥并使用新的 master_key(重新)加密它们,

or with their current master_key if a new one is not given.

参数:

  • 筛选器 (哈希)

    用于查找要更新的密钥的筛选器。

  • 选项 哈希

返回:



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ Mongo/crypt/explicit_encrypter.rb', line 269

def rewrap_many_data_key(筛选器, opts = {})
  validate_rewrap_options!(opts)

  master_key_document = master_key_for_provider(opts)

  rewrap_result = Crypt::RewrapManyDataKeyContext.new(
    @crypt_handle,
    @encryption_io,
    筛选器,
    master_key_document
  ).run_state_machine(timeout_holder)

  return RewrapManyDataKeyResult.new(nil) if rewrap_result.nil?

  更新 = update_from_data_key_documents(rewrap_result.获取(' v '))
  RewrapManyDataKeyResult.new(
    @encryption_io.update_data_keys(更新, timeout_ms: @timeout_ms)
  )
end