Skip to content

Commit f709b9d

Browse files
refactor(Go): Update examples to use utils and fix var name (#1860)
1 parent 40fe664 commit f709b9d

File tree

4 files changed

+51
-59
lines changed

4 files changed

+51
-59
lines changed

Examples/runtimes/go/keyring/awskmskeyring.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
1414
dbesdkstructuredencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkstructuredencryptionsmithygeneratedtypes"
1515
"github.com/aws/aws-database-encryption-sdk-dynamodb/dbesdkmiddleware"
16+
"github.com/aws/aws-database-encryption-sdk-dynamodb/examples/utils"
1617

1718
"github.com/aws/aws-sdk-go-v2/aws"
1819
"github.com/aws/aws-sdk-go-v2/config"
@@ -40,27 +41,21 @@ func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
4041
// We will use the `CreateMrkMultiKeyring` method to create this keyring,
4142
// as it will correctly handle both single region and Multi-Region KMS Keys.
4243
cfg, err := config.LoadDefaultConfig(context.TODO())
43-
if err != nil {
44-
panic(err)
45-
}
44+
utils.HandleError(err)
4645
// Create KMS client
4746
kmsClient := kms.NewFromConfig(cfg, func(o *kms.Options) {
4847
o.Region = "us-west-2"
4948
})
5049
// Initialize the mpl client
5150
matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{})
52-
if err != nil {
53-
panic(err)
54-
}
51+
utils.HandleError(err)
5552
// Create the Aws Kms Keyring
5653
awsKmsKeyringInput := mpltypes.CreateAwsKmsKeyringInput{
5754
KmsClient: kmsClient,
5855
KmsKeyId: kmsKeyID,
5956
}
6057
keyring, err := matProv.CreateAwsKmsKeyring(context.Background(), awsKmsKeyringInput)
61-
if err != nil {
62-
panic(err)
63-
}
58+
utils.HandleError(err)
6459

6560
// 2. Configure which attributes are encrypted and/or signed when writing new items.
6661
// For each attribute that may exist on the items we plan to write to our DynamoDbTable,
@@ -109,15 +104,15 @@ func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
109104
// 4. Create the DynamoDb Encryption configuration for the table we will be writing to.
110105
partitionKey := "partition_key"
111106
sortKeyName := "sort_key"
112-
algorithmSuiteId := mpltypes.DBEAlgorithmSuiteIdAlgAes256GcmHkdfSha512CommitKeyEcdsaP384SymsigHmacSha384
107+
algorithmSuiteID := mpltypes.DBEAlgorithmSuiteIdAlgAes256GcmHkdfSha512CommitKeyEcdsaP384SymsigHmacSha384
113108
tableConfig := dbesdkdynamodbencryptiontypes.DynamoDbTableEncryptionConfig{
114109
LogicalTableName: ddbTableName,
115110
PartitionKeyName: partitionKey,
116111
SortKeyName: &sortKeyName,
117112
AttributeActionsOnEncrypt: attributeActions,
118113
Keyring: keyring,
119114
AllowedUnsignedAttributePrefix: &allowedUnsignedAttributePrefix,
120-
AlgorithmSuiteId: &algorithmSuiteId,
115+
AlgorithmSuiteId: &algorithmSuiteID,
121116
}
122117
tableConfigsMap := make(map[string]dbesdkdynamodbencryptiontypes.DynamoDbTableEncryptionConfig)
123118
tableConfigsMap[ddbTableName] = tableConfig
@@ -126,9 +121,7 @@ func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
126121
}
127122
// 5. Create a new AWS SDK DynamoDb client using the TableEncryptionConfigs
128123
dbEsdkMiddleware, err := dbesdkmiddleware.NewDBEsdkMiddleware(listOfTableConfigs)
129-
if err != nil {
130-
panic(err)
131-
}
124+
utils.HandleError(err)
132125
ddb := dynamodb.NewFromConfig(cfg, dbEsdkMiddleware.CreateMiddleware())
133126

134127
// 6. Put an item into our table using the above client.
@@ -146,9 +139,7 @@ func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
146139
Item: item,
147140
}
148141
_, err = ddb.PutItem(context.TODO(), putInput)
149-
if err != nil {
150-
panic(err)
151-
}
142+
utils.HandleError(err)
152143

