Docs Menu
Docs Home
/
データベース マニュアル
/ / / / /

Azure で自動クライアント側フィールドレベル暗号化を使用

このガイドでは、Azure Key Vault を使用してクライアント側フィールド レベル暗号化(CSFLE)対応のアプリケーションを構築する方法について説明します。

このガイドの手順を完了すると、次のものが作成されます。

  • Azure Key Vault インスタンスでホストされている CMK 。

  • カスタマー マスター キーを使用して暗号化されたフィールドを持つドキュメントを挿入する動作するクライアント アプリケーション。

このガイドのコードを完了して実行するには、 インストール要件ページに記載されているように開発環境を設定する必要があります。

このガイド全体を通して、コード例ではプレースホルダー テキストを使用します。例を実行する前に、これらのプレースホルダーを独自の値に置き換えてください。

以下に例を挙げます。

dek_id := "<Your Base64 DEK ID>"

引用符で囲まれたすべての内容を DEK ID に置き換えます。

dek_id := "abc123"

ページの右側にある Select your languageドロップダウン メニューから、コード例を表示するプログラミング言語の選択します。

サンプルアプリケーションの完全なコードを表示するには、言語セレクターでプログラミング言語を選択します。

完全な C# アプリケーション

// You are viewing the C# driver code examples.
// Use the dropdown menu to select a different driver.
// You are viewing the Golang driver code examples.
// Use the dropdown menu to select a different driver.

重要

