-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycrypter.go
79 lines (66 loc) · 2.53 KB
/
keycrypter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Package dyno provides a simple way to encrypt and decrypt dynamodb items with a KMS key.
// It is useful for passing sensitive information to a client. For example, the LastEvaluatedKey
// returned by a dynamodb query can be encrypted and passed to a client. The client can then
// pass the encrypted LastEvaluatedKey back to the server, which can decrypt it and use it
// to continue the query.
package dyno
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// KeyCrypter is an interface that encrypts and decrypts DynamoDB primary key attribute values.
type KeyCrypter interface {
// Encrypt encrypts a DynamoDB primary key item along with an encryption context.
Encrypt(ctx context.Context, item map[string]types.AttributeValue) (string, error)
// Decrypt decrypts a DynamoDB primary key item.
// If the item was encrypted with an encryption context,
// the same context must be provided to decrypt the item.
Decrypt(ctx context.Context, item string) (map[string]types.AttributeValue, error)
}
type encryptionContextKey struct{}
// WithEncryptionContext returns a new context with the given AWS KMS encryption context.
func WithEncryptionContext(ctx context.Context, ec map[string]string) context.Context {
return context.WithValue(ctx, encryptionContextKey{}, ec)
}
func getEncryptionContext(ctx context.Context) (ec map[string]string, ok bool) {
ec, ok = ctx.Value(encryptionContextKey{}).(map[string]string)
return
}
func serialize(input map[string]types.AttributeValue) ([]byte, error) {
for _, v := range input {
switch v.(type) {
case *types.AttributeValueMemberS, *types.AttributeValueMemberB, *types.AttributeValueMemberN:
continue
default:
return nil, fmt.Errorf("unsupported type: %T", v)
}
}
var jsonMap map[string]any
if err := attributevalue.UnmarshalMap(input, &jsonMap); err != nil {
return nil, err
}
return json.Marshal(jsonMap)
}
func deserialize(input []byte) (map[string]types.AttributeValue, error) {
var jsonMap map[string]any
if err := json.Unmarshal(input, &jsonMap); err != nil {
return nil, err
}
output, err := attributevalue.MarshalMap(jsonMap)
if err != nil {
return nil, err
}
// Convert hex strings back to byte slices
for k, v := range output {
if s, ok := v.(*types.AttributeValueMemberS); ok {
if val, err := base64.StdEncoding.DecodeString(s.Value); err == nil {
output[k] = &types.AttributeValueMemberB{Value: val}
}
}
}
return output, nil
}