Skip to content

Commit a15a8c5

Browse files
committed
WIP
1 parent fea55c5 commit a15a8c5

File tree

1 file changed

+74
-34
lines changed

1 file changed

+74
-34
lines changed

README.md

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,59 +41,99 @@ Here's a basic example of how to use the EncryptedClient to perform encrypted Dy
4141

4242
```go
4343
import (
44-
"context"
44+
"github.com/aws/aws-sdk-go-v2/config"
4545
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
4646
"github.com/cloudopsy/dynamodb-encryption-go/pkg/encrypted"
4747
"github.com/cloudopsy/dynamodb-encryption-go/pkg/provider"
4848
)
4949

5050
func main() {
51-
// Create a regular DynamoDB client
52-
dynamodbClient := dynamodb.NewFromConfig(cfg)
53-
54-
// Create a key material store
55-
materialStore, err := store.NewMetaStore(dynamodbClient, "metastore-table")
51+
// Create a new AWS session
52+
cfg, err := config.LoadDefaultConfig(context.TODO())
5653
if err != nil {
57-
log.Fatalf("Failed to create key material store: %v", err)
58-
}
59-
if err := materialStore.CreateTableIfNotExists(context.Background()); err != nil {
60-
log.Fatalf("Failed to ensure metastore table exists: %v", err)
54+
log.Fatalf("failed to load AWS configuration: %v", err)
6155
}
6256

63-
// Create a cryptographic materials provider
64-
keyURI := "aws-kms://arn:aws:kms:eu-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
65-
cmp, err := provider.NewAwsKmsCryptographicMaterialsProvider(keyURI, nil, materialStore)
57+
// Create a DynamoDB client
58+
dynamodbClient := dynamodb.NewFromConfig(cfg)
59+
60+
// Create a MetaStore for storing and retrieving metadata
61+
metaStore, err := store.NewMetaStore(dynamodbClient, "metadata-table")
6662
if err != nil {
67-
log.Fatalf("Failed to create cryptographic materials provider: %v", err)
63+
log.Fatalf("failed to create MetaStore: %v", err)
6864
}
6965

70-
// Create an encrypted DynamoDB client
71-
clientConfig := encrypted.NewClientConfig(
72-
encrypted.WithDefaultEncryption(encrypted.EncryptStandard),
73-
. }
74-
encryptedClient := encrypted.NewEncryptedClient(dynamodbClient, cmp, clientConfig)
75-
76-
// Perform encrypted DynamoDB operations
77-
putItemInput := &dynamodb.PutItemInput{
78-
TableName: aws.String("my-table"),
79-
Item: map[string]types.AttributeValue{
80-
"PK": &types.AttributeValueMemberS{Value: "123"},
81-
"SK": &types.AttributeValueMemberS{Value: "456"},
82-
"SensitiveData": &types.AttributeValueMemberS{Value: "my secret data"},
83-
},
84-
}
85-
_, err = encryptedClient.PutItem(context.Background(), putItemInput)
66+
// Create a CryptographicMaterialsProvider with the desired key provider (e.g., AWS KMS)
67+
keyARN := "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
68+
cmProvider, err := provider.NewAwsKmsCryptographicMaterialsProvider(keyARN, nil, metaStore)
8669
if err != nil {
87-
log.Fatalf("Failed to put encrypted item: %v", err)
70+
log.Fatalf("failed to create CryptographicMaterialsProvider: %v", err)
8871
}
8972

90-
// ... perform other operations ...
73+
// Create a ClientConfig with the desired encryption options
74+
clientConfig := encrypted.NewClientConfig(
75+
encrypted.WithDefaultEncryption(encrypted.EncryptNone),
76+
encrypted.WithEncryption("SensitiveAttribute", encrypted.EncryptStandard),
77+
)
78+
79+
// Create an EncryptedClient
80+
encryptedClient := encrypted.NewEncryptedClient(dynamodbClient, cmProvider, clientConfig)
9181
}
9282
```
9383

94-
In this example, we create a regular `dynamodb.Client`, a key material store, and a cryptographic materials provider. Then, we create an `EncryptedClient` instance with custom attribute actions to specify which attributes should be encrypted. Finally, we use the `EncryptedClient` to perform operations like PutItem, and the library automatically handles the encryption and decryption of sensitive data.
84+
Encrypting and Decrypting Items
85+
86+
With the EncryptedClient, you can perform various DynamoDB operations on encrypted items:
87+
88+
```go
89+
// PutItem
90+
item := map[string]types.AttributeValue{
91+
"ID": {S: aws.String("123")},
92+
"Name": {S: aws.String("John")},
93+
"SensitiveAttribute": {S: aws.String("Sensitive Value")},
94+
}
95+
input := &dynamodb.PutItemInput{
96+
TableName: aws.String("my-table"),
97+
Item: item,
98+
}
99+
_, err := encryptedClient.PutItem(context.TODO(), input)
100+
101+
// GetItem
102+
key := map[string]types.AttributeValue{
103+
"ID": {S: aws.String("123")},
104+
}
105+
input := &dynamodb.GetItemInput{
106+
TableName: aws.String("my-table"),
107+
Key: key,
108+
}
109+
result, err := encryptedClient.GetItem(context.TODO(), input)
110+
111+
// Query
112+
input := &dynamodb.QueryInput{
113+
TableName: aws.String("my-table"),
114+
KeyConditionExpression: aws.String("ID = :id"),
115+
ExpressionAttributeValues: map[string]types.AttributeValue{
116+
":id": {S: aws.String("123")},
117+
},
118+
}
119+
result, err := encryptedClient.Query(context.TODO(), input)
120+
```
121+
122+
The EncryptedClient transparently encrypts and decrypts items based on the specified encryption options in the ClientConfig. It also handles the storage and retrieval of metadata using the MetaStore.
123+
124+
## MetaStore
125+
126+
The MetaStore is responsible for storing and retrieving metadata associated with encrypted items. It uses a separate DynamoDB table to store the metadata, which includes the encrypted data keys and other relevant information.
127+
128+
When an item is encrypted, the EncryptedClient generates a unique material name based on the item's primary key and stores the encrypted data key and metadata in the MetaStore. When decrypting an item, the EncryptedClient retrieves the corresponding metadata from the MetaStore to obtain the necessary decryption materials.
129+
130+
The MetaStore provides the following key functions:
131+
132+
- **StoreNewMaterial**: Stores new encryption metadata for an item.
133+
- **RetrieveMaterial**: Retrieves the encryption metadata for an item based on its material name and version.
134+
- **CreateTableIfNotExists**: Creates the metadata table if it doesn't exist.
95135

96-
For more detailed examples and usage instructions, please refer to the documentation and the examples directory in the repository.
136+
The MetaStore ensures that the encryption metadata is securely stored and can be accessed efficiently during encryption and decryption operations.
97137

98138
## Contributing
99139

0 commit comments

Comments
 (0)