/
cmd.go
397 lines (328 loc) · 13.6 KB
/
cmd.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
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/fs"
"net"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/NYTimes/gziphandler"
"github.com/alexedwards/scs/v2"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
httpmiddleware "github.com/slok/go-http-metrics/middleware"
httpmiddlewarestd "github.com/slok/go-http-metrics/middleware/std"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
"github.com/weaveworks/weave-gitops/core/clustersmngr/cluster"
"github.com/weaveworks/weave-gitops/core/clustersmngr/fetcher"
"github.com/weaveworks/weave-gitops/core/logger"
"github.com/weaveworks/weave-gitops/core/nsaccess"
core "github.com/weaveworks/weave-gitops/core/server"
"github.com/weaveworks/weave-gitops/pkg/featureflags"
"github.com/weaveworks/weave-gitops/pkg/health"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/server"
"github.com/weaveworks/weave-gitops/pkg/server/auth"
"github.com/weaveworks/weave-gitops/pkg/server/middleware"
"github.com/weaveworks/weave-gitops/pkg/telemetry"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
k8sMetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)
const (
// Allowed login requests per second
loginRequestRateLimit = 20
InsecureNoAuthenticationUserFlag = "insecure-no-authentication-user"
)
// Options contains all the options for the gitops-server command.
type Options struct {
// System config
Host string
LogLevel string
NotificationControllerAddress string
Path string
RoutePrefix string
Port string
AuthMethods []string
// TLS config
Insecure bool
MTLS bool
TLSCertFile string
TLSKeyFile string
// Stuff for profiles apparently
HelmRepoName string
HelmRepoNamespace string
// OIDC
OIDC auth.OIDCConfig
OIDCSecret string
// Auth
NoAuthUser string
// Dev mode
DevMode bool
// Metrics
EnableMetrics bool
MetricsAddress string
UseK8sCachedClients bool
}
var options Options
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Short: "Runs the gitops-server",
RunE: runCmd,
}
options = Options{
OIDC: auth.OIDCConfig{
ClaimsConfig: &auth.ClaimsConfig{},
},
}
// System config
cmd.Flags().StringVar(&options.Host, "host", server.DefaultHost, "UI host")
cmd.Flags().StringVar(&options.LogLevel, "log-level", logger.DefaultLogLevel, "log level")
cmd.Flags().StringVar(&options.NotificationControllerAddress, "notification-controller-address", "", "the address of the notification-controller running in the cluster")
cmd.Flags().StringVar(&options.RoutePrefix, "route-prefix", "", "Mount the UI and API endpoint under a path prefix, e.g. /weave-gitops")
cmd.Flags().StringVar(&options.Port, "port", server.DefaultPort, "UI port")
cmd.Flags().StringSliceVar(&options.AuthMethods, "auth-methods", auth.DefaultAuthMethodStrings(), fmt.Sprintf("Which auth methods to use, valid values are %s", strings.Join(auth.AllUserAuthMethods(), ",")))
cmd.Flags().BoolVar(&options.UseK8sCachedClients, "use-k8s-cached-clients", false, "Enables the use of cached clients")
// TLS
cmd.Flags().BoolVar(&options.Insecure, "insecure", false, "do not attempt to read TLS certificates")
cmd.Flags().BoolVar(&options.MTLS, "mtls", false, "disable enforce mTLS")
cmd.Flags().StringVar(&options.TLSCertFile, "tls-cert-file", "", "filename for the TLS certificate, in-memory generated if omitted")
cmd.Flags().StringVar(&options.TLSKeyFile, "tls-private-key-file", "", "filename for the TLS key, in-memory generated if omitted")
// OIDC
cmd.Flags().StringVar(&options.OIDCSecret, "oidc-secret-name", auth.DefaultOIDCAuthSecretName, "Name of the secret that contains OIDC configuration")
cmd.Flags().StringVar(&options.OIDC.ClientID, "oidc-client-id", "", "The client ID for the OpenID Connect client")
cmd.Flags().StringVar(&options.OIDC.ClientSecret, "oidc-client-secret", "", "The client secret to use with OpenID Connect issuer")
cmd.Flags().StringVar(&options.OIDC.IssuerURL, "oidc-issuer-url", "", "The URL of the OpenID Connect issuer")
cmd.Flags().StringVar(&options.OIDC.RedirectURL, "oidc-redirect-url", "", "The OAuth2 redirect URL")
cmd.Flags().DurationVar(&options.OIDC.TokenDuration, "oidc-token-duration", time.Hour, "The duration of the ID token. It should be set in the format: number + time unit (s,m,h) e.g., 20m")
cmd.Flags().StringVar(&options.OIDC.ClaimsConfig.Username, "oidc-username-claim", auth.ClaimUsername, "JWT claim to use as the user name. By default email, which is expected to be a unique identifier of the end user. Admins can choose other claims, such as sub or name, depending on their provider")
cmd.Flags().StringVar(&options.OIDC.ClaimsConfig.Groups, "oidc-groups-claim", auth.ClaimGroups, "JWT claim to use as the user's group. If the claim is present it must be an array of strings")
cmd.Flags().StringSliceVar(&options.OIDC.Scopes, "custom-oidc-scopes", auth.DefaultScopes, "Customise the requested scopes for then OIDC authentication flow - openid will always be requested")
// OIDC prefixes
cmd.Flags().StringVar(&options.OIDC.UsernamePrefix, "oidc-username-prefix", "", "Prefix to add to the username when impersonating")
cmd.Flags().StringVar(&options.OIDC.GroupsPrefix, "oidc-group-prefix", "", "Prefix to add to the groups when impersonating")
// auth
cmd.Flags().StringVar(&options.NoAuthUser, InsecureNoAuthenticationUserFlag, "", "A kubernetes user to impersonate for all requests, no authentication will be performed")
// Metrics
cmd.Flags().BoolVar(&options.EnableMetrics, "enable-metrics", false, "Starts the metrics listener")
cmd.Flags().StringVar(&options.MetricsAddress, "metrics-address", ":2112", "If the metrics listener is enabled, bind to this address")
return cmd
}
func runCmd(cmd *cobra.Command, args []string) error {
log, err := logger.New(options.LogLevel, options.Insecure)
if err != nil {
return err
}
log.Info("Version", "version", core.Version, "git-commit", core.GitCommit, "branch", core.Branch, "buildtime", core.Buildtime)
featureflags.SetFromEnv(os.Environ())
if cmd.Flags().Changed(InsecureNoAuthenticationUserFlag) && options.NoAuthUser == "" {
return fmt.Errorf("%s flag set but no user specified", InsecureNoAuthenticationUserFlag)
}
mux := http.NewServeMux()
mux.Handle("/health/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok"))
if err != nil {
log.Error(err, "error writing health check")
}
}))
clusterName := kube.InClusterConfigClusterName()
rest, err := config.GetConfig()
if err != nil {
return fmt.Errorf("could not create client config: %w", err)
}
scheme, err := kube.CreateScheme()
if err != nil {
return fmt.Errorf("could not create scheme: %w", err)
}
rawClient, err := client.New(rest, client.Options{
Scheme: scheme,
})
if err != nil {
return fmt.Errorf("could not create kube http client: %w", err)
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
namespace, _, err := kubeConfig.Namespace()
if err != nil {
return fmt.Errorf("couldn't get current namespace")
}
sessionManager := scs.New()
// TODO: Make this configurable
sessionManager.Lifetime = 24 * time.Hour
authServer, err := auth.InitAuthServer(cmd.Context(), log, rawClient, auth.AuthParams{
OIDCConfig: options.OIDC,
OIDCSecretName: options.OIDCSecret,
AuthMethodStrings: options.AuthMethods,
NoAuthUser: options.NoAuthUser,
Namespace: namespace,
SessionManager: sessionManager,
})
if err != nil {
return fmt.Errorf("could not initialise authentication server: %w", err)
}
log.Info("Registering auth routes")
if err := auth.RegisterAuthServer(mux, "/oauth2", authServer, loginRequestRateLimit); err != nil {
return fmt.Errorf("failed to register auth routes: %w", err)
}
ctx := context.Background()
oidcPrefixes := kube.UserPrefixes{
UsernamePrefix: options.OIDC.UsernamePrefix,
GroupsPrefix: options.OIDC.GroupsPrefix,
}
// Incorporate values from authServer.AuthConfig.OIDCConfig
if authServer.OIDCConfig.UsernamePrefix != "" {
log.V(logger.LogLevelWarn).Info("OIDC username prefix configured by both CLI and secret. Secret values will take precedence.")
oidcPrefixes.UsernamePrefix = authServer.OIDCConfig.UsernamePrefix
}
if authServer.OIDCConfig.GroupsPrefix != "" {
log.V(logger.LogLevelWarn).Info("OIDC groups prefix configured by both CLI and secret. Secret values will take precedence.")
oidcPrefixes.GroupsPrefix = authServer.OIDCConfig.GroupsPrefix
}
cl, err := cluster.NewSingleCluster(cluster.DefaultCluster, rest, scheme, oidcPrefixes, cluster.DefaultKubeConfigOptions...)
if err != nil {
return fmt.Errorf("failed to create cluster client; %w", err)
}
if featureflags.Get("WEAVE_GITOPS_FEATURE_TELEMETRY") == "true" {
err := telemetry.InitTelemetry(ctx, cl)
if err != nil {
// If there's an error turning on telemetry, that's not a
// thing that should interrupt anything else
log.Info("Couldn't enable telemetry", "error", err)
}
}
log.Info("Using cached clients", "enabled", options.UseK8sCachedClients)
if options.UseK8sCachedClients {
cl = cluster.NewDelegatingCacheCluster(cl, rest, scheme)
}
fetcher := fetcher.NewSingleClusterFetcher(cl)
clustersManager := clustersmngr.NewClustersManager([]clustersmngr.ClusterFetcher{fetcher}, nsaccess.NewChecker(nsaccess.DefautltWegoAppRules), log)
clustersManager.Start(ctx)
healthChecker := health.NewHealthChecker()
coreConfig, err := core.NewCoreConfig(log, rest, clusterName, clustersManager, healthChecker)
if err != nil {
return fmt.Errorf("could not create core config: %w", err)
}
appAndProfilesHandlers, err := server.NewHandlers(ctx, log,
&server.Config{
CoreServerConfig: coreConfig,
AuthServer: authServer,
},
sessionManager,
)
if err != nil {
return fmt.Errorf("could not create handler: %w", err)
}
mux.Handle("/v1/", gziphandler.GzipHandler(appAndProfilesHandlers))
// Static asset handling
assetFS := getAssets()
assertFSHandler := http.FileServer(http.FS(assetFS))
redirectHandler := server.IndexHTMLHandler(assetFS, log, options.RoutePrefix)
assetHandler := server.AssetHandler(assertFSHandler, redirectHandler)
mux.Handle("/", gziphandler.GzipHandler(assetHandler))
if options.RoutePrefix != "" {
mux = server.WithRoutePrefix(mux, options.RoutePrefix)
}
handler := http.Handler(mux)
if options.EnableMetrics {
mdlw := httpmiddleware.New(httpmiddleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})
handler = httpmiddlewarestd.Handler("", mdlw, handler)
}
handler = middleware.WithLogging(log, handler)
handler = sessionManager.LoadAndSave(handler)
addr := net.JoinHostPort(options.Host, options.Port)
srv := &http.Server{
Addr: addr,
Handler: handler,
}
go func() {
log.Info("Starting server", "address", addr)
if err := listenAndServe(log, srv, options); err != nil {
log.Error(err, "server exited")
os.Exit(1)
}
}()
var metricsServer *http.Server
if options.EnableMetrics {
metricsMux := http.NewServeMux()
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
k8sMetrics.Registry,
clustersmngr.Registry,
}
metricsMux.Handle("/metrics", promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{}))
metricsServer = &http.Server{
Addr: options.MetricsAddress,
Handler: metricsMux,
}
go func() {
log.Info("Starting metrics endpoint", "address", metricsServer.Addr)
if err := metricsServer.ListenAndServe(); err != nil {
log.Error(err, "Error starting metrics endpoint, continuing anyway")
}
}()
}
// graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-quit
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
return fmt.Errorf("server shutdown failed: %w", err)
}
if options.EnableMetrics {
if err := metricsServer.Shutdown(ctx); err != nil {
return fmt.Errorf("metrics server shutdown failed: %w", err)
}
}
return nil
}
func listenAndServe(log logr.Logger, srv *http.Server, options Options) error {
if options.Insecure {
log.Info("TLS connections disabled")
return srv.ListenAndServe()
}
if options.TLSCertFile == "" || options.TLSKeyFile == "" {
return cmderrors.ErrNoTLSCertOrKey
}
if options.MTLS {
caCert, err := os.ReadFile(options.TLSCertFile)
if err != nil {
return fmt.Errorf("failed reading cert file %s. %s", options.TLSCertFile, err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
srv.TLSConfig = &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
} else {
log.Info("Using TLS", "cert_file", options.TLSCertFile, "key_file", options.TLSKeyFile)
}
// if tlsCert and tlsKey are both empty (""), ListenAndServeTLS will ignore
// and happily use the TLSConfig supplied above
return srv.ListenAndServeTLS(options.TLSCertFile, options.TLSKeyFile)
}
func getAssets() fs.FS {
exec, err := os.Executable()
if err != nil {
panic(err)
}
f := os.DirFS(path.Join(path.Dir(exec), "dist"))
return f
}