Skip to content

Commit

Permalink
(clientagentcmd): plumb thru env var (#5123)
Browse files Browse the repository at this point in the history
* (clientagentcmd): plumb thru env var
  • Loading branch information
irenarindos authored Sep 20, 2024
1 parent fe129b4 commit eb33649
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
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")
}
}

0 comments on commit eb33649

Please sign in to comment.