153144
// 7. Get the item back from our table using the same client.
154145
// The client will decrypt the item client-side, and return
@@ -168,9 +159,7 @@ func AwsKmsKeyringExample(kmsKeyID, ddbTableName string) {
168159
ConsistentRead: aws.Bool(true),
169160
}
170161
result, err := ddb.GetItem(context.TODO(), getInput)
171-
if err != nil {
172-
panic(err)
173-
}
162+
utils.HandleError(err)
174163
// Verify the decrypted item
175164
if !reflect.DeepEqual(item, result.Item) {
176165
panic("Decrypted item does not match original item")

Examples/runtimes/go/keyring/rawaeskeyring.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package keyring
55

66
import (
77
"context"
8-
"crypto/rand"
98
"fmt"
109
"reflect"
1110

@@ -14,6 +13,7 @@ import (
1413
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
1514
dbesdkstructuredencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/awscryptographydbencryptionsdkstructuredencryptionsmithygeneratedtypes"
1615
"github.com/aws/aws-database-encryption-sdk-dynamodb/dbesdkmiddleware"
16+
"github.com/aws/aws-database-encryption-sdk-dynamodb/examples/utils"
1717
"github.com/aws/aws-sdk-go-v2/aws"
1818
"github.com/aws/aws-sdk-go-v2/config"
1919
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
@@ -45,32 +45,23 @@ import (
4545
- Sort key is named "sort_key" with type (S)
4646
*/
4747

48-
func RawAesExample(ddbTableName string) {
49-
aesKeyBytes, err := generateAes256KeyBytes()
50-
if err != nil {
51-
panic(err)
52-
}
48+
func RawAesExample(ddbTableName, keyNamespace, keyName string, aesKeyBytes []byte) {
49+
// Initialize the mpl client
50+
matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{})
51+
utils.HandleError(err)
52+
5353
// 1. Create the keyring.
5454
// The DynamoDb encryption client uses this to encrypt and decrypt items.
5555

56-
// Initialize the mpl client
57-
matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{})
58-
if err != nil {
59-
panic(err)
60-
}
6156
// Create the Raw Aes Keyring
62-
var keyNamespace = "my-key-namespace"
63-
var keyName = "my-aes-key-name"
6457
rawAesKeyRingInput := mpltypes.CreateRawAesKeyringInput{
6558
KeyName: keyName,
6659
KeyNamespace: keyNamespace,
6760
WrappingKey: aesKeyBytes,
6861
WrappingAlg: mpltypes.AesWrappingAlgAlgAes256GcmIv12Tag16,
6962
}
7063
rawAesKeyring, err := matProv.CreateRawAesKeyring(context.Background(), rawAesKeyRingInput)
71-
if err != nil {
72-
panic(err)
73-
}
64+
utils.HandleError(err)
7465
// 2. Configure which attributes are encrypted and/or signed when writing new items.
7566
// For each attribute that may exist on the items we plan to write to our DynamoDbTable,
7667
// we must explicitly configure how they should be treated during item encryption:
@@ -132,14 +123,10 @@ func RawAesExample(ddbTableName string) {
132123

133124
// Create DBESDK middleware
134125
dbEsdkMiddleware, err := dbesdkmiddleware.NewDBEsdkMiddleware(listOfTableConfigs)
135-
if err != nil {
136-
panic(err)
137-
}
126+
utils.HandleError(err)
138127
// Create aws config
139128
cfg, err := config.LoadDefaultConfig(context.TODO())
140-
if err != nil {
141-
panic(err)
142-
}
129+
utils.HandleError(err)
143130
ddb := dynamodb.NewFromConfig(cfg, dbEsdkMiddleware.CreateMiddleware())
144131

145132
// 6. Put an item into our table using the above client.
@@ -155,9 +142,7 @@ func RawAesExample(ddbTableName string) {
155142
Item: item,
156143
}
157144
_, err = ddb.PutItem(context.TODO(), putInput)
158-
if err != nil {
159-
panic(err)
160-
}
145+
utils.HandleError(err)
161146
// 7. Get the item back from our table using the same client.
162147
// The client will decrypt the item client-side, and return
163148
// back the original item.
@@ -176,22 +161,10 @@ func RawAesExample(ddbTableName string) {
176161
ConsistentRead: aws.Bool(true),
177162
}
178163
result, err := ddb.GetItem(context.TODO(), getInput)
179-
if err != nil {
180-
panic(err)
181-
}
164+
utils.HandleError(err)
182165
// Verify the decrypted item
183166
if !reflect.DeepEqual(item, result.Item) {
184167
panic("Decrypted item does not match original item")
185168
}
186169
fmt.Println("Raw Aes Example successful.")
187170
}
188-
189-
func generateAes256KeyBytes() ([]byte, error) {
190-
key := make([]byte, 32) // 256 bits = 32 bytes
191-
// Use crypto/rand for cryptographically secure random numbers
192-
_, err := rand.Read(key)
193-
if err != nil {
194-
return nil, err
195-
}
196-
return key, nil
197-
}

Examples/runtimes/go/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77

88
func main() {
99
keyring.AwsKmsKeyringExample(utils.KmsKeyID(), utils.DdbTableName())
10-
keyring.RawAesExample(utils.DdbTableName())
10+
keyring.RawAesExample(utils.DdbTableName(), utils.KeyNamespace(), utils.KeyName(), utils.GenerateAes256KeyBytes())
1111
}

Examples/runtimes/go/utils/exampleUtils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
package utils
55

6+
import "crypto/rand"
7+
68
const (
79
kmsKeyID = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
810
ddbTableName = "DynamoDbEncryptionInterceptorTestTableCS"
11+
keyNamespace = "my-key-namespace"
12+
keyName = "my-key-name"
13+
aesKeyBytes = 32 // 256 bits = 32 bytes
914
)
1015

1116
func KmsKeyID() string {
@@ -16,6 +21,14 @@ func DdbTableName() string {
1621
return ddbTableName
1722
}
1823

24+
func KeyNamespace() string {
25+
return keyNamespace
26+
}
27+
28+
func KeyName() string {
29+
return keyName
30+
}
31+
1932
func AreMapsEqual(map1, map2 map[string]string) bool {
2033
if len(map1) != len(map2) {
2134
return false
@@ -29,3 +42,20 @@ func AreMapsEqual(map1, map2 map[string]string) bool {
2942
}
3043
return true
3144
}
45+
46+
func HandleError(err error) {
47+
// Error handling is limited to panic for demonstration purposes only.
48+
// In your code, errors should be properly handled.
49+
if err != nil {
50+
panic(err)
51+
}
52+
}
53+
54+
func GenerateAes256KeyBytes() []byte {
55+
key := make([]byte, aesKeyBytes)
56+
// crypto/rand is used here for demonstration.
57+
// In your code, you should implement a key generation strategy that meets your security needs.
58+
_, err := rand.Read(key)
59+
HandleError(err)
60+
return key
61+
}

0 commit comments

Comments
 (0)