forked from coinbase/mongobetween
/
config.go
250 lines (215 loc) · 6.12 KB
/
config.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package config
import (
"errors"
"flag"
"fmt"
"os"
"regexp"
"strings"
"github.com/DataDog/datadog-go/statsd"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/rdeavilafloqast/mongobetween/mongo"
"github.com/rdeavilafloqast/mongobetween/proxy"
)
const usernamePlaceholder = "_"
const defaultStatsdAddress = "localhost:8125"
var validNetworks = []string{"tcp", "tcp4", "tcp6", "unix", "unixpacket"}
type Config struct {
network string
unlink bool
ping bool
pretty bool
clients []client
statsd string
level zapcore.Level
dynamic string
}
type client struct {
address string
label string
opts *options.ClientOptions
}
func ParseFlags() *Config {
config, err := parseFlags()
if err != nil {
fmt.Printf("Error: %v\n", err)
flag.Usage()
os.Exit(2)
}
return config
}
func (c *Config) LogLevel() zapcore.Level {
return c.level
}
func (c *Config) Pretty() bool {
return c.pretty
}
func (c *Config) Proxies(log *zap.Logger) (proxies []*proxy.Proxy, err error) {
sd, err := statsd.New(c.statsd, statsd.WithNamespace("mongobetween"))
if err != nil {
return nil, err
}
d, err := proxy.NewDynamic(c.dynamic, log)
if err != nil {
return nil, err
}
mongos := make(map[string]*mongo.Mongo)
for _, client := range c.clients {
m, err := mongo.Connect(log, sd, client.opts, c.ping)
if err != nil {
return nil, err
}
mongos[client.address] = m
}
mongoLookup := func(address string) *mongo.Mongo {
return mongos[address]
}
for _, client := range c.clients {
p, err := proxy.NewProxy(log, sd, client.label, c.network, client.address, c.unlink, mongoLookup, d)
if err != nil {
return nil, err
}
proxies = append(proxies, p)
}
return
}
func validNetwork(network string) bool {
for _, n := range validNetworks {
if n == network {
return true
}
}
return false
}
func parseFlags() (*Config, error) {
flag.Usage = func() {
fmt.Printf("Usage: %s [OPTIONS] address1=uri1 [address2=uri2] ...\n", os.Args[0])
flag.PrintDefaults()
}
var unlink, ping, pretty bool
var network, username, password, stats, loglevel, dynamic string
flag.StringVar(&network, "network", "tcp4", "One of: tcp, tcp4, tcp6, unix or unixpacket")
flag.StringVar(&username, "username", "", "MongoDB username")
flag.StringVar(&password, "password", "", "MongoDB password")
flag.StringVar(&stats, "statsd", defaultStatsdAddress, "Statsd address")
flag.BoolVar(&unlink, "unlink", false, "Unlink existing unix sockets before listening")
flag.BoolVar(&ping, "ping", false, "Ping downstream MongoDB before listening")
flag.BoolVar(&pretty, "pretty", false, "Pretty print logging")
flag.StringVar(&loglevel, "loglevel", "info", "One of: debug, info, warn, error, dpanic, panic, fatal")
flag.StringVar(&dynamic, "dynamic", "", "File or URL to query for dynamic configuration")
flag.Parse()
network = expandEnv(network)
username = expandEnv(username)
password = expandEnv(password)
stats = expandEnv(stats)
loglevel = expandEnv(loglevel)
dynamic = expandEnv(dynamic)
level := zap.InfoLevel
if loglevel != "" {
err := level.Set(loglevel)
if err != nil {
return nil, fmt.Errorf("invalid loglevel: %s", loglevel)
}
}
if !validNetwork(network) {
return nil, fmt.Errorf("invalid network: %s", network)
}
addressMap := make(map[string]string)
for _, arg := range flag.Args() {
arg = expandEnv(arg)
all := strings.FieldsFunc(arg, func(r rune) bool {
return r == '|' || r == '\n'
})
for _, v := range all {
split := strings.SplitN(v, "=", 2)
if len(split) != 2 {
return nil, errors.New("malformed address=uri option")
}
if _, ok := addressMap[split[0]]; ok {
return nil, fmt.Errorf("uri already defined for address: %s", split[0])
}
addressMap[split[0]] = split[1]
}
}
if len(addressMap) == 0 {
return nil, errors.New("missing address=uri(s)")
}
var clients []client
for address, uri := range addressMap {
label, opts, err := clientOptions(uri, username, password)
if err != nil {
return nil, err
}
clients = append(clients, client{
address: address,
label: label,
opts: opts,
})
}
return &Config{
network: network,
unlink: unlink,
ping: ping,
pretty: pretty,
clients: clients,
statsd: stats,
level: level,
dynamic: dynamic,
}, nil
}
func expandEnv(config string) string {
// more restrictive version of os.ExpandEnv that only replaces exact matches of ${ENV}
return regexp.MustCompile(`\${(\w+)}`).ReplaceAllStringFunc(config, func(s string) string {
return os.ExpandEnv(s)
})
}
func clientOptions(uri, username, password string) (string, *options.ClientOptions, error) {
uri = uriWorkaround(uri, username)
cs, err := connstring.Parse(uri)
if err != nil {
return "", nil, err
}
label := ""
if len(cs.UnknownOptions["label"]) > 0 {
label = cs.UnknownOptions["label"][0]
}
opts := options.Client()
opts.ApplyURI(uri)
if username != "" {
if opts.Auth == nil {
opts.SetAuth(options.Credential{Username: username})
} else if opts.Auth.Username == "" || opts.Auth.Username == usernamePlaceholder {
opts.Auth.Username = username
}
}
if password != "" {
if opts.Auth == nil {
opts.SetAuth(options.Credential{Password: password})
} else if opts.Auth.Password == "" {
opts.Auth.Password = password
}
}
if err := opts.Validate(); err != nil {
return "", nil, err
}
return label, opts, nil
}
func uriWorkaround(uri, username string) string {
// Workaround for a feature in the Mongo driver URI parsing where you can't set a URI
// without setting the username ("error parsing uri: authsource without username is
// invalid"). This method force-adds a username in the URI, which can be overridden
// using SetAuth(). This workaround can be removed once the 1.4 driver is released
// (see https://jira.mongodb.org/browse/GODRIVER-1473).
if !strings.Contains(uri, "@") && username != "" {
split := strings.SplitN(uri, "//", 2)
if len(split) == 2 {
uri = fmt.Sprintf("%s//%s@%s", split[0], usernamePlaceholder, split[1])
} else {
uri = fmt.Sprintf("%s@%s", usernamePlaceholder, split[0])
}
}
return uri
}