| Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| Serge Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 4 | "context" |
| 5 | "crypto/x509" |
| Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 6 | "log" |
| Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 7 | "path/filepath" |
| 8 | |
| 9 | "github.com/adrg/xdg" |
| Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 10 | "github.com/spf13/cobra" |
| Serge Bazanski | 1f8cad7 | 2023-03-20 16:58:10 +0100 | [diff] [blame] | 11 | |
| 12 | "source.monogon.dev/metropolis/cli/metroctl/core" |
| Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | // rootCmd represents the base command when called without any subcommands |
| 16 | var rootCmd = &cobra.Command{ |
| 17 | Use: "metroctl", |
| 18 | Short: "metroctl controls Metropolis nodes and clusters.", |
| 19 | } |
| 20 | |
| Mateusz Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 21 | type 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 Zalega | f777496 | 2022-07-08 12:26:55 +0200 | [diff] [blame] | 25 | // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through. |
| 26 | proxyAddr string |
| Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 27 | // configPath overrides the default XDG config path |
| 28 | configPath string |
| Mateusz Zalega | b2cac08 | 2022-07-14 14:55:43 +0200 | [diff] [blame] | 29 | // verbose, if set, will make this utility log additional runtime |
| 30 | // information. |
| 31 | verbose bool |
| Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 32 | // 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 Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 40 | // 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 Bazanski | 9884034 | 2024-05-22 13:03:55 +0200 | [diff] [blame] | 44 | // 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 Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | var flags metroctlFlags |
| 51 | |
| 52 | func init() { |
| Serge Bazanski | cf33f68 | 2023-03-17 00:16:16 +0100 | [diff] [blame] | 53 | rootCmd.PersistentFlags().StringSliceVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.") |
| Mateusz Zalega | f777496 | 2022-07-08 12:26:55 +0200 | [diff] [blame] | 54 | rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address") |
| Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 55 | rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path") |
| Mateusz Zalega | b2cac08 | 2022-07-14 14:55:43 +0200 | [diff] [blame] | 56 | rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information") |
| Serge Bazanski | e012b72 | 2023-03-29 17:49:04 +0200 | [diff] [blame] | 57 | rootCmd.PersistentFlags().StringVar(&flags.format, "format", "plaintext", "Data output format") |
| Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 58 | rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data") |
| Serge Bazanski | 9884034 | 2024-05-22 13:03:55 +0200 | [diff] [blame] | 59 | rootCmd.PersistentFlags().StringVar(&flags.columns, "columns", "", "Comma-separated list of column names to show. If not set, all columns will be shown") |
| Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 60 | rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file") |
| Serge Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 61 | 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 Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 64 | // rpcLogger passes through the cluster resolver logs, if "--verbose" flag was |
| 65 | // used. |
| 66 | func rpcLogger(f string, args ...interface{}) { |
| 67 | if flags.verbose { |
| Serge Bazanski | 4c50f99 | 2022-09-05 18:43:01 +0200 | [diff] [blame] | 68 | log.Printf("resolver: "+f, args...) |
| Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 72 | func main() { |
| 73 | cobra.CheckErr(rootCmd.Execute()) |
| 74 | } |
| Serge Bazanski | 1f8cad7 | 2023-03-20 16:58:10 +0100 | [diff] [blame] | 75 | |
| Serge Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 76 | type acceptall struct{} |
| 77 | |
| 78 | func (a *acceptall) Ask(ctx context.Context, _ *core.ConnectOptions, _ *x509.Certificate) (bool, error) { |
| 79 | return true, nil |
| 80 | } |
| 81 | |
| Serge Bazanski | 1f8cad7 | 2023-03-20 16:58:10 +0100 | [diff] [blame] | 82 | // connectOptions returns core.ConnectOptions as defined by the metroctl flags |
| 83 | // currently set. |
| 84 | func connectOptions() *core.ConnectOptions { |
| Serge Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 85 | var tofu core.CertificateTOFU |
| 86 | if flags.acceptAnyCA { |
| 87 | tofu = &acceptall{} |
| 88 | } |
| Serge Bazanski | 1f8cad7 | 2023-03-20 16:58:10 +0100 | [diff] [blame] | 89 | return &core.ConnectOptions{ |
| Serge Bazanski | 925ec3d | 2024-02-05 14:38:20 +0100 | [diff] [blame] | 90 | ConfigPath: flags.configPath, |
| 91 | ProxyServer: flags.proxyAddr, |
| 92 | Endpoints: flags.clusterEndpoints, |
| 93 | ResolverLogger: rpcLogger, |
| Serge Bazanski | 7eeef0f | 2024-02-05 14:40:15 +0100 | [diff] [blame] | 94 | TOFU: tofu, |
| Serge Bazanski | 1f8cad7 | 2023-03-20 16:58:10 +0100 | [diff] [blame] | 95 | } |
| 96 | } |