-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (140 loc) · 4.55 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"regexp"
"github.com/utilitywarehouse/vault-kube-cloud-credentials/operator"
"github.com/utilitywarehouse/vault-kube-cloud-credentials/sidecar"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var (
operatorCommand = flag.NewFlagSet("operator", flag.ExitOnError)
flagOperatorConfigFile = operatorCommand.String("config-file", "", "Path to a configuration file")
flagOperatorProvider = operatorCommand.String("provider", "aws", "Cloud provider (one of 'aws' or 'gcp')")
sidecarCommand = flag.NewFlagSet("sidecar", flag.ExitOnError)
flagSidecarKubeTokenPath = sidecarCommand.String("kube-token-path", "/var/run/secrets/kubernetes.io/serviceaccount/token", "Path to the kubernetes serviceaccount token")
flagSidecarListenAddr = sidecarCommand.String("listen-address", "127.0.0.1:8098", "Listen address")
flagSidecarOpsAddr = sidecarCommand.String("operational-address", ":8099", "Listen address for operational status endpoints")
flagSidecarVaultRole = sidecarCommand.String("vault-role", "", "Must be in the format: `<prefix>_<provider>_<namespace>_<service-account>`")
flagSidecarVaultStaticAccount = sidecarCommand.String("vault-static-account", "", "Must be in the format: `<prefix>_<provider>_<namespace>_<service-account>`")
flagSidecarSecretType = sidecarCommand.String("secret-type", "access_token", "Secret type (one of 'service_account_key' or 'access_token')")
log = ctrl.Log.WithName("main")
// example: exp-1_aws_sys-prom_thanos-compact
//
// 0: exp-1_aws_sys-prom_thanos-compact
// 1: prefix (Vault instance) example values: "exp-1", "dev", "prod"
// 2: provider (AWS|GCP)
// 3: kubernetes_namespace
// 4: kubernetes_service_account
vaultRoleRegex = regexp.MustCompile(`([-\w]+)_([-\w]+)_([-\w]+)_([-\w]+)`)
)
func usage() {
fmt.Printf(
`Usage:
%s [command]
Commands:
operator Run the operator
sidecar Sidecar for provider credentials
`, os.Args[0])
}
func main() {
flag.Usage = usage
if len(os.Args) < 2 {
usage()
return
}
logOpts := zap.Options{}
switch os.Args[1] {
case "operator":
logOpts.BindFlags(operatorCommand)
operatorCommand.Parse(os.Args[2:])
case "sidecar":
logOpts.BindFlags(sidecarCommand)
sidecarCommand.Parse(os.Args[2:])
default:
usage()
return
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&logOpts)))
if operatorCommand.Parsed() {
if len(operatorCommand.Args()) > 0 {
operatorCommand.PrintDefaults()
os.Exit(1)
}
o, err := operator.New(*flagOperatorConfigFile, *flagOperatorProvider)
if err != nil {
log.Error(err, "error creating operator")
os.Exit(1)
}
if err := o.Start(); err != nil {
log.Error(err, "error running operator")
os.Exit(1)
}
return
}
if sidecarCommand.Parsed() {
if len(sidecarCommand.Args()) > 0 {
sidecarCommand.PrintDefaults()
os.Exit(1)
}
var sidecarProvider string
if *flagSidecarVaultStaticAccount != "" && *flagSidecarVaultRole != "" {
log.Error(nil, "Only one of 'vault-role' or 'vault-static-account' can be specified.")
os.Exit(1)
}
if *flagSidecarVaultStaticAccount != "" {
sidecarProvider = vaultRoleRegex.FindStringSubmatch(*flagSidecarVaultStaticAccount)[2]
}
if *flagSidecarVaultRole != "" {
sidecarProvider = vaultRoleRegex.FindStringSubmatch(*flagSidecarVaultRole)[2]
}
var pc sidecar.ProviderConfig
var kubeAuthRole string
switch sidecarProvider {
case "aws":
pc = &sidecar.AWSProviderConfig{
Path: "aws",
RoleArn: "",
Role: *flagSidecarVaultRole,
}
kubeAuthRole = *flagSidecarVaultRole
case "gcp":
keyFilePath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if keyFilePath == "" {
keyFilePath = "/gcp/sa.json"
}
pc = &sidecar.GCPProviderConfig{
Path: "gcp",
StaticAccount: *flagSidecarVaultStaticAccount,
SecretType: *flagSidecarSecretType,
KeyFileDestinationPath: keyFilePath,
}
kubeAuthRole = *flagSidecarVaultStaticAccount
default:
usage()
return
}
sidecarConfig := &sidecar.Config{
KubeAuthPath: "kubernetes",
KubeAuthRole: kubeAuthRole,
ListenAddress: *flagSidecarListenAddr,
OpsAddress: *flagSidecarOpsAddr,
ProviderConfig: pc,
TokenPath: *flagSidecarKubeTokenPath,
}
s, err := sidecar.New(sidecarConfig)
if err != nil {
log.Error(err, "error creating sidecar")
os.Exit(1)
}
if err := s.Run(); err != nil {
log.Error(err, "error running sidecar")
os.Exit(1)
}
return
}
usage()
return
}