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

agent: add -exit-after-auth flag #7920

Merged
merged 3 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 21 additions & 4 deletions command/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ type AgentCommand struct {

startedCh chan (struct{}) // for tests

flagConfigs []string
flagLogLevel string
flagConfigs []string
flagLogLevel string
flagExitAfterAuth bool

flagTestVerifyOnly bool
flagCombineLogs bool
Expand Down Expand Up @@ -115,6 +116,15 @@ func (c *AgentCommand) Flags() *FlagSets {
"\"trace\", \"debug\", \"info\", \"warn\", and \"err\".",
})

f.BoolVar(&BoolVar{
Name: "exit-after-auth",
Target: &c.flagExitAfterAuth,
Default: false,
Usage: "If set to true, the agent will exit with code 0 after a single " +
"successful auth, where success means that a token was retrieved and " +
"all sinks successfully wrote it",
})

// Internal-only flags to follow.
//
// Why hello there little source code reader! Welcome to the Vault source
Expand Down Expand Up @@ -223,6 +233,13 @@ func (c *AgentCommand) Run(args []string) int {
config.Vault = new(agentConfig.Vault)
}

exitAfterAuth := config.ExitAfterAuth
f.Visit(func(fl *flag.Flag) {
if fl.Name == "exit-after-auth" {
exitAfterAuth = c.flagExitAfterAuth
}
})

c.setStringFlag(f, config.Vault.Address, &StringVar{
Name: flagNameAddress,
Target: &c.flagAddress,
Expand Down Expand Up @@ -524,7 +541,7 @@ func (c *AgentCommand) Run(args []string) int {
ss := sink.NewSinkServer(&sink.SinkServerConfig{
Logger: c.logger.Named("sink.server"),
Client: client,
ExitAfterAuth: config.ExitAfterAuth,
ExitAfterAuth: exitAfterAuth,
})
ssDoneCh = ss.DoneCh

Expand All @@ -534,7 +551,7 @@ func (c *AgentCommand) Run(args []string) int {
LogWriter: c.logWriter,
VaultConf: config.Vault,
Namespace: namespace,
ExitAfterAuth: config.ExitAfterAuth,
ExitAfterAuth: exitAfterAuth,
})
tsDoneCh = ts.DoneCh

Expand Down
26 changes: 23 additions & 3 deletions command/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ cache {
*/

func TestAgent_ExitAfterAuth(t *testing.T) {
t.Run("via_config", func(t *testing.T) {
testAgentExitAfterAuth(t, false)
})

t.Run("via_flag", func(t *testing.T) {
testAgentExitAfterAuth(t, true)
})
}

func testAgentExitAfterAuth(t *testing.T, viaFlag bool) {
logger := logging.NewVaultLogger(hclog.Trace)
coreConfig := &vault.CoreConfig{
Logger: logger,
Expand Down Expand Up @@ -313,8 +323,13 @@ func TestAgent_ExitAfterAuth(t *testing.T) {
logger.Trace("wrote test jwt", "path", in)
}

exitAfterAuthTemplText := "exit_after_auth = true"
if viaFlag {
exitAfterAuthTemplText = ""
}

config := `
exit_after_auth = true
%s

auto_auth {
method {
Expand All @@ -340,7 +355,7 @@ auto_auth {
}
`

config = fmt.Sprintf(config, in, sink1, sink2)
config = fmt.Sprintf(config, exitAfterAuthTemplText, in, sink1, sink2)
if err := ioutil.WriteFile(conf, []byte(config), 0600); err != nil {
t.Fatal(err)
} else {
Expand All @@ -352,7 +367,12 @@ auto_auth {
ui, cmd := testAgentCommand(t, logger)
cmd.client = client

code := cmd.Run([]string{"-config", conf})
args := []string{"-config", conf}
if viaFlag {
args = append(args, "-exit-after-auth")
}

code := cmd.Run(args)
if code != 0 {
t.Errorf("expected %d to be %d", code, 0)
t.Logf("output from agent:\n%s", ui.OutputWriter.String())
Expand Down
1 change: 1 addition & 0 deletions sdk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107 // indirect
google.golang.org/grpc v1.22.0
)