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 5 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: 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 @@ -270,6 +270,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.21",
1: "1.22",
}
)

Expand Down
3 changes: 2 additions & 1 deletion mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,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 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
10 changes: 10 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,11 @@ 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.

// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
func (a *AutoEncryptionOptions) SetKeyExpiration(expiration time.Duration) *AutoEncryptionOptions {
a.KeyExpiration = &expiration

return a
}
14 changes: 14 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,18 @@ 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".
// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
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