On this page本页内容
New in version 4.2.在版本4.2中新增。
KeyVault.createKey(keyManagementService, customerMasterKey, ["keyAltName"])
Adds a data encryption key to the key vault associated to the database connection. 将数据加密密钥添加到与数据库连接关联的密钥库中。Client-side field level encryption uses data encryption keys for supporting encryption and decryption of field values.客户端字段级加密使用数据加密密钥来支持字段值的加密和解密。
createKey()
has the following syntax:具有以下语法:
keyVault = db.getMongo().getKeyVault()
keyVault.createKey(
keyManagementService,
customerMasterKey,
[ "keyAltName" ]
)
keyManagementService | string |
The Key Management Service (KMS) to use for retrieving the Customer Master Key (CMK). Accepts the following parameters:
If the |
customerMasterKey | string or document | The Customer Master Key (CMK) to use for encrypting the data encryption key. Required if Provide the CMK as follows depending on your KMS provider:
keyManagementService is local and can be safely omitted. Prior to MongoDB 4.2.3, if keyManagementService is local this parameter must be an empty string " .
|
keyAltName | array of strings |
The alternative name for the data encryption key. Use The |
UUID unique identifier of the created data encryption key.UUID 唯一标识符。 |
The mongo
client-side field level encryption methods require a database connection with client-side field level encryption enabled. mongo
客户端字段级加密方法需要启用客户端字段级密码的数据库连接。If the current database connection was not initiated with client-side field level encryption enabled, either:如果当前数据库连接未在启用客户端字段级加密的情况下启动,请执行以下任一操作:
Use the 使用Mongo()
constructor from the mongo
shell to establish a connection with the required client-side field level encryption options. mongo
shell中的Mongo()
构造函数与所需的客户端字段级加密选项建立连接。The Mongo()
method supports the following Key Management Service (KMS) providers for Customer Master Key (CMK) management:Mongo()
方法支持以下密钥管理服务(KMS)提供商进行客户主密钥(CMK)管理:
or
mongo
shell command line options to establish a connection with the required options. mongo
shell命令行选项与所需选项建立连接。The following example is intended for rapid evaluation of client-side field level encryption. 以下示例用于快速评估客户端字段级加密。For specific examples of using 有关在每个受支持的KMS提供程序中使用KeyVault.createKey()
with each supported KMS provider, see Create a Data Encryption Key.KeyVault.createKey()
的具体示例,请参阅创建数据加密密钥。
Configuring client-side field level encryption for a locally managed key requires specifying a base64-encoded 96-byte string with no line breaks. 为本地管理的密钥配置客户端字段级加密需要指定一个base64编码的96字节字符串,不带换行符。The following operation generates a key that meets the stated requirements and loads it into the 以下操作生成满足所述要求的密钥,并将其加载到mongoshell中:mongo
shell:
TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')") mongosh --nodb --shell --eval "var TEST_LOCAL_KEY='$TEST_LOCAL_KEY'"
Create the client-side field level encryption object using the generated local key string:使用生成的本地密钥字符串创建客户端字段级加密对象:
var ClientSideFieldLevelEncryptionOptions = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } }
Use the 使用Mongo()
constructor to create a database connection with the client-side field level encryption options. Mongo()
构造函数创建带有客户端字段级加密选项的数据库连接。Replace the 更换mongodb://myMongo.example.net
URI with the connection string URI of the target cluster.mongodb://myMongo.example.net
带有目标群集的连接字符串URI的URI。
encryptedClient = Mongo(
"mongodb://myMongo.example.net:27017/?replSetName=myMongo",
ClientSideFieldLevelEncryptionOptions
)
Retrieve the 检索keyVault
object and use the KeyVault.createKey()
method to create a new data encryption key using the locally managed key:keyVault
对象并使用KeyVault.createKey()
方法使用本地托管密钥创建新的数据加密密钥:
keyVault = encryptedClient.getKeyVault() keyVault.createKey("local", ["data-encryption-key"])
If successful, 如果成功,createKey()
returns the UUID
of the new data encryption key. createKey()
将返回新数据加密密钥的UUID。To retrieve the new data encryption key document from the key vault, either:要从密钥库中检索新的数据加密密钥文档,请执行以下操作之一:
Use 使用getKey()
to retrieve the created key by UUID
.getKey()
检索UUID创建的密钥。
-or-
getKeyByAltName()
to retrieve the key by its alternate name.getKeyByAltName()
按备用名称检索密钥。