generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_encryption.go
More file actions
177 lines (159 loc) · 5.59 KB
/
client_encryption.go
File metadata and controls
177 lines (159 loc) · 5.59 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// ClientEncryption is an interface for `mongo.ClientEncryption` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#ClientEncryption
type ClientEncryption interface {
AddKeyAltName(
ctx context.Context,
id bson.Binary,
keyAltName string,
) SingleResult
Close(ctx context.Context) error
CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...options.Lister[options.DataKeyOptions],
) (bson.Binary, error)
CreateEncryptedCollection(
ctx context.Context,
db Database,
coll string,
createOpts options.Lister[options.CreateCollectionOptions],
kmsProvider string,
masterKey any,
) (Collection, bson.M, error)
Decrypt(ctx context.Context, val bson.Binary) (bson.RawValue, error)
DeleteKey(ctx context.Context, id bson.Binary) (*mongo.DeleteResult, error)
Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...options.Lister[options.EncryptOptions],
) (bson.Binary, error)
EncryptExpression(
ctx context.Context,
expr any,
result any,
opts ...options.Lister[options.EncryptOptions],
) error
GetKey(ctx context.Context, id bson.Binary) SingleResult
GetKeyByAltName(ctx context.Context, keyAltName string) SingleResult
GetKeys(ctx context.Context) (Cursor, error)
RemoveKeyAltName(
ctx context.Context,
id bson.Binary,
keyAltName string,
) SingleResult
RewrapManyDataKey(
ctx context.Context,
filter any,
opts ...options.Lister[options.RewrapManyDataKeyOptions],
) (*mongo.RewrapManyDataKeyResult, error)
}
type clientEncryption struct {
ce *mongo.ClientEncryption
}
// AddKeyAltName is a wrapper for `mongo.ClientEncryption.AddKeyAltName` method
func (c *clientEncryption) AddKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) SingleResult {
return wrapSingleResult(c.ce.AddKeyAltName(ctx, id, keyAltName))
}
// Close is a wrapper for `mongo.ClientEncryption.Close` method
func (c *clientEncryption) Close(ctx context.Context) error {
return c.ce.Close(ctx)
}
// CreateDataKey is a wrapper for `mongo.ClientEncryption.CreateDataKey` method
func (c *clientEncryption) CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...options.Lister[options.DataKeyOptions],
) (bson.Binary, error) {
return c.ce.CreateDataKey(ctx, kmsProvider, opts...)
}
// CreateEncryptedCollection is a wrapper for `mongo.ClientEncryption.CreateEncryptedCollection` method
func (c *clientEncryption) CreateEncryptedCollection(
ctx context.Context,
db Database,
coll string,
createOpts options.Lister[options.CreateCollectionOptions],
kmsProvider string,
masterKey any,
) (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
}
// Decrypt is a wrapper for `mongo.ClientEncryption.Decrypt` method
func (c *clientEncryption) Decrypt(ctx context.Context, val bson.Binary) (bson.RawValue, error) {
return c.ce.Decrypt(ctx, val)
}
// DeleteKey is a wrapper for `mongo.ClientEncryption.DeleteKey` method
func (c *clientEncryption) DeleteKey(ctx context.Context, id bson.Binary) (*mongo.DeleteResult, error) {
return c.ce.DeleteKey(ctx, id)
}
// Encrypt is a wrapper for `mongo.ClientEncryption.Encrypt` method
func (c *clientEncryption) Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...options.Lister[options.EncryptOptions],
) (bson.Binary, error) {
return c.ce.Encrypt(ctx, val, opts...)
}
// EncryptExpression is a wrapper for `mongo.ClientEncryption.EncryptExpression` method
func (c *clientEncryption) EncryptExpression(
ctx context.Context,
expr any,
result any,
opts ...options.Lister[options.EncryptOptions],
) error {
return c.ce.EncryptExpression(ctx, expr, result, opts...)
}
// GetKey is a wrapper for `mongo.ClientEncryption.GetKey` method
func (c *clientEncryption) GetKey(ctx context.Context, id bson.Binary) SingleResult {
return wrapSingleResult(c.ce.GetKey(ctx, id))
}
// GetKeyByAltName is a wrapper for `mongo.ClientEncryption.GetKeyByAltName` method
func (c *clientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) SingleResult {
return wrapSingleResult(c.ce.GetKeyByAltName(ctx, keyAltName))
}
// GetKeys is a wrapper for `mongo.ClientEncryption.GetKeys` method
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
}
// RemoveKeyAltName is a wrapper for `mongo.ClientEncryption.RemoveKeyAltName` method
func (c *clientEncryption) RemoveKeyAltName(
ctx context.Context,
id bson.Binary,
keyAltName string,
) SingleResult {
return wrapSingleResult(c.ce.RemoveKeyAltName(ctx, id, keyAltName))
}
// RewrapManyDataKey is a wrapper for `mongo.ClientEncryption.RewrapManyDataKey` method
func (c *clientEncryption) RewrapManyDataKey(
ctx context.Context,
filter any,
opts ...options.Lister[options.RewrapManyDataKeyOptions],
) (*mongo.RewrapManyDataKeyResult, error) {
return c.ce.RewrapManyDataKey(ctx, filter, opts...)
}
// NewClientEncryption creates a new ClientEncryption
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#NewClientEncryption
func NewClientEncryption(
keyVaultClient Client,
opts ...options.Lister[options.ClientEncryptionOptions],
) (ClientEncryption, error) {
ce, err := mongo.NewClientEncryption(UnWrapClient(keyVaultClient), opts...)
if err != nil {
return nil, err
}
return &clientEncryption{ce: ce}, nil
}