Go{0go build または を使用してこのガイドのgo run コードをビルドまたは実行する場合は、 を有効にするために常にcse CSFLEビルド制約を含めてください。ビルド制約を含めた例については、次の shell コマンドを参照してください。

go run -tags cse insert-encrypted-document.go
// You are viewing the Java synchronous driver code examples.
// Use the dropdown menu to select a different driver.
// You are viewing the Node.js driver code examples.
// Use the dropdown menu to select a different driver.
# You are viewing the Python driver code examples.
# Use the dropdown menu to select a different driver.
1
  1. Azureにログインします。

  2. Azure Active Directory でアプリケーションを登録する

    Azure Active Directory にアプリケーションを登録するには、Microsoft の公式の「 Microsoft ID プラットフォームにアプリケーションを登録するクイック スタート 」に従います。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • テナント ID

    • clientId

    • クライアントシークレット

    このチュートリアルの後半では、 kmsProvidersオブジェクトを構築するためにこれらが必要になります。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • テナント ID

    • clientId

    • クライアントシークレット

    このチュートリアルの後半では、 kmsProvidersオブジェクトを構築するためにこれらが必要になります。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • テナント ID

    • clientId

    • クライアントシークレット

    Azure Virtual Machines 内でクライアントを実行していない限り、このチュートリアルの後半でkmsProvidersオブジェクトを構築するためにこれらの認証情報が必要になります。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • テナント ID

    • clientId

    • クライアントシークレット

    このチュートリアルの後半では、 kmsProvidersオブジェクトを構築するためにこれらが必要になります。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • テナント ID

    • clientId

    • クライアントシークレット

    このチュートリアルの後半では、 kmsProvidersオブジェクトを構築するためにこれらが必要になります。

2
  1. Azure Key Vault と CMK の作成

    新しいAzure Key Vaultインスタンスとカスタマー マスター キーを作成するには、Microsoft の公式の セット に従い、 Azure portal クイック スタートを使用してAzure Key Vault からキーを取得します。

    注意

    カスタマー マスター キーの RSA キー サイズは、2048 ビットまたは 4096 ビットである必要があります。

    重要

    認証情報の記録

    次の認証情報を必ず記録してください。

    • キー名

    • キー識別子(このガイドの後半ではkeyVaultEndpointと参照されます)

    • キー バージョン

    このチュートリアルの後半では、 dataKeyOptsオブジェクトを構築するためにこれらが必要になります。

  2. 権限の付与

1

encryption.__keyVault名前空間のkeyAltNamesフィールドに一意のインデックスを作成します。

使用する MongoDB ドライバーに対応するタブを選択します。

var connectionString = "<Your MongoDB URI>";
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
var keyVaultClient = new MongoClient(connectionString);
var indexOptions = new CreateIndexOptions<BsonDocument>();
indexOptions.Unique = true;
indexOptions.PartialFilterExpression = new BsonDocument { { "keyAltNames", new BsonDocument { { "$exists", new BsonBoolean(true) } } } };
var builder = Builders<BsonDocument>.IndexKeys;
var indexKeysDocument = builder.Ascending("keyAltNames");
var indexModel = new CreateIndexModel<BsonDocument>(indexKeysDocument, indexOptions);
var keyVaultDatabase = keyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.ToString());
// Drop the Key Vault Collection in case you created this collection
// in a previous run of this application.
keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName);
// Drop the database storing your encrypted fields as all
// the DEKs encrypting those fields were deleted in the preceding line.
keyVaultClient.GetDatabase("medicalRecords").DropCollection("patients");
var keyVaultCollection = keyVaultDatabase.GetCollection<BsonDocument>(keyVaultNamespace.CollectionName.ToString());
keyVaultCollection.Indexes.CreateOne(indexModel);
uri := "<Your MongoDB URI>"
keyVaultClient, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
return fmt.Errorf("Connect error for regular client: %v", err)
}
defer func() {
_ = keyVaultClient.Disconnect(context.TODO())
}()
keyVaultColl := "__keyVault"
keyVaultDb := "encryption"
keyVaultNamespace := keyVaultDb + "." + keyVaultColl
keyVaultIndex := mongo.IndexModel{
Keys: bson.D{{"keyAltNames", 1}},
Options: options.Index().
SetUnique(true).
SetPartialFilterExpression(bson.D{
{"keyAltNames", bson.D{
{"$exists", true},
}},
}),
}
// Drop the Key Vault Collection in case you created this collection
// in a previous run of this application.
if err = keyVaultClient.Database(keyVaultDb).Collection(keyVaultColl).Drop(context.TODO()); err != nil {
log.Fatalf("Collection.Drop error: %v", err)
}
// Drop the database storing your encrypted fields as all
// the DEKs encrypting those fields were deleted in the preceding line.
if err = keyVaultClient.Database("medicalRecords").Collection("patients").Drop(context.TODO()); err != nil {
log.Fatalf("Collection.Drop error: %v", err)
}
_, err = keyVaultClient.Database(keyVaultDb).Collection(keyVaultColl).Indexes().CreateOne(context.TODO(), keyVaultIndex)
if err != nil {
panic(err)
}
String connectionString = "<Your MongoDB URI>";
String keyVaultDb = "encryption";
String keyVaultColl = "__keyVault";
String keyVaultNamespace = keyVaultDb + "." + keyVaultColl;
MongoClient keyVaultClient = MongoClients.create(connectionString);
// Drop the Key Vault Collection in case you created this collection
// in a previous run of this application.
keyVaultClient.getDatabase(keyVaultDb).getCollection(keyVaultColl).drop();
// Drop the database storing your encrypted fields as all
// the DEKs encrypting those fields were deleted in the preceding line.
keyVaultClient.getDatabase("medicalRecords").getCollection("patients").drop();
MongoCollection keyVaultCollection = keyVaultClient.getDatabase(keyVaultDb).getCollection(keyVaultColl);
IndexOptions indexOpts = new IndexOptions().partialFilterExpression(new BsonDocument("keyAltNames", new BsonDocument("$exists", new BsonBoolean(true) ))).unique(true);
keyVaultCollection.createIndex(new BsonDocument("keyAltNames", new BsonInt32(1)), indexOpts);
keyVaultClient.close();
const uri = "<Your Connection String>";
const keyVaultDatabase = "encryption";
const keyVaultCollection = "__keyVault";
const keyVaultNamespace = `${keyVaultDatabase}.${keyVaultCollection}`;
const keyVaultClient = new MongoClient(uri);
await keyVaultClient.connect();
const keyVaultDB = keyVaultClient.db(keyVaultDatabase);
// Drop the Key Vault Collection in case you created this collection
// in a previous run of this application.
await keyVaultDB.dropDatabase();
// Drop the database storing your encrypted fields as all
// the DEKs encrypting those fields were deleted in the preceding line.
await keyVaultClient.db("medicalRecords").dropDatabase();
const keyVaultColl = keyVaultDB.collection(keyVaultCollection);
await keyVaultColl.createIndex(
{ keyAltNames: 1 },
{
unique: true,
partialFilterExpression: { keyAltNames: { $exists: true } },
}
);
connection_string = "<your connection string here>"
key_vault_coll = "__keyVault"
key_vault_db = "encryption"
key_vault_namespace = f"{key_vault_db}.{key_vault_coll}"
key_vault_client = MongoClient(connection_string)
# Drop the Key Vault Collection in case you created this collection
# in a previous run of this application.
key_vault_client.drop_database(key_vault_db)
# Drop the database storing your encrypted fields as all
# the DEKs encrypting those fields were deleted in the preceding line.
key_vault_client["medicalRecords"].drop_collection("patients")
key_vault_client[key_vault_db][key_vault_coll].create_index(
[("keyAltNames", ASCENDING)],
unique=True,
partialFilterExpression={"keyAltNames": {"$exists": True}},
)
2
  1. Azure Key Vault 認証情報の追加

    サービス アカウントの認証情報を CSFLE 対応のクライアント コードに追加します。

    var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
    var provider = "azure";
    var azureKmsOptions = new Dictionary<string, object>
    {
    { "tenantId", "<Your Azure Tenant ID>" },
    { "clientId", "<Your Azure Client ID>" },
    { "clientSecret", "<Your Azure Client Secret>" },
    };

    また、KMS プロバイダーの名前とそれに続くコロンとカスタム名を含む string を渡すことで、KMS プロバイダーのカスタム名を指定することもできます。KMS プロバイダーに一意の名前を指定すると、同じタイプの複数の KMS プロバイダーを指定できます。

    例、次のコードに示すように、KMS 認証情報変数でAzure KMS プロバイダーに "my_zure_provider" という名前を付けることができます。

    var kmsProviderCredentials = new Dictionary<string, IReadOnlyDictionary<string, object>>();
    var kmsOptions = new Dictionary<string, object>
    {
    { "tenantId", _appSettings["Azure:TenantId"] }, // Your Azure tenant ID
    { "clientId", _appSettings["Azure:ClientId"] }, // Your Azure client ID
    { "clientSecret", _appSettings["Azure:ClientSecret"] } // Your Azure client secret
    };
    kmsProviderCredentials.Add("azure:my_azure_provider", kmsOptions);
    provider := "azure"
    kmsProviders := map[string]map[string]interface{}{
    provider: {
    "tenantId": "<Your Azure Tenant ID>",
    "clientId": "<Your Azure Client ID>",
    "clientSecret": "<Your Azure Client Secret>",
    },
    }
    String kmsProvider = "azure";
    Map<String, Map<String, Object>> kmsProviderDetails = new HashMap<String, Map<String, Object>>();
    Map<String, Object> providerDetails = new HashMap<>();
    providerDetails.put("tenantId", "<Azure account organization>");
    providerDetails.put("clientId", "<Azure client ID>");
    providerDetails.put("clientSecret", "<Azure client secret>");
    kmsProviderDetails.put(kmsProvider, providerDetails);

    また、KMS プロバイダーの名前とそれに続くコロンとカスタム名を含む string を渡すことで、KMS プロバイダーのカスタム名を指定することもできます。KMS プロバイダーに一意の名前を指定すると、同じタイプの複数の KMS プロバイダーを指定できます。

    例、次のコードに示すように、KMS 認証情報変数でAzure KMS プロバイダーに "my_zure_provider" という名前を付けることができます。

    Map<String, Object> kmsProviderDetails = new HashMap<>();
    kmsProviderDetails.put("tenantId", getEnv("AZURE_TENANT_ID")); // Your Azure tenant ID
    kmsProviderDetails.put("clientId", getEnv("AZURE_CLIENT_ID")); // Your Azure client ID
    kmsProviderDetails.put("clientSecret", getEnv("AZURE_CLIENT_SECRET")); // Your Azure client secret
    Map<String, Map<String, Object>> kmsProviderCredentials = new HashMap<String, Map<String, Object>>();
    kmsProviderCredentials.put("azure:my_azure_provider", kmsProviderDetails);

    Tip

    Azure Virtual Machines で管理されている ID

    クライアントが Azure Virtual Machine(VM)で実行されている場合は、VM がマネージド ID を使用してキーヴォールトを認証できるようにできます。

    Azure VM が認証情報を自動的に提供できるようにするには、次のコードに示すように、Azure 認証情報を含むマップではなく空のマップを割り当てます。

    String kmsProvider = "azure";
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> providerDetails = new HashMap<>();
    kmsProviders.put(kmsProvider, providerDetails);
    const provider = "azure";
    const kmsProviderCredentials = {
    azure: {
    tenantId: "<Your Tenant ID>",
    clientId: "<Your Client ID>",
    clientSecret: "<Your Client Secret>",
    },
    };

    また、KMS プロバイダーの名前とそれに続くコロンとカスタム名を含む string を渡すことで、KMS プロバイダーのカスタム名を指定することもできます。KMS プロバイダーに一意の名前を指定すると、同じタイプの複数の KMS プロバイダーを指定できます。

    例、次のコードに示すように、KMS 認証情報変数でAzure KMS プロバイダーに "my_zure_provider" という名前を付けることができます。

    kmsProviderCredentials = {
    "azure:my_azure_provider": {
    tenantId: process.env.AZURE_TENANT_ID, // Your Azure tenant ID
    clientId: process.env.AZURE_CLIENT_ID, // Your Azure client ID
    clientSecret: process.env.AZURE_CLIENT_SECRET, // Your Azure client secret
    },
    };
    provider = "azure"
    kms_provider_credentials = {
    provider: {
    "tenantId": "<Azure account organization>",
    "clientId": "<Azure client ID>",
    "clientSecret": "<Azure client secret>",
    }
    }

    また、KMS プロバイダーの名前とそれに続くコロンとカスタム名を含む string を渡すことで、KMS プロバイダーのカスタム名を指定することもできます。KMS プロバイダーに一意の名前を指定すると、同じタイプの複数の KMS プロバイダーを指定できます。

    例、次のコードに示すように、KMS 認証情報変数でAzure KMS プロバイダーに "my_zure_provider" という名前を付けることができます。

    kms_provider_credentials = {
    "azure:my_azure_provider": {
    "tenantId": os.environ['AZURE_TENANT_ID'], # Your Azure tenant ID
    "clientId": os.environ['AZURE_CLIENT_ID'], # Your Azure client ID
    "clientSecret": os.environ['AZURE_CLIENT_SECRET'] # Your Azure client secret
    }
    }
  2. キー情報を追加する

    次のコードを更新して、CMK を指定します。

    Tip

    このガイドの「 カスタマー マスター キーの 作成 」ステップで、 カスタマー マスター キーの ARN とリージョンを記録しました。

    kmsProviderCredentials.Add(provider, azureKmsOptions);
    var dataKeyOptions = new DataKeyOptions(
    masterKey: new BsonDocument
    {
    { "keyName", "<Your Azure Key Name>" },
    { "keyVaultEndpoint", "<Your Azure Key Vault Endpoint>" },
    });
    masterKey := map[string]interface{}{
    "keyVaultEndpoint": "<Your Azure Key Vault Endpoint>",
    "keyName": "<Your Azure Key Name>",
    }
    BsonDocument masterKeyProperties = new BsonDocument();
    masterKeyProperties.put("provider", new BsonString(kmsProvider));
    masterKeyProperties.put("keyName", new BsonString("<Azure key name>"));
    masterKeyProperties.put("keyVaultEndpoint", new BsonString("<Azure key vault endpoint"));
    const masterKey = {
    keyVaultEndpoint: "<Your Key Vault Endpoint>",
    keyName: "<Your Key Name>",
    };
    master_key = {
    "keyName": "<Azure key name>",
    "keyVersion": "<Azure key version>",
    "keyVaultEndpoint": "<Azure key vault endpoint/key identifier>",
    }
  3. データ暗号化キーの生成

    このチュートリアルのステップ 1で宣言された変数を使用して、 データ暗号化キー を生成します。

    var clientEncryptionOptions = new ClientEncryptionOptions(
    keyVaultClient: keyVaultClient,
    keyVaultNamespace: keyVaultNamespace,
    kmsProviders: kmsProviderCredentials
    );
    var clientEncryption = new ClientEncryption(clientEncryptionOptions);
    var dataKeyId = clientEncryption.CreateDataKey(provider, dataKeyOptions, CancellationToken.None);
    var dataKeyIdBase64 = Convert.ToBase64String(GuidConverter.ToBytes(dataKeyId, GuidRepresentation.Standard));
    Console.WriteLine($"DataKeyId [base64]: {dataKeyIdBase64}");
    clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(keyVaultNamespace).
    SetKmsProviders(kmsProviders)
    clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
    if err != nil {
    return fmt.Errorf("NewClientEncryption error %v", err)
    }
    defer func() {
    _ = clientEnc.Close(context.TODO())
    }()
    dataKeyOpts := options.DataKey().
    SetMasterKey(masterKey)
    dataKeyID, err := clientEnc.CreateDataKey(context.TODO(), provider, dataKeyOpts)
    if err != nil {
    return fmt.Errorf("create data key error %v", err)
    }
    fmt.Printf("DataKeyId [base64]: %s\n", base64.StdEncoding.EncodeToString(dataKeyID.Data))
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
    .keyVaultMongoClientSettings(MongoClientSettings.builder()
    .applyConnectionString(new ConnectionString(connectionString))
    .build())
    .keyVaultNamespace(keyVaultNamespace)
    .kmsProviders(kmsProviderDetails)
    .build();
    MongoClient regularClient = MongoClients.create(connectionString);
    ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    BsonBinary dataKeyId = clientEncryption.createDataKey(kmsProvider, new DataKeyOptions().masterKey(masterKeyProperties));
    String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
    System.out.println("DataKeyId [base64]: " + base64DataKeyId);
    clientEncryption.close();
    const client = new MongoClient(uri);
    await client.connect();
    const encryption = new ClientEncryption(client, {
    keyVaultNamespace,
    kmsProviderCredentials,
    });
    const key = await encryption.createDataKey(provider, {
    masterKey: masterKey,
    });
    console.log("DataKeyId [base64]: ", key.toString("base64"));
    await keyVaultClient.close();
    await client.close();

    注意

    Import ClientEncryption

    Node.js ドライバー v6.0 以降を使用する場合は、 mongodb ClientEncryptionをインポートする必要があります。

    以前のドライバー バージョンの場合は、 mongodb-client-encryption ClientEncryptionをインポートします。

    key_vault_database = "encryption"
    key_vault_collection = "__keyVault"
    key_vault_namespace = f"{key_vault_database}.{key_vault_collection}"
    client = MongoClient(connection_string)
    client_encryption = ClientEncryption(
    kms_provider_credentials, # pass in the kms_provider_credentials variable from the previous step
    key_vault_namespace,
    client,
    CodecOptions(uuid_representation=STANDARD),
    )
    data_key_id = client_encryption.create_data_key(provider, master_key)
    base_64_data_key_id = base64.b64encode(data_key_id)
    print("DataKeyId [base64]: ", base_64_data_key_id)

