Skip to content

Commit df0a3f2

Browse files
Complete item encryptor example
1 parent 49c9483 commit df0a3f2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Examples/runtimes/go/itemencryptor/itemencryptdecrypt.go

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

66
import (
77
"context"
8+
"fmt"
89
"reflect"
910

1011
mpl "github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygenerated"
@@ -142,24 +143,38 @@ func Itemencryptdecrypt(kmsKeyID, ddbTableName string) {
142143
encryptItemOutput, err := itemEncryptorClient.EncryptItem(context.Background(), *encryptItemInput)
143144
utils.HandleError(err)
144145

145-
// Check if partition_key is "ItemEncryptDecryptExample"
146-
if partitionKeyAttr, ok := encryptItemOutput.EncryptedItem["partition_key"].(*types.AttributeValueMemberS); ok {
146+
// Demonstrate that the item has been encrypted
147+
encryptedItem := encryptItemOutput.EncryptedItem
148+
// Check partition_key is still a string and equals "ItemEncryptDecryptExample"
149+
if partitionKeyAttr, ok := encryptedItem["partition_key"].(*types.AttributeValueMemberS); ok {
147150
if partitionKeyAttr.Value != "ItemEncryptDecryptExample" {
148151
panic("Partition key is not 'ItemEncryptDecryptExample'")
149152
}
150153
} else {
151-
// Handle the case where partition_key is not a string attribute
152154
panic("Partition key is not a string attribute or doesn't exist")
153155
}
156+
// Check sort_key is a string and equals "0"
157+
if sortKeyAttr, ok := encryptedItem["sort_key"].(*types.AttributeValueMemberS); ok {
158+
if sortKeyAttr.Value != "0" {
159+
panic("Sort key is not '0'")
160+
}
161+
} else {
162+
panic("Sort key is not a string attribute or doesn't exist")
163+
}
164+
// Check attribute1 is binary (encrypted) and not a string anymore
165+
if _, ok := encryptedItem["attribute1"].(*types.AttributeValueMemberB); !ok {
166+
panic("attribute1 is not binary. It might not be encrypted.")
167+
}
154168

169+
// 7. Directly decrypt the encrypted item using the DynamoDb Item Encryptor
155170
decryptItemInput := &dbesdkitemencryptortypes.DecryptItemInput{
156-
EncryptedItem: encryptItemOutput.EncryptedItem,
171+
EncryptedItem: encryptedItem,
157172
}
158-
159173
decryptedItem, err := itemEncryptorClient.DecryptItem(context.Background(), *decryptItemInput)
160174
utils.HandleError(err)
161175

162176
if !reflect.DeepEqual(item, decryptedItem.PlaintextItem) {
163177
panic("Decrypted item does not match original item")
164178
}
179+
fmt.Println("Item Encryptor example successful")
165180
}

0 commit comments

Comments
 (0)