Skip to content

Commit

Permalink
add interface for ClientEncryption
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Aug 10, 2023
1 parent 82edc28 commit 34603c3
Show file tree
Hide file tree
Showing 6 changed files with 755 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MONGO_USERNAME="admin"
MONGO_PASSWORD="adminpass"
MONGO_URI="mongodb://$(MONGO_USERNAME):$(MONGO_PASSWORD)@127.0.0.1:$(MONGO_PORT)/?authSource=admin&directConnection=true"

all: go-install generate-mocks tidy lint test done
all: go-install generate-mocks tidy test done

done:
@echo "$(OK_COLOR)==> Done.$(NO_COLOR)"
Expand Down Expand Up @@ -49,10 +49,10 @@ lint:

tidy:
@echo "$(OK_COLOR)==> Updating go.mod...$(NO_COLOR)"
@go mod tidy -compat=1.19
@go mod tidy -compat=1.21

run-mockgen:
@mockgen -destination=mocks/gomock/mocks.go -package mocks . ChangeStream,Client,Collection,Cursor,Database,IndexView,Session,SingleResult,SessionContext
@mockgen -destination=mocks/gomock/mocks.go -package mocks . ChangeStream,Client,Collection,Cursor,Database,IndexView,Session,SingleResult,SessionContext,ClientEncryption

run-mockery:
@mockery --all --srcpkg github.com/sv-tools/mongoifc --output mocks/mockery --disable-version-string --case underscore
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ This is the main reason of wrapping the original objects and using the `mongoifc
- [x] SingleResult: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#SingleResult
- [x] IndexView: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#IndexView
- [x] SessionContext: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#SessionContext
- [x] ClientEncryption: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#ClientEncryption

## Mocks

Expand Down
17 changes: 14 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type Client interface {
Connect(ctx context.Context) error
Database(name string, opts ...*options.DatabaseOptions) Database
Disconnect(ctx context.Context) error
ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) ([]string, error)
ListDatabaseNames(
ctx context.Context,
filter interface{},
opts ...*options.ListDatabasesOptions,
) ([]string, error)
ListDatabases(
ctx context.Context,
filter interface{},
Expand All @@ -25,8 +29,15 @@ type Client interface {
Ping(ctx context.Context, rp *readpref.ReadPref) error
StartSession(opts ...*options.SessionOptions) (Session, error)
Timeout() *time.Duration
UseSession(ctx context.Context, fn func(sc SessionContext) error) error
UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(sc SessionContext) error) error
UseSession(
ctx context.Context,
fn func(sc SessionContext) error,
) error
UseSessionWithOptions(
ctx context.Context,
opts *options.SessionOptions,
fn func(sc SessionContext) error,
) error
Watch(
ctx context.Context,
pipeline interface{},
Expand Down
160 changes: 160 additions & 0 deletions client_encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))

Check warning on line 67 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L66-L67

Added lines #L66 - L67 were not covered by tests
}

func (c *clientEncryption) Close(ctx context.Context) error {
return c.ce.Close(ctx)

Check warning on line 71 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}

func (c *clientEncryption) CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...*options.DataKeyOptions,
) (primitive.Binary, error) {
return c.ce.CreateDataKey(ctx, kmsProvider, opts...)

Check warning on line 79 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}

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

Check warning on line 94 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L89-L94

Added lines #L89 - L94 were not covered by tests
}

func (c *clientEncryption) Decrypt(ctx context.Context, val primitive.Binary) (bson.RawValue, error) {
return c.ce.Decrypt(ctx, val)

Check warning on line 98 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

func (c *clientEncryption) DeleteKey(ctx context.Context, id primitive.Binary) (*mongo.DeleteResult, error) {
return c.ce.DeleteKey(ctx, id)

Check warning on line 102 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

func (c *clientEncryption) Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...*options.EncryptOptions,
) (primitive.Binary, error) {
return c.ce.Encrypt(ctx, val, opts...)

Check warning on line 110 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L109-L110

Added lines #L109 - L110 were not covered by tests
}

func (c *clientEncryption) EncryptExpression(
ctx context.Context,
expr interface{},
result interface{},
opts ...*options.EncryptOptions,
) error {
return c.ce.EncryptExpression(ctx, expr, result, opts...)

Check warning on line 119 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}

func (c *clientEncryption) GetKey(ctx context.Context, id primitive.Binary) SingleResult {
return wrapSingleResult(c.ce.GetKey(ctx, id))

Check warning on line 123 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}

func (c *clientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) SingleResult {
return wrapSingleResult(c.ce.GetKeyByAltName(ctx, keyAltName))

Check warning on line 127 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}

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

Check warning on line 135 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L130-L135

Added lines #L130 - L135 were not covered by tests
}

func (c *clientEncryption) RemoveKeyAltName(
ctx context.Context,
id primitive.Binary,
keyAltName string,
) SingleResult {
return wrapSingleResult(c.ce.RemoveKeyAltName(ctx, id, keyAltName))

Check warning on line 143 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L142-L143

Added lines #L142 - L143 were not covered by tests
}

func (c *clientEncryption) RewrapManyDataKey(
ctx context.Context,
filter interface{},
opts ...*options.RewrapManyDataKeyOptions,
) (*mongo.RewrapManyDataKeyResult, error) {
return c.ce.RewrapManyDataKey(ctx, filter, opts...)

Check warning on line 151 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L150-L151

Added lines #L150 - L151 were not covered by tests
}

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

Check warning on line 159 in client_encryption.go

View check run for this annotation

Codecov / codecov/patch

client_encryption.go#L154-L159

Added lines #L154 - L159 were not covered by tests
}
Loading

0 comments on commit 34603c3

Please sign in to comment.