Skip to content

Commit c435d4f

Browse files
committed
WIP
1 parent 0aef528 commit c435d4f

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

pkg/encrypted/client.go

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ type PrimaryKeyInfo struct {
3232

3333
// EncryptedPaginator is a paginator for encrypted DynamoDB items.
3434
type EncryptedPaginator struct {
35-
client *EncryptedClient
36-
nextToken map[string]types.AttributeValue
35+
Client *EncryptedClient
36+
NextToken map[string]types.AttributeValue
3737
}
3838

3939
// NewEncryptedPaginator creates a new instance of EncryptedPaginator.
4040
func NewEncryptedPaginator(client *EncryptedClient) *EncryptedPaginator {
4141
return &EncryptedPaginator{
42-
client: client,
43-
nextToken: nil,
42+
Client: client,
43+
NextToken: nil,
4444
}
4545
}
4646

4747
func (p *EncryptedPaginator) Query(ctx context.Context, input *dynamodb.QueryInput, fn func(*dynamodb.QueryOutput, bool) bool) error {
4848
for {
49-
if p.nextToken != nil {
50-
input.ExclusiveStartKey = p.nextToken
49+
if p.NextToken != nil {
50+
input.ExclusiveStartKey = p.NextToken
5151
}
5252

53-
output, err := p.client.Query(ctx, input)
53+
output, err := p.Client.Query(ctx, input)
5454
if err != nil {
5555
return err
5656
}
@@ -64,19 +64,19 @@ func (p *EncryptedPaginator) Query(ctx context.Context, input *dynamodb.QueryInp
6464
break
6565
}
6666

67-
p.nextToken = output.LastEvaluatedKey
67+
p.NextToken = output.LastEvaluatedKey
6868
}
6969

7070
return nil
7171
}
7272

7373
func (p *EncryptedPaginator) Scan(ctx context.Context, input *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool) error {
7474
for {
75-
if p.nextToken != nil {
76-
input.ExclusiveStartKey = p.nextToken
75+
if p.NextToken != nil {
76+
input.ExclusiveStartKey = p.NextToken
7777
}
7878

79-
output, err := p.client.Scan(ctx, input)
79+
output, err := p.Client.Scan(ctx, input)
8080
if err != nil {
8181
return err
8282
}
@@ -90,29 +90,28 @@ func (p *EncryptedPaginator) Scan(ctx context.Context, input *dynamodb.ScanInput
9090
break
9191
}
9292

93-
p.nextToken = output.LastEvaluatedKey
93+
p.NextToken = output.LastEvaluatedKey
9494
}
9595

9696
return nil
9797
}
9898

9999
// EncryptedClient facilitates encrypted operations on DynamoDB items.
100100
type EncryptedClient struct {
101-
client DynamoDBClientInterface
102-
materialsProvider provider.CryptographicMaterialsProvider
103-
primaryKeyCache map[string]*PrimaryKeyInfo
104-
attributeActions *AttributeActions
105-
106-
lock sync.RWMutex
101+
Client DynamoDBClientInterface
102+
MaterialsProvider provider.CryptographicMaterialsProvider
103+
PrimaryKeyCache map[string]*PrimaryKeyInfo
104+
AttributeActions *AttributeActions
105+
lock sync.RWMutex
107106
}
108107

109108
// NewEncryptedClient creates a new instance of EncryptedClient.
110109
func NewEncryptedClient(client DynamoDBClientInterface, materialsProvider provider.CryptographicMaterialsProvider, attributeActions *AttributeActions) *EncryptedClient {
111110
return &EncryptedClient{
112-
client: client,
113-
materialsProvider: materialsProvider,
114-
primaryKeyCache: make(map[string]*PrimaryKeyInfo),
115-
attributeActions: attributeActions,
111+
Client: client,
112+
MaterialsProvider: materialsProvider,
113+
PrimaryKeyCache: make(map[string]*PrimaryKeyInfo),
114+
AttributeActions: attributeActions,
116115
lock: sync.RWMutex{},
117116
}
118117
}
@@ -139,13 +138,13 @@ func (ec *EncryptedClient) PutItem(ctx context.Context, input *dynamodb.PutItemI
139138
}
140139

141140
// Put the encrypted item into the DynamoDB table
142-
return ec.client.PutItem(ctx, encryptedInput)
141+
return ec.Client.PutItem(ctx, encryptedInput)
143142
}
144143

145144
// GetItem retrieves an item from a DynamoDB table and decrypts it.
146145
func (ec *EncryptedClient) GetItem(ctx context.Context, input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
147146
// First, retrieve the encrypted item from DynamoDB
148-
encryptedOutput, err := ec.client.GetItem(ctx, input)
147+
encryptedOutput, err := ec.Client.GetItem(ctx, input)
149148
if err != nil {
150149
return nil, fmt.Errorf("error retrieving encrypted item: %v", err)
151150
}
@@ -171,7 +170,7 @@ func (ec *EncryptedClient) GetItem(ctx context.Context, input *dynamodb.GetItemI
171170

172171
// Query executes a Query operation on DynamoDB and decrypts the returned items.
173172
func (ec *EncryptedClient) Query(ctx context.Context, input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) {
174-
encryptedOutput, err := ec.client.Query(ctx, input)
173+
encryptedOutput, err := ec.Client.Query(ctx, input)
175174
if err != nil {
176175
return nil, fmt.Errorf("error querying encrypted items: %v", err)
177176
}
@@ -190,7 +189,7 @@ func (ec *EncryptedClient) Query(ctx context.Context, input *dynamodb.QueryInput
190189

191190
// Scan executes a Scan operation on DynamoDB and decrypts the returned items.
192191
func (ec *EncryptedClient) Scan(ctx context.Context, input *dynamodb.ScanInput) (*dynamodb.ScanOutput, error) {
193-
encryptedOutput, err := ec.client.Scan(ctx, input)
192+
encryptedOutput, err := ec.Client.Scan(ctx, input)
194193
if err != nil {
195194
return nil, fmt.Errorf("error scanning encrypted items: %v", err)
196195
}
@@ -223,12 +222,12 @@ func (ec *EncryptedClient) BatchWriteItem(ctx context.Context, input *dynamodb.B
223222
}
224223
}
225224

226-
return ec.client.BatchWriteItem(ctx, input)
225+
return ec.Client.BatchWriteItem(ctx, input)
227226
}
228227

229228
// BatchGetItem retrieves a batch of items from DynamoDB and decrypts them.
230229
func (ec *EncryptedClient) BatchGetItem(ctx context.Context, input *dynamodb.BatchGetItemInput) (*dynamodb.BatchGetItemOutput, error) {
231-
encryptedOutput, err := ec.client.BatchGetItem(ctx, input)
230+
encryptedOutput, err := ec.Client.BatchGetItem(ctx, input)
232231
if err != nil {
233232
return nil, fmt.Errorf("error batch getting encrypted items: %v", err)
234233
}
@@ -250,7 +249,7 @@ func (ec *EncryptedClient) BatchGetItem(ctx context.Context, input *dynamodb.Bat
250249
// DeleteItem deletes an item and its associated metadata from a DynamoDB table.
251250
func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) {
252251
// First, delete the item from DynamoDB
253-
deleteOutput, err := ec.client.DeleteItem(ctx, input)
252+
deleteOutput, err := ec.Client.DeleteItem(ctx, input)
254253
if err != nil {
255254
return nil, fmt.Errorf("error deleting encrypted item: %v", err)
256255
}
@@ -268,7 +267,7 @@ func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.Delet
268267
}
269268

270269
// Delete the associated metadata
271-
tableName := ec.materialsProvider.TableName()
270+
tableName := ec.MaterialsProvider.TableName()
272271
queryInput := &dynamodb.QueryInput{
273272
TableName: aws.String(tableName),
274273
KeyConditionExpression: aws.String("MaterialName = :materialName"),
@@ -277,7 +276,7 @@ func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.Delet
277276
},
278277
}
279278

280-
queryOutput, err := ec.client.Query(ctx, queryInput)
279+
queryOutput, err := ec.Client.Query(ctx, queryInput)
281280
if err != nil {
282281
return nil, fmt.Errorf("error querying for versions: %v", err)
283282
}
@@ -297,7 +296,7 @@ func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.Delet
297296
}
298297

299298
batchWriteInput := &dynamodb.BatchWriteItemInput{RequestItems: deleteRequest}
300-
_, err = ec.client.BatchWriteItem(ctx, batchWriteInput)
299+
_, err = ec.Client.BatchWriteItem(ctx, batchWriteInput)
301300
if err != nil {
302301
return nil, fmt.Errorf("error deleting a version: %v", err)
303302
}
@@ -309,7 +308,7 @@ func (ec *EncryptedClient) DeleteItem(ctx context.Context, input *dynamodb.Delet
309308
// getPrimaryKeyInfo lazily loads and caches primary key information in a thread-safe manner.
310309
func (ec *EncryptedClient) getPrimaryKeyInfo(ctx context.Context, tableName string) (*PrimaryKeyInfo, error) {
311310
ec.lock.RLock()
312-
pkInfo, exists := ec.primaryKeyCache[tableName]
311+
pkInfo, exists := ec.PrimaryKeyCache[tableName]
313312
ec.lock.RUnlock()
314313

315314
if exists {
@@ -319,17 +318,17 @@ func (ec *EncryptedClient) getPrimaryKeyInfo(ctx context.Context, tableName stri
319318
ec.lock.Lock()
320319
defer ec.lock.Unlock()
321320

322-
pkInfo, exists = ec.primaryKeyCache[tableName]
321+
pkInfo, exists = ec.PrimaryKeyCache[tableName]
323322
if exists {
324323
return pkInfo, nil
325324
}
326325

327-
pkInfo, err := TableInfo(ctx, ec.client, tableName)
326+
pkInfo, err := TableInfo(ctx, ec.Client, tableName)
328327
if err != nil {
329328
return nil, err
330329
}
331330

332-
ec.primaryKeyCache[tableName] = pkInfo
331+
ec.PrimaryKeyCache[tableName] = pkInfo
333332

334333
return pkInfo, nil
335334
}
@@ -347,7 +346,7 @@ func (ec *EncryptedClient) encryptItem(ctx context.Context, tableName string, it
347346
if err != nil {
348347
return nil, fmt.Errorf("error constructing material name: %v", err)
349348
}
350-
encryptionMaterials, err := ec.materialsProvider.EncryptionMaterials(ctx, materialName)
349+
encryptionMaterials, err := ec.MaterialsProvider.EncryptionMaterials(ctx, materialName)
351350
if err != nil {
352351
return nil, fmt.Errorf("failed to fetch encryption materials: %v", err)
353352
}
@@ -365,7 +364,7 @@ func (ec *EncryptedClient) encryptItem(ctx context.Context, tableName string, it
365364
return nil, fmt.Errorf("error converting attribute value to bytes: %v", err)
366365
}
367366

368-
action := ec.attributeActions.GetAttributeAction(key)
367+
action := ec.AttributeActions.GetAttributeAction(key)
369368
switch action {
370369
case AttributeActionEncrypt, AttributeActionEncryptDeterministically:
371370
// TODO: Implement deterministic encryption
@@ -394,7 +393,7 @@ func (ec *EncryptedClient) decryptItem(ctx context.Context, tableName string, it
394393
if err != nil {
395394
return nil, fmt.Errorf("error constructing material name: %v", err)
396395
}
397-
decryptionMaterials, err := ec.materialsProvider.DecryptionMaterials(ctx, materialName, 0)
396+
decryptionMaterials, err := ec.MaterialsProvider.DecryptionMaterials(ctx, materialName, 0)
398397
if err != nil {
399398
return nil, fmt.Errorf("failed to fetch decryption materials: %v", err)
400399
}
@@ -414,7 +413,7 @@ func (ec *EncryptedClient) decryptItem(ctx context.Context, tableName string, it
414413
continue
415414
}
416415

417-
action := ec.attributeActions.GetAttributeAction(key)
416+
action := ec.AttributeActions.GetAttributeAction(key)
418417
switch action {
419418
case AttributeActionEncrypt, AttributeActionEncryptDeterministically:
420419
// TODO: Implement deterministic encryption

0 commit comments

Comments
 (0)