...
1
2
3
4
5
6
7 package options
8
9 import (
10 "crypto/tls"
11 "fmt"
12 "net/http"
13
14 "go.mongodb.org/mongo-driver/internal/httputil"
15 )
16
17
18 type ClientEncryptionOptions struct {
19 KeyVaultNamespace string
20 KmsProviders map[string]map[string]interface{}
21 TLSConfig map[string]*tls.Config
22 HTTPClient *http.Client
23 }
24
25
26 func ClientEncryption() *ClientEncryptionOptions {
27 return &ClientEncryptionOptions{
28 HTTPClient: httputil.DefaultHTTPClient,
29 }
30 }
31
32
33 func (c *ClientEncryptionOptions) SetKeyVaultNamespace(ns string) *ClientEncryptionOptions {
34 c.KeyVaultNamespace = ns
35 return c
36 }
37
38
39 func (c *ClientEncryptionOptions) SetKmsProviders(providers map[string]map[string]interface{}) *ClientEncryptionOptions {
40 c.KmsProviders = providers
41 return c
42 }
43
44
45
46
47
48 func (c *ClientEncryptionOptions) SetTLSConfig(tlsOpts map[string]*tls.Config) *ClientEncryptionOptions {
49 tlsConfigs := make(map[string]*tls.Config)
50 for provider, config := range tlsOpts {
51
52 if config.MinVersion == 0 {
53 config.MinVersion = tls.VersionTLS12
54 }
55 tlsConfigs[provider] = config
56 }
57 c.TLSConfig = tlsConfigs
58 return c
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 func BuildTLSConfig(tlsOpts map[string]interface{}) (*tls.Config, error) {
85
86 cfg := &tls.Config{MinVersion: tls.VersionTLS12}
87
88 for name := range tlsOpts {
89 var err error
90 switch name {
91 case "tlsCertificateKeyFile", "sslClientCertificateKeyFile":
92 clientCertPath, ok := tlsOpts[name].(string)
93 if !ok {
94 return nil, fmt.Errorf("expected %q value to be of type string, got %T", name, tlsOpts[name])
95 }
96
97 if keyPwd, found := tlsOpts["tlsCertificateKeyFilePassword"].(string); found {
98 _, err = addClientCertFromConcatenatedFile(cfg, clientCertPath, keyPwd)
99 } else if keyPwd, found := tlsOpts["sslClientCertificateKeyPassword"].(string); found {
100 _, err = addClientCertFromConcatenatedFile(cfg, clientCertPath, keyPwd)
101 } else {
102 _, err = addClientCertFromConcatenatedFile(cfg, clientCertPath, "")
103 }
104 case "tlsCertificateKeyFilePassword", "sslClientCertificateKeyPassword":
105 continue
106 case "tlsCAFile", "sslCertificateAuthorityFile":
107 caPath, ok := tlsOpts[name].(string)
108 if !ok {
109 return nil, fmt.Errorf("expected %q value to be of type string, got %T", name, tlsOpts[name])
110 }
111 err = addCACertFromFile(cfg, caPath)
112 default:
113 return nil, fmt.Errorf("unrecognized TLS option %v", name)
114 }
115
116 if err != nil {
117 return nil, err
118 }
119 }
120
121 return cfg, nil
122 }
123
124
125
126
127
128 func MergeClientEncryptionOptions(opts ...*ClientEncryptionOptions) *ClientEncryptionOptions {
129 ceo := ClientEncryption()
130 for _, opt := range opts {
131 if opt == nil {
132 continue
133 }
134
135 if opt.KeyVaultNamespace != "" {
136 ceo.KeyVaultNamespace = opt.KeyVaultNamespace
137 }
138 if opt.KmsProviders != nil {
139 ceo.KmsProviders = opt.KmsProviders
140 }
141 if opt.TLSConfig != nil {
142 ceo.TLSConfig = opt.TLSConfig
143 }
144 if opt.HTTPClient != nil {
145 ceo.HTTPClient = opt.HTTPClient
146 }
147 }
148
149 return ceo
150 }
151
View as plain text