Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-3289 Add option to configure DEK cache lifetime. #1922

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/install-libmongocrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This script installs libmongocrypt into an "install" directory.
set -eux

LIBMONGOCRYPT_TAG="1.11.0"
LIBMONGOCRYPT_TAG="1.12.0"

# Install libmongocrypt based on OS.
if [ "Windows_NT" = "${OS:-}" ]; then
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/json_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func createAutoEncryptionOptions(t testing.TB, opts bson.Raw) *options.AutoEncry
aeo.SetEncryptedFieldsMap(encryptedFieldsMap)
case "bypassQueryAnalysis":
aeo.SetBypassQueryAnalysis(opt.Boolean())
case "keyExpirationMS":
aeo.SetKeyExpiration(time.Duration(opt.Int32()) * time.Millisecond)
default:
t.Fatalf("unrecognized auto encryption option: %v", name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,26 @@ func executeRewrapManyDataKey(ctx context.Context, operation *operation) (*opera
}
return rewrapManyDataKeyResultsOpResult(result)
}

// executeDecrypt will decrypt the given value.
func executeDecrypt(ctx context.Context, operation *operation) (*operationResult, error) {
cee, err := entities(ctx).clientEncryption(operation.Object)
if err != nil {
return nil, err
}

rawValue, err := operation.Arguments.LookupErr("value")
if err != nil {
return nil, err
}
t, d, ok := rawValue.BinaryOK()
if !ok {
return nil, fmt.Errorf("'value' argument is not a BSON binary")
}

rawValue, err = cee.Decrypt(ctx, bson.Binary{Subtype: t, Data: d})
if err != nil {
return newErrorResult(err), nil
}
return newValueResult(rawValue.Type, rawValue.Value, err), nil
}
16 changes: 10 additions & 6 deletions internal/integration/unified/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type clientEncryptionOpts struct {
KeyVaultClient string `bson:"keyVaultClient"`
KeyVaultNamespace string `bson:"keyVaultNamespace"`
KmsProviders map[string]bson.Raw `bson:"kmsProviders"`
KeyExpirationMS *int64 `bson:"keyExpirationMS"`
}

// EntityMap is used to store entities during tests. This type enforces uniqueness so no two entities can have the same
Expand Down Expand Up @@ -735,12 +736,15 @@ func (em *EntityMap) addClientEncryptionEntity(entityOptions *entityOptions) err
return newEntityNotFoundError("client", ceo.KeyVaultClient)
}

ce, err := mongo.NewClientEncryption(
keyVaultClient.Client,
options.ClientEncryption().
SetKeyVaultNamespace(ceo.KeyVaultNamespace).
SetTLSConfig(tlsconf).
SetKmsProviders(kmsProviders))
opts := options.ClientEncryption().
SetKeyVaultNamespace(ceo.KeyVaultNamespace).
SetTLSConfig(tlsconf).
SetKmsProviders(kmsProviders)
if ceo.KeyExpirationMS != nil {
opts.SetKeyExpiration(time.Duration(*ceo.KeyExpirationMS) * time.Millisecond)
}

ce, err := mongo.NewClientEncryption(keyVaultClient.Client, opts)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/unified/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
return executeDeleteKey(ctx, op)
case "addKeyAltName":
return executeAddKeyAltName(ctx, op)
case "decrypt":
return executeDecrypt(ctx, op)

// Unsupported operations
case "count", "listIndexNames":
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/unified/schema_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var (
supportedSchemaVersions = map[int]string{
1: "1.17",
1: "1.22",
}
)

