@@ -33,7 +33,7 @@ func NewEncryptedClient(client *dynamodb.Client, materialsProvider provider.Cryp
33
33
// PutItem encrypts an item and puts it into a DynamoDB table.
34
34
func (ec * EncryptedClient ) PutItem (ctx context.Context , input * dynamodb.PutItemInput ) (* dynamodb.PutItemOutput , error ) {
35
35
// Encrypt the item, excluding primary keys
36
- encryptedItem , err := ec .encryptItem (ctx , * input .TableName , input .Item )
36
+ encryptedItem , err := ec .encryptItem (ctx , aws . StringValue ( input .TableName ) , input .Item )
37
37
if err != nil {
38
38
return nil , fmt .Errorf ("failed to encrypt item: %v" , err )
39
39
}
@@ -62,7 +62,7 @@ func (ec *EncryptedClient) GetItem(ctx context.Context, input *dynamodb.GetItemI
62
62
}
63
63
64
64
// Decrypt the item, excluding primary keys
65
- decryptedItem , err := ec .decryptItem (ctx , * input .TableName , encryptedOutput .Item )
65
+ decryptedItem , err := ec .decryptItem (ctx , aws . StringValue ( input .TableName ) , encryptedOutput .Item )
66
66
if err != nil {
67
67
return nil , fmt .Errorf ("failed to decrypt item: %v" , err )
68
68
}
@@ -84,7 +84,7 @@ func (ec *EncryptedClient) Query(ctx context.Context, input *dynamodb.QueryInput
84
84
85
85
// Decrypt the items in the response
86
86
for i , item := range encryptedOutput .Items {
87
- decryptedItem , decryptErr := ec .decryptItem (ctx , * input .TableName , item )
87
+ decryptedItem , decryptErr := ec .decryptItem (ctx , aws . StringValue ( input .TableName ) , item )
88
88
if decryptErr != nil {
89
89
return nil , decryptErr
90
90
}
@@ -103,7 +103,7 @@ func (ec *EncryptedClient) Scan(ctx context.Context, input *dynamodb.ScanInput)
103
103
104
104
// Decrypt the items in the response
105
105
for i , item := range encryptedOutput .Items {
106
- decryptedItem , decryptErr := ec .decryptItem (ctx , * input .TableName , item )
106
+ decryptedItem , decryptErr := ec .decryptItem (ctx , aws . StringValue ( input .TableName ) , item )
107
107
if decryptErr != nil {
108
108
return nil , decryptErr
109
109
}
@@ -168,7 +168,10 @@ func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.Delet
168
168
}
169
169
170
170
// Construct material name based on the primary key of the item being deleted
171
- materialName := ec .constructMaterialName (input .Key , pkInfo )
171
+ materialName , err := utils .ConstructMaterialName (input .Key , pkInfo )
172
+ if err != nil {
173
+ return nil , fmt .Errorf ("error constructing material name: %v" , err )
174
+ }
172
175
173
176
// Delete the associated metadata
174
177
tableName := ec .materialsProvider .TableName ()
@@ -246,7 +249,10 @@ func (ec *EncryptedClient) encryptItem(ctx context.Context, tableName string, it
246
249
}
247
250
248
251
// Generate and fetch encryption materials
249
- materialName := ec .constructMaterialName (item , pkInfo )
252
+ materialName , err := utils .ConstructMaterialName (item , pkInfo )
253
+ if err != nil {
254
+ return nil , fmt .Errorf ("error constructing material name: %v" , err )
255
+ }
250
256
encryptionMaterials , err := ec .materialsProvider .EncryptionMaterials (ctx , materialName )
251
257
if err != nil {
252
258
return nil , fmt .Errorf ("failed to fetch encryption materials: %v" , err )
@@ -284,7 +290,10 @@ func (ec *EncryptedClient) decryptItem(ctx context.Context, tableName string, it
284
290
}
285
291
286
292
// Construct the material name based on primary keys
287
- materialName := ec .constructMaterialName (item , pkInfo )
293
+ materialName , err := utils .ConstructMaterialName (item , pkInfo )
294
+ if err != nil {
295
+ return nil , fmt .Errorf ("error constructing material name: %v" , err )
296
+ }
288
297
decryptionMaterials , err := ec .materialsProvider .DecryptionMaterials (ctx , materialName , 0 )
289
298
if err != nil {
290
299
return nil , fmt .Errorf ("failed to fetch decryption materials: %v" , err )
@@ -318,19 +327,3 @@ func (ec *EncryptedClient) decryptItem(ctx context.Context, tableName string, it
318
327
319
328
return decryptedItem , nil
320
329
}
321
-
322
- // constructMaterialName constructs a material name based on an item's primary key.
323
- func (ec * EncryptedClient ) constructMaterialName (item map [string ]types.AttributeValue , pkInfo * utils.PrimaryKeyInfo ) string {
324
- partitionKeyValue := item [pkInfo .PartitionKey ].(* types.AttributeValueMemberS ).Value
325
- sortKeyValue := ""
326
- if pkInfo .SortKey != "" && item [pkInfo .SortKey ] != nil {
327
- sortKeyValue = item [pkInfo .SortKey ].(* types.AttributeValueMemberS ).Value
328
- }
329
-
330
- rawMaterialName := pkInfo .Table + "-" + partitionKeyValue
331
- if sortKeyValue != "" {
332
- rawMaterialName += "-" + sortKeyValue
333
- }
334
-
335
- return utils .HashString (rawMaterialName )
336
- }
0 commit comments