forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
662 lines (575 loc) · 26.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"runtime"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/openshift/console/pkg/auth"
"github.com/openshift/console/pkg/bridge"
"github.com/openshift/console/pkg/knative"
"github.com/openshift/console/pkg/proxy"
"github.com/openshift/console/pkg/server"
"github.com/openshift/console/pkg/serverconfig"
oscrypto "github.com/openshift/library-go/pkg/crypto"
"k8s.io/klog"
)
const (
k8sInClusterCA = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
k8sInClusterBearerToken = "/var/run/secrets/kubernetes.io/serviceaccount/token"
// Well-known location of the tenant aware Thanos service for OpenShift exposing the query and query_range endpoints. This is only accessible in-cluster.
// Thanos proxies requests to both cluster monitoring and user workload monitoring prometheus instances.
openshiftThanosTenancyHost = "thanos-querier.openshift-monitoring.svc:9092"
// Well-known location of the tenant aware Thanos service for OpenShift exposing the rules endpoint. This is only accessible in-cluster.
// Thanos proxies requests to the cluster monitoring and user workload monitoring prometheus instances as well as Thanos ruler instances.
openshiftThanosTenancyForRulesHost = "thanos-querier.openshift-monitoring.svc:9093"
// Well-known location of the Thanos service for OpenShift. This is only accessible in-cluster.
// This is used for non-tenant global query requests
// proxying to both cluster monitoring and user workload monitoring prometheus instances.
openshiftThanosHost = "thanos-querier.openshift-monitoring.svc:9091"
// Well-known location of Alert Manager service for OpenShift. This is only accessible in-cluster.
openshiftAlertManagerHost = "alertmanager-main.openshift-monitoring.svc:9094"
// Well-known location of the tenant aware Alert Manager service for OpenShift. This is only accessible in-cluster.
openshiftAlertManagerTenancyHost = "alertmanager-main.openshift-monitoring.svc:9092"
// Well-known location of metering service for OpenShift. This is only accessible in-cluster.
openshiftMeteringHost = "reporting-operator.openshift-metering.svc:8080"
// Well-known location of the GitOps service. This is only accessible in-cluster
openshiftGitOpsHost = "cluster.openshift-pipelines-app-delivery.svc:8080"
)
func main() {
fs := flag.NewFlagSet("bridge", flag.ExitOnError)
klog.InitFlags(fs)
defer klog.Flush()
// Define commandline / env / config options
fs.String("config", "", "The YAML config file.")
fListen := fs.String("listen", "http://0.0.0.0:9000", "")
fBaseAddress := fs.String("base-address", "", "Format: <http | https>://domainOrIPAddress[:port]. Example: https://openshift.example.com.")
fBasePath := fs.String("base-path", "/", "")
// See https://github.com/openshift/service-serving-cert-signer
fServiceCAFile := fs.String("service-ca-file", "", "CA bundle for OpenShift services signed with the service signing certificates.")
fUserAuth := fs.String("user-auth", "disabled", "disabled | oidc | openshift")
fUserAuthOIDCIssuerURL := fs.String("user-auth-oidc-issuer-url", "", "The OIDC/OAuth2 issuer URL.")
fUserAuthOIDCCAFile := fs.String("user-auth-oidc-ca-file", "", "PEM file for the OIDC/OAuth2 issuer.")
fUserAuthOIDCClientID := fs.String("user-auth-oidc-client-id", "", "The OIDC OAuth2 Client ID.")
fUserAuthOIDCClientSecret := fs.String("user-auth-oidc-client-secret", "", "The OIDC OAuth2 Client Secret.")
fUserAuthOIDCClientSecretFile := fs.String("user-auth-oidc-client-secret-file", "", "File containing the OIDC OAuth2 Client Secret.")
fUserAuthLogoutRedirect := fs.String("user-auth-logout-redirect", "", "Optional redirect URL on logout needed for some single sign-on identity providers.")
fInactivityTimeout := fs.Int("inactivity-timeout", 0, "Number of seconds, after which user will be logged out if inactive. Ignored if less than 300 seconds (5 minutes).")
fK8sMode := fs.String("k8s-mode", "in-cluster", "in-cluster | off-cluster")
fK8sModeOffClusterEndpoint := fs.String("k8s-mode-off-cluster-endpoint", "", "URL of the Kubernetes API server.")
fK8sModeOffClusterSkipVerifyTLS := fs.Bool("k8s-mode-off-cluster-skip-verify-tls", false, "DEV ONLY. When true, skip verification of certs presented by k8s API server.")
fK8sModeOffClusterThanos := fs.String("k8s-mode-off-cluster-thanos", "", "DEV ONLY. URL of the cluster's Thanos server.")
fK8sModeOffClusterAlertmanager := fs.String("k8s-mode-off-cluster-alertmanager", "", "DEV ONLY. URL of the cluster's AlertManager server.")
fK8sModeOffClusterMetering := fs.String("k8s-mode-off-cluster-metering", "", "DEV ONLY. URL of the cluster's metering server.")
fK8sAuth := fs.String("k8s-auth", "service-account", "service-account | bearer-token | oidc | openshift")
fK8sAuthBearerToken := fs.String("k8s-auth-bearer-token", "", "Authorization token to send with proxied Kubernetes API requests.")
fK8sModeOffClusterGitOps := fs.String("k8s-mode-off-cluster-gitops", "", "DEV ONLY. URL of the GitOps backend service")
fRedirectPort := fs.Int("redirect-port", 0, "Port number under which the console should listen for custom hostname redirect.")
fLogLevel := fs.String("log-level", "", "level of logging information by package (pkg=level).")
fPublicDir := fs.String("public-dir", "./frontend/public/dist", "directory containing static web assets.")
fTlSCertFile := fs.String("tls-cert-file", "", "TLS certificate. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.")
fTlSKeyFile := fs.String("tls-key-file", "", "The TLS certificate key.")
fCAFile := fs.String("ca-file", "", "PEM File containing trusted certificates of trusted CAs. If not present, the system's Root CAs will be used.")
fDexClientCertFile := fs.String("dex-client-cert-file", "", "PEM File containing certificates of dex client.")
fDexClientKeyFile := fs.String("dex-client-key-file", "", "PEM File containing certificate key of the dex client.")
fDexClientCAFile := fs.String("dex-client-ca-file", "", "PEM File containing trusted CAs for Dex client configuration. If blank, defaults to value of ca-file argument")
fKubectlClientID := fs.String("kubectl-client-id", "", "The OAuth2 client_id of kubectl.")
fKubectlClientSecret := fs.String("kubectl-client-secret", "", "The OAuth2 client_secret of kubectl.")
fKubectlClientSecretFile := fs.String("kubectl-client-secret-file", "", "File containing the OAuth2 client_secret of kubectl.")
fK8sPublicEndpoint := fs.String("k8s-public-endpoint", "", "Endpoint to use when rendering kubeconfigs for clients. Useful for when bridge uses an internal endpoint clients can't access for communicating with the API server.")
fDexAPIHost := fs.String("dex-api-host", "", "Target host and port of the Dex API service.")
fBranding := fs.String("branding", "okd", "Console branding for the masthead logo and title. One of okd, openshift, ocp, online, dedicated, or azure. Defaults to okd.")
fCustomProductName := fs.String("custom-product-name", "", "Custom product name for console branding.")
fCustomLogoFile := fs.String("custom-logo-file", "", "Custom product image for console branding.")
fStatuspageID := fs.String("statuspage-id", "", "Unique ID assigned by statuspage.io page that provides status info.")
fDocumentationBaseURL := fs.String("documentation-base-url", "", "The base URL for documentation links.")
fAlermanagerPublicURL := fs.String("alermanager-public-url", "", "Public URL of the cluster's AlertManager server.")
fGrafanaPublicURL := fs.String("grafana-public-url", "", "Public URL of the cluster's Grafana server.")
fPrometheusPublicURL := fs.String("prometheus-public-url", "", "Public URL of the cluster's Prometheus server.")
fThanosPublicURL := fs.String("thanos-public-url", "", "Public URL of the cluster's Thanos server.")
fLoadTestFactor := fs.Int("load-test-factor", 0, "DEV ONLY. The factor used to multiply k8s API list responses for load testing purposes.")
fDevCatalogCategories := fs.String("developer-catalog-categories", "", "Allow catalog categories customization. (JSON as string)")
if err := serverconfig.Parse(fs, os.Args[1:], "BRIDGE"); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if err := serverconfig.Validate(fs); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
baseURL := &url.URL{}
if *fBaseAddress != "" {
baseURL = bridge.ValidateFlagIsURL("base-address", *fBaseAddress)
}
if *fDexClientCAFile == "" {
*fDexClientCAFile = *fCAFile
}
if !strings.HasPrefix(*fBasePath, "/") || !strings.HasSuffix(*fBasePath, "/") {
bridge.FlagFatalf("base-path", "value must start and end with slash")
}
baseURL.Path = *fBasePath
caCertFilePath := *fCAFile
if *fK8sMode == "in-cluster" {
caCertFilePath = k8sInClusterCA
}
logoutRedirect := &url.URL{}
if *fUserAuthLogoutRedirect != "" {
logoutRedirect = bridge.ValidateFlagIsURL("user-auth-logout-redirect", *fUserAuthLogoutRedirect)
}
documentationBaseURL := &url.URL{}
if *fDocumentationBaseURL != "" {
if !strings.HasSuffix(*fDocumentationBaseURL, "/") {
bridge.FlagFatalf("documentation-base-url", "value must end with slash")
}
documentationBaseURL = bridge.ValidateFlagIsURL("documentation-base-url", *fDocumentationBaseURL)
}
alertManagerPublicURL := &url.URL{}
if *fAlermanagerPublicURL != "" {
alertManagerPublicURL = bridge.ValidateFlagIsURL("alermanager-public-url", *fAlermanagerPublicURL)
}
grafanaPublicURL := &url.URL{}
if *fGrafanaPublicURL != "" {
grafanaPublicURL = bridge.ValidateFlagIsURL("grafana-public-url", *fGrafanaPublicURL)
}
prometheusPublicURL := &url.URL{}
if *fPrometheusPublicURL != "" {
prometheusPublicURL = bridge.ValidateFlagIsURL("prometheus-public-url", *fPrometheusPublicURL)
}
thanosPublicURL := &url.URL{}
if *fThanosPublicURL != "" {
thanosPublicURL = bridge.ValidateFlagIsURL("thanos-public-url", *fThanosPublicURL)
}
branding := *fBranding
if branding == "origin" {
branding = "okd"
}
switch branding {
case "okd":
case "openshift":
case "ocp":
case "online":
case "dedicated":
case "azure":
default:
bridge.FlagFatalf("branding", "value must be one of okd, openshift, ocp, online, dedicated, or azure")
}
if *fCustomLogoFile != "" {
if _, err := os.Stat(*fCustomLogoFile); err != nil {
klog.Fatalf("could not read logo file: %v", err)
}
}
if *fInactivityTimeout < 300 {
klog.Warning("Flag inactivity-timeout is set to less then 300 seconds and will be ignored!")
} else {
if *fK8sAuth != "oidc" && *fK8sAuth != "openshift" {
fmt.Fprintln(os.Stderr, "In order activate the user inactivity timout, flag --user-auth must be one of: oidc, openshift")
os.Exit(1)
}
klog.Infof("Setting user inactivity timout to %d seconds", *fInactivityTimeout)
}
srv := &server.Server{
PublicDir: *fPublicDir,
BaseURL: baseURL,
LogoutRedirect: logoutRedirect,
Branding: branding,
CustomProductName: *fCustomProductName,
CustomLogoFile: *fCustomLogoFile,
StatuspageID: *fStatuspageID,
DocumentationBaseURL: documentationBaseURL,
AlertManagerPublicURL: alertManagerPublicURL,
GrafanaPublicURL: grafanaPublicURL,
PrometheusPublicURL: prometheusPublicURL,
ThanosPublicURL: thanosPublicURL,
LoadTestFactor: *fLoadTestFactor,
InactivityTimeout: *fInactivityTimeout,
DevCatalogCategories: *fDevCatalogCategories,
}
// if !in-cluster (dev) we should not pass these values to the frontend
if *fK8sMode == "in-cluster" {
srv.GOARCH = runtime.GOARCH
srv.GOOS = runtime.GOOS
}
if (*fKubectlClientID == "") != (*fKubectlClientSecret == "" && *fKubectlClientSecretFile == "") {
fmt.Fprintln(os.Stderr, "Must provide both --kubectl-client-id and --kubectl-client-secret or --kubectrl-client-secret-file")
os.Exit(1)
}
if *fKubectlClientSecret != "" && *fKubectlClientSecretFile != "" {
fmt.Fprintln(os.Stderr, "Cannot provide both --kubectl-client-secret and --kubectrl-client-secret-file")
os.Exit(1)
}
if *fLogLevel != "" {
klog.Warningf("DEPRECATED: --log-level is now deprecated, use verbosity flag --v=Level instead")
}
var (
// Hold on to raw certificates so we can render them in kubeconfig files.
k8sCertPEM []byte
)
var (
k8sAuthServiceAccountBearerToken string
)
if *fDexClientCertFile != "" && *fDexClientKeyFile != "" && *fDexAPIHost != "" {
var err error
if srv.DexClient, err = auth.NewDexClient(*fDexAPIHost, *fDexClientCAFile, *fDexClientCertFile, *fDexClientKeyFile); err != nil {
klog.Fatalf("Failed to create a Dex API client: %v", err)
}
}
var secureCookies bool
if baseURL.Scheme == "https" {
secureCookies = true
klog.Info("cookies are secure!")
} else {
secureCookies = false
klog.Warning("cookies are not secure because base-address is not https!")
}
var k8sEndpoint *url.URL
switch *fK8sMode {
case "in-cluster":
k8sEndpoint = &url.URL{Scheme: "https", Host: "kubernetes.default.svc"}
var err error
k8sCertPEM, err = ioutil.ReadFile(k8sInClusterCA)
if err != nil {
klog.Fatalf("Error inferring Kubernetes config from environment: %v", err)
}
rootCAs := x509.NewCertPool()
if !rootCAs.AppendCertsFromPEM(k8sCertPEM) {
klog.Fatal("No CA found for the API server")
}
tlsConfig := oscrypto.SecureTLSConfig(&tls.Config{
RootCAs: rootCAs,
})
bearerToken, err := ioutil.ReadFile(k8sInClusterBearerToken)
if err != nil {
klog.Fatalf("failed to read bearer token: %v", err)
}
srv.K8sProxyConfig = &proxy.Config{
TLSClientConfig: tlsConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: k8sEndpoint,
}
k8sAuthServiceAccountBearerToken = string(bearerToken)
// If running in an OpenShift cluster, set up a proxy to the prometheus-k8s service running in the openshift-monitoring namespace.
if *fServiceCAFile != "" {
serviceCertPEM, err := ioutil.ReadFile(*fServiceCAFile)
if err != nil {
klog.Fatalf("failed to read service-ca.crt file: %v", err)
}
serviceProxyRootCAs := x509.NewCertPool()
if !serviceProxyRootCAs.AppendCertsFromPEM(serviceCertPEM) {
klog.Fatal("no CA found for Kubernetes services")
}
serviceProxyTLSConfig := oscrypto.SecureTLSConfig(&tls.Config{
RootCAs: serviceProxyRootCAs,
})
srv.ThanosProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftThanosHost, Path: "/api"},
}
srv.ThanosTenancyProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftThanosTenancyHost, Path: "/api"},
}
srv.ThanosTenancyProxyForRulesConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftThanosTenancyForRulesHost, Path: "/api"},
}
srv.AlertManagerProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftAlertManagerHost, Path: "/api"},
}
srv.AlertManagerTenancyProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftAlertManagerTenancyHost, Path: "/api"},
}
srv.MeteringProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftMeteringHost, Path: "/api"},
}
srv.TerminalProxyTLSConfig = serviceProxyTLSConfig
srv.GitOpsProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftGitOpsHost},
}
}
case "off-cluster":
k8sEndpoint = bridge.ValidateFlagIsURL("k8s-mode-off-cluster-endpoint", *fK8sModeOffClusterEndpoint)
serviceProxyTLSConfig := oscrypto.SecureTLSConfig(&tls.Config{
InsecureSkipVerify: *fK8sModeOffClusterSkipVerifyTLS,
})
srv.K8sProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: k8sEndpoint,
}
if *fK8sModeOffClusterThanos != "" {
offClusterThanosURL := bridge.ValidateFlagIsURL("k8s-mode-off-cluster-thanos", *fK8sModeOffClusterThanos)
offClusterThanosURL.Path = "/api"
srv.ThanosTenancyProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterThanosURL,
}
srv.ThanosTenancyProxyForRulesConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterThanosURL,
}
srv.ThanosProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterThanosURL,
}
}
if *fK8sModeOffClusterAlertmanager != "" {
offClusterAlertManagerURL := bridge.ValidateFlagIsURL("k8s-mode-off-cluster-alertmanager", *fK8sModeOffClusterAlertmanager)
offClusterAlertManagerURL.Path = "/api"
srv.AlertManagerProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterAlertManagerURL,
}
srv.AlertManagerTenancyProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterAlertManagerURL,
}
}
if *fK8sModeOffClusterMetering != "" {
offClusterMeteringURL := bridge.ValidateFlagIsURL("k8s-mode-off-cluster-metering", *fK8sModeOffClusterMetering)
offClusterMeteringURL.Path = "/api"
srv.MeteringProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterMeteringURL,
}
}
srv.TerminalProxyTLSConfig = serviceProxyTLSConfig
if *fK8sModeOffClusterGitOps != "" {
offClusterGitOpsURL := bridge.ValidateFlagIsURL("k8s-mode-off-cluster-gitops", *fK8sModeOffClusterGitOps)
srv.GitOpsProxyConfig = &proxy.Config{
TLSClientConfig: serviceProxyTLSConfig,
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: offClusterGitOpsURL,
}
}
default:
bridge.FlagFatalf("k8s-mode", "must be one of: in-cluster, off-cluster")
}
apiServerEndpoint := *fK8sPublicEndpoint
if apiServerEndpoint == "" {
apiServerEndpoint = srv.K8sProxyConfig.Endpoint.String()
}
srv.KubeAPIServerURL = apiServerEndpoint
srv.K8sClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: srv.K8sProxyConfig.TLSClientConfig,
},
}
switch *fUserAuth {
case "oidc", "openshift":
bridge.ValidateFlagNotEmpty("base-address", *fBaseAddress)
bridge.ValidateFlagNotEmpty("user-auth-oidc-client-id", *fUserAuthOIDCClientID)
if *fUserAuthOIDCClientSecret == "" && *fUserAuthOIDCClientSecretFile == "" {
fmt.Fprintln(os.Stderr, "Must provide either --user-auth-oidc-client-secret or --user-auth-oidc-client-secret-file")
os.Exit(1)
}
if *fUserAuthOIDCClientSecret != "" && *fUserAuthOIDCClientSecretFile != "" {
fmt.Fprintln(os.Stderr, "Cannot provide both --user-auth-oidc-client-secret and --user-auth-oidc-client-secret-file")
os.Exit(1)
}
var (
err error
userAuthOIDCIssuerURL *url.URL
authLoginErrorEndpoint = proxy.SingleJoiningSlash(srv.BaseURL.String(), server.AuthLoginErrorEndpoint)
authLoginSuccessEndpoint = proxy.SingleJoiningSlash(srv.BaseURL.String(), server.AuthLoginSuccessEndpoint)
oidcClientSecret = *fUserAuthOIDCClientSecret
// Abstraction leak required by NewAuthenticator. We only want the browser to send the auth token for paths starting with basePath/api.
cookiePath = proxy.SingleJoiningSlash(srv.BaseURL.Path, "/api/")
refererPath = srv.BaseURL.String()
)
scopes := []string{"openid", "email", "profile", "groups"}
authSource := auth.AuthSourceTectonic
if *fUserAuth == "openshift" {
// Scopes come from OpenShift documentation
// https://docs.openshift.com/container-platform/3.9/architecture/additional_concepts/authentication.html#service-accounts-as-oauth-clients
//
// TODO(ericchiang): Support other scopes like view only permissions.
scopes = []string{"user:full"}
authSource = auth.AuthSourceOpenShift
if *fUserAuthOIDCIssuerURL != "" {
bridge.FlagFatalf("user-auth-oidc-issuer-url", "cannot be used with --user-auth=\"openshift\"")
}
userAuthOIDCIssuerURL = k8sEndpoint
} else {
userAuthOIDCIssuerURL = bridge.ValidateFlagIsURL("user-auth-oidc-issuer-url", *fUserAuthOIDCIssuerURL)
}
if *fUserAuthOIDCClientSecretFile != "" {
buf, err := ioutil.ReadFile(*fUserAuthOIDCClientSecretFile)
if err != nil {
klog.Fatalf("Failed to read client secret file: %v", err)
}
oidcClientSecret = string(buf)
}
// Config for logging into console.
oidcClientConfig := &auth.Config{
AuthSource: authSource,
IssuerURL: userAuthOIDCIssuerURL.String(),
IssuerCA: *fUserAuthOIDCCAFile,
ClientID: *fUserAuthOIDCClientID,
ClientSecret: oidcClientSecret,
RedirectURL: proxy.SingleJoiningSlash(srv.BaseURL.String(), server.AuthLoginCallbackEndpoint),
Scope: scopes,
// Use the k8s CA file for OpenShift OAuth metadata discovery.
// This might be different than IssuerCA.
K8sCA: caCertFilePath,
ErrorURL: authLoginErrorEndpoint,
SuccessURL: authLoginSuccessEndpoint,
CookiePath: cookiePath,
RefererPath: refererPath,
SecureCookies: secureCookies,
}
// NOTE: This won't work when using the OpenShift auth mode.
if *fKubectlClientID != "" {
srv.KubectlClientID = *fKubectlClientID
// Assume kubectl is the client ID trusted by kubernetes, not bridge.
// These additional flags causes Dex to issue an ID token valid for
// both bridge and kubernetes.
oidcClientConfig.Scope = append(
oidcClientConfig.Scope,
"audience:server:client_id:"+*fUserAuthOIDCClientID,
"audience:server:client_id:"+*fKubectlClientID,
)
}
if srv.Auther, err = auth.NewAuthenticator(context.Background(), oidcClientConfig); err != nil {
klog.Fatalf("Error initializing authenticator: %v", err)
}
case "disabled":
klog.Warning("running with AUTHENTICATION DISABLED!")
default:
bridge.FlagFatalf("user-auth", "must be one of: oidc, openshift, disabled")
}
switch *fK8sAuth {
case "service-account":
bridge.ValidateFlagIs("k8s-mode", *fK8sMode, "in-cluster")
srv.StaticUser = &auth.User{
Token: k8sAuthServiceAccountBearerToken,
}
srv.ServiceAccountToken = k8sAuthServiceAccountBearerToken
case "bearer-token":
bridge.ValidateFlagNotEmpty("k8s-auth-bearer-token", *fK8sAuthBearerToken)
srv.StaticUser = &auth.User{
Token: *fK8sAuthBearerToken,
}
srv.ServiceAccountToken = *fK8sAuthBearerToken
case "oidc", "openshift":
bridge.ValidateFlagIs("user-auth", *fUserAuth, "oidc", "openshift")
srv.ServiceAccountToken = k8sAuthServiceAccountBearerToken
default:
bridge.FlagFatalf("k8s-mode", "must be one of: service-account, bearer-token, oidc, openshift")
}
srv.MonitoringDashboardConfigMapLister = server.NewResourceLister(
srv.ServiceAccountToken,
&url.URL{
Scheme: k8sEndpoint.Scheme,
Host: k8sEndpoint.Host,
Path: "/api/v1/namespaces/openshift-config-managed/configmaps",
RawQuery: url.Values{
"labelSelector": {"console.openshift.io/dashboard=true"},
}.Encode(),
},
&http.Client{
Transport: &http.Transport{
TLSClientConfig: srv.K8sProxyConfig.TLSClientConfig,
},
},
nil,
)
srv.KnativeEventSourceCRDLister = server.NewResourceLister(
srv.ServiceAccountToken,
&url.URL{
Scheme: k8sEndpoint.Scheme,
Host: k8sEndpoint.Host,
Path: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions",
RawQuery: url.Values{
"labelSelector": {"duck.knative.dev/source=true"},
}.Encode(),
},
&http.Client{
Transport: &http.Transport{
TLSClientConfig: srv.K8sProxyConfig.TLSClientConfig,
},
},
knative.EventSourceFilter,
)
srv.KnativeChannelCRDLister = server.NewResourceLister(
srv.ServiceAccountToken,
&url.URL{
Scheme: k8sEndpoint.Scheme,
Host: k8sEndpoint.Host,
Path: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions",
RawQuery: url.Values{
"labelSelector": {"duck.knative.dev/addressable=true,messaging.knative.dev/subscribable=true"},
}.Encode(),
},
&http.Client{
Transport: &http.Transport{
TLSClientConfig: srv.K8sProxyConfig.TLSClientConfig,
},
},
knative.ChannelFilter,
)
listenURL := bridge.ValidateFlagIsURL("listen", *fListen)
switch listenURL.Scheme {
case "http":
case "https":
bridge.ValidateFlagNotEmpty("tls-cert-file", *fTlSCertFile)
bridge.ValidateFlagNotEmpty("tls-key-file", *fTlSKeyFile)
default:
bridge.FlagFatalf("listen", "scheme must be one of: http, https")
}
httpsrv := &http.Server{
Addr: listenURL.Host,
Handler: srv.HTTPHandler(),
// Disable HTTP/2, which breaks WebSockets.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
TLSConfig: oscrypto.SecureTLSConfig(&tls.Config{}),
}
if *fRedirectPort != 0 {
go func() {
// Listen on passed port number to be redirected to the console
redirectServer := http.NewServeMux()
redirectServer.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
redirectURL := &url.URL{
Scheme: srv.BaseURL.Scheme,
Host: srv.BaseURL.Host,
RawQuery: req.URL.RawQuery,
Path: req.URL.Path,
}
http.Redirect(res, req, redirectURL.String(), http.StatusMovedPermanently)
})
redirectPort := fmt.Sprintf(":%d", *fRedirectPort)
klog.Infof("Listening on %q for custom hostname redirect...", redirectPort)
klog.Fatal(http.ListenAndServe(redirectPort, redirectServer))
}()
}
klog.Infof("Binding to %s...", httpsrv.Addr)
if listenURL.Scheme == "https" {
klog.Info("using TLS")
klog.Fatal(httpsrv.ListenAndServeTLS(*fTlSCertFile, *fTlSKeyFile))
} else {
klog.Info("not using TLS")
klog.Fatal(httpsrv.ListenAndServe())
}
}