Expand Down
81 changes: 30 additions & 51 deletions mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func newClient(opts ...*options.ClientOptions) (*Client, error) {
}
// AutoEncryptionOptions
if clientOpts.AutoEncryptionOptions != nil {
if err := client.configureAutoEncryption(clientOpts); err != nil {
if err = client.configureAutoEncryption(clientOpts); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a particular scoping or style reason for migrating from the variable declaration to assignment?

return nil, err
}
} else {
Expand Down Expand Up @@ -471,30 +471,48 @@ func (c *Client) endSessions(ctx context.Context) {
}

func (c *Client) configureAutoEncryption(args *options.ClientOptions) error {
c.encryptedFieldsMap = args.AutoEncryptionOptions.EncryptedFieldsMap
aeOpts := args.AutoEncryptionOptions
c.encryptedFieldsMap = aeOpts.EncryptedFieldsMap
if err := c.configureKeyVaultClientFLE(args); err != nil {
return err
}

if err := c.configureMetadataClientFLE(args); err != nil {
return err
}

mc, err := c.newMongoCrypt(args.AutoEncryptionOptions)
mc, err := c.newMongoCrypt(aeOpts)
if err != nil {
return err
}

// If the crypt_shared library was not loaded, try to spawn and connect to mongocryptd.
if mc.CryptSharedLibVersionString() == "" {
mongocryptdFLE, err := newMongocryptdClient(args.AutoEncryptionOptions)
c.mongocryptdFLE, err = newMongocryptdClient(aeOpts)
if err != nil {
return err
}
c.mongocryptdFLE = mongocryptdFLE
}

c.configureCryptFLE(mc, args.AutoEncryptionOptions)
kr := keyRetriever{coll: c.keyVaultCollFLE}
var cir collInfoRetriever
bypass := aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption
if !bypass {
if args.MaxPoolSize != nil && *args.MaxPoolSize == 0 {
c.metadataClientFLE = c
} else {
c.metadataClientFLE, err = c.getOrCreateInternalClient(args)
if err != nil {
return err
}
}
cir.client = c.metadataClientFLE
}

c.cryptFLE = driver.NewCrypt(&driver.CryptOptions{
MongoCrypt: mc,
CollInfoFn: cir.cryptCollInfo,
KeyFn: kr.cryptKeys,
MarkFn: c.mongocryptdFLE.markCommand,
TLSConfig: aeOpts.TLSConfig,
BypassAutoEncryption: bypass,
})
Comment on lines +493 to +515
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactor seems unrelated to DEK cache lifetime. I think this should be reverted to avoid confusing the scope of this PR.

return nil
}

Expand Down Expand Up @@ -537,24 +555,6 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e
return nil
}

func (c *Client) configureMetadataClientFLE(clientOpts *options.ClientOptions) error {
aeOpts := clientOpts.AutoEncryptionOptions

if aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption {
// no need for a metadata client.
return nil
}
if clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0 {
c.metadataClientFLE = c
return nil
}

var err error
c.metadataClientFLE, err = c.getOrCreateInternalClient(clientOpts)

return err
}

func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt.MongoCrypt, error) {
// convert schemas in SchemaMap to bsoncore documents
cryptSchemaMap := make(map[string]bsoncore.Document)
Expand Down Expand Up @@ -611,7 +611,8 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
SetEncryptedFieldsMap(cryptEncryptedFieldsMap).
SetCryptSharedLibDisabled(cryptSharedLibDisabled || bypassAutoEncryption).
SetCryptSharedLibOverridePath(cryptSharedLibPath).
SetHTTPClient(opts.HTTPClient))
SetHTTPClient(opts.HTTPClient).
SetKeyExpiration(opts.KeyExpiration))
if err != nil {
return nil, err
}
Expand All @@ -637,28 +638,6 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
return mc, nil
}

