-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
570 lines (487 loc) · 19.2 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/jaytaylor/go-hostsfile"
authenticationv1 "k8s.io/api/authentication/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
certutil "k8s.io/client-go/util/cert"
"k8s.io/klog"
"github.com/maelvls/kubectl-incluster/logutil"
)
var (
kubeconfig = flag.String("kubeconfig", "", "Path to the kubeconfig file to use.")
kubecontext = flag.String("context", "", "The name of the kubeconfig context to use.")
root = flag.String("root", os.Getenv("CONTAINER_ROOT"), "The container root. You can also set CONTAINER_ROOT instead. If TELEPRESENCE_ROOT is set, it will default to that.")
deprecated = flag.Bool("embed", false, "Deprecated since this is now the default behavior. Embeds the token and ca.crt data inside the kubeconfig instead of using file paths.")
replacecacert = flag.String("replace-ca-cert", "", "Instead of using the cacert provided in /var/run/secrets or in the kube config, use this one. Useful when using a proxy like mitmproxy.")
replacecacertD = flag.String("replace-cacert", "", "Deprecated, please use --replace-ca-cert instead.")
printClientCert = flag.Bool("print-client-cert", false, "Instead of printing the kube config, print the content of the kube config's client-certificate-data followed by the client-key-data.")
printCACert = flag.Bool("print-ca-cert", false, "Instead of printing a kubeconfig, print the content of the kube config's certificate-authority-data.")
debug = flag.Bool("d", false, "Print debug logs.")
serviceaccount = flag.String("serviceaccount", "", strings.ReplaceAll(
`Instead of using the current pod's /var/run/secrets (when in cluster)
or the local kubeconfig (when out-of-cluster), you can use this flag to
use the token and ca.crt from a given service account, for example
'namespace-1/serviceaccount-1'. Useful when you want to force using a
token (only available using service accounts) over client certificates
provided in the kubeconfig, which is useful whenusing mitmproxy since
the token is passed as a header (HTTP) instead of a client certificate
(TLS).`, "\t", ""))
sa = flag.String("sa", "", "Shorthand for --serviceaccount.")
)
func main() {
flag.Parse()
if *debug {
logutil.EnableDebug = true
}
if *deprecated {
logutil.Infof("--embed is deprecated since it is now turned on by default")
}
if *replacecacertD != "" {
logutil.Infof("--replace-cacert is deprecated, please use --replace-ca-cert instead")
*replacecacert = *replacecacertD
}
// Defaults to TELEPRESENCE_ROOT only if --root is not passed.
if os.Getenv("TELEPRESENCE_ROOT") != "" && *root == "" {
*root = os.Getenv("TELEPRESENCE_ROOT")
}
proxy := os.Getenv("HTTPS_PROXY")
var proxyCACert string
var err error
if proxy != "" {
proxyCACert, err = fetchCACertFromMitmproxy(proxy)
if err != nil {
logutil.Debugf("fetching the CA certificate from mitmproxy: %s", err)
}
}
c, err := RestConfig(*kubeconfig, *kubecontext, "kubectl-incluster")
if err != nil {
logutil.Errorf("loading: %s", err)
os.Exit(1)
}
// The flag --serviceaccount takes precedence over the --sa flag.
if *sa != "" && *serviceaccount == "" {
*serviceaccount = *sa
}
if *serviceaccount != "" {
// We don't use the above 'c' because 'c' is meant to be customized (the
// CA cert is changed, etc.). Here, we want the "unmodified" config so
// that we can connect to the Kubernetes API.
untouched, err := RestConfig(*kubeconfig, *kubecontext, "kubectl-incluster")
if err != nil {
logutil.Errorf("loading: %s", err)
os.Exit(1)
}
// Chicken and egg: the whole purpose of kubectl incluster is to create
// a kubeconfig that will work when used for MITM proxying over the HTTP
// proxy protocol, i.e., when using HTTPS_PROXY and HTTP_PROXY. For
// that, kubectl incluster needs to talk to the Kubernetes API, which is
// impossible since HTTPS_PROXY is enabled but without the correct
// adjustments to the kubeconfig. So we disable HTTPS_PROXY here.
//
// We can't just 'os.Unsetenv("HTTPS_PROXY")' because the default
// http.Transport loads HTTPS_PROXY before this code runs.
untouched.Proxy = func(r *http.Request) (*url.URL, error) {
return nil, nil
}
token, err := getServiceAccount(untouched)
if err != nil {
logutil.Errorf("while processing flag --serviceaccount: %s", err)
os.Exit(1)
}
c.BearerToken = token
c.KeyData = nil
c.KeyFile = ""
c.CertData = nil
c.CertFile = ""
}
if proxy != "" {
// Now, let's check whether the proxy supports streaming. This check is
// performed because mitmproxy doesn't stream reponses by default, which
// blocks Kubernetes' watching mechanism.
// Create a temporary server that listens on a random port.
logutil.Debugf("creating a temporary server to test whether the proxy supports streaming")
srv := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
logutil.Debugf("client connected, temporary server sending 'DONE'")
w.Write([]byte("DONE"))
if fl, ok := w.(http.Flusher); ok {
fl.Flush()
}
// Pretend that the server is streaming data.
time.Sleep(10 * time.Minute)
}))
l, err := net.Listen("tcp", ":0")
if err != nil {
logutil.Errorf("creating a temporary server: %s", err)
os.Exit(1)
}
go func() {
_ = http.Serve(l, srv)
}()
// Create a temporary client that connects to the temporary server.
client := &http.Client{
Transport: &http.Transport{
Proxy: func(r *http.Request) (*url.URL, error) {
return url.Parse(proxy)
},
},
}
// The query parameter 'watch=true' is what I use in the mitmproxy
// script to enable response streaming.
req, err := http.NewRequest("GET", "http://"+l.Addr().String()+"?watch=true", nil)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(req.Context(), 100*time.Millisecond)
defer cancel()
resp, err := client.Do(req.WithContext(ctx))
operr := &net.OpError{}
if errors.As(err, &operr) && operr.Op == "proxyconnect" {
logutil.Errorf("the env var HTTPS_PROXY is set to %q, but the proxy doesn't seem to be running: %s", proxy, operr.Err)
os.Exit(1)
}
if errors.Is(err, context.DeadlineExceeded) {
logutil.Errorf(strings.ReplaceAll(`the proxy does not supports streaming responses.
If you are using mitmproxy, you can enable streaming by using a custom script with the flag '-s':
mitmproxy -s <(curl -L https://raw.githubusercontent.com/maelvls/kubectl-incluster/main/watch-stream.py)`, "\t", ""))
os.Exit(1)
}
if err != nil {
logutil.Errorf("checking whether proxy supports response streaming using a fake streaming server: %s", err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logutil.Errorf("checking whether proxy supports response streaming using a fake streaming server: the fake server returned a non-200 status code: %d", resp.StatusCode)
os.Exit(1)
}
buf := make([]byte, 1024)
for {
d, err := resp.Body.Read(buf)
logutil.Debugf("checking whether proxy supports response streaming: read %d bytes from the temporary server: %s", d, string(buf))
if err == io.EOF {
break
}
if err != nil {
logutil.Errorf("checking whether proxy supports response streaming: while reading the response body: %s", err)
os.Exit(1)
}
if bytes.Contains(buf, []byte("DONE")) {
logutil.Debugf("the proxy supports streaming responses")
break
}
}
l.Close()
resp.Body.Close()
}
// Go skips the HTTPS_PROXY env var if the host is a localhost address
// (e.g., 127.0.0.1 or localhost). To work around that,
if proxy != "" && (strings.Contains(c.Host, "localhost") || strings.Contains(c.Host, "127.0.0.1")) {
// Let's figure out if we have an alias to 127.0.0.1 other than
// "localhost" in /etc/hosts to work around the Go issue.
addrs, err := hostsfile.ReverseLookup("127.0.0.1")
if err != nil {
logutil.Infof(strings.ReplaceAll(
`while trying to figure out whether you will have a problem with
Go ignoring HTTPS_PROXY when the host is "127.0.0.1" or "localhost",
we encountered an error while reading /etc/hosts: %s.`, "\t", ""), err)
os.Exit(1)
}
logutil.Debugf("aliases found for 127.0.0.1: %s", addrs)
alias := ""
for _, addr := range addrs {
if addr != "localhost" {
alias = addr
break
}
}
if alias == "" {
logutil.Infof(strings.ReplaceAll(
`no 127.0.0.1 alias found in /etc/hosts other than "localhost". If
you run a Go program which tries to dial "127.0.0.1" or "localhost", Go
will ignore the HTTPS_PROXY env var.
To fix this issue, run the following command:
sudo tee -a /etc/hosts <<<"127.0.0.1 me"`, "\t", ""))
}
logutil.Debugf("using the alias '%s'", alias)
c.Host = strings.ReplaceAll(c.Host, "localhost", alias)
c.Host = strings.ReplaceAll(c.Host, "127.0.0.1", alias)
}
if proxyCACert != "" {
c.TLSClientConfig.CAData = []byte(proxyCACert)
}
switch {
case *printClientCert:
pem, err := clientCertPEMFromRestConfig(c)
if err != nil {
logutil.Errorf("building the PEM bundle with the client-certificate-data and client-key-data: %s", err)
os.Exit(1)
}
fmt.Printf("%s", pem)
case *printCACert:
pem, err := caCertPEMFromRestConfig(c)
if err != nil {
logutil.Errorf("building the PEM bundle with the ca-certificate-data: %s", err)
os.Exit(1)
}
fmt.Printf("%s", pem)
default:
kubeconfig, err := kubeconfigFromRestConfig(c, *replacecacert, proxyCACert)
if err != nil {
logutil.Errorf("building the kubeconfig: %s", err)
os.Exit(1)
}
err = clientcmd.WriteToFile(*kubeconfig, "/dev/stdout")
if err != nil {
logutil.Errorf("writing: %s", err)
os.Exit(1)
}
}
}
func fetchCACertFromMitmproxy(proxy string) (pem string, _ error) {
proxyURL, _ := url.Parse(proxy)
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
resp, err := client.Get("http://mitm.it/cert/pem")
if err != nil {
return "", fmt.Errorf("while trying to fetch the CA cert at GET mitm.it/cert/pem: %s", err)
}
defer resp.Body.Close()
if resp.Header.Get("Content-Type") != "application/x-x509-ca-cert" {
logutil.Errorf("unexpected content type of GET mitm.it/cert/pem: %s", resp.Header.Get("Content-Type"))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("while reading the body of GET mitm.it/cert/pem: %s", err)
}
return string(body), nil
}
func getServiceAccount(c *rest.Config) (token string, _ error) {
splits := strings.Split(*serviceaccount, "/")
if len(splits) != 2 {
return "", fmt.Errorf("--serviceaccount: expected value of the form 'namespace/serviceaccount', got: %s", *serviceaccount)
}
namespace := splits[0]
name := splits[1]
cl, err := kubernetes.NewForConfig(c)
if err != nil {
return "", fmt.Errorf("while processing flag --serviceaccount: creating Kubernetes client: %s", err)
}
serviceaccount, err := cl.CoreV1().ServiceAccounts(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("getting serviceaccount %s in namespace %s: %v", name, namespace, err)
}
// By default, we try to use the default service account token. Since
// Kubernetes 1.20, the default service account token is not created, so we
// try to generate a token instead.
if len(serviceaccount.Secrets) < 1 {
logutil.Debugf("serviceaccount %s has no default service account secret, now trying to generate a token", serviceaccount.GetName())
token, err := cl.CoreV1().ServiceAccounts(namespace).CreateToken(context.TODO(), name, &authenticationv1.TokenRequest{}, metav1.CreateOptions{})
if err != nil {
return "", fmt.Errorf("failed to generate a token for serviceaccount %s in namespace %s: %v", name, namespace, err)
}
return token.Status.Token, nil
}
var secret *v1.Secret
for _, secretRef := range serviceaccount.Secrets {
secret, err = cl.CoreV1().Secrets(namespace).Get(context.TODO(), secretRef.Name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get the secret %s in namespace %s: %v", secretRef.Name, namespace, err)
}
if secret.Type == v1.SecretTypeServiceAccountToken {
break
}
}
if secret == nil {
return "", fmt.Errorf("serviceaccount %s has no secret type %s", name, v1.SecretTypeServiceAccountToken)
}
tokenBytes, ok := secret.Data["token"]
if !ok {
return "", fmt.Errorf("key 'token' not found in %s", secret.GetName())
}
return string(tokenBytes), nil
}
// The PEM-encoded private key is displayed first.
func clientCertPEMFromRestConfig(restconf *rest.Config) ([]byte, error) {
var clientPEM []byte
if restconf.TLSClientConfig.KeyFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.KeyFile)
if err != nil {
return nil, fmt.Errorf("reading client key file: %w", err)
}
clientPEM = append(clientPEM, bytes...)
} else if len(restconf.TLSClientConfig.KeyData) > 0 {
clientPEM = append(clientPEM, restconf.TLSClientConfig.KeyData...)
} else if restconf.BearerTokenFile != "" {
return nil, fmt.Errorf("cannot produce a PEM client certificate bundle when the kube config uses a token")
}
if len(restconf.TLSClientConfig.CertData) > 0 {
clientPEM = append(clientPEM, restconf.TLSClientConfig.CertData...)
} else if restconf.TLSClientConfig.CertFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.CertFile)
if err != nil {
return nil, fmt.Errorf("reading client certificate file: %w", err)
}
clientPEM = append(clientPEM, bytes...)
}
return clientPEM, nil
}
func caCertPEMFromRestConfig(restconf *rest.Config) ([]byte, error) {
if len(restconf.TLSClientConfig.CAData) > 0 {
return restconf.TLSClientConfig.CAData, nil
} else if restconf.TLSClientConfig.CAFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.CAFile)
if err != nil {
return nil, fmt.Errorf("reading client certificate file: %w", err)
}
return bytes, nil
}
return nil, fmt.Errorf("no ca-certificate-data nor ca-certificate-file")
}
// When embed is true, the ca certificate and token are embedded in the
// kube config as a base64 string. Otherwise, the paths to the token and to
// the ca file are used in the kube config.
// https://github.com/kubernetes/client-go/issues/711
func kubeconfigFromRestConfig(restconf *rest.Config, replaceCACertFile, replaceCAData string) (*clientcmdapi.Config, error) {
apiconf := clientcmdapi.NewConfig()
apiconf.Clusters["kubectl-incluster"] = &clientcmdapi.Cluster{
Server: restconf.Host,
}
apiconf.Clusters["kubectl-incluster"].CertificateAuthorityData = restconf.TLSClientConfig.CAData
if replaceCACertFile != "" {
restconf.TLSClientConfig.CAFile = replaceCACertFile
}
if restconf.TLSClientConfig.CAFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.CAFile)
if err != nil {
return nil, fmt.Errorf("reading CA file: %w", err)
}
apiconf.Clusters["kubectl-incluster"].CertificateAuthorityData = bytes
}
apiconf.AuthInfos["kubectl-incluster"] = &clientcmdapi.AuthInfo{}
apiconf.AuthInfos["kubectl-incluster"].ClientCertificateData = restconf.TLSClientConfig.CertData
if restconf.TLSClientConfig.CertFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.CertFile)
if err != nil {
return nil, fmt.Errorf("reading client certificate file: %w", err)
}
apiconf.AuthInfos["kubectl-incluster"].ClientCertificateData = bytes
}
apiconf.AuthInfos["kubectl-incluster"].ClientKeyData = restconf.TLSClientConfig.KeyData
if restconf.TLSClientConfig.KeyFile != "" {
bytes, err := ioutil.ReadFile(restconf.TLSClientConfig.KeyFile)
if err != nil {
return nil, fmt.Errorf("reading client key file: %w", err)
}
apiconf.AuthInfos["kubectl-incluster"].ClientKeyData = bytes
}
apiconf.AuthInfos["kubectl-incluster"].Token = restconf.BearerToken
if restconf.BearerTokenFile != "" {
bytes, err := ioutil.ReadFile(restconf.BearerTokenFile)
if err != nil {
return nil, fmt.Errorf("reading token file: %w", err)
}
apiconf.AuthInfos["kubectl-incluster"].Token = string(bytes)
}
apiconf.CurrentContext = "kubectl-incluster"
apiconf.Contexts["kubectl-incluster"] = clientcmdapi.NewContext()
apiconf.Contexts["kubectl-incluster"].Cluster = "kubectl-incluster"
apiconf.Contexts["kubectl-incluster"].AuthInfo = "kubectl-incluster"
return apiconf, nil
}
// RestConfig creates a clientset by first trying to find the in-cluster config
// (i.e., in a Kubernetes pod). Otherwise, it loads the kube config from the
// given kubeconfig path. If the kubeconfig variable if left empty, the kube
// config will be loaded from $KUBECONFIG or by default ~/.kube/config.
//
// The context is useful for selecting which entry of the kube config you want
// to use. If context is left empty, the default context of the kube config is
// used.
//
// The userAgent can be for example "controller/v0.1.4/0848c95".
func RestConfig(kubeconfig, kubecontext, userAgent string) (*rest.Config, error) {
var cfg *rest.Config
var err error
if kubeconfig != "" {
logutil.Debugf("using you local kube config since --kubeconfig was passed")
cfg, err = outClusterConfig(kubeconfig, kubecontext)
if err != nil {
return nil, fmt.Errorf("error loading kube config: %w", err)
}
}
cfg, err = InClusterConfig()
if err != nil {
logutil.Debugf("in-cluster config was not found, now trying with your local kube config")
cfg, err = outClusterConfig("", kubecontext)
if err != nil {
return nil, fmt.Errorf("error loading kube config: %w", err)
}
} else {
logutil.Debugf("in-cluster config found")
}
cfg.UserAgent = userAgent
return cfg, nil
}
func outClusterConfig(kubeconfig, kubecontext string) (*rest.Config, error) {
loadRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadRules.ExplicitPath = kubeconfig
apicfg, err := loadRules.Load()
if err != nil {
return nil, fmt.Errorf("error loading kubeconfig: %v", err)
}
if kubecontext == "" && apicfg.CurrentContext == "" {
return nil, fmt.Errorf("no context was provided and no current context was found in the kubeconfig")
}
return clientcmd.NewDefaultClientConfig(*apicfg, &clientcmd.ConfigOverrides{
CurrentContext: kubecontext,
}).ClientConfig()
}
// InClusterConfig is the vendored version of rest.InClusterConfig:
// https://github.com/kubernetes/client-go/blob/fb61a7c/rest/config.go
func InClusterConfig() (*rest.Config, error) {
var (
tokenFile = *root + "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = *root + "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
)
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, rest.ErrNotInCluster
}
token, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, err
}
tlsClientConfig := rest.TLSClientConfig{}
if _, err := certutil.NewPool(rootCAFile); err != nil {
klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
} else {
tlsClientConfig.CAFile = rootCAFile
}
return &rest.Config{
// TODO: switch to using cluster DNS.
Host: "https://" + net.JoinHostPort(host, port),
TLSClientConfig: tlsClientConfig,
BearerToken: string(token),
BearerTokenFile: tokenFile,
}, nil
}