|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package misc |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + dbesdkdynamodbencryption "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkdynamodbsmithygenerated" |
| 10 | + dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes" |
| 11 | + "github.com/aws/aws-database-encryption-sdk-dynamodb/examples/utils" |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + "github.com/aws/aws-sdk-go-v2/config" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 16 | +) |
| 17 | + |
| 18 | +func GetEncryptedDataKeyDescriptionExample(kmsKeyID, ddbTableName string) { |
| 19 | + cfg, err := config.LoadDefaultConfig(context.TODO()) |
| 20 | + utils.HandleError(err) |
| 21 | + ddbec, err := dbesdkdynamodbencryption.NewClient(dbesdkdynamodbencryptiontypes.DynamoDbEncryptionConfig{}) |
| 22 | + utils.HandleError(err) |
| 23 | + // 1. Define keys that will be used to retrieve item from the DynamoDB table. |
| 24 | + keyToGet := map[string]types.AttributeValue{ |
| 25 | + "partition_key": &types.AttributeValueMemberS{Value: "BasicPutGetExample"}, |
| 26 | + "sort_key": &types.AttributeValueMemberN{Value: "0"}, |
| 27 | + } |
| 28 | + |
| 29 | + // 2. Create a Amazon DynamoDB Client and retrieve item from DynamoDB table |
| 30 | + ddb := dynamodb.NewFromConfig(cfg) |
| 31 | + |
| 32 | + // 3. Get the item from the dynamoDB table and prepare input for the GetEncryptedDataKeyDescription method. |
| 33 | + // Here, we are sending dynamodb item but you can also input the header itself by extracting the header from |
| 34 | + // "aws_dbe_head" attribute in the dynamoDB item. The part of the code where we send input as the header is commented. |
| 35 | + getInput := &dynamodb.GetItemInput{ |
| 36 | + TableName: aws.String(ddbTableName), |
| 37 | + Key: keyToGet, |
| 38 | + // In this example we configure a strongly consistent read |
| 39 | + // because we perform a read immediately after a write (for demonstrative purposes). |
| 40 | + // By default, reads are only eventually consistent. |
| 41 | + // Read our docs to determine which read consistency to use for your application: |
| 42 | + // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html |
| 43 | + ConsistentRead: aws.Bool(true), |
| 44 | + } |
| 45 | + returnedItem, err := ddb.GetItem(context.TODO(), getInput) |
| 46 | + utils.HandleError(err) |
| 47 | + |
| 48 | + inputUnion := dbesdkdynamodbencryptiontypes.GetEncryptedDataKeyDescriptionUnionMemberitem{ |
| 49 | + Value: returnedItem.Item, |
| 50 | + } |
| 51 | + encryptedDataKeyDescriptionInput := dbesdkdynamodbencryptiontypes.GetEncryptedDataKeyDescriptionInput{ |
| 52 | + Input: &inputUnion, |
| 53 | + } |
| 54 | + encryptedDataKeyDescription, err := ddbec.GetEncryptedDataKeyDescription(context.TODO(), encryptedDataKeyDescriptionInput) |
| 55 | + utils.HandleError(err) |
| 56 | + |
| 57 | + if encryptedDataKeyDescription.EncryptedDataKeyDescriptionOutput[0].KeyProviderId != "aws-kms" { |
| 58 | + panic("Key provider should have been aws-kms") |
| 59 | + } |
| 60 | + if *encryptedDataKeyDescription.EncryptedDataKeyDescriptionOutput[0].KeyProviderInfo != kmsKeyID { |
| 61 | + panic("Key provider info should have been " + kmsKeyID) |
| 62 | + } |
| 63 | + fmt.Println("Get encrypted data Key description example successful.") |
| 64 | +} |
0 commit comments