バージョン1.16の新機能。
定義
MongoDB\Database::createEncryptedCollection()暗号化されたコレクションを明示的に作成します。
function createEncryptedCollection( string $collectionName, MongoDB\Driver\ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options ): array This method will automatically create data keys for any encrypted fields where
keyIdisnull. Data keys will be created using MongoDB\Driver\ClientEncryption::createDataKey() and the provided$kmsProviderand$masterKeyparameters. A copy of the modifiedencryptedFieldsoption will be returned in addition to the result from creating the collection.この方法は、既存の
MongoDB\Clientオブジェクトの自動暗号化設定には影響しません。 ユーザーは、createEncryptedCollection()を使用して暗号化されたコレクションを作成した後、自動暗号化を設定する必要があります。
パラメーター
$collectionName: string- 作成する暗号化されたコレクションの名前。
$clientEncryption: MongoDB$Driver$ClientEncryption- データキーの作成に使用される ClientEncryption オブジェクト。
$kmsProvider: string- KMS プロバイダー(例:「local」、「Amazon Web Services」)、新しいデータキーの暗号化に使用されます。 これは、MongoDB$Driver\ClientEncryption::createKey() の
$kmsProviderパラメータに対応します。 $masterKey: array|null新しいデータキーの暗号化に使用される KMS 固有のキー オプション。これは、MongoDB\Driver\ClientEncryption::createDataKey() の
masterKeyオプションに相当します。$kmsProviderが「local」の場合、これはnullになります。$options: 配列必要なオプションを指定する配列。
$optionsパラメータはMongoDB\Database::createCollection()と同じオプションをサポートしています。encryptedFieldsオプションは必要です。
Return Values
作成コマンドからの結果ドキュメント(typeMap オプションに準拠した配列またはオブジェクト)と変更された オプションを含むタイプ(要素の 2encryptedFields つ)。
エラーと例外
データキーまたはコレクションの作成でエラーが発生した場合は、 MongoDB\Exception\CreateEncryptedCollectionException 。 元の例外と変更されたencryptedFields getPrevious()getEncryptedFields()オプションには、それぞれ メソッドと メソッドを介してアクセスできます。
MongoDB\Exception\InvalidArgumentException は、パラメータまたはオプションの解析に関連するエラーの場合は です。
例
次の例では、 testデータベースに暗号化されたusersコレクションを作成しています。 usersコレクション内のssnフィールドは、暗号化された string フィールドとして定義されます。
// 96-byte master key used to encrypt/decrypt data keys define('LOCAL_MASTERKEY', '...'); $client = new MongoDB\Client; $clientEncryption = $client->createClientEncryption([ 'keyVaultNamespace' => 'keyvault.datakeys', 'kmsProviders' => [ 'local' => ['key' => new MongoDB\BSON\Binary(base64_decode(LOCAL_MASTERKEY), 0)], ], ); [$result, $encryptedFields] = $client->test->createEncryptedCollection( 'users', $clientEncryption, 'local', null, [ 'encryptedFields' => [ 'fields' => [ ['path' => 'ssn', 'bsonType' => 'string', 'keyId' => null], ], ], ] );
暗号化されたコレクションが正常に作成された場合、$result には コマンドからの応答ドキュメントが含まれ、 にはサブタイプcreate $encryptedFields['fields'][0]['keyId'](UUID)を持つMongoDB\ BSON\Binaryオブジェクトが含まれます。4
変更されたencryptedFieldsオプションは、自動暗号化を有効にする新しいMongoDB\Clientを構築するために使用できます。
$encryptedClient = new MongoDB\Client( null, // Connection string [], // Additional connection string options [ 'autoEncryption' => [ 'keyVaultNamespace' => 'keyvault.datakeys', 'kmsProviders' => [ 'local' => ['key' => new MongoDB\BSON\Binary(base64_decode(LOCAL_MASTERKEY), 0)], ], 'encryptedFieldsMap' => [ 'test.users' => $encryptedFields, ], ], ] );
その他の参照
createコマンドリファレンス(MongoDB マニュアル)