//nolint:unused // the unused linter thinks that this function is unreachable because "c.newMongoCrypt" always panics without the "cse" build tag set.
func (c *Client) configureCryptFLE(mc *mongocrypt.MongoCrypt, opts *options.AutoEncryptionOptions) {
bypass := opts.BypassAutoEncryption != nil && *opts.BypassAutoEncryption
kr := keyRetriever{coll: c.keyVaultCollFLE}
var cir collInfoRetriever
// If bypass is true, c.metadataClientFLE is nil and the collInfoRetriever
// will not be used. If bypass is false, to the parent client or the
// internal client.
if !bypass {
cir = collInfoRetriever{client: c.metadataClientFLE}
}

c.cryptFLE = driver.NewCrypt(&driver.CryptOptions{
MongoCrypt: mc,
CollInfoFn: cir.cryptCollInfo,
KeyFn: kr.cryptKeys,
MarkFn: c.mongocryptdFLE.markCommand,
TLSConfig: opts.TLSConfig,
BypassAutoEncryption: bypass,
})
}

// validSession returns an error if the session doesn't belong to the client
func (c *Client) validSession(sess *session.Client) error {
if sess != nil && sess.ClientID != c.id {
Expand Down
3 changes: 2 additions & 1 deletion mongo/client_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func NewClientEncryption(keyVaultClient *Client, opts ...options.Lister[options.
// ClientEncryption because it's only needed for AutoEncryption and we don't expect users to
// have the crypt_shared library installed if they're using ClientEncryption.
SetCryptSharedLibDisabled(true).
SetHTTPClient(cea.HTTPClient))
SetHTTPClient(cea.HTTPClient).
SetKeyExpiration(cea.KeyExpiration))
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions mongo/options/autoencryptionoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package options
import (
"crypto/tls"
"net/http"
"time"

"go.mongodb.org/mongo-driver/v2/internal/httputil"
)
Expand Down Expand Up @@ -40,6 +41,7 @@ type AutoEncryptionOptions struct {
HTTPClient *http.Client
EncryptedFieldsMap map[string]interface{}
BypassQueryAnalysis *bool
KeyExpiration *time.Duration
}

// AutoEncryption creates a new AutoEncryptionOptions configured with default values.
Expand Down Expand Up @@ -164,3 +166,10 @@ func (a *AutoEncryptionOptions) SetBypassQueryAnalysis(bypass bool) *AutoEncrypt

return a
}

// SetKeyExpiration specifies duration for the key expiration. 0 or negative value means "never expire".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are negative values interpreted by libmongocrypt as "never expire" or are we enforcing that behavior in the Go Driver? I can't find documentation on the negative case. The C and Rust implementations use uint64.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative values are handled in x/mongo/driver/mongocrypt/mongocrypt.go by passing 0 to libmongocrypt. I'm open to using uint64 to align the API with other drivers.

func (a *AutoEncryptionOptions) SetKeyExpiration(expiration time.Duration) *AutoEncryptionOptions {
a.KeyExpiration = &expiration

return a
}
13 changes: 13 additions & 0 deletions mongo/options/clientencryptionoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/tls"
"fmt"
"net/http"
"time"

"go.mongodb.org/mongo-driver/v2/internal/httputil"
)
Expand All @@ -22,6 +23,7 @@ type ClientEncryptionOptions struct {
KmsProviders map[string]map[string]interface{}
TLSConfig map[string]*tls.Config
HTTPClient *http.Client
KeyExpiration *time.Duration
}

// ClientEncryptionOptionsBuilder contains options to configure client
Expand Down Expand Up @@ -80,6 +82,17 @@ func (c *ClientEncryptionOptionsBuilder) SetTLSConfig(cfg map[string]*tls.Config
return c
}

// SetKeyExpiration specifies duration for the key expiration. 0 or negative value means "never expire".
func (c *ClientEncryptionOptionsBuilder) SetKeyExpiration(expiration time.Duration) *ClientEncryptionOptionsBuilder {
c.Opts = append(c.Opts, func(opts *ClientEncryptionOptions) error {
opts.KeyExpiration = &expiration

return nil
})

return c
}

// BuildTLSConfig specifies tls.Config options for each KMS provider to use to configure TLS on all connections created
// to the KMS provider. The input map should contain a mapping from each KMS provider to a document containing the necessary
// options, as follows:
Expand Down
Loading
Loading