@@ -5,6 +5,7 @@ package itemencryptor
5
5
6
6
import (
7
7
"context"
8
+ "fmt"
8
9
"reflect"
9
10
10
11
mpl "github.com/aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygenerated"
@@ -142,24 +143,38 @@ func Itemencryptdecrypt(kmsKeyID, ddbTableName string) {
142
143
encryptItemOutput , err := itemEncryptorClient .EncryptItem (context .Background (), * encryptItemInput )
143
144
utils .HandleError (err )
144
145
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 {
147
150
if partitionKeyAttr .Value != "ItemEncryptDecryptExample" {
148
151
panic ("Partition key is not 'ItemEncryptDecryptExample'" )
149
152
}
150
153
} else {
151
- // Handle the case where partition_key is not a string attribute
152
154
panic ("Partition key is not a string attribute or doesn't exist" )
153
155
}
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
+ }
154
168
169
+ // 7. Directly decrypt the encrypted item using the DynamoDb Item Encryptor
155
170
decryptItemInput := & dbesdkitemencryptortypes.DecryptItemInput {
156
- EncryptedItem : encryptItemOutput . EncryptedItem ,
171
+ EncryptedItem : encryptedItem ,
157
172
}
158
-
159
173
decryptedItem , err := itemEncryptorClient .DecryptItem (context .Background (), * decryptItemInput )
160
174
utils .HandleError (err )
161
175
162
176
if ! reflect .DeepEqual (item , decryptedItem .PlaintextItem ) {
163
177
panic ("Decrypted item does not match original item" )
164
178
}
179
+ fmt .Println ("Item Encryptor example successful" )
165
180
}
0 commit comments