forked from mothership/rds-auth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_server.go
72 lines (66 loc) · 2.03 KB
/
proxy_server.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
package cmd
import (
"context"
"fmt"
"time"
"github.com/JacobJohansen/rds-auth-proxy/pkg/aws"
"github.com/JacobJohansen/rds-auth-proxy/pkg/config"
"github.com/JacobJohansen/rds-auth-proxy/pkg/log"
"github.com/JacobJohansen/rds-auth-proxy/pkg/proxy"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var proxyServerCommand = &cobra.Command{
Use: "server",
Short: "Launches the server proxy",
Long: `Runs a proxy service in-cluster for connecting to RDS.`,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: make this gracefully shutdown on sigterm / sigint
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := log.NewLogger()
filepath, err := cmd.Flags().GetString("configfile")
if err != nil {
return err
}
rdsClient, err := aws.NewRDSClient(ctx)
if err != nil {
return err
}
redshiftClient, err := aws.NewRedshiftClient(ctx)
if err != nil {
return err
}
cfg, err := config.LoadConfig(ctx, rdsClient, redshiftClient, filepath)
if err != nil {
return err
}
opts, err := proxySSLOptions(cfg.Proxy.SSL)
if err != nil {
return err
}
logger.Info("starting server", zap.String("listen_addr", cfg.Proxy.ListenAddr))
manager, err := proxy.NewManager(proxy.MergeOptions(opts, []proxy.Option{
proxy.WithListenAddress(cfg.Proxy.ListenAddr),
proxy.WithMode(proxy.ServerSide),
proxy.WithCredentialInterceptor(func(creds *proxy.Credentials) error {
hostConfig, ok := cfg.HostMap[creds.Host]
if !ok {
logger.Warn("client attempted to login to unknown host", zap.String("host", creds.Host))
return fmt.Errorf("host not allowed by ACL, or not configured for this proxy")
}
return overrideSSLConfig(creds, hostConfig.SSL)
})})...,
)
if err != nil {
return err
}
config.RefreshTargets(ctx, &cfg, rdsClient, redshiftClient, 1*time.Minute)
err = manager.Start(ctx)
return err
},
}
func init() {
proxyServerCommand.PersistentFlags().String("configfile", "", "Filepath for proxy config file")
rootCmd.AddCommand(proxyServerCommand)
}