-
Notifications
You must be signed in to change notification settings - Fork 918
GODRIVER-3289 Add option to configure DEK cache lifetime. #1922
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
Changes from 2 commits
32e7246
c7a6219
9add837
1eb7c89
573812e
c71e2c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ import ( | |
|
|
||
| var ( | ||
| supportedSchemaVersions = map[int]string{ | ||
| 1: "1.17", | ||
| 1: "1.22", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
||
| return nil, err | ||
| } | ||
| } else { | ||
|
|
@@ -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, | ||
| }) | ||
|
||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -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) | ||
|
|
@@ -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 | ||
| } | ||
|
|
@@ -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 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ package options | |
| import ( | ||
| "crypto/tls" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "go.mongodb.org/mongo-driver/v2/internal/httputil" | ||
| ) | ||
|
|
@@ -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. | ||
|
|
@@ -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". | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should use |
||
| func (a *AutoEncryptionOptions) SetKeyExpiration(expiration time.Duration) *AutoEncryptionOptions { | ||
| a.KeyExpiration = &expiration | ||
|
|
||
| return a | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Since we don't need dynamic formatting, suggest using errors.New