blob: fcdbccda0b07a77ab2e63c1e364fa416fb794fd1 [file] [log] [blame]
Lorenz Brun6adf8842021-10-05 13:39:11 +02001package main
2
3import (
Serge Bazanski7eeef0f2024-02-05 14:40:15 +01004 "context"
5 "crypto/x509"
Serge Bazanskica8d9512024-09-12 14:20:57 +02006 "os"
Mateusz Zalega8234c162022-07-08 17:05:50 +02007 "path/filepath"
8
9 "github.com/adrg/xdg"
Lorenz Brun6adf8842021-10-05 13:39:11 +020010 "github.com/spf13/cobra"
Serge Bazanski1f8cad72023-03-20 16:58:10 +010011
Serge Bazanskica8d9512024-09-12 14:20:57 +020012 "source.monogon.dev/go/logging"
Serge Bazanski1f8cad72023-03-20 16:58:10 +010013 "source.monogon.dev/metropolis/cli/metroctl/core"
Lorenz Brun6adf8842021-10-05 13:39:11 +020014)
15
16// rootCmd represents the base command when called without any subcommands
17var rootCmd = &cobra.Command{
18 Use: "metroctl",
19 Short: "metroctl controls Metropolis nodes and clusters.",
20}
21
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020022type metroctlFlags struct {
23 // clusterEndpoints is a list of the targeted cluster's endpoints, used by
24 // commands that perform RPC on it.
25 clusterEndpoints []string
Mateusz Zalegaf7774962022-07-08 12:26:55 +020026 // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through.
27 proxyAddr string
Mateusz Zalega8234c162022-07-08 17:05:50 +020028 // configPath overrides the default XDG config path
29 configPath string
Mateusz Zalegab2cac082022-07-14 14:55:43 +020030 // verbose, if set, will make this utility log additional runtime
31 // information.
32 verbose bool
Mateusz Zalegadb75e212022-08-04 17:31:34 +020033 // format refers to how the output data, except logs, is formatted.
34 format string
35 // filter specifies a CEL filter used to narrow down the set of output
36 // objects.
37 filter string
38 // output is an optional output file path the resulting data will be saved
39 // at. If unspecified, the data will be written to stdout.
40 output string
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010041 // acceptAnyCA will persist the first encountered (while connecting) CA
42 // certificate of the cluster as the trusted CA certificate for this cluster.
43 // This is unsafe and should only be used for testing.
44 acceptAnyCA bool
Serge Bazanski98840342024-05-22 13:03:55 +020045 // columns is a comma-separated list of column names which selects which columns
46 // will be output to the user. An empty string means all columns will be
47 // displayed.
48 columns string
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020049}
50
51var flags metroctlFlags
52
53func init() {
Serge Bazanskicf33f682023-03-17 00:16:16 +010054 rootCmd.PersistentFlags().StringSliceVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.")
Mateusz Zalegaf7774962022-07-08 12:26:55 +020055 rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address")
Mateusz Zalega8234c162022-07-08 17:05:50 +020056 rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path")
Mateusz Zalegab2cac082022-07-14 14:55:43 +020057 rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information")
Serge Bazanskie012b722023-03-29 17:49:04 +020058 rootCmd.PersistentFlags().StringVar(&flags.format, "format", "plaintext", "Data output format")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020059 rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data")
Serge Bazanski98840342024-05-22 13:03:55 +020060 rootCmd.PersistentFlags().StringVar(&flags.columns, "columns", "", "Comma-separated list of column names to show. If not set, all columns will be shown")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020061 rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file")
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010062 rootCmd.PersistentFlags().BoolVar(&flags.acceptAnyCA, "insecure-accept-and-persist-first-encountered-ca", false, "Accept the first encountered CA while connecting as the trusted CA for future metroctl connections with this config path. This is very insecure and should only be used for testing.")
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020063}
64
Lorenz Brun6adf8842021-10-05 13:39:11 +020065func main() {
66 cobra.CheckErr(rootCmd.Execute())
67}
Serge Bazanski1f8cad72023-03-20 16:58:10 +010068
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010069type acceptall struct{}
70
71func (a *acceptall) Ask(ctx context.Context, _ *core.ConnectOptions, _ *x509.Certificate) (bool, error) {
72 return true, nil
73}
74
Serge Bazanski1f8cad72023-03-20 16:58:10 +010075// connectOptions returns core.ConnectOptions as defined by the metroctl flags
76// currently set.
77func connectOptions() *core.ConnectOptions {
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010078 var tofu core.CertificateTOFU
79 if flags.acceptAnyCA {
80 tofu = &acceptall{}
81 }
Serge Bazanskica8d9512024-09-12 14:20:57 +020082 logger := logging.NewWriterBackend(os.Stderr)
83 if !flags.verbose {
84 logger.MinimumSeverity = logging.WARNING
85 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +010086 return &core.ConnectOptions{
Serge Bazanski925ec3d2024-02-05 14:38:20 +010087 ConfigPath: flags.configPath,
88 ProxyServer: flags.proxyAddr,
89 Endpoints: flags.clusterEndpoints,
Serge Bazanskica8d9512024-09-12 14:20:57 +020090 ResolverLogger: logger,
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010091 TOFU: tofu,
Serge Bazanski1f8cad72023-03-20 16:58:10 +010092 }
93}