

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# DynamoDB での検索可能な暗号化
<a name="ddb-searchable-encryption"></a>

検索可能な暗号化のために Amazon DynamoDB テーブルを設定するには、[AWS KMS 階層キーリング](use-hierarchical-keyring.md)を使用して、項目を保護するために使用されるデータキーを生成、暗号化、および復号する必要があります。また、テーブル暗号化設定に [`SearchConfig`](ddb-net-using.md#ddb-net-search-config) を含める必要があります。

**注記**  
DynamoDB 用の Java クライアント側の暗号化ライブラリを使用している場合は、低レベルの AWS Database Encryption SDK for DynamoDB API を使用して、テーブル項目を暗号化、署名、検証、復号化する必要があります。DynamoDB Enhanced Client と下位レベルの `DynamoDBItemEncryptor` は、検索可能な暗号化をサポートしていません。

**Topics**
+ [ビーコンを使用したセカンダリインデックスの設定](#ddb-beacon-indexes)
+ [ビーコン出力のテスト](#ddb-beacon-testing)

## ビーコンを使用したセカンダリインデックスの設定
<a name="ddb-beacon-indexes"></a>

[ビーコンを設定](configure-beacons.md)した後、暗号化された属性を検索する前に、各ビーコンを反映するセカンダリインデックスを設定する必要があります。

標準ビーコンまたは複合ビーコンを設定すると、 AWS Database Encryption SDK はビーコン名に`aws_dbe_b_`プレフィックスを追加して、サーバーがビーコンを簡単に識別できるようにします。例えば、複合ビーコンに `compoundBeacon` という名前を付けた場合、実際の完全なビーコン名は `aws_dbe_b_compoundBeacon` です。標準ビーコンまたは複合ビーコンを含む[セカンダリインデックス](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html)を設定する場合は、ビーコン名を識別するときに `aws_dbe_b_` プレフィックスを含める必要があります。

**パーティションキーとソートキー**  
プライマリキーの値を暗号化することはできません。パーティションキーとソートキーは署名されている必要があります。プライマリキーの値を標準ビーコンまたは複合ビーコンにすることはできません。  
属性を指定しない限り`SIGN_ONLY`、プライマリキーの値は である必要があります。`SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT`パーティション属性とソート属性も である必要があります`SIGN_AND_INCLUDE_IN_ENCRYPTION_CONTEXT`。  
プライマリキーの値を署名付きビーコンにすることができます。プライマリキーの値ごとに個別の署名付きビーコンを設定した場合は、プライマリキーの値を識別する属性名を署名付きビーコン名として指定する必要があります。ただし、 AWS Database Encryption SDK は署名付きビーコンに`aws_dbe_b_`プレフィックスを追加しません。プライマリキーの値に個別の署名付きビーコンを設定した場合でも、必要なのは、セカンダリインデックスを設定する際に、プライマリキーの値の属性名を指定することだけです。

**ローカルセカンダリインデックス**  
[ローカルセカンダリインデックス](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html)のソートキーはビーコンにすることができます。  
ソートキーにビーコンを指定する場合、タイプは String である必要があります。ソートキーに標準ビーコンまたは複合ビーコンを指定する場合は、ビーコン名を指定する際に `aws_dbe_b_` プレフィックスを含める必要があります。署名付きビーコンを指定する場合は、プレフィックスなしでビーコン名を指定します。

**グローバルセカンダリインデックス**  
[グローバルセカンダリインデックス](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html)のパーティションキーとソートキーは両方ともビーコンにすることができます。  
パーティションキーまたはソートキーにビーコンを指定する場合、タイプは String である必要があります。ソートキーに標準ビーコンまたは複合ビーコンを指定する場合は、ビーコン名を指定する際に `aws_dbe_b_` プレフィックスを含める必要があります。署名付きビーコンを指定する場合は、プレフィックスなしでビーコン名を指定します。

**属性の射影**  
[射影](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html#GSI.Projections)とは、テーブルからセカンダリインデックスにコピーされる属性のセットです。テーブルのパーティションキーとソートキーは常にインデックスに射影されます。アプリケーションのクエリ要件をサポートするために、他の属性を射影できます。DynamoDB は、属性プロジェクションのために、`KEYS_ONLY`、`INCLUDE`、`ALL` の 3 つの異なるオプションを提供します。  
INCLUDE 属性プロジェクションを使用してビーコンを検索する場合は、ビーコンが構築されるすべての属性の名前と、`aws_dbe_b_` プレフィックスを持つビーコン名を指定する必要があります。例えば、`field1`、`field2`、および `field3` から複合ビーコン `compoundBeacon` を設定した場合、プロジェクション内で、`aws_dbe_b_compoundBeacon`、`field1`、`field2`、`field3` を指定する必要があります。  
グローバルセカンダリインデックスはプロジェクションで明示的に指定された属性のみを使用できますが、ローカルセカンダリインデックスは任意の属性を使用できます。

## ビーコン出力のテスト
<a name="ddb-beacon-testing"></a>

[複合ビーコンを設定](configure-beacons.md#config-compound-beacons)した場合、または[仮想フィールド](configure-beacons.md#create-virtual-field)を使用してビーコンを構築した場合は、DynamoDB テーブルに入力する前に、これらのビーコンが期待される出力を生成することを確認することをお勧めします。

 AWS Database Encryption SDK は、仮想フィールドと複合ビーコン出力のトラブルシューティングに役立つ `DynamoDbEncryptionTransforms`サービスを提供します。

### 仮想フィールドのテスト
<a name="ddb-beacon-testing-virtual-field"></a>

次のスニペットは、テスト項目を作成し、[DynamoDB テーブル暗号化設定](ddb-java-using.md#ddb-config-encrypt)を使用して`DynamoDbEncryptionTransforms`サービスを定義し、 `ResolveAttributes` を使用して仮想フィールドが期待される出力を生成することを確認する方法を示しています。

------
#### [ Java ]

**完全なコードサンプルを参照**: [VirtualBeaconSearchableEncryptionExample.java](https://github.com/aws/aws-database-encryption-sdk-dynamodb//blob/main/Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/searchableencryption/VirtualBeaconSearchableEncryptionExample.java) 

```
// Create test items
final PutItemRequest itemWithHasTestResultPutRequest = PutItemRequest.builder()
    .tableName(ddbTableName)
    .item(itemWithHasTestResult)
    .build();

final PutItemResponse itemWithHasTestResultPutResponse = ddb.putItem(itemWithHasTestResultPutRequest);

final PutItemRequest itemWithNoHasTestResultPutRequest = PutItemRequest.builder()
    .tableName(ddbTableName)
    .item(itemWithNoHasTestResult)
    .build();
    
final PutItemResponse itemWithNoHasTestResultPutResponse = ddb.putItem(itemWithNoHasTestResultPutRequest);    

// Define the DynamoDbEncryptionTransforms service
final DynamoDbEncryptionTransforms trans = DynamoDbEncryptionTransforms.builder()
    .DynamoDbTablesEncryptionConfig(encryptionConfig).build();

// Verify configuration
final ResolveAttributesInput resolveInput = ResolveAttributesInput.builder()
    .TableName(ddbTableName)
    .Item(itemWithHasTestResult)
    .Version(1)
    .build();
final ResolveAttributesOutput resolveOutput = trans.ResolveAttributes(resolveInput);

// Verify that VirtualFields has the expected value
Map<String, String> vf = new HashMap<>();
vf.put("stateAndHasTestResult", "CAt");
assert resolveOutput.VirtualFields().equals(vf);
```

------
#### [ C\$1 / .NET ]

**完全なコードサンプル**「[VirtualBeaconSearchableEncryptionExample.cs](https://github.com/aws/aws-database-encryption-sdk-dynamodb/tree/main/Examples/runtimes/net/src/searchableencryption/VirtualBeaconSearchableEncryptionExample.cs)」を参照してください。

```
 // Create item with hasTestResult=true
var itemWithHasTestResult = new Dictionary<String, AttributeValue>
{
    ["customer_id"] = new AttributeValue("ABC-123"),
    ["create_time"] = new AttributeValue { N = "1681495205" },
    ["state"] = new AttributeValue("CA"),
    ["hasTestResult"] = new AttributeValue { BOOL = true }
};

// Create item with hasTestResult=false
var itemWithNoHasTestResult = new Dictionary<String, AttributeValue>
{
    ["customer_id"] = new AttributeValue("DEF-456"),
    ["create_time"] = new AttributeValue { N = "1681495205" },
    ["state"] = new AttributeValue("CA"),
    ["hasTestResult"] = new AttributeValue { BOOL = false }
};

// Define the DynamoDbEncryptionTransforms service
var trans = new DynamoDbEncryptionTransforms(encryptionConfig);

// Verify configuration
var resolveInput = new ResolveAttributesInput
{
    TableName = ddbTableName,
    Item = itemWithHasTestResult,
    Version = 1
};
var resolveOutput = trans.ResolveAttributes(resolveInput);

// Verify that VirtualFields has the expected value
Debug.Assert(resolveOutput.VirtualFields.Count == 1);
Debug.Assert(resolveOutput.VirtualFields["stateAndHasTestResult"] == "CAt");
```

------
#### [ Rust ]

**完全なコードサンプル**[「virtual\$1beacon\$1searchable\$1encryption.rs](https://github.com/aws/aws-database-encryption-sdk-dynamodb/blob/main/releases/rust/db_esdk/examples/searchableencryption/virtual_beacon_searchable_encryption.rs)」を参照してください。

```
// Create item with hasTestResult=true
let item_with_has_test_result = HashMap::from([
    (
        "customer_id".to_string(),
        AttributeValue::S("ABC-123".to_string()),
    ),
    (
        "create_time".to_string(),
        AttributeValue::N("1681495205".to_string()),
    ),
    ("state".to_string(), AttributeValue::S("CA".to_string())),
    ("hasTestResult".to_string(), AttributeValue::Bool(true)),
]);

// Create item with hasTestResult=false
let item_with_no_has_test_result = HashMap::from([
    (
        "customer_id".to_string(),
        AttributeValue::S("DEF-456".to_string()),
    ),
    (
        "create_time".to_string(),
        AttributeValue::N("1681495205".to_string()),
    ),
    ("state".to_string(), AttributeValue::S("CA".to_string())),
    ("hasTestResult".to_string(), AttributeValue::Bool(false)),
]);

// Define the transform service
let trans = transform_client::Client::from_conf(encryption_config.clone())?;

// Verify the configuration 
let resolve_output = trans
    .resolve_attributes()
    .table_name(ddb_table_name)
    .item(item_with_has_test_result.clone())
    .version(1)
    .send()
    .await?;

// Verify that VirtualFields has the expected value
let virtual_fields = resolve_output.virtual_fields.unwrap();
assert_eq!(virtual_fields.len(), 1);
assert_eq!(virtual_fields["stateAndHasTestResult"], "CAt");
```

------

### 複合ビーコンのテスト
<a name="ddb-beacon-testing-compound-beacon"></a>

次のスニペットでは、テスト項目を作成し、[DynamoDB テーブル暗号化設定](ddb-java-using.md#ddb-config-encrypt)を使用して`DynamoDbEncryptionTransforms`サービスを定義し、 `ResolveAttributes`を使用して複合ビーコンが期待される出力を生成することを確認する方法を示します。

------
#### [ Java ]

**完全なコードサンプルを参照**: [CompoundBeaconSearchableEncryptionExample.java](https://github.com/aws/aws-database-encryption-sdk-dynamodb//blob/main/Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/searchableencryption/CompoundBeaconSearchableEncryptionExample.java) 

```
// Create an item with both attributes used in the compound beacon.
final HashMap<String, AttributeValue> item = new HashMap<>();
item.put("work_id", AttributeValue.builder().s("9ce39272-8068-4efd-a211-cd162ad65d4c").build());
item.put("inspection_date", AttributeValue.builder().s("2023-06-13").build());
item.put("inspector_id_last4", AttributeValue.builder().s("5678").build());
item.put("unit", AttributeValue.builder().s("011899988199").build());
                            
// Define the DynamoDbEncryptionTransforms service
final DynamoDbEncryptionTransforms trans = DynamoDbEncryptionTransforms.builder()
    .DynamoDbTablesEncryptionConfig(encryptionConfig).build();

// Verify configuration 
final ResolveAttributesInput resolveInput = ResolveAttributesInput.builder()
    .TableName(ddbTableName)
    .Item(item)
    .Version(1)
    .build();

final ResolveAttributesOutput resolveOutput = trans.ResolveAttributes(resolveInput);
                            
// Verify that CompoundBeacons has the expected value   
Map<String, String> cbs = new HashMap<>();
cbs.put("last4UnitCompound", "L-5678.U-011899988199");
assert resolveOutput.CompoundBeacons().equals(cbs);
// Note : the compound beacon actually stored in the table is not "L-5678.U-011899988199"
// but rather something like "L-abc.U-123", as both parts are EncryptedParts
// and therefore the text is replaced by the associated beacon
```

------
#### [ C\$1 / .NET ]

**完全なコードサンプルを参照**: [CompoundBeaconSearchableEncryptionExample.cs](https://github.com/aws/aws-database-encryption-sdk-dynamodb/tree/main/Examples/runtimes/net/src/searchableencryption/CompoundBeaconSearchableEncryptionExample.cs)

```
// Create an item with both attributes used in the compound beacon
var item = new Dictionary<String, AttributeValue>
{
    ["work_id"] = new AttributeValue("9ce39272-8068-4efd-a211-cd162ad65d4c"),
    ["inspection_date"] = new AttributeValue("2023-06-13"),
    ["inspector_id_last4"] = new AttributeValue("5678"),
    ["unit"] = new AttributeValue("011899988199")
};                           
                            
// Define the DynamoDbEncryptionTransforms service
var trans = new DynamoDbEncryptionTransforms(encryptionConfig);

// Verify configuration
var resolveInput = new ResolveAttributesInput
{
    TableName = ddbTableName,
    Item = item,
    Version = 1
};
var resolveOutput = trans.ResolveAttributes(resolveInput);                            
                            
// Verify that CompoundBeacons has the expected value 
Debug.Assert(resolveOutput.CompoundBeacons.Count == 1);
Debug.Assert(resolveOutput.CompoundBeacons["last4UnitCompound"] == "L-5678.U-011899988199");
// Note : the compound beacon actually stored in the table is not "L-5678.U-011899988199"
// but rather something like "L-abc.U-123", as both parts are EncryptedParts
// and therefore the text is replaced by the associated beacon
```

------
#### [ Rust ]

**完全なコードサンプルを参照**: [compound\$1beacon\$1searchable\$1encryption.rs](https://github.com/aws/aws-database-encryption-sdk-dynamodb/blob/main/releases/rust/db_esdk/examples/searchableencryption/compound_beacon_searchable_encryption.rs)

```
// Create an item with both attributes used in the compound beacon
let item = HashMap::from([
    (
        "work_id".to_string(),
        AttributeValue::S("9ce39272-8068-4efd-a211-cd162ad65d4c".to_string()),
    ),
    (
        "inspection_date".to_string(),
        AttributeValue::S("2023-06-13".to_string()),
    ),
    (
        "inspector_id_last4".to_string(),
        AttributeValue::S("5678".to_string()),
    ),
    (
        "unit".to_string(),
        AttributeValue::S("011899988199".to_string()),
    ),
]);                           
                            
// Define the transforms service
let trans = transform_client::Client::from_conf(encryption_config.clone())?;

// Verify configuration
let resolve_output = trans
    .resolve_attributes()
    .table_name(ddb_table_name)
    .item(item.clone())
    .version(1)
    .send()
    .await?;                            
                            
// Verify that CompoundBeacons has the expected value 
let compound_beacons = resolve_output.compound_beacons.unwrap();
assert_eq!(compound_beacons.len(), 1);
assert_eq!(
    compound_beacons["last4UnitCompound"],
    "L-5678.U-011899988199"
);
// but rather something like "L-abc.U-123", as both parts are EncryptedParts
// and therefore the text is replaced by the associated beacon
```

------