From 7f08923ebdffe79ba7ab2427fc61bae8c9934ddc Mon Sep 17 00:00:00 2001 From: dominic <510002+dmah42@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:20:05 +0000 Subject: [PATCH] set auth config from flags (#62) --- cmd/discovery/discover.go | 10 +++++++--- cmd/health/check.go | 7 +++++-- cmd/root/root.go | 2 -- pkg/config/auth.go | 8 ++++++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/discovery/discover.go b/cmd/discovery/discover.go index d3afac4..0562a8e 100644 --- a/cmd/discovery/discover.go +++ b/cmd/discovery/discover.go @@ -61,14 +61,16 @@ type outputDiscover struct { type option struct { aeCMD.Option ctx context.Context + auth *config.Auth + outputFormat *cli.OutputFormat cidr string ip string port uint16 protocol string verbose bool writer io.Writer - output *outputDiscover - outputFormat *cli.OutputFormat + + output *outputDiscover } func (o *option) Complete(args []string) error { @@ -141,7 +143,7 @@ func (o option) checkHost(ip_str string) bool { log.Printf("connecting to %s:%d using protocol %s\n", ip_str, o.port, o.protocol) } - c, err := client.New(o.ctx, config.WithSystem(config.System{Protocol: o.protocol, Socket: net.JoinHostPort(ip_str, fmt.Sprintf("%d", o.port))})) + c, err := client.New(o.ctx, config.WithAuth(*o.auth), config.WithSystem(config.System{Protocol: o.protocol, Socket: net.JoinHostPort(ip_str, fmt.Sprintf("%d", o.port))})) if err != nil { log.Fatalf("failed to create client: %s", err) return false @@ -171,6 +173,7 @@ func (o option) checkHost(ip_str string) bool { func NewCMD(ctx context.Context) *cobra.Command { o := &option{ + auth: &config.Auth{}, outputFormat: cli.NewOutputFormat(). WithDefaultFormat(printer.NewJSON().Format()). WithPrinter(printer.NewJSON()), @@ -183,6 +186,7 @@ func NewCMD(ctx context.Context) *cobra.Command { return aeCMD.Run(ctx, o, cmd, args) }, } + o.auth.AddFlags(cmd) o.outputFormat.AddFlags(cmd) cmd.Flags().Uint16Var(&o.port, "port", o.port, "The port to use when trying to connect") cmd.Flags().BoolVar(&o.verbose, "verbose", o.verbose, "Lots of output") diff --git a/cmd/health/check.go b/cmd/health/check.go index fe9e11a..ada4ae1 100644 --- a/cmd/health/check.go +++ b/cmd/health/check.go @@ -65,13 +65,14 @@ type option struct { // TODO: abstract the next batch of fields into a "clusterOption" ctx context.Context + auth *config.Auth + outputFormat *cli.OutputFormat cidr string ip string port uint16 protocol string verbose bool writer io.Writer - outputFormat *cli.OutputFormat services []string output *outputCheck @@ -158,7 +159,7 @@ func (o option) checkHost(ip_str string) bool { log.Printf("connecting to %s:%d using protocol %s\n", ip_str, o.port, o.protocol) } - c, err := client.New(o.ctx, config.WithSystem(config.System{Protocol: o.protocol, Socket: net.JoinHostPort(ip_str, fmt.Sprintf("%d", o.port))})) + c, err := client.New(o.ctx, config.WithAuth(*o.auth), config.WithSystem(config.System{Protocol: o.protocol, Socket: net.JoinHostPort(ip_str, fmt.Sprintf("%d", o.port))})) if err != nil { log.Fatalf("failed to create client: %s", err) return false @@ -190,6 +191,7 @@ func (o option) checkHost(ip_str string) bool { func NewCMD(ctx context.Context) *cobra.Command { o := &option{ + auth: &config.Auth{}, outputFormat: cli.NewOutputFormat(). WithDefaultFormat(printer.NewJSON().Format()). WithPrinter(printer.NewJSON()), @@ -202,6 +204,7 @@ func NewCMD(ctx context.Context) *cobra.Command { return aeCMD.Run(ctx, o, cmd, args) }, } + o.auth.AddFlags(cmd) o.outputFormat.AddFlags(cmd) cmd.Flags().Uint16Var(&o.port, "port", o.port, "The port to use when trying to connect") cmd.Flags().BoolVar(&o.verbose, "verbose", o.verbose, "Lots of output") diff --git a/cmd/root/root.go b/cmd/root/root.go index e284687..c4b3d0c 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -58,8 +58,6 @@ func Execute() { } func init() { - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") - // add subcommands ctx := context.Background() rootCmd.AddCommand(oci.NewCMD(ctx)) diff --git a/pkg/config/auth.go b/pkg/config/auth.go index ecb2123..b726b2e 100644 --- a/pkg/config/auth.go +++ b/pkg/config/auth.go @@ -1,5 +1,7 @@ package config +import "github.com/spf13/cobra" + type Auth struct { CaCert string ClientCert string @@ -15,3 +17,9 @@ func (a Auth) Set(cfg *Configs) error { func WithAuth(auth Auth) Config { return auth } + +func (a *Auth) AddFlags(cmd *cobra.Command) { + cmd.Flags().StringVar(&a.CaCert, "ca_crt", "/etc/aurae/ca.crt", "The CA certificate") + cmd.Flags().StringVar(&a.ClientCert, "client_crt", "/etc/aurae/_signed.client.crt", "The client certificate") + cmd.Flags().StringVar(&a.ClientKey, "client_key", "/etc/aurae/client.key", "The client certificate key") +}