Tip

詳細

Azure Key Vault を使用するときにクライアント アプリケーションがデータ暗号化キーを作成する方法を示す図は、「アーキテクチャ 」を参照してください。

Azure Key Vault でホストされているカスタマー マスター キーで暗号化されたデータ暗号化キーを作成するオプションの詳細については、「 kmsProviders オブジェクト」と「 dataKeyOps オブジェクト 」を参照してください

Tip

参照: 完全なコード

データ暗号化キーを作成するための完全なコードについては、Githubリポジトリ を参照してください。

データ暗号化キーを作成するための完全なコードについては、Githubリポジトリ を参照してください。

データ暗号化キーを作成するための完全なコードについては、Githubリポジトリ を参照してください。

データ暗号化キーを作成するための完全なコードについては、Githubリポジトリ を参照してください。

データ暗号化キーを作成するための完全なコードについては、Githubリポジトリ を参照してください。

3

Tip

このチュートリアルの残りの手順は、前の手順で作成されたファイルとは別のファイルで実行します。

このファイルの完全なコードを表示するには、Githubリポジトリ を参照してください。

このファイルの完全なコードを表示するには、Githubリポジトリ を参照してください。

このファイルの完全なコードを表示するには、Githubリポジトリ を参照してください。

このファイルの完全なコードを表示するには、Githubリポジトリ を参照してください。

