generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_encryption.go
160 lines (142 loc) · 4.45 KB
/
client_encryption.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ClientEncryption is an interface for `mongo.ClientEncryption` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#ClientEncryption
type ClientEncryption interface {
AddKeyAltName(
ctx context.Context,
id primitive.Binary,
keyAltName string,
) SingleResult
Close(ctx context.Context) error
CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...*options.DataKeyOptions,
) (primitive.Binary, error)
CreateEncryptedCollection(
ctx context.Context,
db Database,
coll string,
createOpts *options.CreateCollectionOptions,
kmsProvider string,
masterKey interface{},
) (Collection, bson.M, error)
Decrypt(ctx context.Context, val primitive.Binary) (bson.RawValue, error)
DeleteKey(ctx context.Context, id primitive.Binary) (*mongo.DeleteResult, error)
Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...*options.EncryptOptions,
) (primitive.Binary, error)
EncryptExpression(
ctx context.Context,
expr interface{},
result interface{},
opts ...*options.EncryptOptions,
) error
GetKey(ctx context.Context, id primitive.Binary) SingleResult
GetKeyByAltName(ctx context.Context, keyAltName string) SingleResult
GetKeys(ctx context.Context) (Cursor, error)
RemoveKeyAltName(
ctx context.Context,
id primitive.Binary,
keyAltName string,
) SingleResult
RewrapManyDataKey(
ctx context.Context,
filter interface{},
opts ...*options.RewrapManyDataKeyOptions,
) (*mongo.RewrapManyDataKeyResult, error)
}
type clientEncryption struct {
ce *mongo.ClientEncryption
}
func (c *clientEncryption) AddKeyAltName(ctx context.Context, id primitive.Binary, keyAltName string) SingleResult {
return wrapSingleResult(c.ce.AddKeyAltName(ctx, id, keyAltName))
}
func (c *clientEncryption) Close(ctx context.Context) error {
return c.ce.Close(ctx)
}
func (c *clientEncryption) CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...*options.DataKeyOptions,
) (primitive.Binary, error) {
return c.ce.CreateDataKey(ctx, kmsProvider, opts...)
}
func (c *clientEncryption) CreateEncryptedCollection(
ctx context.Context,
db Database,
coll string,
createOpts *options.CreateCollectionOptions,
kmsProvider string,
masterKey interface{},
) (Collection, bson.M, error) {
col, doc, err := c.ce.CreateEncryptedCollection(ctx, UnWrapDatabase(db), coll, createOpts, kmsProvider, masterKey)
if err != nil {
return nil, nil, err
}
return wrapCollection(col, db.(*database)), doc, err
}
func (c *clientEncryption) Decrypt(ctx context.Context, val primitive.Binary) (bson.RawValue, error) {
return c.ce.Decrypt(ctx, val)
}
func (c *clientEncryption) DeleteKey(ctx context.Context, id primitive.Binary) (*mongo.DeleteResult, error) {
return c.ce.DeleteKey(ctx, id)
}
func (c *clientEncryption) Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...*options.EncryptOptions,
) (primitive.Binary, error) {
return c.ce.Encrypt(ctx, val, opts...)
}
func (c *clientEncryption) EncryptExpression(
ctx context.Context,
expr interface{},
result interface{},
opts ...*options.EncryptOptions,
) error {
return c.ce.EncryptExpression(ctx, expr, result, opts...)
}
func (c *clientEncryption) GetKey(ctx context.Context, id primitive.Binary) SingleResult {
return wrapSingleResult(c.ce.GetKey(ctx, id))
}
func (c *clientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) SingleResult {
return wrapSingleResult(c.ce.GetKeyByAltName(ctx, keyAltName))
}
func (c *clientEncryption) GetKeys(ctx context.Context) (Cursor, error) {
cr, err := c.ce.GetKeys(ctx)
if err != nil {
return nil, err
}
return wrapCursor(cr), nil
}
func (c *clientEncryption) RemoveKeyAltName(
ctx context.Context,
id primitive.Binary,
keyAltName string,
) SingleResult {
return wrapSingleResult(c.ce.RemoveKeyAltName(ctx, id, keyAltName))
}
func (c *clientEncryption) RewrapManyDataKey(
ctx context.Context,
filter interface{},
opts ...*options.RewrapManyDataKeyOptions,
) (*mongo.RewrapManyDataKeyResult, error) {
return c.ce.RewrapManyDataKey(ctx, filter, opts...)
}
func NewClientEncryption(keyVaultClient Client, opts ...*options.ClientEncryptionOptions) (ClientEncryption, error) {
ce, err := mongo.NewClientEncryption(UnWrapClient(keyVaultClient), opts...)
if err != nil {
return nil, err
}
return &clientEncryption{ce: ce}, nil
}