When working with a MongoDB Enterprise or MongoDB Atlas cluster, you can use 使用MongoDB Enterprise或MongoDB Atlas集群时,您可以使用mongosh to configure Queryable Encryption Client-Side Field Level Encryption and connect with encryption support. mongosh配置可查询加密客户端字段级加密,并连接加密支持。Both Queryable Encryption and CSFLE use data encryption keys for supporting encryption and decryption of field values, and store this encryption key material in a Key Management Service (KMS).可查询加密和CSFLE都使用数据加密键来支持字段值的加密和解密,并将此加密键材料存储在键管理服务(KMS)中。
mongosh supports the following KMS providers for use with Queryable Encryption and CSFLE:支持以下KMS提供程序与可查询加密和CSFLE一起使用:
Amazon Web Services KMS亚马逊网络服务KMSAzure Key VaultAzure键库Google Cloud Platform KMS谷歌云平台KMSLocally Managed Keyfile本地管理键文件
Create a Data Encryption Key创建数据加密键
The following procedure uses 以下过程使用mongosh to create a data encryption key for field level encryption.mongosh为字段级加密创建数据加密键。
Use the tabs below to select the KMS appropriate for your deployment:使用下面的选项卡选择适合您部署的KMS:
Amazon Web Services KMS
Launch the mongosh Shell.启动mongoshShell。
mongosh Shell.Create a 使用mongosh session without connecting to a running database by using the --nodb option:--nodb选项在不连接到正在运行的数据库的情况下创建mongosh会话:
mongosh --nodbCreate the Encryption Configuration.创建加密配置。
Configuring client-side field level encryption for the AWS KMS requires an AWS Access Key ID and its associated Secret Access Key. The AWS Access Key must correspond to an IAM user with all List and Read permissions for the KMS service.为AWS KMS配置客户端字段级加密需要AWS访问键ID及其关联的键访问键。AWS访问键必须对应于具有KMS服务所有列表和读取权限的IAM用户。
In 在mongosh, create a new AutoEncryptionOpts variable for storing the client-side field level encryption configuration, which contains these credentials:mongosh中,创建一个新的AutoEncryptionOpts变量来存储客户端字段级加密配置,其中包含以下凭据:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"aws" : {
"accessKeyId" : "YOUR_AWS_ACCESS_KEY_ID",
"secretAccessKey" : "YOUR_AWS_SECRET_ACCESS_KEY"
}
}
}
Fill in the values for 根据需要填写YOUR_AWS_ACCESS_KEY_ID and YOUR_AWS_SECRET_ACCESS_KEY as appropriate.YOUR_AWS_ACCESS_KEY_ID和YOUR_AWS_SECRET_ACCES_KEY的值。
Connect with Encryption Support.与加密支持连接。
In 在mongosh, use the Mongo() constructor to establish a database connection to the target cluster. mongosh中,使用Mongo()构造函数建立到目标集群的数据库连接。Specify the AutoEncryptionOpts document as the second parameter to the 将Mongo() constructor to configure the connection for client-side field level encryption:AutoEncryptionOpts文档指定为Mongo()构造函数的第二个参数,以配置客户端字段级加密的连接:
csfleDatabaseConnection = Mongo(
"mongodb://replaceMe.example.net:27017/?replicaSet=myMongoCluster",
autoEncryptionOpts
)
Replace the 将replaceMe.example.net URI with the connection string for the target cluster.replaceMe.example.net URI替换为目标集群的连接字符串。
Create the Key Vault Object.创建键库对象。
Create the 使用keyVault object using the getKeyVault() shell method:getKeyVault()shell方法创建keyVault对象:
keyVault = csfleDatabaseConnection.getKeyVault();Create the Encryption Key.创建加密键。
Create the data encryption key using the 使用createKey() shell method:createKey()shell方法创建数据加密键:
keyVault.createKey(
"aws",
{ region: "regionname", key: "awsarn" },
[ "keyAlternateName" ]
)
Where:其中:
The first parameter must be第一个参数必须是"aws"to specify the configured Amazon Web Services KMS."aws",以指定配置的Amazon Web Services KMS。The second parameter must be a document containing the following:第二个参数必须是包含以下内容的文档:the AWS region you are connecting to, such as您正在连接的AWS区域,例如us-west-2us-west-2the Amazon Resource Name (ARN) to the AWS customer master key (CMK).将亚马逊资源名称(ARN)转换为AWS客户主键(CMK)。
The third parameter may be an array of one or more第三参数可以是数据加密键的一个或多个keyAltNamesfor the data encryption key. Each key alternate name must be unique.keyAltNames的数组。每个键备用名称必须唯一。getKeyVault()creates a unique index onkeyAltNamesto enforce uniqueness on the field if one does not already exist.getKeyVault()在keyAltNames上创建一个唯一索引,以在字段不存在的情况下强制其唯一性。Key alternate names facilitate data encryption key findability.键备用名称便于数据加密键的查找。
If successful, 如果成功,createKey() returns the UUID of the new data encryption key. To retrieve the new data encryption key document from the key vault, either:createKey()将返回新数据加密键的UUID。要从键库中检索新的数据加密键文档,请执行以下操作之一:
Use使用getKey()to retrieve the created key by its UUID, orgetKey()按UUID检索创建的键,或Use如果指定了备用名称,请使用getKeyByAltName()to retrieve the key by its alternate name, if specified.getKeyByAltName()按其备用名称检索键。
Azure Key Vault
Launch the mongosh Shell.启动mongoshShell。
mongosh Shell.Create a 使用mongosh session without connecting to a running database by using the --nodb option:--nodb选项在不连接到正在运行的数据库的情况下创建mongosh会话:
mongosh --nodbCreate the Encryption Configuration.创建加密配置。
Configuring client-side field level encryption for Azure Key Vault requires a valid Tenant ID, Client ID, and Client Secret.为Azure键库配置客户端字段级加密需要有效的租户ID、客户端ID和客户端键。
In 在mongosh, create a new AutoEncryptionOpts variable for storing the client-side field level encryption configuration, which contains these credentials:mongosh中,创建一个新的AutoEncryptionOpts变量来存储客户端字段级加密配置,其中包含以下凭据:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"azure" : {
"tenantId" : "YOUR_TENANT_ID",
"clientId" : "YOUR_CLIENT_ID",
"clientSecret" : "YOUR_CLIENT_SECRET"
}
}
}
Fill in the values for 根据需要填写YOUR_TENANT_ID, YOUR_CLIENT_ID, and YOUR_CLIENT_SECRET as appropriate.YOUR_TENNT_ID、YOUR_CLIENT_ID和YOUR_CLIENT_SECRET的值。
Connect with Encryption Support.与加密支持连接。
In 在mongosh, use the Mongo() constructor to establish a database connection to the target cluster. mongosh中,使用Mongo()构造函数建立到目标集群的数据库连接。Specify the AutoEncryptionOpts document as the second parameter to the 将AutoEncryptionOpts文档指定为Mongo() constructor to configure the connection for client-side field level encryption:Mongo()构造函数的第二个参数,以配置客户端字段级加密的连接:
csfleDatabaseConnection = Mongo(
"mongodb://replaceMe.example.net:27017/?replicaSet=myMongoCluster",
autoEncryptionOpts
)
Replace the 将replaceMe.example.net URI with the connection string for the target cluster.replaceMe.example.net URI替换为目标集群的连接字符串。
Create the Key Vault Object.创建键库对象。
Create the 使用keyVault object using the getKeyVault() shell method:getKeyVault() shell方法创建keyVault对象:
keyVault = csfleDatabaseConnection.getKeyVault();Create the Encryption Key.创建加密键。
Create the data encryption key using the 使用createKey() shell method:createKey()shell方法创建数据加密键:
keyVault.createKey(
"azure",
{ keyName: "keyvaultname", keyVaultEndpoint: "endpointname" },
[ "keyAlternateName" ]
)
Where:其中:
The first parameter must be第一个参数必须是"azure"to specify the configured Azure Key Vault."azure"才能指定配置的azure键库。The second parameter must be a document containing:第二个参数必须是包含以下内容的文档:the name of your Azure Key VaultAzure键库的名称the DNS name of the Azure Key Vault to use (e.g.要使用的Azure键库的DNS名称(例如my-key-vault.vault.azure.net)my-key-vault.vault.azure.net)
The third parameter may be an array of one or more第三参数可以是数据加密键的一个或多个keyAltNamesfor the data encryption key. Each key alternate name must be unique.keyAltNames的数组。每个键备用名称必须唯一。getKeyVault()creates a unique index onkeyAltNamesto enforce uniqueness on the field if one does not already exist. Key alternate names facilitate data encryption key findability.getKeyVault()在keyAltNames上创建一个唯一索引,以在字段不存在的情况下强制其唯一性。键备用名称便于数据加密键的查找。
If successful, 如果成功,createKey() returns the UUID of the new data encryption key. To retrieve the new data encryption key document from the key vault, either:createKey()将返回新数据加密键的UUID。要从键库中检索新的数据加密键文档,请执行以下操作之一:
Use使用getKey()to retrieve the created key by its UUID, orgetKey()按UUID检索创建的键,或Use如果指定了备用名称,请使用getKeyByAltName()to retrieve the key by its alternate name, if specified.getKeyByAltName()按其备用名称检索键。
Google Cloud KMS
Launch the mongosh Shell.启动mongosh Shell。
mongosh Shell.Create a 使用mongosh session without connecting to a running database by using the --nodb option:--nodb选项在不连接到正在运行的数据库的情况下创建mongosh会话:
mongosh --nodbCreate the Encryption Configuration.创建加密配置。
Configuring client-side field level encryption for the GCP KMS requires your GCP Email and its associated Private Key.为GCP KMS配置客户端字段级加密需要GCP电子邮件及其关联的私钥。
In 在mongosh, create a new AutoEncryptionOpts variable for storing the client-side field level encryption configuration, which contains these credentials:mongosh中,创建一个新的AutoEncryptionOpts变量来存储客户端字段级加密配置,其中包含以下凭据:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"gcp" : {
"email" : "YOUR_GCP_EMAIL",
"privateKey" : "YOUR_GCP_PRIVATEKEY"
}
}
}
Fill in the values for 根据需要填写YOUR_GCP_EMAIL and YOUR_GCP_PRIVATEKEY as appropriate.YOUR_GCP_EMAIL和YOUR_GCP-PRIVATEKEY的值。
Connect with Encryption Support.与加密支持连接。
In 在mongosh, use the Mongo() constructor to establish a database connection to the target cluster. mongosh中,使用Mongo()构造函数建立到目标集群的数据库连接。Specify the AutoEncryptionOpts document as the second parameter to the 将Mongo() constructor to configure the connection for client-side field level encryption:AutoEncryptionOpts文档指定为Mongo()构造函数的第二个参数,以配置客户端字段级加密的连接:
csfleDatabaseConnection = Mongo(
"mongodb://replaceMe.example.net:27017/?replicaSet=myMongoCluster",
autoEncryptionOpts
)
Replace the 将replaceMe.example.net URI with the connection string for the target cluster.replaceMe.example.net URI替换为目标集群的连接字符串。
Create the Key Vault Object.创建键库对象。
Create the 使用keyVault object using the getKeyVault() shell method:getKeyVault()shell方法创建keyVault对象:
keyVault = csfleDatabaseConnection.getKeyVault();Create the Encryption Key.创建加密键。
Create the data encryption key using the 使用createKey() shell method:createKey()shell方法创建数据加密键:
keyVault.createKey(
"gcp",
{ projectId: "projectid",
location: "locationname",
keyRing: "keyringname",
keyName: "keyname"
},
[ "keyAlternateName" ]
)
Where:其中:
The first parameter must be第一个参数必须是"gcp"to specify the configured Google Cloud KMS."gcp",以指定配置的Google Cloud KMS。The second parameter must be a document containing第二个参数必须是包含以下内容的文档projectidis the name of your GCP project, such as是GCP项目的名称,例如my-projectlocationnameis the location of the KMS keyring, such as是KMS键环的位置,例如globalkeyringnameis the name of the KMS keyring, such as是KMS键环的名称,例如my-keyringkeynameis the name of your key.是键名称。
The third parameter may be an array of one or more第三参数可以是数据加密键的一个或多个keyAltNamesfor the data encryption key. Each key alternate name must be unique.keyAltNames的数组。每个键备用名称必须唯一。getKeyVault()creates a unique index onkeyAltNamesto enforce uniqueness on the field if one does not already exist.getKeyVault()在keyAltNames上创建一个唯一索引,以在字段不存在的情况下强制其唯一性。Key alternate names facilitate data encryption key findability.键备用名称便于数据加密键的查找。
If successful, 如果成功,createKey() returns the UUID of the new data encryption key. To retrieve the new data encryption key document from the key vault, either:createKey()将返回新数据加密键的UUID。要从键库中检索新的数据加密键文档,请执行以下操作之一:
Use使用getKey()to retrieve the created key by its UUID, orgetKey()按UUID检索创建的键,或Use如果指定了备用名称,请使用getKeyByAltName()to retrieve the key by its alternate name, if specified.getKeyByAltName()按其备用名称检索键。
Local Keyfile
Launch the mongosh Shell.启动mongoshShell。
mongosh Shell.Create a 使用mongosh session without connecting to a running database by using the --nodb option:--nodb选项在不连接到正在运行的数据库的情况下创建mongosh会话:
mongosh --nodbGenerate an Encryption Key.生成加密键。
To configure client-side field level encryption for a locally managed key, you must specify a base64-encoded 96-byte string with no line breaks. Run the following command in 要为本地管理的键配置客户端字段级加密,您必须指定一个没有换行符的base64编码的96字节字符串。在mongosh to generate a key matching these requirements:mongosh中运行以下命令以生成符合这些要求的键:
crypto.randomBytes(96).toString('base64')
You will need this key in the next step.下一步您将需要此键。
Create the Encryption Configuration.创建加密配置。
In 在mongosh, create a new AutoEncryptionOpts variable for storing the client-side field level encryption configuration, replacing MY_LOCAL_KEY with the key generated in step 1:mongosh中,创建一个新的AutoEncryptionOpts变量来存储客户端字段级加密配置,用步骤1中生成的键替换MY_LOCAL_KEY:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, "MY_LOCAL_KEY")
}
}
}Connect with Encryption Support.与加密支持连接。
In 在mongosh, use the Mongo() constructor to establish a database connection to the target cluster. mongosh中,使用Mongo()构造函数建立到目标集群的数据库连接。Specify the AutoEncryptionOpts document as the second parameter to the 将Mongo() constructor to configure the connection for client-side field level encryption:AutoEncryptionOpts文档指定为Mongo()构造函数的第二个参数,以配置客户端字段级加密的连接:
csfleDatabaseConnection = Mongo(
"mongodb://replaceMe.example.net:27017/?replicaSet=myMongoCluster",
autoEncryptionOpts
)Create the Key Vault Object.创建键库对象。
Create the 使用keyVault object using the getKeyVault() shell method:getKeyVault()shell方法创建keyVault对象:
keyVault = csfleDatabaseConnection.getKeyVault();Create the Encryption Key.创建加密键。
Create the data encryption key using the 使用createKey() shell method:createKey()shell方法创建数据加密键:
keyVault.createKey(
"local",
[ "keyAlternateName" ]
)
Where:其中:
The first parameter must be第一个参数必须是localto specify the configured Locally Managed Key.local,才能指定配置的本地管理键。The second parameter may be an array of one or more第二参数可以是数据加密键的一个或多个keyAltNamesfor the data encryption key.keyAltNames的数组。Each key alternate name must be unique.每个键备用名称必须唯一。getKeyVault()creates a unique index onkeyAltNamesto enforce uniqueness on the field if one does not already exist. Key alternate names facilitate data encryption key findability.getKeyVault()在keyAltNames上创建一个唯一索引,以在字段不存在的情况下强制其唯一性。键备用名称便于数据加密键的查找。
If successful, 如果成功,createKey() returns the UUID of the new data encryption key. To retrieve the new data encryption key document from the key vault, either:createKey()将返回新数据加密键的UUID。要从键库中检索新的数据加密键文档,请执行以下操作之一:
Use使用getKey()to retrieve the created key by its UUID, orgetKey()按UUID检索创建的键,或Use如果指定了备用名称,请使用getKeyByAltName()to retrieve the key by its alternate name, if specified.getKeyByAltName()按其备用名称检索键。