github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/autoencryptionoptions.go (about)

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package options
     8  
     9  import (
    10  	"crypto/tls"
    11  	"net/http"
    12  
    13  	"go.mongodb.org/mongo-driver/internal"
    14  )
    15  
    16  // AutoEncryptionOptions represents options used to configure auto encryption/decryption behavior for a mongo.Client
    17  // instance.
    18  //
    19  // Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic
    20  // encryption is not supported for operations on a database or view, and operations that are not bypassed will result
    21  // in error. Too bypass automatic encryption for all operations, set BypassAutoEncryption=true.
    22  //
    23  // Auto encryption requires the authenticated user to have the listCollections privilege action.
    24  //
    25  // If automatic encryption fails on an operation, use a MongoClient configured with bypassAutoEncryption=true and use
    26  // ClientEncryption.encrypt() to manually encrypt values.
    27  //
    28  // Enabling Client Side Encryption reduces the maximum document and message size (using a maxBsonObjectSize of 2MiB and
    29  // maxMessageSizeBytes of 6MB) and may have a negative performance impact.
    30  type AutoEncryptionOptions struct {
    31  	KeyVaultClientOptions *ClientOptions
    32  	KeyVaultNamespace     string
    33  	KmsProviders          map[string]map[string]interface{}
    34  	SchemaMap             map[string]interface{}
    35  	BypassAutoEncryption  *bool
    36  	ExtraOptions          map[string]interface{}
    37  	TLSConfig             map[string]*tls.Config
    38  	HTTPClient            *http.Client
    39  	EncryptedFieldsMap    map[string]interface{}
    40  	BypassQueryAnalysis   *bool
    41  }
    42  
    43  // AutoEncryption creates a new AutoEncryptionOptions configured with default values.
    44  func AutoEncryption() *AutoEncryptionOptions {
    45  	return &AutoEncryptionOptions{
    46  		HTTPClient: internal.DefaultHTTPClient,
    47  	}
    48  }
    49  
    50  // SetKeyVaultClientOptions specifies options for the client used to communicate with the key vault collection.
    51  //
    52  // If this is set, it is used to create an internal mongo.Client.
    53  // Otherwise, if the target mongo.Client being configured has an unlimited connection pool size (i.e. maxPoolSize=0),
    54  // it is reused to interact with the key vault collection.
    55  // Otherwise, if the target mongo.Client has a limited connection pool size, a separate internal mongo.Client is used
    56  // (and created if necessary). The internal mongo.Client may be shared during automatic encryption (if
    57  // BypassAutomaticEncryption is false). The internal mongo.Client is configured with the same options as the target
    58  // mongo.Client except minPoolSize is set to 0 and AutoEncryptionOptions is omitted.
    59  func (a *AutoEncryptionOptions) SetKeyVaultClientOptions(opts *ClientOptions) *AutoEncryptionOptions {
    60  	a.KeyVaultClientOptions = opts
    61  	return a
    62  }
    63  
    64  // SetKeyVaultNamespace specifies the namespace of the key vault collection. This is required.
    65  func (a *AutoEncryptionOptions) SetKeyVaultNamespace(ns string) *AutoEncryptionOptions {
    66  	a.KeyVaultNamespace = ns
    67  	return a
    68  }
    69  
    70  // SetKmsProviders specifies options for KMS providers. This is required.
    71  func (a *AutoEncryptionOptions) SetKmsProviders(providers map[string]map[string]interface{}) *AutoEncryptionOptions {
    72  	a.KmsProviders = providers
    73  	return a
    74  }
    75  
    76  // SetSchemaMap specifies a map from namespace to local schema document. Schemas supplied in the schemaMap only apply
    77  // to configuring automatic encryption for client side encryption. Other validation rules in the JSON schema will not
    78  // be enforced by the driver and will result in an error.
    79  //
    80  // Supplying a schemaMap provides more security than relying on JSON Schemas obtained from the server. It protects
    81  // against a malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted
    82  // data that should be encrypted.
    83  func (a *AutoEncryptionOptions) SetSchemaMap(schemaMap map[string]interface{}) *AutoEncryptionOptions {
    84  	a.SchemaMap = schemaMap
    85  	return a
    86  }
    87  
    88  // SetBypassAutoEncryption specifies whether or not auto encryption should be done.
    89  //
    90  // If this is unset or false and target mongo.Client being configured has an unlimited connection pool size
    91  // (i.e. maxPoolSize=0), it is reused in the process of auto encryption.
    92  // Otherwise, if the target mongo.Client has a limited connection pool size, a separate internal mongo.Client is used
    93  // (and created if necessary). The internal mongo.Client may be shared for key vault operations (if KeyVaultClient is
    94  // unset). The internal mongo.Client is configured with the same options as the target mongo.Client except minPoolSize
    95  // is set to 0 and AutoEncryptionOptions is omitted.
    96  func (a *AutoEncryptionOptions) SetBypassAutoEncryption(bypass bool) *AutoEncryptionOptions {
    97  	a.BypassAutoEncryption = &bypass
    98  	return a
    99  }
   100  
   101  // SetExtraOptions specifies a map of options to configure the mongocryptd process or mongo_crypt shared library.
   102  //
   103  // # Supported Extra Options
   104  //
   105  // "mongocryptdURI" - The mongocryptd URI. Allows setting a custom URI used to communicate with the
   106  // mongocryptd process. The default is "mongodb://localhost:27020", which works with the default
   107  // mongocryptd process spawned by the Client. Must be a string.
   108  //
   109  // "mongocryptdBypassSpawn" - If set to true, the Client will not attempt to spawn a mongocryptd
   110  // process. Must be a bool.
   111  //
   112  // "mongocryptdSpawnPath" - The path used when spawning mongocryptd.
   113  // Defaults to empty string and spawns mongocryptd from system path. Must be a string.
   114  //
   115  // "mongocryptdSpawnArgs" - Command line arguments passed when spawning mongocryptd.
   116  // Defaults to ["--idleShutdownTimeoutSecs=60"]. Must be an array of strings.
   117  //
   118  // "cryptSharedLibRequired" - If set to true, Client creation will return an error if the
   119  // crypt_shared library is not loaded. If unset or set to false, Client creation will not return an
   120  // error if the crypt_shared library is not loaded. The default is unset. Must be a bool.
   121  //
   122  // "cryptSharedLibPath" - The crypt_shared library override path. This must be the path to the
   123  // crypt_shared dynamic library file (for example, a .so, .dll, or .dylib file), not the directory
   124  // that contains it. If the override path is a relative path, it will be resolved relative to the
   125  // working directory of the process. If the override path is a relative path and the first path
   126  // component is the literal string "$ORIGIN", the "$ORIGIN" component will be replaced by the
   127  // absolute path to the directory containing the linked libmongocrypt library. Setting an override
   128  // path disables the default system library search path. If an override path is specified but the
   129  // crypt_shared library cannot be loaded, Client creation will return an error. Must be a string.
   130  func (a *AutoEncryptionOptions) SetExtraOptions(extraOpts map[string]interface{}) *AutoEncryptionOptions {
   131  	a.ExtraOptions = extraOpts
   132  	return a
   133  }
   134  
   135  // SetTLSConfig specifies tls.Config instances for each KMS provider to use to configure TLS on all connections created
   136  // to the KMS provider.
   137  //
   138  // This should only be used to set custom TLS configurations. By default, the connection will use an empty tls.Config{} with MinVersion set to tls.VersionTLS12.
   139  func (a *AutoEncryptionOptions) SetTLSConfig(tlsOpts map[string]*tls.Config) *AutoEncryptionOptions {
   140  	tlsConfigs := make(map[string]*tls.Config)
   141  	for provider, config := range tlsOpts {
   142  		// use TLS min version 1.2 to enforce more secure hash algorithms and advanced cipher suites
   143  		if config.MinVersion == 0 {
   144  			config.MinVersion = tls.VersionTLS12
   145  		}
   146  		tlsConfigs[provider] = config
   147  	}
   148  	a.TLSConfig = tlsConfigs
   149  	return a
   150  }
   151  
   152  // SetEncryptedFieldsMap specifies a map from namespace to local EncryptedFieldsMap document.
   153  // EncryptedFieldsMap is used for Queryable Encryption.
   154  func (a *AutoEncryptionOptions) SetEncryptedFieldsMap(ef map[string]interface{}) *AutoEncryptionOptions {
   155  	a.EncryptedFieldsMap = ef
   156  	return a
   157  }
   158  
   159  // SetBypassQueryAnalysis specifies whether or not query analysis should be used for automatic encryption.
   160  // Use this option when using explicit encryption with Queryable Encryption.
   161  func (a *AutoEncryptionOptions) SetBypassQueryAnalysis(bypass bool) *AutoEncryptionOptions {
   162  	a.BypassQueryAnalysis = &bypass
   163  	return a
   164  }
   165  
   166  // MergeAutoEncryptionOptions combines the argued AutoEncryptionOptions in a last-one wins fashion.
   167  //
   168  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
   169  // single options struct instead.
   170  func MergeAutoEncryptionOptions(opts ...*AutoEncryptionOptions) *AutoEncryptionOptions {
   171  	aeo := AutoEncryption()
   172  	for _, opt := range opts {
   173  		if opt == nil {
   174  			continue
   175  		}
   176  
   177  		if opt.KeyVaultClientOptions != nil {
   178  			aeo.KeyVaultClientOptions = opt.KeyVaultClientOptions
   179  		}
   180  		if opt.KeyVaultNamespace != "" {
   181  			aeo.KeyVaultNamespace = opt.KeyVaultNamespace
   182  		}
   183  		if opt.KmsProviders != nil {
   184  			aeo.KmsProviders = opt.KmsProviders
   185  		}
   186  		if opt.SchemaMap != nil {
   187  			aeo.SchemaMap = opt.SchemaMap
   188  		}
   189  		if opt.BypassAutoEncryption != nil {
   190  			aeo.BypassAutoEncryption = opt.BypassAutoEncryption
   191  		}
   192  		if opt.ExtraOptions != nil {
   193  			aeo.ExtraOptions = opt.ExtraOptions
   194  		}
   195  		if opt.TLSConfig != nil {
   196  			aeo.TLSConfig = opt.TLSConfig
   197  		}
   198  		if opt.EncryptedFieldsMap != nil {
   199  			aeo.EncryptedFieldsMap = opt.EncryptedFieldsMap
   200  		}
   201  		if opt.BypassQueryAnalysis != nil {
   202  			aeo.BypassQueryAnalysis = opt.BypassQueryAnalysis
   203  		}
   204  		if opt.HTTPClient != nil {
   205  			aeo.HTTPClient = opt.HTTPClient
   206  		}
   207  	}
   208  
   209  	return aeo
   210  }