Skip to content

Commit 904bac4

Browse files
committed
WIP
1 parent b32bf1d commit 904bac4

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

example/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ func main() {
159159
}
160160
fmt.Println("Encrypted items batch written successfully.")
161161

162+
deleteItem := &dynamodb.DeleteItemInput{
163+
TableName: &tableName,
164+
Key: map[string]types.AttributeValue{
165+
"UserID": &types.AttributeValueMemberS{Value: "user2"},
166+
},
167+
}
168+
169+
_, err = ec.DeleteItem(ctx, deleteItem)
170+
if err != nil {
171+
log.Fatalf("Failed to delete encrypted item: %v", err)
172+
}
173+
fmt.Println("Encrypted item deleted successfully.")
174+
162175
}
163176

164177
func createTableIfNotExists(ctx context.Context, client *dynamodb.Client, tableName string) error {

pkg/client/client.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
99
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
10+
"github.com/aws/aws-sdk-go/aws"
1011
"github.com/cloudopsy/dynamodb-encryption-go/pkg/provider"
1112
"github.com/cloudopsy/dynamodb-encryption-go/pkg/utils"
1213
)
@@ -152,6 +153,62 @@ func (ec *EncryptedClient) BatchGetItem(ctx context.Context, input *dynamodb.Bat
152153
return encryptedOutput, nil
153154
}
154155

156+
// DeleteItem deletes an item and its associated metadata from a DynamoDB table.
157+
func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) {
158+
// First, delete the item from DynamoDB
159+
deleteOutput, err := ec.client.DeleteItem(ctx, input)
160+
if err != nil {
161+
return nil, fmt.Errorf("error deleting encrypted item: %v", err)
162+
}
163+
164+
// Determine the material name or metadata identifier
165+
pkInfo, err := ec.getPrimaryKeyInfo(ctx, *input.TableName)
166+
if err != nil {
167+
return nil, fmt.Errorf("error fetching primary key info: %v", err)
168+
}
169+
170+
// Construct material name based on the primary key of the item being deleted
171+
materialName := ec.constructMaterialName(input.Key, pkInfo)
172+
173+
// Delete the associated metadata
174+
tableName := ec.materialsProvider.TableName()
175+
queryInput := &dynamodb.QueryInput{
176+
TableName: aws.String(tableName),
177+
KeyConditionExpression: aws.String("MaterialName = :materialName"),
178+
ExpressionAttributeValues: map[string]types.AttributeValue{
179+
":materialName": &types.AttributeValueMemberS{Value: materialName},
180+
},
181+
}
182+
183+
queryOutput, err := ec.client.Query(ctx, queryInput)
184+
if err != nil {
185+
return nil, fmt.Errorf("error querying for versions: %v", err)
186+
}
187+
188+
for _, item := range queryOutput.Items {
189+
deleteRequest := map[string][]types.WriteRequest{
190+
tableName: {
191+
{
192+
DeleteRequest: &types.DeleteRequest{
193+
Key: map[string]types.AttributeValue{
194+
"MaterialName": item["MaterialName"],
195+
"Version": item["Version"],
196+
},
197+
},
198+
},
199+
},
200+
}
201+
202+
batchWriteInput := &dynamodb.BatchWriteItemInput{RequestItems: deleteRequest}
203+
_, err = ec.client.BatchWriteItem(ctx, batchWriteInput)
204+
if err != nil {
205+
return nil, fmt.Errorf("error deleting a version: %v", err)
206+
}
207+
}
208+
209+
return deleteOutput, nil
210+
}
211+
155212
// getPrimaryKeyInfo lazily loads and caches primary key information in a thread-safe manner.
156213
func (ec *EncryptedClient) getPrimaryKeyInfo(ctx context.Context, tableName string) (*utils.PrimaryKeyInfo, error) {
157214
ec.lock.RLock()
@@ -221,7 +278,6 @@ func (ec *EncryptedClient) encryptItem(ctx context.Context, tableName string, it
221278

222279
// decryptItem decrypts a DynamoDB item's attributes, excluding primary keys.
223280
func (ec *EncryptedClient) decryptItem(ctx context.Context, tableName string, item map[string]types.AttributeValue) (map[string]types.AttributeValue, error) {
224-
// Fetch primary key info to identify these attributes
225281
pkInfo, err := ec.getPrimaryKeyInfo(ctx, tableName)
226282
if err != nil {
227283
return nil, err

pkg/provider/kms.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@ func (p *AwsKmsCryptographicMaterialsProvider) DecryptionMaterials(ctx context.C
8989
// Construct DecryptionMaterials with the actual delegatedKey
9090
return materials.NewDecryptionMaterials(materialDescMap, delegatedKey, nil), nil
9191
}
92+
93+
func (p *AwsKmsCryptographicMaterialsProvider) TableName() string {
94+
return p.MaterialStore.TableName
95+
}

pkg/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ import (
99
type CryptographicMaterialsProvider interface {
1010
EncryptionMaterials(ctx context.Context, materialName string) (*materials.EncryptionMaterials, error)
1111
DecryptionMaterials(ctx context.Context, materialName string, version int64) (*materials.DecryptionMaterials, error)
12+
TableName() string
1213
}

0 commit comments

Comments
 (0)