Skip to content

Commit 1f0b8f4

Browse files
chore(Go): Add KMS and raw AES keyring example with CI (#1726)
1 parent 7b8699f commit 1f0b8f4

File tree

7 files changed

+520
-0
lines changed

7 files changed

+520
-0
lines changed

.github/workflows/ci_test_go.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,9 @@ jobs:
133133
working-directory: ./${{ matrix.library }}
134134
run: |
135135
make test_go
136+
137+
- name: Test Examples
138+
if: matrix.library == 'DynamoDbEncryption'
139+
working-directory: ./Examples/runtimes/go
140+
run: |
141+
go run main.go

Examples/runtimes/go/go.mod

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module github.com/aws/aws-database-encryption-sdk-dynamodb/examples
2+
3+
go 1.23.2
4+
5+
replace github.com/aws/aws-database-encryption-sdk-dynamodb => ../../../DynamoDbEncryption/runtimes/go/ImplementationFromDafny-go
6+
7+
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl => ../../../submodules/MaterialProviders/AwsCryptographicMaterialProviders/runtimes/go/ImplementationFromDafny-go/
8+
9+
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/primitives => ../../../submodules/MaterialProviders/AwsCryptographyPrimitives/runtimes/go/ImplementationFromDafny-go/
10+
11+
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/dynamodb => ../../../submodules/MaterialProviders/ComAmazonawsDynamodb/runtimes/go/ImplementationFromDafny-go/
12+
13+
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/kms => ../../../submodules/MaterialProviders/ComAmazonawsKms/runtimes/go/ImplementationFromDafny-go/
14+
15+
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library => ../../../submodules/MaterialProviders/StandardLibrary/runtimes/go/ImplementationFromDafny-go/
16+
17+
require (
18+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl v0.0.0
19+
github.com/aws/aws-database-encryption-sdk-dynamodb v0.0.0
20+
github.com/aws/aws-sdk-go-v2 v1.32.8
21+
github.com/aws/aws-sdk-go-v2/config v1.28.10
22+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.2
23+
github.com/aws/aws-sdk-go-v2/service/kms v1.36.0
24+
)
25+
26+
require (
27+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/dynamodb v0.1.0 // indirect
28+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/kms v0.0.1 // indirect
29+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/primitives v0.0.1 // indirect
30+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library v0.1.0 // indirect
31+
github.com/aws/aws-sdk-go-v2/credentials v1.17.51 // indirect
32+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.23 // indirect
33+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.27 // indirect
34+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.27 // indirect
35+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
36+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
37+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.8 // indirect
38+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.8 // indirect
39+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.9 // indirect
40+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.8 // indirect
41+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.6 // indirect
42+
github.com/aws/smithy-go v1.22.1 // indirect
43+
github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.2 // indirect
44+
github.com/google/uuid v1.6.0 // indirect
45+
github.com/jmespath/go-jmespath v0.4.0 // indirect
46+
)

Examples/runtimes/go/go.sum

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library v0.1.0 h1:Nw3zDK7jQ/ylj1isG91PdsEKdojIlI+iX3I43h6uj1I=
2+
github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library v0.1.0/go.mod h1:m3mzHKiNiSC0LWeWX6ZAxSe6mKbJHgliux1Yu/sjCYI=
3+
github.com/aws/aws-sdk-go-v2 v1.32.8 h1:cZV+NUS/eGxKXMtmyhtYPJ7Z4YLoI/V8bkTdRZfYhGo=
4+
github.com/aws/aws-sdk-go-v2 v1.32.8/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
5+
github.com/aws/aws-sdk-go-v2/config v1.28.10 h1:fKODZHfqQu06pCzR69KJ3GuttraRJkhlC8g80RZ0Dfg=
6+
github.com/aws/aws-sdk-go-v2/config v1.28.10/go.mod h1:PvdxRYZ5Um9QMq9PQ0zHHNdtKK+he2NHtFCUFMXWXeg=
7+
github.com/aws/aws-sdk-go-v2/credentials v1.17.51 h1:F/9Sm6Y6k4LqDesZDPJCLxQGXNNHd/ZtJiWd0lCZKRk=
8+
github.com/aws/aws-sdk-go-v2/credentials v1.17.51/go.mod h1:TKbzCHm43AoPyA+iLGGcruXd4AFhF8tOmLex2R9jWNQ=
9+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.23 h1:IBAoD/1d8A8/1aA8g4MBVtTRHhXRiNAgwdbo/xRM2DI=
10+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.23/go.mod h1:vfENuCM7dofkgKpYzuzf1VT1UKkA/YL3qanfBn7HCaA=
11+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.27 h1:jSJjSBzw8VDIbWv+mmvBSP8ezsztMYJGH+eKqi9AmNs=
12+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.27/go.mod h1:/DAhLbFRgwhmvJdOfSm+WwikZrCuUJiA4WgJG0fTNSw=
13+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.27 h1:l+X4K77Dui85pIj5foXDhPlnqcNRG2QUyvca300lXh8=
14+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.27/go.mod h1:KvZXSFEXm6x84yE8qffKvT3x8J5clWnVFXphpohhzJ8=
15+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
16+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
17+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.2 h1:XcdIh35yg1J8bAiUOLtL/PoPMSGsD72Zanwmim8jEXc=
18+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.2/go.mod h1:516U/KQM3zdcahNBjHUZKGWNfNnIYyt7sxLeqOx78b0=
19+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhvDxB6kPvEXgsDhGaZCSC6LQET5ZHSdJozeI0Y=
20+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
21+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.8 h1:h56mLNgpqWIL7RZOIQO634Xr569bXGTlIE83t/a0LSE=
22+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.8/go.mod h1:kK04550Xx95KI0sNmwoB7ciS9QkRwt9TojhoTMXyJdo=
23+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.8 h1:cWno7lefSH6Pp+mSznagKCgfDGeZRin66UvYUqAkyeA=
24+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.8/go.mod h1:tPD+VjU3ABTBoEJ3nctu5Nyg4P4yjqSH5bJGGkY4+XE=
25+
github.com/aws/aws-sdk-go-v2/service/kms v1.36.0 h1:jwWMpQ/1obJRdHaix9k10zWSnSMZGdDTZIDiS5CGzq8=
26+
github.com/aws/aws-sdk-go-v2/service/kms v1.36.0/go.mod h1:OHmlX4+o0XIlJAQGAHPIy0N9yZcYS/vNG+T7geSNcFw=
27+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.9 h1:YqtxripbjWb2QLyzRK9pByfEDvgg95gpC2AyDq4hFE8=
28+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.9/go.mod h1:lV8iQpg6OLOfBnqbGMBKYjilBlf633qwHnBEiMSPoHY=
29+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.8 h1:6dBT1Lz8fK11m22R+AqfRsFn8320K0T5DTGxxOQBSMw=
30+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.8/go.mod h1:/kiBvRQXBc6xeJTYzhSdGvJ5vm1tjaDEjH+MSeRJnlY=
31+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.6 h1:VwhTrsTuVn52an4mXx29PqRzs2Dvu921NpGk7y43tAM=
32+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.6/go.mod h1:+8h7PZb3yY5ftmVLD7ocEoE98hdc8PoKS0H3wfx1dlc=
33+
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
34+
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
35+
github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.2 h1:g/xAj4F7Zt9wXJ6QjfbfocVi/ZYlAFpNddHCFyfzRDg=
36+
github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.2/go.mod h1:l2Tm4N2DKuq3ljONC2vOATeM9PUpXbIc8SgXdwwqEto=
37+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
38+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
39+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
40+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
41+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
42+
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
43+
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
44+
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
45+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
49+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
50+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package keyring
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"reflect"
10+
11+
mpl "github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygenerated"
12+
mpltypes "github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygeneratedtypes"
13+
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
14+
dbesdkstructuredencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkstructuredencryptionsmithygeneratedtypes"
15+
"github.com/aws/aws-database-encryption-sdk-dynamodb/dbesdkmiddleware"
16+
17+
"github.com/aws/aws-sdk-go-v2/aws"
18+
"github.com/aws/aws-sdk-go-v2/config"
19+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
20+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
21+
"github.com/aws/aws-sdk-go-v2/service/kms"
22+
)
23+
24+
/*
25+
This example sets up DynamoDb Encryption for the AWS SDK client
26+
and uses the low level PutItem and GetItem DDB APIs to demonstrate
27+
putting a client-side encrypted item into DynamoDb
28+
and then retrieving and decrypting that item from DynamoDb.
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 (N)
36+
*/
37+
func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
38+
// 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
39+
// For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use.
40+
// We will use the `CreateMrkMultiKeyring` method to create this keyring,
41+
// as it will correctly handle both single region and Multi-Region KMS Keys.
42+
cfg, err := config.LoadDefaultConfig(context.TODO())
43+
if err != nil {
44+
panic(err)
45+
}
46+
// Create KMS client
47+
kmsClient := kms.NewFromConfig(cfg, func(o *kms.Options) {
48+
o.Region = "us-west-2"
49+
})
50+
// Initialize the mpl client
51+
matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{})
52+
if err != nil {
53+
panic(err)
54+
}
55+
// Create the Aws Kms Keyring
56+
awsKmsKeyringInput := mpltypes.CreateAwsKmsKeyringInput{
57+
KmsClient: kmsClient,
58+
KmsKeyId: kmsKeyID,
59+
}
60+
keyring, err := matProv.CreateAwsKmsKeyring(context.Background(), awsKmsKeyringInput)
61+
if err != nil {
62+
panic(err)
63+
}
64+
65+
// 2. Configure which attributes are encrypted and/or signed when writing new items.
66+
// For each attribute that may exist on the items we plan to write to our DynamoDbTable,
67+
// we must explicitly configure how they should be treated during item encryption:
68+
// - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
69+
// - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
70+
// - DO_NOTHING: The attribute is not encrypted and not included in the signature
71+
attributeActions := map[string]dbesdkstructuredencryptiontypes.CryptoAction{
72+
"partition_key": dbesdkstructuredencryptiontypes.CryptoActionSignOnly, // Partition key must be SIGN_ONLY
73+
"sort_key": dbesdkstructuredencryptiontypes.CryptoActionSignOnly, // Sort key must be SIGN_ONLY
74+
"attribute1": dbesdkstructuredencryptiontypes.CryptoActionEncryptAndSign,
75+
"attribute2": dbesdkstructuredencryptiontypes.CryptoActionSignOnly,
76+
":attribute3": dbesdkstructuredencryptiontypes.CryptoActionDoNothing,
77+
}
78+
79+
// 3. Configure which attributes we expect to be included in the signature
80+
// when reading items. There are two options for configuring this:
81+
//
82+
// - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
83+
// When defining your DynamoDb schema and deciding on attribute names,
84+
// choose a distinguishing prefix (such as ":") for all attributes that
85+
// you do not want to include in the signature.
86+
// This has two main benefits:
87+
// - It is easier to reason about the security and authenticity of data within your item
88+
// when all unauthenticated data is easily distinguishable by their attribute name.
89+
// - If you need to add new unauthenticated attributes in the future,
90+
// you can easily make the corresponding update to your `attributeActionsOnEncrypt`
91+
// and immediately start writing to that new attribute, without
92+
// any other configuration update needed.
93+
// Once you configure this field, it is not safe to update it.
94+
//
95+
// - Configure `allowedUnsignedAttributes`: You may also explicitly list
96+
// a set of attributes that should be considered unauthenticated when encountered
97+
// on read. Be careful if you use this configuration. Do not remove an attribute
98+
// name from this configuration, even if you are no longer writing with that attribute,
99+
// as old items may still include this attribute, and our configuration needs to know
100+
// to continue to exclude this attribute from the signature scope.
101+
// If you add new attribute names to this field, you must first deploy the update to this
102+
// field to all readers in your host fleet before deploying the update to start writing
103+
// with that new attribute.
104+
//
105+
// For this example, we have designed our DynamoDb table such that any attribute name with
106+
// the ":" prefix should be considered unauthenticated.
107+
allowedUnsignedAttributePrefix := ":"
108+
109+
// 4. Create the DynamoDb Encryption configuration for the table we will be writing to.
110+
partitionKey := "partition_key"
111+
sortKeyName := "sort_key"
112+
algorithmSuiteId := mpltypes.DBEAlgorithmSuiteIdAlgAes256GcmHkdfSha512CommitKeyEcdsaP384SymsigHmacSha384
113+
tableConfig := dbesdkdynamodbencryptiontypes.DynamoDbTableEncryptionConfig{
114+
LogicalTableName: ddbTableName,
115+
PartitionKeyName: partitionKey,
116+
SortKeyName: &sortKeyName,
117+
AttributeActionsOnEncrypt: attributeActions,
118+
Keyring: keyring,
119+
AllowedUnsignedAttributePrefix: &allowedUnsignedAttributePrefix,
120+
AlgorithmSuiteId: &algorithmSuiteId,
121+
}
122+
tableConfigsMap := make(map[string]dbesdkdynamodbencryptiontypes.DynamoDbTableEncryptionConfig)
123+
tableConfigsMap[ddbTableName] = tableConfig
124+
listOfTableConfigs := dbesdkdynamodbencryptiontypes.DynamoDbTablesEncryptionConfig{
125+
TableEncryptionConfigs: tableConfigsMap,
126+
}
127+
// 5. Create a new AWS SDK DynamoDb client using the TableEncryptionConfigs
128+
dbEsdkMiddleware, err := dbesdkmiddleware.NewDBEsdkMiddleware(listOfTableConfigs)
129+
if err != nil {
130+
panic(err)
131+
}
132+
ddb := dynamodb.NewFromConfig(cfg, dbEsdkMiddleware.CreateMiddleware())
133+
134+
// 6. Put an item into our table using the above client.
135+
// Before the item gets sent to DynamoDb, it will be encrypted
136+
// client-side, according to our configuration.
137+
item := map[string]types.AttributeValue{
138+
"partition_key": &types.AttributeValueMemberS{Value: "BasicPutGetExample"},
139+
"sort_key": &types.AttributeValueMemberN{Value: "0"},
140+
"attribute1": &types.AttributeValueMemberS{Value: "encrypt and sign me!"},
141+
"attribute2": &types.AttributeValueMemberS{Value: "sign me!"},
142+
":attribute3": &types.AttributeValueMemberS{Value: "ignore me!"},
143+
}
144+
putInput := &dynamodb.PutItemInput{
145+
TableName: aws.String(ddbTableName),
146+
Item: item,
147+
}
148+
_, err = ddb.PutItem(context.TODO(), putInput)
149+
if err != nil {
150+
panic(err)
151+
}
152+
153+
// 7. Get the item back from our table using the same client.
154+
// The client will decrypt the item client-side, and return
155+
// back the original item.
156+
key := map[string]types.AttributeValue{
157+
"partition_key": &types.AttributeValueMemberS{Value: "BasicPutGetExample"},
158+
"sort_key": &types.AttributeValueMemberN{Value: "0"},
159+
}
160+
getInput := &dynamodb.GetItemInput{
161+
TableName: aws.String(ddbTableName),
162+
Key: key,
163+
// In this example we configure a strongly consistent read
164+
// because we perform a read immediately after a write (for demonstrative purposes).
165+
// By default, reads are only eventually consistent.
166+
// Read our docs to determine which read consistency to use for your application:
167+
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
168+
ConsistentRead: aws.Bool(true),
169+
}
170+
result, err := ddb.GetItem(context.TODO(), getInput)
171+
if err != nil {
172+
panic(err)
173+
}
174+
// Verify the decrypted item
175+
if !reflect.DeepEqual(item, result.Item) {
176+
panic("Decrypted item does not match original item")
177+
}
178+
fmt.Println("Aws Kms Keyring Example successful.")
179+
}

0 commit comments

Comments
 (0)