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

KeyVault.createKey()(mongoshメソッド)

KeyVault.createKey(keyManagementService, customerMasterKey, ["keyAltName"])

データベース接続に関連付けられたキーヴォールトにデータ暗号化キーを追加します。 クライアント側のフィールドレベル暗号化は、フィールド値の暗号化と復号化をサポートするためにデータ暗号化キーを使用します。

次の値を返します。

作成されたデータ暗号化キーのUUID一意の識別子。

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

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

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

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

keyVault = db.getMongo().getKeyVault()
keyVault.createKey(
keyManagementService,
customerMasterKey,
[ "keyAltName" ]
)
Parameter
タイプ
説明

keyManagementService

string

必須

CMK(Customer Master Key)の取得に使用するKMS ( KMS )。 次のパラメータを受け入れます。

  • aws Amazon Web Services KMSの。 customerMasterKey の CMK(Customer Master Key) stringを指定する必要があります。

  • azure Azure Key Vault用の。 customerMasterKeyの CMK(Customer Master Key)ドキュメントを指定する必要があります。

    バージョン 5.0 の新機能。

  • gcp Google Cloud Platform KMS用。 customerMasterKeyの CMK(Customer Master Key)ドキュメントを指定する必要があります。

    バージョン 5.0 の新機能。

database connectionが指定された KMS で構成されていない場合、データ暗号化キーの作成は失敗します。

customerMasterKey

文字列またはドキュメント

データ暗号化キーの暗号化に使用する CMK(Customer Master Key)。 keyManagementServiceawsazure 、またはgcpの場合に必須です。

KMS プロバイダーに応じて、次のように CMK を指定します。

  • For the Amazon Web Services KMS の場合は、マスター キーの完全なAmazonリソース名(ARN)を単一の文字列として指定します。

  • Azure Key Vault KMS の場合は、次のキーと値のペアを含むドキュメントを指定します。

    • keyVaultEndpoint - 使用する Azure Key Vault の DNS 名

    • keyVersion - 任意。 keyNameで指定されたキーのバージョン(該当する場合)

    バージョン 5.0 の新機能。

  • Google Cloud Platform KMSの場合は、次のキーと値のペアを含むドキュメントを指定します。

    • projectId - GCP プロジェクト名

    • location - KMS キーの場所

    • keyRing - KMS キーリングの名前(多くの場合「グローバル」)

    • keyName - 使用するキーの名前

    • keyVersion - 任意。 keyNameで指定されたキーのバージョン(該当する場合)

    バージョン 5.0 の新機能。

createKey() requests that the KMS encrypt the data encryption key material using the specified CMK. If the CMK does not exist or if the AutoEncryptionOpts configuration does not have sufficient privileges to use the CMK, createKey() returns an error.

このパラメータは、 keyManagementServicelocalである場合は効果がなく、省略しても問題ありません。

keyAltName

文字列の配列

任意

データ暗号化キーの別名。 特定のデータ暗号化キーを見つけやすくするには、またはコメントに類似するものとしてkeyAltNameを使用します。

メソッドは、 getKeyVault()が存在するドキュメントのみを対象とする 部分インデックス keyAltNamesフィルターを使用して、 フィールドに 一意のインデックスkeyAltNames を自動的に作成します。

options

ドキュメント

任意

新しいキーのオプションを指定するドキュメント。 optionsには次のフィールドがあります:

  • masterKey: データを暗号化するための新しいマスター キー。

  • keyAltNames: マスターキーごとに 1 つの代替名の配列。

  • keyMaterial: キーを作成するために使用されるバインデータ。

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 following example is intended for rapid evaluation of client-side field level encryption. For specific examples of using KeyVault.createKey() with each supported KMS provider, see Create a Data Key.

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
)

Retrieve the keyVault object and use the KeyVault.createKey() method to create a new data encryption key using the locally managed key:

keyVault = encryptedClient.getKeyVault()
keyVault.createKey("local", ["data-encryption-key"])

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:

  • getKey()を使用して、 UUIDによって作成されたキーを取得します。

    または

  • 別名でキーを検索するには、 getKeyByAltName()を使用します。

このページを評価