Skip to content

Commit 6b9206a

Browse files
authored
support ClientEncryption as already provided by MongoDB (#14)
Fixes #13
1 parent fc5bf96 commit 6b9206a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

wrapped_client_ecryption.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mongowrapper
16+
17+
import (
18+
"context"
19+
20+
"go.mongodb.org/mongo-driver/bson"
21+
"go.mongodb.org/mongo-driver/bson/primitive"
22+
"go.mongodb.org/mongo-driver/mongo"
23+
"go.mongodb.org/mongo-driver/mongo/options"
24+
)
25+
26+
type WrappedClientEncryption struct {
27+
cc *mongo.ClientEncryption
28+
}
29+
30+
func (wc *WrappedClient) NewClientEncryption(opts ...*options.ClientEncryptionOptions) (*WrappedClientEncryption, error) {
31+
client, err := mongo.NewClientEncryption(wc.Client(), opts...)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return &WrappedClientEncryption{cc: client}, nil
36+
}
37+
38+
func (wce *WrappedClientEncryption) CreateDataKey(ctx context.Context, kmsProvider string, opts ...*options.DataKeyOptions) (primitive.Binary, error) {
39+
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.ClientEncryption.CreateDataKey")
40+
defer span.end(ctx)
41+
42+
id, err := wce.cc.CreateDataKey(ctx, kmsProvider, opts...)
43+
if err != nil {
44+
span.setError(err)
45+
}
46+
return id, err
47+
}
48+
49+
func (wce *WrappedClientEncryption) Encrypt(ctx context.Context, val bson.RawValue, opts ...*options.EncryptOptions) (primitive.Binary, error) {
50+
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.ClientEncryption.Encrypt")
51+
defer span.end(ctx)
52+
53+
value, err := wce.cc.Encrypt(ctx, val, opts...)
54+
if err != nil {
55+
span.setError(err)
56+
}
57+
return value, err
58+
}
59+
60+
func (wce *WrappedClientEncryption) Decrypt(ctx context.Context, val primitive.Binary) (bson.RawValue, error) {
61+
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.ClientEncryption.Decrypt")
62+
defer span.end(ctx)
63+
64+
value, err := wce.cc.Decrypt(ctx, val)
65+
if err != nil {
66+
span.setError(err)
67+
}
68+
return value, err
69+
}
70+
71+
func (wce *WrappedClientEncryption) Close(ctx context.Context) error {
72+
ctx, span := roundtripTrackingSpan(ctx, "go.mongodb.org/mongo-driver.ClientEncryption.Close")
73+
defer span.end(ctx)
74+
75+
err := wce.cc.Close(ctx)
76+
if err != nil {
77+
span.setError(err)
78+
}
79+
return err
80+
}

0 commit comments

Comments
 (0)