Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(clientagentcmd): plumb thru env var #5123

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions internal/cmd/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ 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"
envSkipCacheDaemon = "BOUNDARY_SKIP_CACHE_DAEMON"
envSkipClientAgent = "BOUNDARY_SKIP_CLIENT_AGENT"
EnvClientAgentPort = "BOUNDARY_CLIENT_AGENT_LISTENING_PORT"
envToken = "BOUNDARY_TOKEN"
EnvTokenName = "BOUNDARY_TOKEN_NAME"
EnvKeyringType = "BOUNDARY_KEYRING_TYPE"
envRecoveryConfig = "BOUNDARY_RECOVERY_CONFIG"
envSkipCacheDaemon = "BOUNDARY_SKIP_CACHE_DAEMON"
envSkipClientAgent = "BOUNDARY_SKIP_CLIENT_AGENT"
EnvClientAgentPort = "BOUNDARY_CLIENT_AGENT_LISTENING_PORT"
EnvBoundaryClientAgentCliErrorOutput = "BOUNDARY_CLIENT_AGENT_CLI_ERROR_OUTPUT"

StoredTokenName = "HashiCorp Boundary Auth Token"
)
Expand Down Expand Up @@ -107,14 +108,15 @@ type Command struct {
flagTLSServerName string
flagTLSInsecure bool

flagFormat string
FlagToken string
FlagTokenName string
FlagKeyringType string
FlagRecoveryConfig string
FlagOutputCurlString bool
FlagSkipCacheDaemon bool
FlagSkipClientAgent bool
flagFormat string
FlagToken string
FlagTokenName string
FlagKeyringType string
FlagRecoveryConfig string
FlagOutputCurlString bool
FlagSkipCacheDaemon bool
FlagSkipClientAgent bool
FlagOutputClientAgentCliError bool

FlagClientAgentPort uint16

Expand Down Expand Up @@ -491,6 +493,14 @@ func (c *Command) FlagSet(bit FlagSetBit) *FlagSets {
Usage: "Skips starting the caching daemon or sending the current used/retrieved token to the caching daemon.",
})

f.BoolVar(&BoolVar{
Name: "output-client-agent-cli-error",
Target: &c.FlagOutputClientAgentCliError,
Default: false,
EnvVar: EnvBoundaryClientAgentCliErrorOutput,
Usage: "Enables outputting CLI errors encountered for client-agent callbacks.",
})

f.BoolVar(&BoolVar{
Name: "skip-client-agent",
Target: &c.FlagSkipClientAgent,
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/commands/clientagentcmd/addtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func addToken(ctx context.Context, apiClient *api.Client, port uint16) (*api.Res
client.RetryWaitMin = 100 * time.Millisecond
client.RetryWaitMax = 1500 * time.Millisecond

// Explicitly setting this to 1, since this runs after every command and we don't want any delays
client.RetryMax = 1
// Explicitly setting this to 0, since this runs after every command and we don't want any delays
client.RetryMax = 0

req, err := retryablehttp.NewRequestWithContext(ctx, "POST", clientAgentUrl(port, "v1/tokens"),
retryablehttp.ReaderFunc(func() (io.Reader, error) {
Expand Down
19 changes: 3 additions & 16 deletions internal/cmd/commands/clientagentcmd/wrapper_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ package clientagentcmd

import (
"context"
"os"
"strings"

"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/wrapper"
)

var allowErrorOutput = false

const EnvBoundaryClientAgentCliErrorOutput = "BOUNDARY_CLIENT_AGENT_CLI_ERROR_OUTPUT"

func init() {
errOutput := os.Getenv(EnvBoundaryClientAgentCliErrorOutput)
if strings.ToLower(errOutput) == "true" {
allowErrorOutput = true
}
}

func init() {
if err := wrapper.RegisterSuccessfulCommandCallback("client-agent", hook); err != nil {
panic(err)
Expand All @@ -34,18 +21,18 @@ func hook(ctx context.Context, baseCmd *base.Command, token string) {
return
}
client, err := baseCmd.Client()
if err != nil && allowErrorOutput {
if err != nil && baseCmd.FlagOutputClientAgentCliError {
baseCmd.PrintCliError(err)
return
}
if token != "" {
client.SetToken(token)
}
_, apiErr, err := addToken(ctx, client, baseCmd.FlagClientAgentPort)
if err != nil && allowErrorOutput {
if err != nil && baseCmd.FlagOutputClientAgentCliError {
baseCmd.PrintCliError(err)
}
if apiErr != nil && allowErrorOutput {
if apiErr != nil && baseCmd.FlagOutputClientAgentCliError {
baseCmd.PrintApiError(apiErr, "sending token to client agent in the background")
}
}