/
main.go
494 lines (423 loc) · 15.6 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"errors"
"fmt"
"log"
"path"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"context"
"os"
"strconv"
"strings"
"github.com/metal-stack/gardener-vpn-gateway/pkg/proxy"
"github.com/metal-stack/v"
"github.com/go-playground/validator/v10"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
cfgFileType = "yaml"
moduleName = "gardener-vpn-gateway"
proxyListenAddress = "0.0.0.0"
proxyListenPort = "9876"
)
var (
cfgFile string
logger *zap.SugaredLogger
logLevel zapcore.Level
stop context.Context
targetService *corev1.Service
secretCronID cron.EntryID
serviceCronID cron.EntryID
clusterProxy *proxy.Proxy
)
// CronLogger is used for logging within the cron function.
type CronLogger struct {
l *zap.SugaredLogger
}
// Info logs info messages from the cron function.
func (c *CronLogger) Info(msg string, keysAndValues ...interface{}) {
c.l.Infow(msg, keysAndValues...)
}
func (c *CronLogger) Error(err error, msg string, keysAndValues ...interface{}) {
c.l.Errorw(msg, keysAndValues...)
}
// Opts is required in order to have proper validation for args from cobra and viper.
// this is because MarkFlagRequired from cobra does not work well with viper, see:
// https://github.com/spf13/viper/issues/397
type Opts struct {
ShootKubeconfig string `validate:"required"`
SeedKubeconfig string
SeedNamespace string `validate:"required"`
Namespace string `validate:"required"`
ServiceName string `validate:"required"`
CheckSchedule string
BackoffTimer time.Duration
LogLevel string
ProxyHost string
ProxyPort string
ProxyCASecret string
ProxyClientSecret string
TLSBaseDir string
ProxyCaFilename string
ProxyClientCrtFilename string
ProxyClientKeyFilename string
}
var cmd = &cobra.Command{
Use: moduleName,
Short: "A program to forward a connection transparently to a service in the cluster. It looks for a matching service, then listens for connections and tunnels the connection through the VPN to the target service.",
Version: v.V.String(),
Run: func(cmd *cobra.Command, args []string) {
initConfig()
opts, err := initOpts()
if err != nil {
log.Fatalf("unable to init options, error: %v", err)
}
initLogging(opts)
initSignalHandlers()
err = run(opts)
if err != nil {
log.Printf("run() function run returned with error: %v", err)
}
},
}
func init() {
homedir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
cmd.Flags().StringVarP(&cfgFile, "config", "c", "", "alternative path to config file")
cmd.Flags().String("shoot-kubeconfig", homedir+"/.kube/config", "path to the shoot kubeconfig")
cmd.Flags().String("seed-kubeconfig", "", "path to the seed kubeconfig. Uses default path if empty.")
cmd.Flags().String("seed-namespace", "", "the namespace in the seed cluster in which the proxy certificates reside")
cmd.Flags().String("namespace", "", "the namespace of the target service")
cmd.Flags().String("service-name", "", "the service name of the target service")
cmd.Flags().String("check-schedule", "*/1 * * * *", "cron schedule when to check for service changes")
cmd.Flags().Duration("backoff-timer", time.Duration(10*time.Second), "Backoff time for restarting the forwarder process when it has been killed by external influences")
cmd.Flags().String("log-level", "info", "sets the application log level")
cmd.Flags().String("proxy-host", "vpn-seed-server", "Name of the mTLS proxy to connect to the shoot through the VPN. Expected method is http-connect.")
cmd.Flags().String("proxy-port", "9443", "Port of the mTLS proxy specified with proxy-host.")
cmd.Flags().String("proxy-ca-secret", "ca-vpn-bundle", "Name of the secret that contains the CA certificate use for the proxy server certificate.")
cmd.Flags().String("proxy-client-secret", "kube-apiserver-http-proxy", "Name of the secret that contains the client certificate needed for connecting to the proxy.")
cmd.Flags().String("tls-base-dir", "/proxy/tls", "the path to the CA file for checking the mTLS proxy server certificate")
cmd.Flags().String("proxy-ca-file", "bundle.crt", "the path to the CA file for checking the mTLS proxy server certificate")
cmd.Flags().String("proxy-client-crt-file", "tls.crt", "the path to the proxy client certificate used to authenticate to the mTLS proxy server")
cmd.Flags().String("proxy-client-key-file", "tls.key", "the path to the private key file belonging to the proy client certificate")
err = viper.BindPFlags(cmd.Flags())
if err != nil {
log.Fatalf("unable to construct root command, error: %v", err)
}
}
func initOpts() (*Opts, error) {
opts := &Opts{
ShootKubeconfig: viper.GetString("shoot-kubeconfig"),
SeedKubeconfig: viper.GetString("seed-kubeconfig"),
SeedNamespace: viper.GetString("seed-namespace"),
Namespace: viper.GetString("namespace"),
ServiceName: viper.GetString("service-name"),
CheckSchedule: viper.GetString("check-schedule"),
BackoffTimer: viper.GetDuration("backoff-timer"),
LogLevel: viper.GetString("log-level"),
ProxyHost: viper.GetString("proxy-host"),
ProxyPort: viper.GetString("proxy-port"),
ProxyCASecret: viper.GetString("proxy-ca-secret"),
ProxyClientSecret: viper.GetString("proxy-client-secret"),
ProxyCaFilename: viper.GetString("proxy-ca-file"),
ProxyClientCrtFilename: viper.GetString("proxy-client-crt-file"),
ProxyClientKeyFilename: viper.GetString("proxy-client-key-file"),
}
validate := validator.New()
err := validate.Struct(opts)
if err != nil {
return nil, err
}
return opts, nil
}
func main() {
zap, _ := zap.NewProduction()
defer func() {
_ = zap.Sync()
}()
logger = zap.Sugar()
if err := cmd.Execute(); err != nil {
logger.Error("Failed executing root command", "Error", err)
}
}
func initConfig() {
viper.SetEnvPrefix("gateway")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
viper.SetConfigType(cfgFileType)
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
logger.Errorw("Config file path set explicitly, but unreadable", "error", err)
os.Exit(1)
}
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/" + moduleName)
viper.AddConfigPath("$HOME/." + moduleName)
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
usedCfg := viper.ConfigFileUsed()
if usedCfg != "" {
logger.Errorw("Config file unreadable", "config-file", usedCfg, "error", err)
os.Exit(1)
}
}
}
usedCfg := viper.ConfigFileUsed()
if usedCfg != "" {
logger.Infow("Read config file", "config-file", usedCfg)
}
}
func initLogging(opts *Opts) {
err := logLevel.UnmarshalText([]byte(opts.LogLevel))
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
cfg := zap.NewProductionConfig()
cfg.Level = zap.NewAtomicLevelAt(logLevel)
log.Printf("Log level: %s", cfg.Level)
l, err := cfg.Build()
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
logger = l.Sugar()
}
func initSignalHandlers() {
stop = signals.SetupSignalHandler()
}
func run(opts *Opts) error {
logger.Debugw("Options", "opts", opts)
// Prepare K8s
shootClient, err := loadClient(opts.ShootKubeconfig)
if err != nil {
logger.Errorw("Unable to connect to shoot k8s", "Error", err)
return err
}
var seedClient *k8s.Clientset
if opts.SeedKubeconfig != "" {
seedClient, err = loadClient(opts.SeedKubeconfig)
} else {
seedConfig := ctrl.GetConfigOrDie()
seedClient, err = k8s.NewForConfig(seedConfig)
}
if err != nil {
logger.Errorw("Unable to connect to seed k8s", "Kubeconfig", opts.SeedKubeconfig, "Error", err)
return err
}
// Set up (and run) service checker cron job
cronjob := cron.New(cron.WithChain(
cron.SkipIfStillRunning(&CronLogger{l: logger.Named("cron")}),
))
secretCronID, err = cronjob.AddFunc(opts.CheckSchedule, func() {
err := readSecrets(opts, seedClient)
if err != nil {
logger.Errorw("error during secret check", "error", err)
}
logger.Debugw("scheduling next secret check", "at", cronjob.Entry(secretCronID).Next)
})
if err != nil {
return fmt.Errorf("could not initialize cron schedule %w", err)
}
serviceCronID, err = cronjob.AddFunc(opts.CheckSchedule, func() {
err := checkService(opts, shootClient)
if err != nil {
logger.Errorw("error during service check", "error", err)
}
logger.Debugw("scheduling next service check", "at", cronjob.Entry(serviceCronID).Next)
})
if err != nil {
return fmt.Errorf("could not initialize cron schedule %w", err)
}
logger.Infow("start initial checks", "version", v.V.String())
err = readSecrets(opts, seedClient)
if err != nil {
logger.Errorw("error during initial secret check", "error", err)
return err
}
err = checkService(opts, shootClient)
if err != nil {
logger.Errorw("error during initial service check", "error", err)
return err
}
cronjob.Start()
logger.Infow("cronjob interval", "check-schedule", opts.CheckSchedule)
logger.Debugw("Cronjob", "entries:", cronjob.Entries())
<-stop.Done()
logger.Info("received stop signal, shutting down...")
cronjob.Stop()
return nil
}
func loadClient(kubeconfigPath string) (*k8s.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, err
}
return k8s.NewForConfig(config)
}
// I think this can be implemented much easier with a Watch on the service
/*
example from client-go
func main() {
config, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
glog.Errorln(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Errorln(err)
}
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(clientset, time.Second*30)
svcInformer := kubeInformerFactory.Core().V1().Services().Informer()
svcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("service added: %s \n", obj)
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("service deleted: %s \n", obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
fmt.Printf("service changed: %s \n", newObj)
},
},)
stop := make(chan struct{})
defer close(stop)
kubeInformerFactory.Start(stop)
for {
time.Sleep(time.Second)
}
}
*/
func checkService(opts *Opts, client *k8s.Clientset) error {
logger.Debugw("Checking service")
// logger.Debugw("Current service", "targetService", targetService)
kubectx, cancel := context.WithTimeout(context.Background(), time.Duration(10*time.Second))
defer cancel()
service, err := client.CoreV1().Services(opts.Namespace).Get(kubectx, opts.ServiceName, metav1.GetOptions{})
if err != nil { // That means no matching service found
if targetService != nil { // This means a service was previously seen, and the proxy should already be running.
logger.Infow("Service went away, stopping proxy")
if clusterProxy != nil { // This means there should be a running proxy, we need to stop it too.
clusterProxy.DestroyProxy()
clusterProxy = nil
}
targetService = nil
}
return err
}
// logger.Debugw("Service gotten", "service", service)
serviceIP := service.Spec.ClusterIP
if len(service.Spec.Ports) != 1 {
logger.Errorw("Service must have exactly one port", "Ports", service.Spec.Ports)
return errors.New("service must have exactly one port")
}
servicePort := strconv.Itoa(int(service.Spec.Ports[0].Port))
if targetService != nil { // This means a service was previously seen, and the proxy should already be running.
if targetService.Spec.ClusterIP == service.Spec.ClusterIP && targetService.Spec.Ports[0].Port == service.Spec.Ports[0].Port {
logger.Debugw("Service stayed the same, nothing to do.")
return nil
}
if clusterProxy != nil { // This means there should be a running proxy, we need to stop it too.
clusterProxy.DestroyProxy()
clusterProxy = nil
}
}
logger.Infow("Target identified", "IP", serviceIP, "Port", servicePort)
if opts.ProxyHost != "" { // This means we need to start a mTLS proxy
logger.Infow("Starting proxy", "host", opts.ProxyHost, "port", opts.ProxyPort)
clusterProxy, err = proxy.NewProxyMTLS(logger, opts.ProxyHost, opts.ProxyPort, opts.ProxyClientCrtFilename, opts.ProxyClientKeyFilename, opts.ProxyCaFilename, serviceIP, servicePort, proxyListenAddress, proxyListenPort)
if err != nil {
logger.Errorw("Could not start mTLS proxy", "error", err)
return err
}
}
targetService = service
return nil
}
func readSecrets(opts *Opts, client *k8s.Clientset) error {
logger.Debugw("Reading secrets")
keys := []string{opts.ProxyCaFilename, opts.ProxyClientCrtFilename, opts.ProxyClientKeyFilename}
for _, secretName := range []string{opts.ProxyCASecret, opts.ProxyClientSecret} {
kubectx, kubecancel := context.WithTimeout(context.Background(), time.Duration(10*time.Second))
defer kubecancel()
secret, err := getLatestSecret(kubectx, client, opts.SeedNamespace, secretName)
if err != nil {
return fmt.Errorf("did not find secret %q in namespace %s: %w", secretName, opts.SeedNamespace, err)
}
logger.Debugw("Got secret", secretName, secret.Name)
// Now we attempt to write the certificates to file
for key, value := range secret.Data {
for _, k := range keys {
if key == k {
f := path.Join(opts.TLSBaseDir, key)
logger.Debugw("Writing certificate to file", key, f)
err := os.WriteFile(f, value, 0600)
if err != nil {
return fmt.Errorf("could not write secret to certificate base folder:%w", err)
}
}
}
}
}
return nil
}
func getLatestSecret(ctx context.Context, c *k8s.Clientset, namespace string, name string) (*corev1.Secret, error) {
selector := labels.SelectorFromSet(map[string]string{
"managed-by": "secrets-manager",
"manager-identity": "gardenlet",
"name": name,
})
secretList, err := c.CoreV1().Secrets(namespace).List(ctx, metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
return nil, err
}
return getLatestIssuedSecret(secretList.Items)
}
// getLatestIssuedSecret returns the secret with the "issued-at-time" label that represents the latest point in time
func getLatestIssuedSecret(secrets []corev1.Secret) (*corev1.Secret, error) {
const (
labelKeyIssuedAtTime = "issued-at-time"
)
if len(secrets) == 0 {
return nil, fmt.Errorf("no secret found")
}
var newestSecret *corev1.Secret
var currentIssuedAtTime time.Time
for i := 0; i < len(secrets); i++ {
// if some of the secrets have no "issued-at-time" label
// we have a problem since this is the source of truth
issuedAt, ok := secrets[i].Labels[labelKeyIssuedAtTime]
if !ok {
// there are some old secrets from ancient gardener versions which have to be skipped... (e.g. ssh-keypair.old)
continue
}
issuedAtUnix, err := strconv.ParseInt(issuedAt, 10, 64)
if err != nil {
return nil, err
}
issuedAtTime := time.Unix(issuedAtUnix, 0).UTC()
if newestSecret == nil || issuedAtTime.After(currentIssuedAtTime) {
newestSecret = &secrets[i]
currentIssuedAtTime = issuedAtTime
}
}
if newestSecret == nil {
return nil, fmt.Errorf("no secret found")
}
return newestSecret, nil
}