forked from mothership/rds-auth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_client.go
355 lines (314 loc) · 9.89 KB
/
proxy_client.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"github.com/AlecAivazis/survey/v2"
"github.com/JacobJohansen/rds-auth-proxy/pkg/aws"
"github.com/JacobJohansen/rds-auth-proxy/pkg/config"
"github.com/JacobJohansen/rds-auth-proxy/pkg/kubernetes"
"github.com/JacobJohansen/rds-auth-proxy/pkg/log"
"github.com/JacobJohansen/rds-auth-proxy/pkg/proxy"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var proxyClientCommand = &cobra.Command{
Use: "client",
Short: "Launches the localhost proxy",
Long: `Runs a localhost proxy service in-cluster for connecting to RDS.`,
RunE: func(cmd *cobra.Command, args []string) error {
if os.Getenv("AWS_REGION") == "" {
os.Setenv("AWS_REGION", "us-east-1")
}
logCfg := zap.NewDevelopmentConfig()
logCfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
logCfg.Development = false
logger, err := logCfg.Build(zap.WithCaller(false))
if err != nil {
return err
}
log.SetLogger(logger)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rdsClient, err := aws.NewRDSClient(ctx)
if err != nil {
return err
}
redshiftClient, err := aws.NewRedshiftClient(ctx)
if err != nil {
return err
}
filepath, err := cmd.Flags().GetString("configfile")
if err != nil {
return err
}
cfg, err := config.LoadConfig(ctx, rdsClient, redshiftClient, filepath)
if err != nil {
return err
}
// Look up the proxy target
proxyTarget, err := getProxyTarget(cmd, cfg.ProxyTargets)
if err != nil {
return err
}
// Look up the real target name in the target list
target, err := getTarget(cmd, cfg.Targets, cfg.RDSTargets, cfg.RedshiftTargets)
if err != nil {
return err
}
// Override local port if needed
if target.LocalPort != nil {
addr, err := net.ResolveTCPAddr("tcp", cfg.Proxy.ListenAddr)
if err != nil {
return err
}
cfg.Proxy.ListenAddr = fmt.Sprintf("%s:%s", addr.IP, *target.LocalPort)
}
// Optionally grab the password
pass, err := cmd.Flags().GetString("password")
if err != nil {
return err
}
err = printConnectionString(cfg.Proxy.ListenAddr, target)
if err != nil {
return err
}
if proxyTarget.PortForward != nil {
// setup port-forward
prtCmd, err := kubernetes.BuildPortForwardCommand(ctx, proxyTarget.PortForward.KubeConfigFilePath, kubernetes.PortForwardOptions{
Namespace: proxyTarget.PortForward.Namespace,
Deployment: proxyTarget.PortForward.DeploymentName,
Ports: []string{fmt.Sprintf("%s:%s", proxyTarget.PortForward.GetLocalPort(), proxyTarget.PortForward.RemotePort)},
Context: proxyTarget.PortForward.Context,
})
if err != nil {
return err
}
go func() {
if err := kubernetes.ForwardPort(ctx, prtCmd); err != nil {
// TODO: blow this up gracefully
log.Error("k8s port-forward caught error", zap.Error(err), zap.String("listen_addr", proxyTarget.GetHost()))
panic(err)
}
log.Info("k8s port-forward exited", zap.String("listen_addr", proxyTarget.GetHost()))
}()
<-prtCmd.ReadyChannel
ports, err := prtCmd.PortForwarder.GetPorts()
if err != nil {
return err
}
portUsed := fmt.Sprintf("%d", ports[0].Local)
proxyTarget.PortForward.LocalPort = &portUsed
log.Info("started k8s port-forward", zap.String("listen_addr", proxyTarget.GetHost()))
}
log.Info("starting client proxy", zap.String("listen_addr", cfg.Proxy.ListenAddr))
opts, err := proxySSLOptions(cfg.Proxy.SSL)
if err != nil {
return err
}
var outboundHost string
if proxyTarget.AwsAuthOnly {
outboundHost = target.Host
} else {
outboundHost = proxyTarget.GetHost()
}
manager, err := proxy.NewManager(proxy.MergeOptions(opts, []proxy.Option{
proxy.WithListenAddress(cfg.Proxy.ListenAddr),
proxy.WithMode(proxy.ClientSide),
proxy.WithAWSAuthOnly(proxyTarget.AwsAuthOnly),
proxy.WithCredentialInterceptor(func(creds *proxy.Credentials) error {
// Send this connection to the proxy host
creds.Host = outboundHost
// Use provided password, or generate an RDS password to forward through
if pass != "" {
creds.Password = pass
} else if _, ok := cfg.RDSTargets[target.Name]; ok {
authToken, err := rdsClient.NewAuthToken(ctx, target.Host, target.Region, creds.Username)
if err != nil {
return err
}
creds.Password = authToken
} else if _, ok := cfg.RedshiftTargets[target.Name]; ok {
authToken, err := redshiftClient.NewAuthToken(ctx, target.Name, target.Region, creds.Username)
if err != nil {
return err
}
creds.Username = "IAM:" + creds.Username
creds.Password = authToken
}
if !proxyTarget.AwsAuthOnly {
creds.Options["host"] = target.Host
}
return overrideSSLConfig(creds, proxyTarget.SSL)
})})...,
)
if err != nil {
return err
}
// Shutdown app on SIGINT/SIGTERM
signals := make(chan os.Signal, 1)
go func() {
_ = manager.Start(ctx)
close(signals)
}()
signal.Notify(signals, os.Interrupt)
<-signals
cancel()
return nil
},
}
func printConnectionString(listenAddr string, target *config.Target) error {
addr, err := net.ResolveTCPAddr("tcp", listenAddr)
if err != nil {
return err
}
start := fmt.Sprintf("psql -h %s -p %d", addr.IP, addr.Port)
if target.DefaultDatabase != nil && *target.DefaultDatabase != "" {
start += fmt.Sprintf(" -d %s", *target.DefaultDatabase)
} else {
start += " -d {your_database}"
}
start += " -U {your user}"
fmt.Printf("Setting up a tunnel to %s\n\nGive this a second, then in a new shell, connect with:\n\n\t%s\n\n", target.Name, start)
return nil
}
func getProxyTarget(cmd *cobra.Command, targets map[string]*config.ProxyTarget) (*config.ProxyTarget, error) {
// Look up the proxy target
proxyName, err := cmd.Flags().GetString("proxy-target")
if err != nil {
return nil, err
}
proxyTarget, ok := targets[proxyName]
if ok {
return proxyTarget, nil
}
opts := make([]string, 0, len(targets))
for name := range targets {
opts = append(opts, name)
}
prompt := &survey.Select{
Message: "Select an upstream proxy",
Options: opts,
}
err = survey.AskOne(prompt, &proxyName)
if err != nil {
return nil, err
}
proxyTarget, ok = targets[proxyName]
if ok {
return proxyTarget, nil
}
return nil, fmt.Errorf("couldn't find a proxy target")
}
func getTarget(cmd *cobra.Command, targets map[string]*config.Target, rdsTargets map[string]*config.Target, redshiftTargets map[string]*config.Target) (*config.Target, error) {
// Look up the proxy target
targetName, err := cmd.Flags().GetString("target")
if err != nil {
return nil, err
}
target, ok := targets[targetName]
if ok {
return target, nil
}
target, ok = rdsTargets[targetName]
if ok {
return target, nil
}
target, ok = redshiftTargets[targetName]
if ok {
return target, nil
}
opts := make([]string, 0, len(targets)+len(rdsTargets))
for name := range targets {
opts = append(opts, name)
}
for name := range rdsTargets {
opts = append(opts, name)
}
for name := range redshiftTargets {
opts = append(opts, name)
}
prompt := &survey.Select{
Message: "Select a database",
Options: opts,
}
err = survey.AskOne(prompt, &targetName)
if err != nil {
return nil, err
}
target, ok = targets[targetName]
if ok {
return target, nil
}
target, ok = rdsTargets[targetName]
if ok {
return target, nil
}
target, ok = redshiftTargets[targetName]
if ok {
return target, nil
}
return nil, fmt.Errorf("couldn't find a target db")
}
func overrideSSLConfig(creds *proxy.Credentials, ssl config.SSL) error {
creds.SSLMode = ssl.Mode
// If the config wants us to use a specific SSL client cert, load it
if ssl.ClientCertificatePath != nil {
// TODO: load sooner / cache
cert, err := tls.LoadX509KeyPair(*ssl.ClientCertificatePath, *ssl.ClientPrivateKeyPath)
if err != nil {
return err
}
creds.ClientCertificate = &cert
}
// If the config wants us to validate the cert chain goes to a specific root cert for the server proxy
// load it, and set it
if ssl.RootCertificatePath != nil {
rootCABytes, err := ioutil.ReadFile(*ssl.RootCertificatePath)
if err != nil {
return err
}
decoded, _ := pem.Decode(rootCABytes)
cert, err := x509.ParseCertificate(decoded.Bytes)
if err != nil {
return err
}
creds.RootCertificate = cert
}
return nil
}
func proxySSLOptions(ssl config.ServerSSL) ([]proxy.Option, error) {
opts := make([]proxy.Option, 0, 2)
if ssl.Enabled {
if ssl.CertificatePath == nil && ssl.PrivateKeyPath == nil {
opts = append(opts, proxy.WithGeneratedServerCertificate())
} else if ssl.CertificatePath != nil && ssl.PrivateKeyPath != nil {
opts = append(opts, proxy.WithServerCertificate(*ssl.CertificatePath, *ssl.PrivateKeyPath))
} else {
return opts, fmt.Errorf("bad options: when ssl is enabled, either both a certificate and key must be provided, or neither provided")
}
}
if ssl.ClientCertificatePath == nil && ssl.ClientPrivateKeyPath == nil {
opts = append(opts, proxy.WithGeneratedClientCertificate())
} else if ssl.ClientCertificatePath != nil && ssl.ClientPrivateKeyPath != nil {
opts = append(opts, proxy.WithClientCertificate(*ssl.ClientCertificatePath, *ssl.ClientPrivateKeyPath))
} else {
return opts, fmt.Errorf("bad options: either both a client certificate and key must be provided, or neither provided")
}
return opts, nil
}
func init() {
proxyClientCommand.PersistentFlags().String("proxy-target", "default", "Name of the proxy target in the configfile")
proxyClientCommand.PersistentFlags().String("target", "", "Name of the target, or db instance identifier that you wish to connect to")
proxyClientCommand.PersistentFlags().String("configfile", "", "Path to the proxy config file")
_ = proxyClientCommand.MarkPersistentFlagDirname("configfile")
proxyClientCommand.PersistentFlags().String("password", "", "Password for the user if IAM auth is not set up")
rootCmd.AddCommand(proxyClientCommand)
}