Skip to content

Commit

Permalink
set auth config from flags (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmah42 authored Mar 13, 2023
1 parent 2b5064f commit 7f08923
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
10 changes: 7 additions & 3 deletions cmd/discovery/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand All @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions cmd/health/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand All @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/auth.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "github.com/spf13/cobra"

type Auth struct {
CaCert string
ClientCert string
Expand All @@ -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")
}

0 comments on commit 7f08923

Please sign in to comment.