このファイルの完全なコードを表示するには、Githubリポジトリ を参照してください。

  1. キーヴォールトコレクションの名前空間の指定

    キーヴォールト コレクションの名前空間としてencryption.__keyVaultを指定します。

    var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
    keyVaultNamespace := "encryption.__keyVault"
    String keyVaultNamespace = "encryption.__keyVault";
    const keyVaultNamespace = "encryption.__keyVault";
    key_vault_namespace = "encryption.__keyVault"
  2. Azure 認証情報の指定

    azure KMS プロバイダーと Azure 認証情報を指定します。

    var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>();
    var provider = "azure";
    var azureKmsOptions = new Dictionary<string, object>
    {
    { "tenantId", "<Your Azure Tenant ID>" },
    { "clientId", "<Your Azure Client ID>" },
    { "clientSecret", "<Your Azure Client Secret>" },
    };
    kmsProviders.Add(provider, azureKmsOptions);
    kmsProviders := map[string]map[string]interface{}{
    "azure": {
    "tenantId": "<Your Azure Tenant ID>",
    "clientId": "<Your Azure Client ID>",
    "clientSecret": "<Your Azure Client Secret>",
    },
    }
    String kmsProvider = "azure";
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> providerDetails = new HashMap<>();
    providerDetails.put("tenantId", "<Azure account organization>");
    providerDetails.put("clientId", "<Azure client ID>");
    providerDetails.put("clientSecret", "<Azure client secret>");
    kmsProviders.put(kmsProvider, providerDetails);

    Tip

    Azure Virtual Machines で管理されている ID

    クライアントが Azure Virtual Machine(VM)で実行されている場合は、VM がマネージド ID を使用してキーヴォールトを認証できるようにできます。

    Azure VM が認証情報を自動的に提供できるようにするには、次のコードに示すように、Azure 認証情報を含むマップではなく空のマップを割り当てます。

    String kmsProvider = "azure";
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> providerDetails = new HashMap<>();
    kmsProviders.put(kmsProvider, providerDetails);
    const kmsProviders = {
    azure: {
    tenantId: "<Your Tenant ID>",
    clientId: "<Your Client ID>",
    clientSecret: "<Your Client Secret>",
    },
    };
    provider = "azure"
    kms_providers = {
    "azure": {
    "tenantId": "<Azure account organization>",
    "clientId": "<Azure client ID>",
    "clientSecret": "<Azure client secret>",
    }
    }
  3. コレクションの暗号化スキーマの作成

    Tip

    データ暗号化キー base64 ID の追加

    base 64 DEK ID を含むように、次のコードを必ず更新してください。 この値は、このガイドの「 データ暗号化キーの生成 」ステップで取得されました。

    var keyId = "<Your base64 DEK ID here>";
    var schema = new BsonDocument
    {
    { "bsonType", "object" },
    {
    "encryptMetadata",
    new BsonDocument("keyId", new BsonArray(new[] { new BsonBinaryData(Convert.FromBase64String(keyId), BsonBinarySubType.UuidStandard) }))
    },
    {
    "properties",
    new BsonDocument
    {
    {
    "ssn", new BsonDocument
    {
    {
    "encrypt", new BsonDocument
    {
    { "bsonType", "int" },
    { "algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" }
    }
    }
    }
    },
    {
    "bloodType", new BsonDocument
    {
    {
    "encrypt", new BsonDocument
    {
    { "bsonType", "string" },
    { "algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Random" }
    }
    }
    }
    },
    {
    "medicalRecords", new BsonDocument
    {
    {
    "encrypt", new BsonDocument
    {
    { "bsonType", "array" },
    { "algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Random" }
    }
    }
    }
    },
    {
    "insurance", new BsonDocument
    {
    { "bsonType", "object" },
    {
    "properties", new BsonDocument
    {
    {
    "policyNumber", new BsonDocument
    {
    {
    "encrypt", new BsonDocument
    {
    { "bsonType", "int" },
    { "algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    };
    var schemaMap = new Dictionary<string, BsonDocument>();
    schemaMap.Add(dbNamespace, schema);
    dek_id := "<Your Base64 DEK ID>"
    schema_template := `{
    "bsonType": "object",
    "encryptMetadata": {
    "keyId": [
    {
    "$binary": {
    "base64": "%s",
    "subType": "04"
    }
    }
    ]
    },
    "properties": {
    "insurance": {
    "bsonType": "object",
    "properties": {
    "policyNumber": {
    "encrypt": {
    "bsonType": "int",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
    }
    }
    }
    },
    "medicalRecords": {
    "encrypt": {
    "bsonType": "array",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
    }
    },
    "bloodType": {
    "encrypt": {
    "bsonType": "string",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
    }
    },
    "ssn": {
    "encrypt": {
    "bsonType": "int",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
    }
    }
    }
    }`
    schema := fmt.Sprintf(schema_template, dek_id)
    var schemaDoc bson.Raw
    if err := bson.UnmarshalExtJSON([]byte(schema), true, &schemaDoc); err != nil {
    return fmt.Errorf("UnmarshalExtJSON error: %v", err)
    }
    schemaMap := map[string]interface{}{
    dbName + "." + collName: schemaDoc,
    }
    String dekId = "<paste-base-64-encoded-data-encryption-key-id>>";
    Document jsonSchema = new Document().append("bsonType", "object").append("encryptMetadata",
    new Document().append("keyId", new ArrayList<>((Arrays.asList(new Document().append("$binary", new Document()
    .append("base64", dekId)
    .append("subType", "04")))))))
    .append("properties", new Document()
    .append("ssn", new Document().append("encrypt", new Document()
    .append("bsonType", "int")
    .append("algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")))
    .append("bloodType", new Document().append("encrypt", new Document()
    .append("bsonType", "string")
    .append("algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Random")))
    .append("medicalRecords", new Document().append("encrypt", new Document()
    .append("bsonType", "array")
    .append("algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Random")))
    .append("insurance", new Document()
    .append("bsonType", "object")
    .append("properties",
    new Document().append("policyNumber", new Document().append("encrypt", new Document()
    .append("bsonType", "int")
    .append("algorithm", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"))))));
    HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>();
    schemaMap.put("medicalRecords.patients", BsonDocument.parse(jsonSchema.toJson()));
    dataKey = "<Your base64 DEK ID>";
    const schema = {
    bsonType: "object",
    encryptMetadata: {
    keyId: [new Binary(Buffer.from(dataKey, "base64"), 4)],
    },
    properties: {
    insurance: {
    bsonType: "object",
    properties: {
    policyNumber: {
    encrypt: {
    bsonType: "int",
    algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
    },
    },
    },
    },
    medicalRecords: {
    encrypt: {
    bsonType: "array",
    algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
    },
    },
    bloodType: {
    encrypt: {
    bsonType: "string",
    algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
    },
    },
    ssn: {
    encrypt: {
    bsonType: "int",
    algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
    },
    },
    },
    };
    var patientSchema = {};
    patientSchema[namespace] = schema;
    dek_id = b"<paste-base-64-encoded-data-encryption-key-id>"
    json_schema = {
    "bsonType": "object",
    "encryptMetadata": {"keyId": [Binary(base64.b64decode(dek_id), UUID_SUBTYPE)]},
    "properties": {
    "insurance": {
    "bsonType": "object",
    "properties": {
    "policyNumber": {
    "encrypt": {
    "bsonType": "int",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
    }
    }
    },
    },
    "medicalRecords": {
    "encrypt": {
    "bsonType": "array",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
    }
    },
    "bloodType": {
    "encrypt": {
    "bsonType": "string",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
    }
    },
    "ssn": {
    "encrypt": {
    "bsonType": "int",
    "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
    }
    },
    },
    }
    patient_schema = {"medicalRecords.patients": json_schema}
  4. 自動暗号化共有ライブラリのロケーションを指定する

    var mongoBinariesPath = "<Full path to your Automatic Encryption Shared Library>";
    var extraOptions = new Dictionary<string, object>()
    {
    { "cryptSharedLibPath", mongoBinariesPath },
    };
    extraOptions := map[string]interface{}{
    "cryptSharedLibPath": "<Full path to your Automatic Encryption Shared Library>",
    }
    Map<String, Object> extraOptions = new HashMap<String, Object>();
    extraOptions.put("cryptSharedLibPath", "<Full path to your Automatic Encryption Shared Library>"));
    const extraOptions = {
    cryptSharedLibPath: "<Full path to your Automatic Encryption Shared Library>",
    };
    extra_options = {
    "cryptSharedLibPath": "<Full path to your Automatic Encryption Shared Library>"
    }

    注意

    自動暗号化オプション

    自動暗号化オプションは、暗号化共有ライブラリに構成情報を提供し、暗号化されたフィールドにアクセスするときにアプリケーションの動作を変更します。

    自動暗号化共有ライブラリの詳細については、「 CSFLE クエリ分析コンポーネントのインストールと構成 」ページを参照してください。

  5. MongoClient の作成

    前の手順で宣言された変数を使用する、次の自動暗号化設定を使用して MongoDB クライアント オブジェクトをインスタンス化します。

    MongoClientSettings.Extensions.AddAutoEncryption(); // .NET/C# Driver v3.0 or later only
    var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
    var autoEncryptionOptions = new AutoEncryptionOptions(
    keyVaultNamespace: keyVaultNamespace,
    kmsProviders: kmsProviders,
    schemaMap: schemaMap,
    extraOptions: extraOptions
    );
    clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
    var secureClient = new MongoClient(clientSettings);
    var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
    var autoEncryptionOptions = new AutoEncryptionOptions(
    keyVaultNamespace: keyVaultNamespace,
    kmsProviders: kmsProviders,
    schemaMap: schemaMap,
    extraOptions: extraOptions
    );
    clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
    var secureClient = new MongoClient(clientSettings);
    autoEncryptionOpts := options.AutoEncryption().
    SetKmsProviders(kmsProviders).
    SetKeyVaultNamespace(keyVaultNamespace).
    SetSchemaMap(schemaMap).
    SetExtraOptions(extraOptions)
    secureClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))
    if err != nil {
    return fmt.Errorf("Connect error for encrypted client: %v", err)
    }
    defer func() {
    _ = secureClient.Disconnect(context.TODO())
    }()
    MongoClientSettings clientSettings = MongoClientSettings.builder()
    .applyConnectionString(new ConnectionString(connectionString))
    .autoEncryptionSettings(AutoEncryptionSettings.builder()
    .keyVaultNamespace(keyVaultNamespace)
    .kmsProviders(kmsProviders)
    .schemaMap(schemaMap)
    .extraOptions(extraOptions)
    .build())
    .build();
    MongoClient mongoClientSecure = MongoClients.create(clientSettings);
    const secureClient = new MongoClient(connectionString, {
    autoEncryption: {
    keyVaultNamespace,
    kmsProviders,
    schemaMap: patientSchema,
    extraOptions: extraOptions,
    },
    });
    fle_opts = AutoEncryptionOpts(
    kms_providers, key_vault_namespace, schema_map=patient_schema, **extra_options
    )
    secureClient = MongoClient(connection_string, auto_encryption_opts=fle_opts)
4

CSFLE で有効化されたMongoClientインスタンスで、次のコード スニペットを使用して、暗号化されたフィールドを含むドキュメントをmedicalRecords.patients名前空間に挿入します。

var sampleDocFields = new BsonDocument
{
{ "name", "Jon Doe" },
{ "ssn", 145014000 },
{ "bloodType", "AB-" },
{
"medicalRecords", new BsonArray
{
new BsonDocument("weight", 180),
new BsonDocument("bloodPressure", "120/80")
}
},
{
"insurance", new BsonDocument
{
{ "policyNumber", 123142 },
{ "provider", "MaestCare" }
}
}
};
// Construct an auto-encrypting client
var secureCollection = secureClient.GetDatabase(db).GetCollection<BsonDocument>(coll);
// Insert a document into the collection
secureCollection.InsertOne(sampleDocFields);
test_patient := map[string]interface{}{
"name": "Jon Doe",
"ssn": 241014209,
"bloodType": "AB+",
"medicalRecords": []map[string]interface{}{{
"weight": 180,
"bloodPressure": "120/80",
}},
"insurance": map[string]interface{}{
"provider": "MaestCare",
"policyNumber": 123142,
},
}
if _, err := secureClient.Database(dbName).Collection(collName).InsertOne(context.TODO(), test_patient); err != nil {
return fmt.Errorf("InsertOne error: %v", err)
}

注意

未加工の BSON ドキュメントを作成する代わりに、 bsonタグを含む構造体をドライバーに直接渡してエンコードできます。

ArrayList<Document> medicalRecords = new ArrayList<>();
medicalRecords.add(new Document().append("weight", "180"));
medicalRecords.add(new Document().append("bloodPressure", "120/80"));
Document insurance = new Document()
.append("policyNumber", 123142)
.append("provider", "MaestCare");
Document patient = new Document()
.append("name", "Jon Doe")
.append("ssn", 241014209)
.append("bloodType", "AB+")
.append("medicalRecords", medicalRecords)
.append("insurance", insurance);
mongoClientSecure.getDatabase(recordsDb).getCollection(recordsColl).insertOne(patient);
try {
const writeResult = await secureClient
.db(db)
.collection(coll)
.insertOne({
name: "Jon Doe",
ssn: 241014209,
bloodType: "AB+",
medicalRecords: [{ weight: 180, bloodPressure: "120/80" }],
insurance: {
policyNumber: 123142,
provider: "MaestCare",
},
});
} catch (writeError) {
console.error("writeError occurred:", writeError);
}
def insert_patient(
collection, name, ssn, blood_type, medical_records, policy_number, provider
):
insurance = {"policyNumber": policy_number, "provider": provider}
doc = {
"name": name,
"ssn": ssn,
"bloodType": blood_type,
"medicalRecords": medical_records,
"insurance": insurance,
}
collection.insert_one(doc)
medical_record = [{"weight": 180, "bloodPressure": "120/80"}]
insert_patient(
secureClient.medicalRecords.patients,
"Jon Doe",
241014209,
"AB+",
medical_record,
123142,
"MaestCare",
)

ドキュメントを挿入すると、CSFLE 対応クライアントは次のようにドキュメントのフィールドを暗号化します。

{
"_id": { "$oid": "<_id of your document>" },
"name": "Jon Doe",
"ssn": {
"$binary": "<cipher-text>",
"$type": "6"
},
"bloodType": {
"$binary": "<cipher-text>",
"$type": "6"
},
"medicalRecords": {
"$binary": "<cipher-text>",
"$type": "6"
},
"insurance": {
"provider": "MaestCare",
"policyNumber": {
"$binary": "<cipher-text>",
"$type": "6"
}
}
}

Tip

参照: 完全なコード

暗号化されたフィールドを含むドキュメントを挿入するための完全なコードについては、Githubリポジトリを参照してください。

暗号化されたフィールドを含むドキュメントを挿入するための完全なコードについては、Githubリポジトリ を参照してください。

暗号化されたフィールドを含むドキュメントを挿入するための完全なコードについては、Githubリポジトリ を参照してください。

暗号化されたフィールドを含むドキュメントを挿入するための完全なコードについては、Githubリポジトリ を参照してください。

暗号化されたフィールドを含むドキュメントを挿入するための完全なコードについては、Githubリポジトリ を参照してください。

5

このガイドの「 暗号化されたフィールドを含むドキュメントの挿入 」ステップで挿入した暗号化されたフィールドを含むドキュメントを取得します。

CSFLE の機能を示すために、次のコード スニペットは、自動 CSFLE 用に構成されたクライアントと、自動 CSFLE 用に構成されていないクライアントを使用して、ドキュメントに対してクエリを実行します。

Console.WriteLine("Finding a document with regular (non-encrypted) client.");
var filter = Builders<BsonDocument>.Filter.Eq("name", "Jon Doe");
var regularResult = regularCollection.Find(filter).Limit(1).ToList()[0];
Console.WriteLine($"\n{regularResult}\n");
Console.WriteLine("Finding a document with encrypted client");
var ssnFilter = Builders<BsonDocument>.Filter.Eq("name", "Jon Doe");
var secureResult = secureCollection.Find(ssnFilter).Limit(1).First();
Console.WriteLine($"\n{secureResult}\n");
fmt.Println("Finding a document with regular (non-encrypted) client.")
var resultRegular bson.M
err = regularClient.Database(dbName).Collection(collName).FindOne(context.TODO(), bson.D{{"name", "Jon Doe"}}).Decode(&resultRegular)
if err != nil {
panic(err)
}
outputRegular, err := json.MarshalIndent(resultRegular, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", outputRegular)
fmt.Println("Finding a document with encrypted client")
var resultSecure bson.M
err = secureClient.Database(dbName).Collection(collName).FindOne(context.TODO(), bson.D{{"name", "Jon Doe"}}).Decode(&resultSecure)
if err != nil {
panic(err)
}
outputSecure, err := json.MarshalIndent(resultSecure, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", outputSecure)
System.out.println("Finding a document with regular (non-encrypted) client.");
Document docRegular = mongoClientRegular.getDatabase(recordsDb).getCollection(recordsColl).find(eq("name", "Jon Doe")).first();
System.out.println(docRegular.toJson());
System.out.println("Finding a document with encrypted client");
Document docSecure = mongoClientSecure.getDatabase(recordsDb).getCollection(recordsColl).find(eq("name", "Jon Doe")).first();
System.out.println(docSecure.toJson());
console.log("Finding a document with regular (non-encrypted) client.");
console.log(
await regularClient.db(db).collection(coll).findOne({ name: /Jon/ })
);
console.log("Finding a document with encrypted client");
console.log(
await secureClient.db(db).collection(coll).findOne({ name: /Jon/ })
);
print("Finding a document with regular (non-encrypted) client.")
result = regularClient.medicalRecords.patients.find_one({"name": "Jon Doe"})
pprint.pprint(result)
print("Finding a document with encrypted client")
pprint.pprint(secureClient.medicalRecords.patients.find_one({"name": "Jon Doe"}))

上記のコード スニペットの出力は次のようになります。

Finding a document with regular (non-encrypted) client.
{
_id: new ObjectId("629a452e0861b3130887103a"),
name: 'Jon Doe',
ssn: new Binary(Buffer.from("0217482732d8014cdd9ffdd6e2966e5e7910c20697e5f4fa95710aafc9153f0a3dc769c8a132a604b468732ff1f4d8349ded3244b59cbfb41444a210f28b21ea1b6c737508d9d30e8baa30c1d8070c4d5e26", "hex"), 6),
bloodType: new Binary(Buffer.from("0217482732d8014cdd9ffdd6e2966e5e79022e238536dfd8caadb4d7751ac940e0f195addd7e5c67b61022d02faa90283ab69e02303c7e4001d1996128428bf037dea8bbf59fbb20c583cbcff2bf3e2519b4", "hex"), 6),
'key-id': 'demo-data-key',
medicalRecords: new Binary(Buffer.from("0217482732d8014cdd9ffdd6e2966e5e790405163a3207cff175455106f57eef14e5610c49a99bcbd14a7db9c5284e45e3ee30c149354015f941440bf54725d6492fb3b8704bc7c411cff6c868e4e13c58233c3d5ed9593eca4e4d027d76d3705b6d1f3b3c9e2ceee195fd944b553eb27eee69e5e67c338f146f8445995664980bf0", "hex"), 6),
insurance: {
policyNumber: new Binary(Buffer.from("0217482732d8014cdd9ffdd6e2966e5e79108decd85c05be3fec099e015f9d26d9234605dc959cc1a19b63072f7ffda99db38c7b487de0572a03b2139ac3ee163bcc40c8508f366ce92a5dd36e38b3c742f7", "hex"), 6),
provider: 'MaestCare'
}
}
Finding a document with encrypted client
{
_id: new ObjectId("629a452e0861b3130887103a"),
name: 'Jon Doe',
ssn: 241014209,
bloodType: 'AB+',
'key-id': 'demo-data-key',
medicalRecords: [ { weight: 180, bloodPressure: '120/80' } ],
insurance: { policyNumber: 123142, provider: 'MaestCare' }
}

Tip

参照: 完全なコード

暗号化されたフィールドを持つドキュメントを検索するための完全なコードについては、Githubリポジトリ を参照してください。

暗号化されたフィールドを持つドキュメントを検索するための完全なコードについては、 Githubリポジトリ を参照してください。

暗号化されたフィールドを持つドキュメントを検索するための完全なコードについては、 Githubリポジトリ を参照してください。

暗号化されたフィールドを持つドキュメントを検索するための完全なコードについては、 Githubリポジトリ を参照してください。

暗号化されたフィールドを持つドキュメントを検索するための完全なコードについては、 Githubリポジトリ を参照してください。

CSFLE の仕組みについては、CSFLE の基礎 を参照してください。

このガイドで言及されているトピックについて詳しくは、次のリンクを参照してください。

戻る

Amazon Web Servicesでの自動クライアント側フィールドレベル暗号化の使用