From 3a7ea64a31cd47d0d94a2470258feda7ba582fd1 Mon Sep 17 00:00:00 2001 From: Todd Date: Wed, 1 Nov 2023 12:45:35 -0700 Subject: [PATCH] Add skip daemon flag and send recently authed tokens to daemon (#3883) * Add skip daemon flag and send recently authed tokens to daemon * Rename flag skip-daemon to skip-cache-daemon * adding option test * move common.Options to base.Option * SocketAddress returns *url.URL, fix search test --- internal/clientcache/cmd/daemon/addtoken.go | 36 +- .../clientcache/cmd/daemon/command_wrapper.go | 89 +++- .../clientcache/cmd/daemon/stop_windows.go | 7 +- internal/clientcache/cmd/search/search.go | 6 +- .../clientcache/cmd/search/search_test.go | 13 +- .../clientcache/internal/daemon/listener.go | 8 +- .../internal/daemon/listener_test.go | 4 +- .../clientcache/internal/daemon/server.go | 22 - .../clientcache/internal/daemon/testing.go | 13 + internal/cmd/base/base.go | 26 +- internal/cmd/base/dev.go | 2 +- internal/cmd/base/format.go | 4 +- internal/cmd/base/option.go | 22 +- internal/cmd/base/option_test.go | 47 ++- internal/cmd/base/servers.go | 2 +- internal/cmd/commands.go | 381 +++++++++--------- .../cmd/commands/authenticate/authenticate.go | 10 +- internal/cmd/commands/authenticate/funcs.go | 7 +- internal/cmd/commands/authenticate/ldap.go | 15 +- internal/cmd/commands/authenticate/oidc.go | 16 +- .../cmd/commands/authenticate/password.go | 15 +- internal/cmd/common/option.go | 39 -- internal/cmd/common/option_test.go | 32 -- internal/cmd/search_cmd_darwin.go | 8 +- internal/cmd/search_cmd_freebsd.go | 8 +- internal/cmd/search_cmd_linux.go | 8 +- internal/cmd/search_cmd_windows.go | 8 +- 27 files changed, 429 insertions(+), 419 deletions(-) delete mode 100644 internal/cmd/common/option.go delete mode 100644 internal/cmd/common/option_test.go diff --git a/internal/clientcache/cmd/daemon/addtoken.go b/internal/clientcache/cmd/daemon/addtoken.go index 3082d230f..b3c35a2db 100644 --- a/internal/clientcache/cmd/daemon/addtoken.go +++ b/internal/clientcache/cmd/daemon/addtoken.go @@ -64,8 +64,19 @@ func (c *AddTokenCommand) Run(args []string) int { c.PrintCliError(err) return base.CommandUserError } + client, err := c.Client() + if err != nil { + c.PrintCliError(err) + return base.CommandCliError + } - apiErr, err := c.Add(ctx) + keyringType, tokenName, err := c.DiscoverKeyringTokenInfo() + if err != nil { + c.PrintCliError(err) + return base.CommandCliError + } + + apiErr, err := c.Add(ctx, client, keyringType, tokenName) if err != nil { c.PrintCliError(err) return base.CommandCliError @@ -78,24 +89,14 @@ func (c *AddTokenCommand) Run(args []string) int { return base.CommandSuccess } -func (c *AddTokenCommand) Add(ctx context.Context) (*api.Error, error) { +func (c *AddTokenCommand) Add(ctx context.Context, client *api.Client, keyringType, tokenName string) (*api.Error, error) { const op = "daemon.(AddTokenCommand).Add" - client, err := c.Client() - if err != nil { - return nil, err - } - - keyringType, tokenName, err := c.DiscoverKeyringTokenInfo() - if err != nil { - return nil, err - } pa := daemon.UpsertTokenRequest{ BoundaryAddr: client.Addr(), } switch keyringType { case "", base.NoneKeyring: - keyringType = base.NoneKeyring token := client.Token() if parts := strings.SplitN(token, "_", 4); len(parts) == 3 { pa.AuthTokenId = strings.Join(parts[:2], "_") @@ -129,12 +130,15 @@ func addToken(ctx context.Context, daemonPath string, p *daemon.UpsertTokenReque if err != nil { return nil, errors.Wrap(ctx, err, op) } - addr := daemon.SocketAddress(daemonPath) - _, err = os.Stat(strings.TrimPrefix(addr, "unix://")) - if strings.HasPrefix(addr, "unix://") && err != nil { + addr, err := daemon.SocketAddress(daemonPath) + if err != nil { return nil, errors.Wrap(ctx, err, op) } - if err := client.SetAddr(addr); err != nil { + _, err = os.Stat(addr.Path) + if strings.EqualFold(addr.Scheme, "unix") && err != nil { + return nil, errors.Wrap(ctx, err, op) + } + if err := client.SetAddr(addr.String()); err != nil { return nil, errors.Wrap(ctx, err, op) } // Because this is using the real lib it can pick up from stored locations diff --git a/internal/clientcache/cmd/daemon/command_wrapper.go b/internal/clientcache/cmd/daemon/command_wrapper.go index 57a872096..0b139c709 100644 --- a/internal/clientcache/cmd/daemon/command_wrapper.go +++ b/internal/clientcache/cmd/daemon/command_wrapper.go @@ -10,24 +10,31 @@ import ( "os" "os/exec" "strings" + "time" + "github.com/hashicorp/boundary/internal/clientcache/internal/daemon" "github.com/hashicorp/boundary/internal/cmd/base" + "github.com/hashicorp/boundary/internal/errors" "github.com/mitchellh/cli" ) +// Keep this interface aligned with the interface at internal/cmd/commands.go +type cacheEnabledCommand interface { + cli.Command + BaseCommand() *base.Command +} + // CommandWrapper starts the boundary daemon after the command was Run and attempts // to send the current persona to any running daemon. type CommandWrapper struct { - cli.Command - ui cli.Ui + cacheEnabledCommand } // Wrap returns a cli.CommandFactory that returns a command wrapped in the CommandWrapper. -func Wrap(ui cli.Ui, wrapped cli.Command) cli.CommandFactory { +func Wrap(c cacheEnabledCommand) cli.CommandFactory { return func() (cli.Command, error) { return &CommandWrapper{ - Command: wrapped, - ui: ui, + cacheEnabledCommand: c, }, nil } } @@ -35,11 +42,18 @@ func Wrap(ui cli.Ui, wrapped cli.Command) cli.CommandFactory { // Run runs the wrapped command and then attempts to start the boundary daemon and send // the current persona func (w *CommandWrapper) Run(args []string) int { - r := w.Command.Run(args) + if w.BaseCommand().FlagSkipCacheDaemon { + return w.cacheEnabledCommand.Run(args) + } + + // potentially intercept the token in case it isn't stored in the keyring + var token string + w.cacheEnabledCommand.BaseCommand().Opts = append(w.cacheEnabledCommand.BaseCommand().Opts, base.WithInterceptedToken(&token)) + r := w.cacheEnabledCommand.Run(args) ctx := context.Background() if w.startDaemon(ctx) { - w.addPersonaInCache(ctx) + w.addTokenToCache(ctx, token) } return r } @@ -49,7 +63,7 @@ func (w *CommandWrapper) Run(args []string) int { func (w *CommandWrapper) startDaemon(ctx context.Context) bool { cmdName, err := os.Executable() if err != nil { - w.ui.Error(fmt.Sprintf("unable to find boundary binary for daemon startup: %s", err.Error())) + w.BaseCommand().UI.Error(fmt.Sprintf("unable to find boundary binary for daemon startup: %s", err.Error())) return false } @@ -64,10 +78,59 @@ func (w *CommandWrapper) startDaemon(ctx context.Context) bool { return err == nil || strings.Contains(stdErr.String(), "already running") } -// addPersonaInCache runs AddPersonaCommand -func (w *CommandWrapper) addPersonaInCache(ctx context.Context) bool { - c := AddTokenCommand{Command: base.NewCommand(w.ui)} - c.Flags() - apiErr, err := c.Add(ctx) +// addTokenToCache runs AddTokenCommand with the token used in, or retrieved by +// the wrapped command. +func (w *CommandWrapper) addTokenToCache(ctx context.Context, token string) bool { + com := AddTokenCommand{Command: base.NewCommand(w.BaseCommand().UI)} + client, err := w.BaseCommand().Client() + if err != nil { + return false + } + keyringType, tokName, err := w.BaseCommand().DiscoverKeyringTokenInfo() + if err != nil { + return false + } + if token != "" { + client.SetToken(token) + } + + // Since the daemon might have just started, we need to wait until it can + // respond to our requests + waitCtx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + if err := waitForDaemon(waitCtx); err != nil { + // TODO: Print the result of this out into a log in the dot directory + return false + } + + apiErr, err := com.Add(ctx, client, keyringType, tokName) return err == nil && apiErr == nil } + +// waitForDaemon continually looks for the unix socket until it is found or the +// provided context is done. It returns an error if the unix socket is not found +// before the context is done. +func waitForDaemon(ctx context.Context) error { + const op = "daemon.waitForDaemon" + dotPath, err := DefaultDotDirectory(ctx) + if err != nil { + return err + } + timer := time.NewTimer(0) + + addr, err := daemon.SocketAddress(dotPath) + if err != nil { + return errors.Wrap(ctx, err, op) + } + _, err = os.Stat(addr.Path) + for os.IsNotExist(err) { + select { + case <-timer.C: + case <-ctx.Done(): + return ctx.Err() + } + _, err = os.Stat(addr.Path) + timer.Reset(10 * time.Millisecond) + } + return nil +} diff --git a/internal/clientcache/cmd/daemon/stop_windows.go b/internal/clientcache/cmd/daemon/stop_windows.go index 3410ddd1e..20b4442d9 100644 --- a/internal/clientcache/cmd/daemon/stop_windows.go +++ b/internal/clientcache/cmd/daemon/stop_windows.go @@ -69,12 +69,15 @@ func (s *StopCommand) stop(ctx context.Context) error { func stopThroughHandler(ctx context.Context, dotPath string) (*api.Error, error) { const op = "daemon.stopThroughHandler" - sockAddr := daemon.SocketAddress(dotPath) + sockAddr, err := daemon.SocketAddress(dotPath) + if err != nil { + return nil, errors.Wrap(ctx, err, op) + } client, err := api.NewClient(nil) if err != nil { return nil, errors.Wrap(ctx, err, op) } - if err := client.SetAddr(sockAddr); err != nil { + if err := client.SetAddr(sockAddr.String()); err != nil { return nil, errors.Wrap(ctx, err, op) } // Because this is using the real lib it can pick up from stored locations diff --git a/internal/clientcache/cmd/search/search.go b/internal/clientcache/cmd/search/search.go index 0e89d9963..906220524 100644 --- a/internal/clientcache/cmd/search/search.go +++ b/internal/clientcache/cmd/search/search.go @@ -169,14 +169,14 @@ func search(ctx context.Context, daemonPath string, fb filterBy) (*api.Response, if err != nil { return nil, errors.Wrap(ctx, err, op) } - addr := daemon.SocketAddress(daemonPath) + addr, err := daemon.SocketAddress(daemonPath) if err != nil { return nil, errors.Wrap(ctx, err, op) } - if _, err := os.Stat(strings.TrimPrefix(addr, "unix://")); strings.HasPrefix(addr, "unix://") && err == os.ErrNotExist { + if _, err := os.Stat(addr.Path); addr.Scheme == "unix" && err == os.ErrNotExist { return nil, errors.New(ctx, errors.Internal, op, "daemon unix socket is not setup") } - if err := client.SetAddr(addr); err != nil { + if err := client.SetAddr(addr.String()); err != nil { return nil, errors.Wrap(ctx, err, op) } diff --git a/internal/clientcache/cmd/search/search_test.go b/internal/clientcache/cmd/search/search_test.go index a2d3ee173..00b84d68a 100644 --- a/internal/clientcache/cmd/search/search_test.go +++ b/internal/clientcache/cmd/search/search_test.go @@ -25,24 +25,12 @@ type testCommander struct { at *authtokens.AuthToken } -func (r *testCommander) keyring() string { - return r.at.Id -} - -func (r *testCommander) tokenName() string { - return r.at.Id -} - func (r *testCommander) Client(opt ...base.Option) (*api.Client, error) { client, err := api.NewClient(nil) require.NoError(r.t, err) return client, nil } -func (r *testCommander) DiscoverKeyringTokenInfo() (string, string, error) { - return r.keyring(), r.tokenName(), nil -} - func (r *testCommander) ReadTokenFromKeyring(k, a string) *authtokens.AuthToken { return r.at } @@ -72,6 +60,7 @@ func TestSearch(t *testing.T) { }() // Give the store some time to get initialized time.Sleep(100 * time.Millisecond) + srv.AddKeyringToken(t, "address", "keyringtype", "tokenname", at.Id, boundaryTokenReaderFn) errorCases := []struct { name string diff --git a/internal/clientcache/internal/daemon/listener.go b/internal/clientcache/internal/daemon/listener.go index 88c21236f..4b0c2a8e1 100644 --- a/internal/clientcache/internal/daemon/listener.go +++ b/internal/clientcache/internal/daemon/listener.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "net" + "net/url" "os" "path/filepath" @@ -52,8 +53,7 @@ func listener(ctx context.Context, path string) (net.Listener, error) { return l, nil } -// SocketAddress returns the unix socket filename with a 'unix://' prefix. -// The returned value can be passed directly into the api.Client -func SocketAddress(path string) string { - return fmt.Sprintf("unix://%s", filepath.Join(path, sockAddr)) +// SocketAddress returns the unix socket *url.URL with the scheme set to 'unix'. +func SocketAddress(path string) (*url.URL, error) { + return url.Parse(fmt.Sprintf("unix://%s", filepath.Join(path, sockAddr))) } diff --git a/internal/clientcache/internal/daemon/listener_test.go b/internal/clientcache/internal/daemon/listener_test.go index bf2461809..de8bec76d 100644 --- a/internal/clientcache/internal/daemon/listener_test.go +++ b/internal/clientcache/internal/daemon/listener_test.go @@ -45,7 +45,9 @@ func TestListenComms(t *testing.T) { client, err := api.NewClient(nil) require.NoError(t, err) - require.NoError(t, client.SetAddr(SocketAddress(path))) + u, err := SocketAddress(path) + require.NoError(t, err) + require.NoError(t, client.SetAddr(u.String())) client.SetToken("") req, err := client.NewRequest(ctx, "GET", "/test", nil) require.NoError(t, err) diff --git a/internal/clientcache/internal/daemon/server.go b/internal/clientcache/internal/daemon/server.go index 866710e06..facc02e37 100644 --- a/internal/clientcache/internal/daemon/server.go +++ b/internal/clientcache/internal/daemon/server.go @@ -35,7 +35,6 @@ import ( // and retrieve the keyring and token information used by a command. type Commander interface { ClientProvider - DiscoverKeyringTokenInfo() (string, string, error) ReadTokenFromKeyring(keyringType, tokenName string) *authtokens.AuthToken } @@ -207,27 +206,6 @@ func (s *CacheServer) Serve(ctx context.Context, cmd Commander, opt ...Option) e return errors.Wrap(ctx, err, op) } - // If we have a token info already, add it to the repository immediately so - // it can start to get updated. - func() { - client, err := cmd.Client() - if err != nil { - event.WriteError(ctx, op, err) - return - } - krType, tokName, err := cmd.DiscoverKeyringTokenInfo() - if err != nil { - event.WriteError(ctx, op, err) - return - } - if at := cmd.ReadTokenFromKeyring(krType, tokName); at != nil { - if err := repo.AddKeyringToken(ctx, client.Addr(), cache.KeyringToken{KeyringType: krType, TokenName: tokName, AuthTokenId: at.Id}); err != nil { - event.WriteError(ctx, op, err) - return - } - } - }() - refreshService, err := cache.NewRefreshService(ctx, repo) if err != nil { return errors.Wrap(ctx, err, op) diff --git a/internal/clientcache/internal/daemon/testing.go b/internal/clientcache/internal/daemon/testing.go index 822c74b2d..112045d60 100644 --- a/internal/clientcache/internal/daemon/testing.go +++ b/internal/clientcache/internal/daemon/testing.go @@ -68,6 +68,19 @@ func (s *TestServer) Serve(t *testing.T, opt ...Option) error { return s.CacheServer.Serve(ctx, s.cmd, opt...) } +func (s *TestServer) AddKeyringToken(t *testing.T, address, keyring, tokenName, tokenId string, atReadFn cache.BoundaryTokenReaderFn) { + t.Helper() + ctx := context.Background() + r, err := cache.NewRepository(ctx, s.CacheServer.store, &sync.Map{}, s.cmd.ReadTokenFromKeyring, atReadFn) + require.NoError(t, err) + + require.NoError(t, r.AddKeyringToken(ctx, address, cache.KeyringToken{ + KeyringType: keyring, + TokenName: tokenName, + AuthTokenId: tokenId, + })) +} + // AddResources adds targets to the cache for the provided address, token name, // and keyring type. They token info must already be known to the server. func (s *TestServer) AddResources(t *testing.T, p *authtokens.AuthToken, tars []*targets.Target, sess []*sessions.Session, atReadFn cache.BoundaryTokenReaderFn) { diff --git a/internal/cmd/base/base.go b/internal/cmd/base/base.go index d638bfc5a..57868ca8a 100644 --- a/internal/cmd/base/base.go +++ b/internal/cmd/base/base.go @@ -64,10 +64,11 @@ const ( // maxLineLength is the maximum width of any line. maxLineLength int = 78 - envToken = "BOUNDARY_TOKEN" - EnvTokenName = "BOUNDARY_TOKEN_NAME" - EnvKeyringType = "BOUNDARY_KEYRING_TYPE" - envRecoveryConfig = "BOUNDARY_RECOVERY_CONFIG" + envToken = "BOUNDARY_TOKEN" + EnvTokenName = "BOUNDARY_TOKEN_NAME" + EnvKeyringType = "BOUNDARY_KEYRING_TYPE" + envRecoveryConfig = "BOUNDARY_RECOVERY_CONFIG" + envSkipCacheDaemon = "BOUNDARY_SKIP_CACHE_DAEMON" StoredTokenName = "HashiCorp Boundary Auth Token" ) @@ -84,6 +85,8 @@ type Command struct { UI cli.Ui ShutdownCh chan struct{} + Opts []Option + flags *FlagSets flagsOnce sync.Once @@ -103,6 +106,7 @@ type Command struct { FlagKeyringType string FlagRecoveryConfig string FlagOutputCurlString bool + FlagSkipCacheDaemon bool FlagScopeId string FlagScopeName string @@ -189,6 +193,10 @@ func MakeShutdownCh() chan struct{} { return resultCh } +func (c *Command) BaseCommand() *Command { + return c +} + // Client returns the HTTP API client. The client is cached on the command to // save performance on future calls. func (c *Command) Client(opt ...Option) (*api.Client, error) { @@ -197,7 +205,7 @@ func (c *Command) Client(opt ...Option) (*api.Client, error) { return c.client, nil } - opts := getOpts(opt...) + opts := GetOpts(opt...) config, err := api.DefaultConfig() if err != nil { @@ -455,6 +463,14 @@ func (c *Command) FlagSet(bit FlagSetBit) *FlagSets { Target: &c.FlagOutputCurlString, Usage: "Instead of executing the request, print an equivalent cURL command string and exit.", }) + + f.BoolVar(&BoolVar{ + Name: "skip-cache-daemon", + Target: &c.FlagSkipCacheDaemon, + Default: false, + EnvVar: envSkipCacheDaemon, + Usage: "Skips starting the caching daemon or sending the current used/retrieved token to the caching daemon.", + }) } if bit&FlagSetOutputFormat != 0 { diff --git a/internal/cmd/base/dev.go b/internal/cmd/base/dev.go index 51071f246..91674a18b 100644 --- a/internal/cmd/base/dev.go +++ b/internal/cmd/base/dev.go @@ -35,7 +35,7 @@ func (b *Server) CreateDevDatabase(ctx context.Context, opt ...Option) error { var err error var c func() error - opts := getOpts(opt...) + opts := GetOpts(opt...) // We should only get back postgres for now, but laying the foundation for non-postgres switch opts.withDialect { diff --git a/internal/cmd/base/format.go b/internal/cmd/base/format.go index 5028a8aec..a4f3ca039 100644 --- a/internal/cmd/base/format.go +++ b/internal/cmd/base/format.go @@ -182,7 +182,7 @@ func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]any) string { // information, to the UI in the appropriate format. WithAttributeFieldPrefix is // used, all other options are ignored. func (c *Command) PrintApiError(in *api.Error, contextStr string, opt ...Option) { - opts := getOpts(opt...) + opts := GetOpts(opt...) switch Format(c.UI) { case "json": var b []byte @@ -310,7 +310,7 @@ func (c *Command) PrintJsonItem(resp *api.Response, opt ...Option) bool { // PrintJson prints the given raw JSON in our common format func (c *Command) PrintJson(input json.RawMessage, opt ...Option) bool { - opts := getOpts(opt...) + opts := GetOpts(opt...) output := struct { StatusCode int `json:"status_code,omitempty"` Item json.RawMessage `json:"item,omitempty"` diff --git a/internal/cmd/base/option.go b/internal/cmd/base/option.go index a748018b4..2fdce8ac3 100644 --- a/internal/cmd/base/option.go +++ b/internal/cmd/base/option.go @@ -9,8 +9,8 @@ import ( wrapping "github.com/hashicorp/go-kms-wrapping/v2" ) -// getOpts - iterate the inbound Options and return a struct. -func getOpts(opt ...Option) Options { +// GetOpts - iterate the inbound Options and return a struct. +func GetOpts(opt ...Option) Options { opts := getDefaultOptions() for _, o := range opt { if o != nil { @@ -45,6 +45,8 @@ type Options struct { withHostPlugin func() (string, plugin.HostPluginServiceClient) withEventGating bool withImplicitId string + WithSkipScopeIdFlag bool + WithInterceptedToken *string } func getDefaultOptions() Options { @@ -209,3 +211,19 @@ func WithImplicitId(with string) Option { o.withImplicitId = with } } + +// WithSkipScopeIdFlag tells a command to not create a scope ID flag (usually +// because it's already been defined) +func WithSkipScopeIdFlag(with bool) Option { + return func(o *Options) { + o.WithSkipScopeIdFlag = with + } +} + +// WithInterceptedToken provides a string pointer that will have the token +// assigned to it when performing an authenticate command. +func WithInterceptedToken(s *string) Option { + return func(o *Options) { + o.WithInterceptedToken = s + } +} diff --git a/internal/cmd/base/option_test.go b/internal/cmd/base/option_test.go index 4305d0746..93265d344 100644 --- a/internal/cmd/base/option_test.go +++ b/internal/cmd/base/option_test.go @@ -15,7 +15,7 @@ func Test_GetOpts(t *testing.T) { t.Parallel() t.Run("nil-options", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(nil, nil) + opts := GetOpts(nil, nil) testOpts := getDefaultOptions() assert.Equal(opts, testOpts) }) @@ -28,7 +28,7 @@ func Test_GetOpts(t *testing.T) { ObservationsEnabled: &isTrue, SysEventsEnabled: &isTrue, } - opts := getOpts(WithEventFlags(&f)) + opts := GetOpts(WithEventFlags(&f)) testOpts := getDefaultOptions() testOpts.withEventFlags = &f assert.Equal(opts, testOpts) @@ -45,84 +45,84 @@ func Test_GetOpts(t *testing.T) { }, }, } - opts := getOpts(WithEventerConfig(&c)) + opts := GetOpts(WithEventerConfig(&c)) testOpts := getDefaultOptions() testOpts.withEventerConfig = &c assert.Equal(opts, testOpts) }) t.Run("WithNoTokenScope", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithNoTokenScope()) + opts := GetOpts(WithNoTokenScope()) testOpts := getDefaultOptions() testOpts.withNoTokenScope = true assert.Equal(opts, testOpts) }) t.Run("WithSkipDatabaseDestruction", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipDatabaseDestruction()) + opts := GetOpts(WithSkipDatabaseDestruction()) testOpts := getDefaultOptions() testOpts.withSkipDatabaseDestruction = true assert.Equal(opts, testOpts) }) t.Run("WithNoTokenValue", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithNoTokenValue()) + opts := GetOpts(WithNoTokenValue()) testOpts := getDefaultOptions() testOpts.withNoTokenValue = true assert.Equal(opts, testOpts) }) t.Run("WithSkipAuthMethodCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipAuthMethodCreation()) + opts := GetOpts(WithSkipAuthMethodCreation()) testOpts := getDefaultOptions() testOpts.withSkipAuthMethodCreation = true assert.Equal(opts, testOpts) }) t.Run("WithSkipOidcAuthMethodCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipOidcAuthMethodCreation()) + opts := GetOpts(WithSkipOidcAuthMethodCreation()) testOpts := getDefaultOptions() testOpts.withSkipOidcAuthMethodCreation = true assert.Equal(opts, testOpts) }) t.Run("WithSkipLdapAuthMethodCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipLdapAuthMethodCreation()) + opts := GetOpts(WithSkipLdapAuthMethodCreation()) testOpts := getDefaultOptions() testOpts.withSkipLdapAuthMethodCreation = true assert.Equal(opts, testOpts) }) t.Run("WithSkipScopesCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipScopesCreation()) + opts := GetOpts(WithSkipScopesCreation()) testOpts := getDefaultOptions() testOpts.withSkipScopesCreation = true assert.Equal(opts, testOpts) }) t.Run("WithSkipHostResourcesCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipHostResourcesCreation()) + opts := GetOpts(WithSkipHostResourcesCreation()) testOpts := getDefaultOptions() testOpts.withSkipHostResourcesCreation = true assert.Equal(opts, testOpts) }) t.Run("WithSkipTargetCreation", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithSkipTargetCreation()) + opts := GetOpts(WithSkipTargetCreation()) testOpts := getDefaultOptions() testOpts.withSkipTargetCreation = true assert.Equal(opts, testOpts) }) t.Run("WithContainerImage", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(WithContainerImage("test-container")) + opts := GetOpts(WithContainerImage("test-container")) testOpts := getDefaultOptions() testOpts.withContainerImage = "test-container" assert.Equal(opts, testOpts) }) t.Run("withDialect", func(t *testing.T) { assert := assert.New(t) - opts := getOpts(withDialect("test-dialect")) + opts := GetOpts(withDialect("test-dialect")) testOpts := getDefaultOptions() testOpts.withDialect = "test-dialect" assert.Equal(opts, testOpts) @@ -131,7 +131,24 @@ func Test_GetOpts(t *testing.T) { assert := assert.New(t) testOpts := getDefaultOptions() assert.False(testOpts.withEventGating) - opts := getOpts(WithEventGating(true)) + opts := GetOpts(WithEventGating(true)) assert.True(opts.withEventGating) }) + + t.Run("WithSkipScopeIdFlag", func(t *testing.T) { + assert := assert.New(t) + opts := GetOpts(WithSkipScopeIdFlag(true)) + testOpts := getDefaultOptions() + testOpts.WithSkipScopeIdFlag = true + assert.Equal(opts, testOpts) + }) + + t.Run("WithSkipScopeIdFlag", func(t *testing.T) { + assert := assert.New(t) + var s string + opts := GetOpts(WithInterceptedToken(&s)) + testOpts := getDefaultOptions() + testOpts.WithInterceptedToken = &s + assert.Equal(opts, testOpts) + }) } diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 988907e1c..8824f9e52 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -175,7 +175,7 @@ func (b *Server) SetupEventing(ctx context.Context, logger hclog.Logger, seriali if serverName == "" { return berrors.New(ctx, berrors.InvalidParameter, op, "missing server name") } - opts := getOpts(opt...) + opts := GetOpts(opt...) if opts.withEventerConfig != nil { if err := opts.withEventerConfig.Validate(); err != nil { return berrors.Wrap(ctx, err, op, berrors.WithMsg("invalid eventer config")) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 761ab92a5..99ec4e17d 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -4,8 +4,6 @@ package cmd import ( - "github.com/hashicorp/boundary/api" - "github.com/hashicorp/boundary/api/authtokens" "github.com/hashicorp/boundary/internal/cmd/base" "github.com/hashicorp/boundary/internal/cmd/commands/accountscmd" "github.com/hashicorp/boundary/internal/cmd/commands/authenticate" @@ -69,19 +67,19 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { }, nil }, - "authenticate": commandFactoryWrapper(ui, + "authenticate": clientCacheWrapper( &authenticate.Command{ Command: base.NewCommand(ui), }), - "authenticate password": commandFactoryWrapper(ui, + "authenticate password": clientCacheWrapper( &authenticate.PasswordCommand{ Command: base.NewCommand(ui), }), - "authenticate oidc": commandFactoryWrapper(ui, + "authenticate oidc": clientCacheWrapper( &authenticate.OidcCommand{ Command: base.NewCommand(ui), }), - "authenticate ldap": commandFactoryWrapper(ui, + "authenticate ldap": clientCacheWrapper( &authenticate.LdapCommand{ Command: base.NewCommand(ui), }), @@ -91,67 +89,67 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "accounts read": commandFactoryWrapper(ui, + "accounts read": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "accounts delete": commandFactoryWrapper(ui, + "accounts delete": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "accounts list": commandFactoryWrapper(ui, + "accounts list": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "accounts set-password": commandFactoryWrapper(ui, + "accounts set-password": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "set-password", }), - "accounts change-password": commandFactoryWrapper(ui, + "accounts change-password": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "change-password", }), - "accounts create": commandFactoryWrapper(ui, + "accounts create": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "accounts create password": commandFactoryWrapper(ui, + "accounts create password": clientCacheWrapper( &accountscmd.PasswordCommand{ Command: base.NewCommand(ui), Func: "create", }), - "accounts create oidc": commandFactoryWrapper(ui, + "accounts create oidc": clientCacheWrapper( &accountscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "create", }), - "accounts create ldap": commandFactoryWrapper(ui, + "accounts create ldap": clientCacheWrapper( &accountscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "create", }), - "accounts update": commandFactoryWrapper(ui, + "accounts update": clientCacheWrapper( &accountscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "accounts update password": commandFactoryWrapper(ui, + "accounts update password": clientCacheWrapper( &accountscmd.PasswordCommand{ Command: base.NewCommand(ui), Func: "update", }), - "accounts update oidc": commandFactoryWrapper(ui, + "accounts update oidc": clientCacheWrapper( &accountscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "update", }), - "accounts update ldap": commandFactoryWrapper(ui, + "accounts update ldap": clientCacheWrapper( &accountscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "update", @@ -162,62 +160,62 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "auth-methods read": commandFactoryWrapper(ui, + "auth-methods read": clientCacheWrapper( &authmethodscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "auth-methods delete": commandFactoryWrapper(ui, + "auth-methods delete": clientCacheWrapper( &authmethodscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "auth-methods list": commandFactoryWrapper(ui, + "auth-methods list": clientCacheWrapper( &authmethodscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "auth-methods create": commandFactoryWrapper(ui, + "auth-methods create": clientCacheWrapper( &authmethodscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "auth-methods create password": commandFactoryWrapper(ui, + "auth-methods create password": clientCacheWrapper( &authmethodscmd.PasswordCommand{ Command: base.NewCommand(ui), Func: "create", }), - "auth-methods create oidc": commandFactoryWrapper(ui, + "auth-methods create oidc": clientCacheWrapper( &authmethodscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "create", }), - "auth-methods create ldap": commandFactoryWrapper(ui, + "auth-methods create ldap": clientCacheWrapper( &authmethodscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "create", }), - "auth-methods update": commandFactoryWrapper(ui, + "auth-methods update": clientCacheWrapper( &authmethodscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "auth-methods update password": commandFactoryWrapper(ui, + "auth-methods update password": clientCacheWrapper( &authmethodscmd.PasswordCommand{ Command: base.NewCommand(ui), Func: "update", }), - "auth-methods update oidc": commandFactoryWrapper(ui, + "auth-methods update oidc": clientCacheWrapper( &authmethodscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "update", }), - "auth-methods update ldap": commandFactoryWrapper(ui, + "auth-methods update ldap": clientCacheWrapper( &authmethodscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "update", }), - "auth-methods change-state oidc": commandFactoryWrapper(ui, + "auth-methods change-state oidc": clientCacheWrapper( &authmethodscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "change-state", @@ -228,17 +226,17 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "auth-tokens read": commandFactoryWrapper(ui, + "auth-tokens read": clientCacheWrapper( &authtokenscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "auth-tokens delete": commandFactoryWrapper(ui, + "auth-tokens delete": clientCacheWrapper( &authtokenscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "auth-tokens list": commandFactoryWrapper(ui, + "auth-tokens list": clientCacheWrapper( &authtokenscmd.Command{ Command: base.NewCommand(ui), Func: "list", @@ -286,32 +284,32 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { }, nil }, - "connect": commandFactoryWrapper(ui, + "connect": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "connect", }), - "connect http": commandFactoryWrapper(ui, + "connect http": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "http", }), - "connect kube": commandFactoryWrapper(ui, + "connect kube": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "kube", }), - "connect postgres": commandFactoryWrapper(ui, + "connect postgres": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "postgres", }), - "connect rdp": commandFactoryWrapper(ui, + "connect rdp": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "rdp", }), - "connect ssh": commandFactoryWrapper(ui, + "connect ssh": clientCacheWrapper( &connect.Command{ Command: base.NewCommand(ui), Func: "ssh", @@ -338,57 +336,57 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "credential-libraries read": commandFactoryWrapper(ui, + "credential-libraries read": clientCacheWrapper( &credentiallibrariescmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "credential-libraries delete": commandFactoryWrapper(ui, + "credential-libraries delete": clientCacheWrapper( &credentiallibrariescmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "credential-libraries list": commandFactoryWrapper(ui, + "credential-libraries list": clientCacheWrapper( &credentiallibrariescmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "credential-libraries create": commandFactoryWrapper(ui, + "credential-libraries create": clientCacheWrapper( &credentiallibrariescmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "credential-libraries create vault": commandFactoryWrapper(ui, + "credential-libraries create vault": clientCacheWrapper( &credentiallibrariescmd.VaultCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credential-libraries create vault-generic": commandFactoryWrapper(ui, + "credential-libraries create vault-generic": clientCacheWrapper( &credentiallibrariescmd.VaultGenericCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credential-libraries create vault-ssh-certificate": commandFactoryWrapper(ui, + "credential-libraries create vault-ssh-certificate": clientCacheWrapper( &credentiallibrariescmd.VaultSshCertificateCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credential-libraries update": commandFactoryWrapper(ui, + "credential-libraries update": clientCacheWrapper( &credentiallibrariescmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "credential-libraries update vault": commandFactoryWrapper(ui, + "credential-libraries update vault": clientCacheWrapper( &credentiallibrariescmd.VaultGenericCommand{ Command: base.NewCommand(ui), Func: "update", }), - "credential-libraries update vault-generic": commandFactoryWrapper(ui, + "credential-libraries update vault-generic": clientCacheWrapper( &credentiallibrariescmd.VaultGenericCommand{ Command: base.NewCommand(ui), Func: "update", }), - "credential-libraries update vault-ssh-certificate": commandFactoryWrapper(ui, + "credential-libraries update vault-ssh-certificate": clientCacheWrapper( &credentiallibrariescmd.VaultSshCertificateCommand{ Command: base.NewCommand(ui), Func: "update", @@ -399,47 +397,47 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "credential-stores read": commandFactoryWrapper(ui, + "credential-stores read": clientCacheWrapper( &credentialstorescmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "credential-stores delete": commandFactoryWrapper(ui, + "credential-stores delete": clientCacheWrapper( &credentialstorescmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "credential-stores list": commandFactoryWrapper(ui, + "credential-stores list": clientCacheWrapper( &credentialstorescmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "credential-stores create": commandFactoryWrapper(ui, + "credential-stores create": clientCacheWrapper( &credentialstorescmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "credential-stores create vault": commandFactoryWrapper(ui, + "credential-stores create vault": clientCacheWrapper( &credentialstorescmd.VaultCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credential-stores create static": commandFactoryWrapper(ui, + "credential-stores create static": clientCacheWrapper( &credentialstorescmd.StaticCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credential-stores update": commandFactoryWrapper(ui, + "credential-stores update": clientCacheWrapper( &credentialstorescmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "credential-stores update vault": commandFactoryWrapper(ui, + "credential-stores update vault": clientCacheWrapper( &credentialstorescmd.VaultCommand{ Command: base.NewCommand(ui), Func: "update", }), - "credential-stores update static": commandFactoryWrapper(ui, + "credential-stores update static": clientCacheWrapper( &credentialstorescmd.StaticCommand{ Command: base.NewCommand(ui), Func: "update", @@ -450,57 +448,57 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "credentials read": commandFactoryWrapper(ui, + "credentials read": clientCacheWrapper( &credentialscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "credentials delete": commandFactoryWrapper(ui, + "credentials delete": clientCacheWrapper( &credentialscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "credentials list": commandFactoryWrapper(ui, + "credentials list": clientCacheWrapper( &credentialscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "credentials create": commandFactoryWrapper(ui, + "credentials create": clientCacheWrapper( &credentialscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "credentials create username-password": commandFactoryWrapper(ui, + "credentials create username-password": clientCacheWrapper( &credentialscmd.UsernamePasswordCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credentials create ssh-private-key": commandFactoryWrapper(ui, + "credentials create ssh-private-key": clientCacheWrapper( &credentialscmd.SshPrivateKeyCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credentials create json": commandFactoryWrapper(ui, + "credentials create json": clientCacheWrapper( &credentialscmd.JsonCommand{ Command: base.NewCommand(ui), Func: "create", }), - "credentials update": commandFactoryWrapper(ui, + "credentials update": clientCacheWrapper( &credentialscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "credentials update username-password": commandFactoryWrapper(ui, + "credentials update username-password": clientCacheWrapper( &credentialscmd.UsernamePasswordCommand{ Command: base.NewCommand(ui), Func: "update", }), - "credentials update ssh-private-key": commandFactoryWrapper(ui, + "credentials update ssh-private-key": clientCacheWrapper( &credentialscmd.SshPrivateKeyCommand{ Command: base.NewCommand(ui), Func: "update", }), - "credentials update json": commandFactoryWrapper(ui, + "credentials update json": clientCacheWrapper( &credentialscmd.JsonCommand{ Command: base.NewCommand(ui), Func: "update", @@ -525,42 +523,42 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "groups create": commandFactoryWrapper(ui, + "groups create": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "groups update": commandFactoryWrapper(ui, + "groups update": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "groups read": commandFactoryWrapper(ui, + "groups read": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "groups delete": commandFactoryWrapper(ui, + "groups delete": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "groups list": commandFactoryWrapper(ui, + "groups list": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "groups add-members": commandFactoryWrapper(ui, + "groups add-members": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "add-members", }), - "groups set-members": commandFactoryWrapper(ui, + "groups set-members": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "set-members", }), - "groups remove-members": commandFactoryWrapper(ui, + "groups remove-members": clientCacheWrapper( &groupscmd.Command{ Command: base.NewCommand(ui), Func: "remove-members", @@ -571,47 +569,47 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "host-catalogs read": commandFactoryWrapper(ui, + "host-catalogs read": clientCacheWrapper( &hostcatalogscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "host-catalogs delete": commandFactoryWrapper(ui, + "host-catalogs delete": clientCacheWrapper( &hostcatalogscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "host-catalogs list": commandFactoryWrapper(ui, + "host-catalogs list": clientCacheWrapper( &hostcatalogscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "host-catalogs create": commandFactoryWrapper(ui, + "host-catalogs create": clientCacheWrapper( &hostcatalogscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "host-catalogs create static": commandFactoryWrapper(ui, + "host-catalogs create static": clientCacheWrapper( &hostcatalogscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "create", }), - "host-catalogs create plugin": commandFactoryWrapper(ui, + "host-catalogs create plugin": clientCacheWrapper( &hostcatalogscmd.PluginCommand{ Command: base.NewCommand(ui), Func: "create", }), - "host-catalogs update": commandFactoryWrapper(ui, + "host-catalogs update": clientCacheWrapper( &hostcatalogscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "host-catalogs update static": commandFactoryWrapper(ui, + "host-catalogs update static": clientCacheWrapper( &hostcatalogscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "update", }), - "host-catalogs update plugin": commandFactoryWrapper(ui, + "host-catalogs update plugin": clientCacheWrapper( &hostcatalogscmd.PluginCommand{ Command: base.NewCommand(ui), Func: "update", @@ -622,62 +620,62 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "host-sets read": commandFactoryWrapper(ui, + "host-sets read": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "host-sets delete": commandFactoryWrapper(ui, + "host-sets delete": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "host-sets list": commandFactoryWrapper(ui, + "host-sets list": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "host-sets create": commandFactoryWrapper(ui, + "host-sets create": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "host-sets create static": commandFactoryWrapper(ui, + "host-sets create static": clientCacheWrapper( &hostsetscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "create", }), - "host-sets create plugin": commandFactoryWrapper(ui, + "host-sets create plugin": clientCacheWrapper( &hostsetscmd.PluginCommand{ Command: base.NewCommand(ui), Func: "create", }), - "host-sets update": commandFactoryWrapper(ui, + "host-sets update": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "host-sets update static": commandFactoryWrapper(ui, + "host-sets update static": clientCacheWrapper( &hostsetscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "update", }), - "host-sets update plugin": commandFactoryWrapper(ui, + "host-sets update plugin": clientCacheWrapper( &hostsetscmd.PluginCommand{ Command: base.NewCommand(ui), Func: "update", }), - "host-sets add-hosts": commandFactoryWrapper(ui, + "host-sets add-hosts": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "add-hosts", }), - "host-sets remove-hosts": commandFactoryWrapper(ui, + "host-sets remove-hosts": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "remove-hosts", }), - "host-sets set-hosts": commandFactoryWrapper(ui, + "host-sets set-hosts": clientCacheWrapper( &hostsetscmd.Command{ Command: base.NewCommand(ui), Func: "set-hosts", @@ -688,37 +686,37 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "hosts read": commandFactoryWrapper(ui, + "hosts read": clientCacheWrapper( &hostscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "hosts delete": commandFactoryWrapper(ui, + "hosts delete": clientCacheWrapper( &hostscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "hosts list": commandFactoryWrapper(ui, + "hosts list": clientCacheWrapper( &hostscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "hosts create": commandFactoryWrapper(ui, + "hosts create": clientCacheWrapper( &hostscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "hosts create static": commandFactoryWrapper(ui, + "hosts create static": clientCacheWrapper( &hostscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "create", }), - "hosts update": commandFactoryWrapper(ui, + "hosts update": clientCacheWrapper( &hostscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "hosts update static": commandFactoryWrapper(ui, + "hosts update static": clientCacheWrapper( &hostscmd.StaticCommand{ Command: base.NewCommand(ui), Func: "update", @@ -735,47 +733,47 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "managed-groups read": commandFactoryWrapper(ui, + "managed-groups read": clientCacheWrapper( &managedgroupscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "managed-groups delete": commandFactoryWrapper(ui, + "managed-groups delete": clientCacheWrapper( &managedgroupscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "managed-groups list": commandFactoryWrapper(ui, + "managed-groups list": clientCacheWrapper( &managedgroupscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "managed-groups create": commandFactoryWrapper(ui, + "managed-groups create": clientCacheWrapper( &managedgroupscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "managed-groups create oidc": commandFactoryWrapper(ui, + "managed-groups create oidc": clientCacheWrapper( &managedgroupscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "create", }), - "managed-groups create ldap": commandFactoryWrapper(ui, + "managed-groups create ldap": clientCacheWrapper( &managedgroupscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "create", }), - "managed-groups update": commandFactoryWrapper(ui, + "managed-groups update": clientCacheWrapper( &managedgroupscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "managed-groups update oidc": commandFactoryWrapper(ui, + "managed-groups update oidc": clientCacheWrapper( &managedgroupscmd.OidcCommand{ Command: base.NewCommand(ui), Func: "update", }), - "managed-groups update ldap": commandFactoryWrapper(ui, + "managed-groups update ldap": clientCacheWrapper( &managedgroupscmd.LdapCommand{ Command: base.NewCommand(ui), Func: "update", @@ -793,57 +791,57 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "roles create": commandFactoryWrapper(ui, + "roles create": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "roles update": commandFactoryWrapper(ui, + "roles update": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "roles read": commandFactoryWrapper(ui, + "roles read": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "roles delete": commandFactoryWrapper(ui, + "roles delete": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "roles list": commandFactoryWrapper(ui, + "roles list": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "roles add-principals": commandFactoryWrapper(ui, + "roles add-principals": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "add-principals", }), - "roles set-principals": commandFactoryWrapper(ui, + "roles set-principals": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "set-principals", }), - "roles remove-principals": commandFactoryWrapper(ui, + "roles remove-principals": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "remove-principals", }), - "roles add-grants": commandFactoryWrapper(ui, + "roles add-grants": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "add-grants", }), - "roles set-grants": commandFactoryWrapper(ui, + "roles set-grants": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "set-grants", }), - "roles remove-grants": commandFactoryWrapper(ui, + "roles remove-grants": clientCacheWrapper( &rolescmd.Command{ Command: base.NewCommand(ui), Func: "remove-grants", @@ -854,44 +852,44 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "scopes create": commandFactoryWrapper(ui, + "scopes create": clientCacheWrapper( &scopescmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "scopes read": commandFactoryWrapper(ui, + "scopes read": clientCacheWrapper( &scopescmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "scopes update": commandFactoryWrapper(ui, + "scopes update": clientCacheWrapper( &scopescmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "scopes delete": commandFactoryWrapper(ui, + "scopes delete": clientCacheWrapper( &scopescmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "scopes list": commandFactoryWrapper(ui, + "scopes list": clientCacheWrapper( &scopescmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "scopes list-keys": commandFactoryWrapper(ui, + "scopes list-keys": clientCacheWrapper( &scopescmd.ListKeysCommand{ Command: base.NewCommand(ui), }), - "scopes rotate-keys": commandFactoryWrapper(ui, + "scopes rotate-keys": clientCacheWrapper( &scopescmd.RotateKeysCommand{ Command: base.NewCommand(ui), }), - "scopes list-key-version-destruction-jobs": commandFactoryWrapper(ui, + "scopes list-key-version-destruction-jobs": clientCacheWrapper( &scopescmd.ListKeyVersionDestructionJobsCommand{ Command: base.NewCommand(ui), }), - "scopes destroy-key-version": commandFactoryWrapper(ui, + "scopes destroy-key-version": clientCacheWrapper( &scopescmd.DestroyKeyVersionCommand{ Command: base.NewCommand(ui), }), @@ -908,17 +906,17 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "sessions read": commandFactoryWrapper(ui, + "sessions read": clientCacheWrapper( &sessionscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "sessions list": commandFactoryWrapper(ui, + "sessions list": clientCacheWrapper( &sessionscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "sessions cancel": commandFactoryWrapper(ui, + "sessions cancel": clientCacheWrapper( &sessionscmd.Command{ Command: base.NewCommand(ui), Func: "cancel", @@ -929,17 +927,17 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "session-recordings read": commandFactoryWrapper(ui, + "session-recordings read": clientCacheWrapper( &sessionrecordingscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "session-recordings list": commandFactoryWrapper(ui, + "session-recordings list": clientCacheWrapper( &sessionrecordingscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "session-recordings download": commandFactoryWrapper(ui, + "session-recordings download": clientCacheWrapper( &sessionrecordingscmd.DownloadCommand{ Command: base.NewCommand(ui), }), @@ -949,27 +947,27 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "storage-buckets read": commandFactoryWrapper(ui, + "storage-buckets read": clientCacheWrapper( &storagebucketscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "storage-buckets delete": commandFactoryWrapper(ui, + "storage-buckets delete": clientCacheWrapper( &storagebucketscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "storage-buckets list": commandFactoryWrapper(ui, + "storage-buckets list": clientCacheWrapper( &storagebucketscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "storage-buckets create": commandFactoryWrapper(ui, + "storage-buckets create": clientCacheWrapper( &storagebucketscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "storage-buckets update": commandFactoryWrapper(ui, + "storage-buckets update": clientCacheWrapper( &storagebucketscmd.Command{ Command: base.NewCommand(ui), Func: "update", @@ -980,82 +978,82 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "targets authorize-session": commandFactoryWrapper(ui, + "targets authorize-session": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "authorize-session", }), - "targets read": commandFactoryWrapper(ui, + "targets read": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "targets delete": commandFactoryWrapper(ui, + "targets delete": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "targets list": commandFactoryWrapper(ui, + "targets list": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "targets create": commandFactoryWrapper(ui, + "targets create": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "targets create tcp": commandFactoryWrapper(ui, + "targets create tcp": clientCacheWrapper( &targetscmd.TcpCommand{ Command: base.NewCommand(ui), Func: "create", }), - "targets create ssh": commandFactoryWrapper(ui, + "targets create ssh": clientCacheWrapper( &targetscmd.SshCommand{ Command: base.NewCommand(ui), Func: "create", }), - "targets update": commandFactoryWrapper(ui, + "targets update": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "targets update tcp": commandFactoryWrapper(ui, + "targets update tcp": clientCacheWrapper( &targetscmd.TcpCommand{ Command: base.NewCommand(ui), Func: "update", }), - "targets update ssh": commandFactoryWrapper(ui, + "targets update ssh": clientCacheWrapper( &targetscmd.SshCommand{ Command: base.NewCommand(ui), Func: "update", }), - "targets add-host-sources": commandFactoryWrapper(ui, + "targets add-host-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "add-host-sources", }), - "targets remove-host-sources": commandFactoryWrapper(ui, + "targets remove-host-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "remove-host-sources", }), - "targets set-host-sources": commandFactoryWrapper(ui, + "targets set-host-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "set-host-sources", }), - "targets add-credential-sources": commandFactoryWrapper(ui, + "targets add-credential-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "add-credential-sources", }), - "targets remove-credential-sources": commandFactoryWrapper(ui, + "targets remove-credential-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "remove-credential-sources", }), - "targets set-credential-sources": commandFactoryWrapper(ui, + "targets set-credential-sources": clientCacheWrapper( &targetscmd.Command{ Command: base.NewCommand(ui), Func: "set-credential-sources", @@ -1073,42 +1071,42 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "users create": commandFactoryWrapper(ui, + "users create": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "users read": commandFactoryWrapper(ui, + "users read": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "users update": commandFactoryWrapper(ui, + "users update": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "users delete": commandFactoryWrapper(ui, + "users delete": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "users list": commandFactoryWrapper(ui, + "users list": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "users add-accounts": commandFactoryWrapper(ui, + "users add-accounts": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "add-accounts", }), - "users set-accounts": commandFactoryWrapper(ui, + "users set-accounts": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "set-accounts", }), - "users remove-accounts": commandFactoryWrapper(ui, + "users remove-accounts": clientCacheWrapper( &userscmd.Command{ Command: base.NewCommand(ui), Func: "remove-accounts", @@ -1119,66 +1117,66 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { Command: base.NewCommand(ui, opts...), }, nil }, - "workers create": commandFactoryWrapper(ui, + "workers create": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "create", }), - "workers create worker-led": commandFactoryWrapper(ui, + "workers create worker-led": clientCacheWrapper( &workerscmd.WorkerLedCommand{ Command: base.NewCommand(ui), Func: "create", }), - "workers create controller-led": commandFactoryWrapper(ui, + "workers create controller-led": clientCacheWrapper( &workerscmd.ControllerLedCommand{ Command: base.NewCommand(ui), Func: "create", }), - "workers read": commandFactoryWrapper(ui, + "workers read": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "read", }), - "workers update": commandFactoryWrapper(ui, + "workers update": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "update", }), - "workers delete": commandFactoryWrapper(ui, + "workers delete": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "delete", }), - "workers list": commandFactoryWrapper(ui, + "workers list": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "list", }), - "workers add-worker-tags": commandFactoryWrapper(ui, + "workers add-worker-tags": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "add-worker-tags", }), - "workers set-worker-tags": commandFactoryWrapper(ui, + "workers set-worker-tags": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "set-worker-tags", }), - "workers remove-worker-tags": commandFactoryWrapper(ui, + "workers remove-worker-tags": clientCacheWrapper( &workerscmd.Command{ Command: base.NewCommand(ui), Func: "remove-worker-tags", }), - "workers certificate-authority": commandFactoryWrapper(ui, + "workers certificate-authority": clientCacheWrapper( &workerscmd.WorkerCACommand{ Command: base.NewCommand(ui), }), - "workers certificate-authority read": commandFactoryWrapper(ui, + "workers certificate-authority read": clientCacheWrapper( &workerscmd.WorkerCACommand{ Command: base.NewCommand(ui), Func: "read", }), - "workers certificate-authority reinitialize": commandFactoryWrapper(ui, + "workers certificate-authority reinitialize": clientCacheWrapper( &workerscmd.WorkerCACommand{ Command: base.NewCommand(ui), Func: "reinitialize", @@ -1194,20 +1192,15 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { var extraCommandsFuncs []func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) -type clientAndTokenProvider interface { - Client(opt ...base.Option) (*api.Client, error) - DiscoverKeyringTokenInfo() (string, string, error) - ReadTokenFromKeyring(keyringType, tokenName string) *authtokens.AuthToken -} - -type wrappableCommand interface { +// Keep this interface aligned with the interface at internal/clientcache/cmd/daemon/command_wrapper.go +type cacheEnabledCommand interface { cli.Command - clientAndTokenProvider + BaseCommand() *base.Command } -// commandFactoryWrapper wraps all short lived, non server, command factories. +// clientCacheWrapper wraps all short lived, non server, command factories. // The default func is a noop. -var commandFactoryWrapper = func(ui cli.Ui, c wrappableCommand) cli.CommandFactory { +var clientCacheWrapper = func(c cacheEnabledCommand) cli.CommandFactory { return func() (cli.Command, error) { return c, nil } diff --git a/internal/cmd/commands/authenticate/authenticate.go b/internal/cmd/commands/authenticate/authenticate.go index 9ca5f7c0b..cde6ab9a3 100644 --- a/internal/cmd/commands/authenticate/authenticate.go +++ b/internal/cmd/commands/authenticate/authenticate.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/cmd/base" - "github.com/hashicorp/boundary/internal/cmd/common" "github.com/hashicorp/boundary/internal/types/scope" "github.com/mitchellh/cli" "github.com/mitchellh/go-wordwrap" @@ -104,15 +103,18 @@ func (c *Command) Run(args []string) int { switch { case strings.HasPrefix(c.FlagAuthMethodId, globals.PasswordAuthMethodPrefix): - cmd := PasswordCommand{Command: c.Command, Opts: []common.Option{common.WithSkipScopeIdFlag(true)}} + cmd := PasswordCommand{Command: c.Command} + cmd.Opts = append(c.Opts, base.WithSkipScopeIdFlag(true)) cmd.Run([]string{}) case strings.HasPrefix(c.FlagAuthMethodId, globals.OidcAuthMethodPrefix): - cmd := OidcCommand{Command: c.Command, Opts: []common.Option{common.WithSkipScopeIdFlag(true)}} + cmd := OidcCommand{Command: c.Command} + cmd.Opts = append(c.Opts, base.WithSkipScopeIdFlag(true)) cmd.Run([]string{}) case strings.HasPrefix(c.FlagAuthMethodId, globals.LdapAuthMethodPrefix): - cmd := LdapCommand{Command: c.Command, Opts: []common.Option{common.WithSkipScopeIdFlag(true)}} + cmd := LdapCommand{Command: c.Command} + cmd.Opts = append(c.Opts, base.WithSkipScopeIdFlag(true)) cmd.Run([]string{}) default: diff --git a/internal/cmd/commands/authenticate/funcs.go b/internal/cmd/commands/authenticate/funcs.go index f7b6b28ca..6bfa7b58b 100644 --- a/internal/cmd/commands/authenticate/funcs.go +++ b/internal/cmd/commands/authenticate/funcs.go @@ -18,12 +18,13 @@ import ( zkeyring "github.com/zalando/go-keyring" ) -func saveAndOrPrintToken(c *base.Command, result *authmethods.AuthenticateResult) int { +func saveAndOrPrintToken(c *base.Command, result *authmethods.AuthenticateResult, opt ...base.Option) int { token := new(authtokens.AuthToken) if err := json.Unmarshal(result.GetRawAttributes(), token); err != nil { c.PrintCliError(fmt.Errorf("Error trying to decode response as an auth token: %w", err)) return base.CommandCliError } + opts := base.GetOpts(opt...) switch base.Format(c.UI) { case "table": @@ -101,6 +102,10 @@ func saveAndOrPrintToken(c *base.Command, result *authmethods.AuthenticateResult c.UI.Warn("\nStoring the token in a keyring was disabled. The token is:") c.UI.Output(token.Token) c.UI.Warn("Please be sure to store it safely!") + + if opts.WithInterceptedToken != nil { + *opts.WithInterceptedToken = token.Token + } } return base.CommandSuccess diff --git a/internal/cmd/commands/authenticate/ldap.go b/internal/cmd/commands/authenticate/ldap.go index e4694ea74..63b28bac3 100644 --- a/internal/cmd/commands/authenticate/ldap.go +++ b/internal/cmd/commands/authenticate/ldap.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/cmd/base" - "github.com/hashicorp/boundary/internal/cmd/common" "github.com/hashicorp/boundary/internal/types/scope" "github.com/hashicorp/go-secure-stdlib/parseutil" "github.com/hashicorp/go-secure-stdlib/password" @@ -32,8 +31,7 @@ type LdapCommand struct { flagLoginName string flagPassword string - Opts []common.Option - parsedOpts *common.Options + parsedOpts base.Options } func (c *LdapCommand) Synopsis() string { @@ -77,7 +75,7 @@ func (c *LdapCommand) Flags() *base.FlagSets { Usage: "The auth-method resource to use for the operation.", }) - if c.parsedOpts == nil || !c.parsedOpts.WithSkipScopeIdFlag { + if !c.parsedOpts.WithSkipScopeIdFlag { f.StringVar(&base.StringVar{ Name: "scope-id", EnvVar: "BOUNDARY_SCOPE_ID", @@ -98,12 +96,7 @@ func (c *LdapCommand) AutocompleteFlags() complete.Flags { } func (c *LdapCommand) Run(args []string) int { - opts, err := common.GetOpts(c.Opts...) - if err != nil { - c.PrintCliError(err) - return base.CommandCliError - } - c.parsedOpts = opts + c.parsedOpts = base.GetOpts(c.Opts...) f := c.Flags() if err := f.Parse(args); err != nil { @@ -193,5 +186,5 @@ func (c *LdapCommand) Run(args []string) int { return base.CommandCliError } - return saveAndOrPrintToken(c.Command, result) + return saveAndOrPrintToken(c.Command, result, c.Opts...) } diff --git a/internal/cmd/commands/authenticate/oidc.go b/internal/cmd/commands/authenticate/oidc.go index 75742f13f..89b1ed2a5 100644 --- a/internal/cmd/commands/authenticate/oidc.go +++ b/internal/cmd/commands/authenticate/oidc.go @@ -14,7 +14,6 @@ import ( "github.com/hashicorp/boundary/api" "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/internal/cmd/base" - "github.com/hashicorp/boundary/internal/cmd/common" "github.com/hashicorp/boundary/internal/types/scope" "github.com/hashicorp/cap/util" "github.com/mitchellh/cli" @@ -30,8 +29,7 @@ var ( type OidcCommand struct { *base.Command - Opts []common.Option - parsedOpts *common.Options + parsedOpts base.Options } func (c *OidcCommand) Synopsis() string { @@ -61,7 +59,7 @@ func (c *OidcCommand) Flags() *base.FlagSets { Usage: "The auth-method resource to use for the operation", }) - if c.parsedOpts == nil || !c.parsedOpts.WithSkipScopeIdFlag { + if !c.parsedOpts.WithSkipScopeIdFlag { f.StringVar(&base.StringVar{ Name: "scope-id", EnvVar: "BOUNDARY_SCOPE_ID", @@ -81,13 +79,7 @@ func (c *OidcCommand) AutocompleteFlags() complete.Flags { } func (c *OidcCommand) Run(args []string) int { - opts, err := common.GetOpts(c.Opts...) - if err != nil { - c.PrintCliError(err) - return base.CommandCliError - } - c.parsedOpts = opts - + c.parsedOpts = base.GetOpts(c.Opts...) f := c.Flags() if err := f.Parse(args); err != nil { @@ -196,5 +188,5 @@ func (c *OidcCommand) Run(args []string) int { return base.CommandCliError } - return saveAndOrPrintToken(c.Command, result) + return saveAndOrPrintToken(c.Command, result, c.Opts...) } diff --git a/internal/cmd/commands/authenticate/password.go b/internal/cmd/commands/authenticate/password.go index af9eb247f..bc0ad8bc2 100644 --- a/internal/cmd/commands/authenticate/password.go +++ b/internal/cmd/commands/authenticate/password.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/cmd/base" - "github.com/hashicorp/boundary/internal/cmd/common" "github.com/hashicorp/go-secure-stdlib/parseutil" "github.com/hashicorp/go-secure-stdlib/password" "github.com/mitchellh/cli" @@ -37,8 +36,7 @@ type PasswordCommand struct { flagLoginName string flagPassword string - Opts []common.Option - parsedOpts *common.Options + parsedOpts base.Options } func (c *PasswordCommand) Synopsis() string { @@ -82,7 +80,7 @@ func (c *PasswordCommand) Flags() *base.FlagSets { Usage: "The auth-method resource to use for the operation.", }) - if c.parsedOpts == nil || !c.parsedOpts.WithSkipScopeIdFlag { + if !c.parsedOpts.WithSkipScopeIdFlag { f.StringVar(&base.StringVar{ Name: "scope-id", EnvVar: "BOUNDARY_SCOPE_ID", @@ -103,12 +101,7 @@ func (c *PasswordCommand) AutocompleteFlags() complete.Flags { } func (c *PasswordCommand) Run(args []string) int { - opts, err := common.GetOpts(c.Opts...) - if err != nil { - c.PrintCliError(err) - return base.CommandCliError - } - c.parsedOpts = opts + c.parsedOpts = base.GetOpts(c.Opts...) f := c.Flags() @@ -199,5 +192,5 @@ func (c *PasswordCommand) Run(args []string) int { return base.CommandCliError } - return saveAndOrPrintToken(c.Command, result) + return saveAndOrPrintToken(c.Command, result, c.Opts...) } diff --git a/internal/cmd/common/option.go b/internal/cmd/common/option.go deleted file mode 100644 index 9a8c24a17..000000000 --- a/internal/cmd/common/option.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package common - -// GetOpts - iterate the inbound Options and return a struct. -func GetOpts(opt ...Option) (*Options, error) { - opts := getDefaultOptions() - for _, o := range opt { - if o == nil { - continue - } - if err := o(opts); err != nil { - return nil, err - } - } - return opts, nil -} - -// Option - how Options are passed as arguments. -type Option func(*Options) error - -// Options - how Options are represented. -type Options struct { - WithSkipScopeIdFlag bool -} - -func getDefaultOptions() *Options { - return &Options{} -} - -// WithSkipScopeIdFlag tells a command to not create a scope ID flag (usually -// because it's already been defined) -func WithSkipScopeIdFlag(with bool) Option { - return func(o *Options) error { - o.WithSkipScopeIdFlag = with - return nil - } -} diff --git a/internal/cmd/common/option_test.go b/internal/cmd/common/option_test.go deleted file mode 100644 index 24ac4c4be..000000000 --- a/internal/cmd/common/option_test.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package common - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -// Test_GetOpts provides unit tests for GetOpts and all the options -func Test_GetOpts(t *testing.T) { - t.Parallel() - t.Run("nil-options", func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - opts, err := GetOpts(nil, nil) - require.NoError(err) - testOpts := getDefaultOptions() - assert.Equal(opts, testOpts) - }) - - t.Run("WithSkipScopeIdFlag", func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - opts, err := GetOpts(WithSkipScopeIdFlag(true)) - require.NoError(err) - testOpts := getDefaultOptions() - testOpts.WithSkipScopeIdFlag = true - assert.Equal(opts, testOpts) - }) -} diff --git a/internal/cmd/search_cmd_darwin.go b/internal/cmd/search_cmd_darwin.go index 8ed0bd8bd..24d5bcc08 100644 --- a/internal/cmd/search_cmd_darwin.go +++ b/internal/cmd/search_cmd_darwin.go @@ -11,7 +11,7 @@ import ( ) func init() { - commandFactoryWrapper = daemonWrap + clientCacheWrapper = daemonWrap extraCommandsFuncs = append(extraCommandsFuncs, func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { delete(Commands, "daemon") @@ -38,8 +38,8 @@ func init() { }) } -// daemonWrap wraps the provided wrappableCommand with a daemon writer which +// daemonWrap wraps the provided cacheEnabledCommand with a daemon writer which // conditionally starts the client side daemon after the command completes -func daemonWrap(ui cli.Ui, c wrappableCommand) cli.CommandFactory { - return daemon.Wrap(ui, c) +func daemonWrap(c cacheEnabledCommand) cli.CommandFactory { + return daemon.Wrap(c) } diff --git a/internal/cmd/search_cmd_freebsd.go b/internal/cmd/search_cmd_freebsd.go index 32a85adf2..707bd8291 100644 --- a/internal/cmd/search_cmd_freebsd.go +++ b/internal/cmd/search_cmd_freebsd.go @@ -13,7 +13,7 @@ import ( ) func init() { - commandFactoryWrapper = daemonWrap + clientCacheWrapper = daemonWrap extraCommandsFuncs = append(extraCommandsFuncs, func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { delete(Commands, "daemon") @@ -40,8 +40,8 @@ func init() { }) } -// daemonWrap wraps the provided wrappableCommand with a daemon writer which +// daemonWrap wraps the provided cacheEnabledCommand with a daemon writer which // conditionally starts the client side daemon after the command completes -func daemonWrap(ui cli.Ui, c wrappableCommand) cli.CommandFactory { - return daemon.Wrap(ui, c) +func daemonWrap(c cacheEnabledCommand) cli.CommandFactory { + return daemon.Wrap(c) } diff --git a/internal/cmd/search_cmd_linux.go b/internal/cmd/search_cmd_linux.go index 8ed0bd8bd..24d5bcc08 100644 --- a/internal/cmd/search_cmd_linux.go +++ b/internal/cmd/search_cmd_linux.go @@ -11,7 +11,7 @@ import ( ) func init() { - commandFactoryWrapper = daemonWrap + clientCacheWrapper = daemonWrap extraCommandsFuncs = append(extraCommandsFuncs, func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { delete(Commands, "daemon") @@ -38,8 +38,8 @@ func init() { }) } -// daemonWrap wraps the provided wrappableCommand with a daemon writer which +// daemonWrap wraps the provided cacheEnabledCommand with a daemon writer which // conditionally starts the client side daemon after the command completes -func daemonWrap(ui cli.Ui, c wrappableCommand) cli.CommandFactory { - return daemon.Wrap(ui, c) +func daemonWrap(c cacheEnabledCommand) cli.CommandFactory { + return daemon.Wrap(c) } diff --git a/internal/cmd/search_cmd_windows.go b/internal/cmd/search_cmd_windows.go index 32a85adf2..707bd8291 100644 --- a/internal/cmd/search_cmd_windows.go +++ b/internal/cmd/search_cmd_windows.go @@ -13,7 +13,7 @@ import ( ) func init() { - commandFactoryWrapper = daemonWrap + clientCacheWrapper = daemonWrap extraCommandsFuncs = append(extraCommandsFuncs, func(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { delete(Commands, "daemon") @@ -40,8 +40,8 @@ func init() { }) } -// daemonWrap wraps the provided wrappableCommand with a daemon writer which +// daemonWrap wraps the provided cacheEnabledCommand with a daemon writer which // conditionally starts the client side daemon after the command completes -func daemonWrap(ui cli.Ui, c wrappableCommand) cli.CommandFactory { - return daemon.Wrap(ui, c) +func daemonWrap(c cacheEnabledCommand) cli.CommandFactory { + return daemon.Wrap(c) }