blob: cdc4947e2d807ffd7c453f9f294c34e2e3f109a4 [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"
Mateusz Zalega18a67b02022-08-02 13:37:50 +02006 "log"
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
12 "source.monogon.dev/metropolis/cli/metroctl/core"
Lorenz Brun6adf8842021-10-05 13:39:11 +020013)
14
15// rootCmd represents the base command when called without any subcommands
16var rootCmd = &cobra.Command{
17 Use: "metroctl",
18 Short: "metroctl controls Metropolis nodes and clusters.",
19}
20
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020021type metroctlFlags struct {
22 // clusterEndpoints is a list of the targeted cluster's endpoints, used by
23 // commands that perform RPC on it.
24 clusterEndpoints []string
Mateusz Zalegaf7774962022-07-08 12:26:55 +020025 // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through.
26 proxyAddr string
Mateusz Zalega8234c162022-07-08 17:05:50 +020027 // configPath overrides the default XDG config path
28 configPath string
Mateusz Zalegab2cac082022-07-14 14:55:43 +020029 // verbose, if set, will make this utility log additional runtime
30 // information.
31 verbose bool
Mateusz Zalegadb75e212022-08-04 17:31:34 +020032 // format refers to how the output data, except logs, is formatted.
33 format string
34 // filter specifies a CEL filter used to narrow down the set of output
35 // objects.
36 filter string
37 // output is an optional output file path the resulting data will be saved
38 // at. If unspecified, the data will be written to stdout.
39 output string
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010040 // acceptAnyCA will persist the first encountered (while connecting) CA
41 // certificate of the cluster as the trusted CA certificate for this cluster.
42 // This is unsafe and should only be used for testing.
43 acceptAnyCA bool
Serge Bazanski98840342024-05-22 13:03:55 +020044 // columns is a comma-separated list of column names which selects which columns
45 // will be output to the user. An empty string means all columns will be
46 // displayed.
47 columns string
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020048}
49
50var flags metroctlFlags
51
52func init() {
Serge Bazanskicf33f682023-03-17 00:16:16 +010053 rootCmd.PersistentFlags().StringSliceVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.")
Mateusz Zalegaf7774962022-07-08 12:26:55 +020054 rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address")
Mateusz Zalega8234c162022-07-08 17:05:50 +020055 rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path")
Mateusz Zalegab2cac082022-07-14 14:55:43 +020056 rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information")
Serge Bazanskie012b722023-03-29 17:49:04 +020057 rootCmd.PersistentFlags().StringVar(&flags.format, "format", "plaintext", "Data output format")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020058 rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data")
Serge Bazanski98840342024-05-22 13:03:55 +020059 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 +020060 rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file")
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010061 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 +020062}
63
Mateusz Zalega18a67b02022-08-02 13:37:50 +020064// rpcLogger passes through the cluster resolver logs, if "--verbose" flag was
65// used.
66func rpcLogger(f string, args ...interface{}) {
67 if flags.verbose {
Serge Bazanski4c50f992022-09-05 18:43:01 +020068 log.Printf("resolver: "+f, args...)
Mateusz Zalega18a67b02022-08-02 13:37:50 +020069 }
70}
71
Lorenz Brun6adf8842021-10-05 13:39:11 +020072func main() {
73 cobra.CheckErr(rootCmd.Execute())
74}
Serge Bazanski1f8cad72023-03-20 16:58:10 +010075
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010076type acceptall struct{}
77
78func (a *acceptall) Ask(ctx context.Context, _ *core.ConnectOptions, _ *x509.Certificate) (bool, error) {
79 return true, nil
80}
81
Serge Bazanski1f8cad72023-03-20 16:58:10 +010082// connectOptions returns core.ConnectOptions as defined by the metroctl flags
83// currently set.
84func connectOptions() *core.ConnectOptions {
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010085 var tofu core.CertificateTOFU
86 if flags.acceptAnyCA {
87 tofu = &acceptall{}
88 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +010089 return &core.ConnectOptions{
Serge Bazanski925ec3d2024-02-05 14:38:20 +010090 ConfigPath: flags.configPath,
91 ProxyServer: flags.proxyAddr,
92 Endpoints: flags.clusterEndpoints,
93 ResolverLogger: rpcLogger,
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010094 TOFU: tofu,
Serge Bazanski1f8cad72023-03-20 16:58:10 +010095 }
96}