Skip to content

Commit 46c116b

Browse files
auto commit
1 parent d5e36b8 commit 46c116b

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package awsdbe
2+
3+
import (
4+
// Standard imports
5+
"context"
6+
"fmt"
7+
"reflect"
8+
9+
// AWS SDK imports
10+
11+
"github.com/aws/aws-sdk-go-v2/aws"
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
14+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
15+
16+
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
17+
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/dbesdkmiddleware"
18+
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/utils"
19+
)
20+
21+
/*
22+
Migration Step 3: This is an example demonstrating how to update your configuration
23+
to stop accepting reading plaintext items.
24+
25+
Once you complete Step 3, all items being read by your system are encrypted.
26+
27+
Before you move onto this step, you will need to encrypt all plaintext items in your dataset.
28+
How you will want to do this depends on your system.
29+
30+
Running this example requires access to the DDB Table whose name
31+
is provided in CLI arguments.
32+
This table must be configured with the following
33+
primary key configuration:
34+
- Partition key is named "partition_key" with type (S)
35+
- Sort key is named "sort_key" with type (S)
36+
*/
37+
func MigrationStep3(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyValue string) {
38+
cfg, err := config.LoadDefaultConfig(context.TODO())
39+
utils.HandleError(err)
40+
41+
// 1. Configure your Keyring, attribute actions,
42+
// allowedUnsignedAttributes, and encryption configuration for table.
43+
// This is common across all the steps.
44+
45+
// When creating encryption configuration for your table,
46+
// you must use the plaintext override `PlaintextOverrideForbidPlaintextWriteForbidPlaintextRead`.
47+
// This plaintext override means:
48+
49+
listOfTableConfigs := configureTable(kmsKeyID, ddbTableName, dbesdkdynamodbencryptiontypes.PlaintextOverrideForbidPlaintextWriteForbidPlaintextRead)
50+
51+
// 5. Create DynamoDB client with dbEsdkMiddleware
52+
dbEsdkMiddleware, err := dbesdkmiddleware.NewDBEsdkMiddleware(listOfTableConfigs)
53+
utils.HandleError(err)
54+
55+
ddb := dynamodb.NewFromConfig(cfg, dbEsdkMiddleware.CreateMiddleware())
56+
57+
// 6. Put an item into your table.
58+
// This item will be encrypted.
59+
item := map[string]types.AttributeValue{
60+
"partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue},
61+
"sort_key": &types.AttributeValueMemberN{Value: sortKeyValue},
62+
"attribute1": &types.AttributeValueMemberS{Value: "this will be encrypted and signed"},
63+
"attribute2": &types.AttributeValueMemberS{Value: "this will never be encrypted, but it will be signed"},
64+
"attribute3": &types.AttributeValueMemberS{Value: "this will never be encrypted nor signed"},
65+
}
66+
67+
putInput := dynamodb.PutItemInput{
68+
TableName: &ddbTableName,
69+
Item: item,
70+
}
71+
72+
_, err = ddb.PutItem(context.TODO(), &putInput)
73+
utils.HandleError(err)
74+
75+
// 7. Get an item back from the table.
76+
// If this is an item written in plaintext (i.e. any item written
77+
// during Step 0 or 1), then the item will still be in plaintext.
78+
// If this is an item that was encrypted client-side (i.e. any item written
79+
// during Step 2 or after), then the DDB enhanced client will decrypt the
80+
// item client-sid and surface it in our code as a plaintext item.
81+
key := map[string]types.AttributeValue{
82+
"partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue},
83+
"sort_key": &types.AttributeValueMemberN{Value: sortKeyValue},
84+
}
85+
86+
getInput := &dynamodb.GetItemInput{
87+
TableName: aws.String(ddbTableName),
88+
Key: key,
89+
ConsistentRead: aws.Bool(true),
90+
}
91+
92+
result, err := ddb.GetItem(context.TODO(), getInput)
93+
utils.HandleError(err)
94+
95+
// Demonstrate we get the expected item back
96+
if !reflect.DeepEqual(item, result.Item) {
97+
panic("Decrypted item does not match original item")
98+
}
99+
fmt.Println("MigrationStep2 completed successfully")
100+
}

0 commit comments

Comments
 (0)