-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
171 lines (137 loc) · 3.4 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package driver
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/url"
"os"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func buildConnection(ctx context.Context, uri, certPath string, minSize, maxSize int) (*mongo.Client, error) {
tlsConfig, err := getCustomTLSConfig(certPath)
if err != nil {
return nil, err
}
opt := options.Client().ApplyURI(uri)
if minSize > 0 {
opt = opt.SetMinPoolSize(uint64(minSize))
}
if maxSize > 0 {
opt = opt.SetMaxPoolSize(uint64(maxSize))
}
if tlsConfig != nil {
opt = opt.SetTLSConfig(tlsConfig)
}
client, err := mongo.Connect(ctx, opt)
if err != nil {
return nil, err
}
if err = client.Ping(ctx, nil); err != nil {
return nil, err
}
return client, nil
}
// buildConnectionURI builds a connection URI from a configuration
func buildConnectionURI(config *Config) (string, error) {
if config.URI != "" {
dbName, err := getDBNameFromURI(config.URI)
if err != nil {
return "", err
}
config.DBName = dbName
return config.URI, nil
}
if !validateMinimumConfig(config) {
return "", errors.New("mongo-driver: missing required configuration")
}
template := "mongodb://%s:%s@%s/%s"
flag := false
token := "?"
type params struct {
check string
property []string
value string
}
paramsList := []params{
{
check: config.ReadPreference,
property: []string{config.ReadPreference},
value: "%s%sreadPreference=%s",
},
{
check: config.AuthSource,
property: []string{config.AuthSource},
value: "%s%sauthSource=%s",
},
{
check: config.AuthMechanism,
property: []string{config.AuthMechanism},
value: "%s%sauthMechanism=%s",
},
{
check: config.ReplicaSet,
property: []string{config.ReplicaSet},
value: "%s%sreplicaSet=%s",
},
{
check: config.RetryWrites,
property: []string{config.RetryWrites},
value: "%s%sretryWrites=%s",
},
{
check: config.CertPath,
property: []string{},
value: "%s%s" + fmt.Sprintf("tls=true&sslVerifyCertificate=%t", config.SSLVerifyCertificate),
},
}
for _, param := range paramsList {
if param.check == "" {
continue
}
replaces := []any{template, token}
for _, p := range param.property {
replaces = append(replaces, p)
}
template = fmt.Sprintf(param.value, replaces...)
if !flag {
token = "&"
flag = true
}
}
return fmt.Sprintf(template, config.Username, config.Password, config.ClusterEndpoint, config.DBName), nil
}
func validateMinimumConfig(config *Config) bool {
if config.Username == "" || config.Password == "" || config.ClusterEndpoint == "" || config.DBName == "" {
return false
}
return true
}
func getCustomTLSConfig(caFile string) (*tls.Config, error) {
if caFile == "" {
return nil, nil
}
tlsConfig := new(tls.Config)
certs, err := os.ReadFile(caFile)
if err != nil {
return tlsConfig, err
}
tlsConfig.RootCAs = x509.NewCertPool()
ok := tlsConfig.RootCAs.AppendCertsFromPEM(certs)
if !ok {
return tlsConfig, errors.New("mongo-driver: failed parsing pem file")
}
return tlsConfig, nil
}
func getDBNameFromURI(uri string) (string, error) {
parsed, err := url.Parse(uri)
if err != nil {
return "", errors.Join(errors.New("mongo-driver: error getting db name from uri"), err)
}
if parsed.Path == "" {
return "", errors.New("mongo-driver: no db name specified in uri")
}
return parsed.Path[1:], nil
}