AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

getKeyVault()(mongoshメソッド)

getKeyVault()

現在のデータベース接続のKeyVaultオブジェクトを返します。 KeyVaultオブジェクトは、クライアント側のフィールドレベル暗号化 のデータ暗号化キー管理をサポートしています。

次の値を返します。

現在のデータベース接続のKeyVaultオブジェクト。

このコマンドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

getKeyVault() の構文は次のとおりです。

keyVault = db.getMongo().getKeyVault();

次のデータ暗号化キー管理メソッドにアクセスするには、 KeyVaultオブジェクトを使用します。

次の例では、クライアント側のフィールドレベル暗号化構成にローカルで管理されているキーを使用しています。

The mongosh client-side field level encryption methods require a database connection with client-side field level encryption enabled. If the current database connection was not initiated with client-side field level encryption enabled, either:

The getKeyVault() method automatically creates a unique index on the keyAltNames field with a partial index filter for only documents where keyAltNames exists. getKeyVault() creates this index in the key vault collection. This prevents any two data encryption keys in the same key vault from having the same key alternative name and therefore avoids ambiguity around which data encryption key is appropriate for encryption/decryption.

警告

Do not drop the unique index created by getKeyVault(). Client-Side Field Level Encryption operations depend on server-enforced uniqueness of keyAltNames. Removing the index may lead to unexpected or unpredictable behavior.

次の例では、クライアント側のフィールドレベル暗号化構成にローカルで管理されているキーを使用しています。

1

mongoshクライアントを起動します。

mongosh --nodb
2

ローカルで管理されているキーのクライアント側フィールドレベル暗号化を構成するには、改行を含まない base64 でエンコードされた 96 バイトのstringを生成します。

const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
3

生成されたローカルキーstringを使用して、クライアント側のフィールドレベル暗号化オプションを作成します。

let autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)
}
}
}
4

データベース接続を作成するには、クライアント側のフィールドレベル暗号化オプションが構成されたMongo()コンストラクターを使用します。 mongodb://myMongo.example.net URI を、ターゲットクラスターの接続string URIに置き換えます。

encryptedClient = Mongo(
"mongodb://myMongo.example.net:27017/?replSetName=myMongo",
autoEncryptionOpts
)

Use the getKeyVault() method to retrieve the key vault object:

keyVault = encryptedClient.getKeyVault()

クライアント側のフィールドレベル暗号化を有効にして MongoDB 接続を開始する方法に関する詳細なドキュメントについては、 Mongo()を参照してください。

このページを評価