Skip to content

Commit

Permalink
Add exitOnWait flag (#126)
Browse files Browse the repository at this point in the history
* Add exitOnWait flag

This enables the exitOnWait behavior from the commandline so most
users don't need to define two different config files.

Signed-off-by: Kevin Fox <[email protected]>
  • Loading branch information
kfox1111 authored Jan 16, 2024
1 parent 3f6ea7f commit a2f5caf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The SPIFFE Helper is a simple utility for fetching X.509 SVID certificates from

If `-config` is not specified, the default value `helper.conf` is assumed.

The flag `-exitWhenReady` is also supported.

## Configuration
The configuration file is an [HCL](https://github.com/hashicorp/hcl) formatted file that defines the following configurations:

Expand Down
7 changes: 4 additions & 3 deletions cmd/spiffe-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ func main() {
// 2. Run Sidecar's Daemon

configFile := flag.String("config", "helper.conf", "<configFile> Configuration file path")
exitWhenReady := flag.Bool("exitWhenReady", false, "Exit once the requested objects are retrieved")
flag.Parse()

log := logrus.WithField("system", "spiffe-helper")
log.Infof("Using configuration file: %q\n", *configFile)

if err := startSidecar(*configFile, log); err != nil {
if err := startSidecar(*configFile, *exitWhenReady, log); err != nil {
log.WithError(err).Error("Exiting due this error")
os.Exit(1)
}

log.Infof("Exiting")
}

func startSidecar(configPath string, log logrus.FieldLogger) error {
func startSidecar(configPath string, exitWhenReady bool, log logrus.FieldLogger) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

spiffeSidecar, err := sidecar.New(configPath, log)
spiffeSidecar, err := sidecar.New(configPath, exitWhenReady, log)
if err != nil {
return fmt.Errorf("Failed to create sidecar: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/sidecar/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Sidecar struct {
}

// New creates a new SPIFFE sidecar
func New(configPath string, log logrus.FieldLogger) (*Sidecar, error) {
func New(configPath string, exitWhenReady bool, log logrus.FieldLogger) (*Sidecar, error) {
config, err := ParseConfig(configPath)
if err != nil {
return nil, fmt.Errorf("failed to parse %q: %w", configPath, err)
Expand All @@ -68,6 +68,8 @@ func New(configPath string, log logrus.FieldLogger) (*Sidecar, error) {
config.Log.Warn("No cmd defined to execute.")
}

config.ExitWhenReady = config.ExitWhenReady || exitWhenReady

return &Sidecar{
config: config,
certReadyChan: make(chan struct{}, 1),
Expand Down
14 changes: 11 additions & 3 deletions pkg/sidecar/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,22 @@ func TestSidecar_RunDaemon(t *testing.T) {

func TestDefaultAgentAddress(t *testing.T) {
log, _ := test.NewNullLogger()
spiffeSidecar, err := New("../../test/sidecar/config/helper.conf", log)
spiffeSidecar, err := New("../../test/sidecar/config/helper.conf", false, log)
require.NoError(t, err)
assert.Equal(t, spiffeSidecar.config.AgentAddress, "/tmp/spire-agent/public/api.sock")
}

func TestExitOnWaitFlag(t *testing.T) {
log, _ := test.NewNullLogger()
spiffeSidecar, err := New("../../test/sidecar/config/helper.conf", true, log)
require.NoError(t, err)
assert.Equal(t, spiffeSidecar.config.ExitWhenReady, true)
}

func TestEnvAgentAddress(t *testing.T) {
os.Setenv("SPIRE_AGENT_ADDRESS", "/tmp/spire-agent/public/api.sock")
log, _ := test.NewNullLogger()
spiffeSidecar, err := New("../../test/sidecar/config/helper.conf", log)
spiffeSidecar, err := New("../../test/sidecar/config/helper.conf", false, log)
require.NoError(t, err)
assert.Equal(t, spiffeSidecar.config.AgentAddress, "/tmp/spire-agent/public/api.sock")
}
Expand All @@ -210,7 +218,7 @@ func TestAgentAddress(t *testing.T) {
// This test is used to verify that we get the agent_address of the .conf file instead of the ENV value, if we have both
os.Setenv("SPIRE_AGENT_ADDRESS", "/tmp/spire-agent/public/api.sock")
log, _ := test.NewNullLogger()
spiffeSidecar, err := New("../../test/sidecar/configWithAddress/helper.conf", log)
spiffeSidecar, err := New("../../test/sidecar/configWithAddress/helper.conf", false, log)
require.NoError(t, err)
assert.Equal(t, spiffeSidecar.config.AgentAddress, "/tmp/spire-agent/public/api.sock")
}
Expand Down

0 comments on commit a2f5caf

Please sign in to comment.