This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
/
root.go
323 lines (235 loc) · 10.5 KB
/
root.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
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/Boostport/kubernetes-vault/cmd/controller/client"
"github.com/Boostport/kubernetes-vault/cmd/controller/cluster"
"github.com/Boostport/kubernetes-vault/cmd/controller/metrics"
"github.com/Boostport/kubernetes-vault/common"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const defaultWrappingTTL = "60s"
func init() {
RootCmd.Flags().String("config", "", "Path to the configuration file. By default, this is kubernetes-vault.yml in the current working directory.")
RootCmd.Flags().String("log-level", "debug", `Log verbosity. Defaults to "debug" and written to stdout and stderr. Supported values: "debug", "error"`)
}
type config struct {
RaftDir string `mapstructure:"raftDir"`
Vault struct {
Addr string `mapstructure:"addr"`
Token string `mapstructure:"token"`
SkipTokenRoleNameValidation bool `mapstructure:"skipTokenRoleNameValidation"`
TLS struct {
VaultCABackends []string `mapstructure:"vaultCABackends"`
CACert string `mapstructure:"caCert"`
} `mapstructure:"tls"`
WrappingTTL string `mapstructure:"wrappingTTL"`
} `mapstructure:"vault"`
Kubernetes struct {
WatchNamespace string `mapstructure:"watchNamespace"`
ServiceNamespace string `mapstructure:"serviceNamespace"`
Service string `mapstructure:"service"`
} `mapstructure:"kubernetes"`
Prometheus struct {
TLS struct {
VaultCertBackend string `mapstructure:"vaultCertBackend"`
VaultCertRole string `mapstructure:"vaultCertRole"`
VaultCABackends []string `mapstructure:"VaultCABackends"`
CertFile string `mapstructure:"certFile"`
CertKey string `mapstructure:"certKey"`
CACert string `mapstructure:"caCert"`
} `mapstructure:"tls"`
} `mapstructure:"prometheus"`
}
func (c *config) Validate() error {
var errs error
hasTLSConfig := c.Prometheus.TLS.VaultCertBackend != "" || c.Prometheus.TLS.VaultCertRole != "" || c.Prometheus.TLS.CertFile != "" || c.Prometheus.TLS.CertKey != ""
hasBothVaultCAAndExternalCAConfig := (c.Prometheus.TLS.VaultCertBackend != "" || c.Prometheus.TLS.VaultCertRole != "") == (c.Prometheus.TLS.CertFile != "" || c.Prometheus.TLS.CertKey != "")
if hasTLSConfig && hasBothVaultCAAndExternalCAConfig {
errs = multierror.Append(errs, errors.New(`Contraditory TLS configuration. You must use either Vault (tls.vaultCertBackend and tls.vaultCertRole) or your own certificate files (tls.certFilePath and tls.certKeyPath) to manage the TLS certificate, not both.`))
} else {
if (c.Prometheus.TLS.VaultCertBackend != "") != (c.Prometheus.TLS.VaultCertRole != "") {
errs = multierror.Append(errs, errors.New("Both prometheus.tls.vaultCertBackend and prometheus.tls.vaultCertRole must be provided if you want to serve the metrics endpoint over https using Vault as your certificate authority."))
}
if (c.Prometheus.TLS.CertFile != "") != (c.Prometheus.TLS.CertKey != "") {
errs = multierror.Append(errs, errors.New("Both prometheus.tls.certFile and prometheus.tls.certKey must be provided if you want to serve the metrics endpoint over https using your own certificate."))
}
}
if c.Vault.Addr == "" {
errs = multierror.Append(errs, errors.New("vault.addr is required"))
}
if c.Vault.Token == "" {
errs = multierror.Append(errs, errors.New("vault.token is required"))
}
if len(c.Vault.TLS.VaultCABackends) > 0 && c.Vault.TLS.CACert != "" {
errs = multierror.Append(errs, errors.New(`Contraditory Vault TLS configuration. You must use either Vault CA backends (vault.tls.vaultCABackends) or your own Root CA file (vault.tls.caCertFilePath) to verify the Vault server TLS certificate, not both.`))
}
if c.Kubernetes.WatchNamespace == "" {
errs = multierror.Append(errs, errors.New("kubernetes.watchNamespace is required"))
}
if c.Kubernetes.ServiceNamespace == "" {
errs = multierror.Append(errs, errors.New("kubernetes.serviceNamespace is required"))
}
if c.Kubernetes.Service == "" {
errs = multierror.Append(errs, errors.New("kubernetes.service is required"))
}
if len(c.Prometheus.TLS.VaultCABackends) > 0 && c.Prometheus.TLS.CACert != "" {
errs = multierror.Append(errs, errors.New(`Contraditory Prometheus TLS configuration. You use either Vault CA backends (prometheus.tls.vaultCABackends) or your own Root CA file (prometheus.tls.caCertFilePath) to verify the Prometheus scraper TLS certificate, not both.`))
}
hasPrometheusRootCAs := len(c.Prometheus.TLS.VaultCABackends) > 0 || c.Prometheus.TLS.CACert != ""
hasVaultCAOrExternalCAConfig := (c.Prometheus.TLS.VaultCertBackend != "" && c.Prometheus.TLS.VaultCertRole != "") || (c.Prometheus.TLS.CertFile != "" && c.Prometheus.TLS.CertKey != "")
if hasPrometheusRootCAs && !hasVaultCAOrExternalCAConfig {
errs = multierror.Append(errs, errors.New("You need to set a Vault certificate backend ( prometheus.tls.vaultCertBackend and prometheus.tls.vaultCertRole) or use an external certificate (prometheus.tls.certFile and prometheus.tls.certKey) if you want to secure the prometheus endpoint with client-side certificate authentication."))
}
return errs
}
func newConfigWithDefaults() *config {
cfg := &config{
RaftDir: "/var/lib/kubernetes-vault/",
}
cfg.Vault.WrappingTTL = defaultWrappingTTL
return cfg
}
func certificateFromFile(certFile string, keyFile string) (<-chan tls.Certificate, error) {
ch := make(chan tls.Certificate, 1)
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return ch, errors.Wrap(err, "could not load certificate")
}
ch <- cert
return ch, nil
}
var RootCmd = &cobra.Command{
Use: "kubernetes-vault",
Short: "Kubernetes-vaults is a Kubernetes controller that pushes Vault tokens into pods",
Long: `Kubernetes-vault is a Kubernetes controller that watches new pods for certain annotations
and pushes a wrapped secret into an init container in the pod. The init container exchanges
the secret and the pod's configured AppRole with Vault for a token and writes the token
to a shared volume for the pod. More information at https://github.com/Boostport/kubernetes-vault`,
Run: func(cmd *cobra.Command, args []string) {
logger := logrus.New()
logger.Level = logrus.DebugLevel
logLevel := strings.ToLower(cmd.Flags().Lookup("log-level").Value.String())
if logLevel != "debug" && logLevel != "error" {
logger.Fatalf(`logLevel should be either "debug" or "error", got "%s"`, logLevel)
}
if logLevel == "error" {
logger.Level = logrus.ErrorLevel
}
if c := cmd.Flags().Lookup("config").Value.String(); c != "" {
viper.SetConfigFile(c)
} else {
viper.AddConfigPath(".")
viper.SetConfigName("kubernetes-vault")
}
err := viper.ReadInConfig()
if err != nil {
logger.Fatalf("Error reading config file: %s", err)
}
conf := newConfigWithDefaults()
err = viper.Unmarshal(conf)
if err != nil {
logger.Fatalf("Error processing config file: %s", err)
}
conf = expandEnvironmentVariables(conf)
err = conf.Validate()
if err != nil {
logger.Fatalf("Invalid config file: %s", err)
}
err = os.MkdirAll(conf.RaftDir, 0666)
if err != nil {
logger.Fatalf("Error while trying to create raft directory (%s): %s", conf.RaftDir, err)
}
bindAddr, err := common.ExternalIP()
if err != nil {
logger.Fatalf("Could not determine external ip address: %s", err)
}
kube, err := client.NewKube(conf.Kubernetes.WatchNamespace, logger)
if err != nil {
logger.Fatalf("Could not create the kubernetes client: %s", err)
}
// Wait between 3 and 10 seconds before discovering other nodes
time.Sleep(time.Duration(rand.Intn(7)+3) * time.Second)
nodes, err := kube.Discover(conf.Kubernetes.ServiceNamespace, conf.Kubernetes.Service)
if err != nil {
logger.Fatalf("Error while discovering nodes: %s", err)
}
logger.Debugf("Discovered %d nodes: %s", len(nodes), nodes)
var rootCAResolver client.RootCAResolver
if len(conf.Vault.TLS.VaultCABackends) > 0 {
rootCAResolver = &client.VaultRootCAsResolver{
Backends: conf.Vault.TLS.VaultCABackends,
VaultAddr: conf.Vault.Addr,
}
} else if conf.Vault.TLS.CACert != "" {
rootCAResolver = &client.ExternalRootCAsResolver{
CAFile: conf.Vault.TLS.CACert,
}
}
vault, err := client.NewVault(conf.Vault.Addr, conf.Vault.Token, conf.Vault.SkipTokenRoleNameValidation, conf.Kubernetes.Service, conf.Vault.WrappingTTL, rootCAResolver, logger)
if err != nil {
logger.Fatalf("Could not create the vault client: %s", err)
}
var certCh <-chan tls.Certificate
if conf.Prometheus.TLS.VaultCertBackend != "" && conf.Prometheus.TLS.VaultCertRole != "" {
certCh, err = vault.GetAndRenewCertificate(bindAddr, conf.Prometheus.TLS.VaultCertBackend, conf.Prometheus.TLS.VaultCertRole)
if err != nil {
logger.Fatalf("Could not get Vault certificate for metrics server: %s", err)
}
}
if conf.Prometheus.TLS.CertFile != "" && conf.Prometheus.TLS.CertKey != "" {
certCh, err = certificateFromFile(conf.Prometheus.TLS.CertFile, conf.Prometheus.TLS.CertKey)
if err != nil {
logger.Fatalf("Could not load certificate for metrics server: %s", err)
}
}
var roots *x509.CertPool
if len(conf.Prometheus.TLS.VaultCABackends) > 0 {
roots, err = vault.RootCertificates(conf.Prometheus.TLS.VaultCABackends)
if err != nil {
logger.Fatalf("Could not get root certificates from Vault: %s", err)
}
}
if conf.Prometheus.TLS.CACert != "" {
roots = x509.NewCertPool()
p, err := ioutil.ReadFile(conf.Prometheus.TLS.CACert)
if err != nil {
logger.Fatalf("Could not read CA certificates from the file (%s): %s", conf.Prometheus.TLS.CACert, err)
}
roots.AppendCertsFromPEM(p)
}
metrics.StartServer(certCh, roots)
gossip, err := cluster.NewGossip(bindAddr.String(), nodes, 0, logger.WriterLevel(logrus.DebugLevel))
if err != nil {
logger.Fatalf("Could not create gossip: %s", err)
}
storeConfig := cluster.DefaultStoreConfig()
storeConfig.Logger = logger
store := cluster.NewStore(gossip, kube, vault, storeConfig)
err = store.StartRaft(conf.RaftDir, bindAddr.String(), logger.WriterLevel(logrus.DebugLevel))
if err != nil {
logger.Fatalf("Could not start raft: %s", err)
}
sigs := make(chan os.Signal, 1)
done := make(chan struct{}, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
store.Shutdown()
vault.Shutdown()
done <- struct{}{}
}()
<-done
},
}