diff --git a/README.md b/README.md index 975421b9..92b25138 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/cmd/spiffe-helper/main.go b/cmd/spiffe-helper/main.go index 0ea6eef0..0373d3d4 100644 --- a/cmd/spiffe-helper/main.go +++ b/cmd/spiffe-helper/main.go @@ -17,12 +17,13 @@ func main() { // 2. Run Sidecar's Daemon configFile := flag.String("config", "helper.conf", " 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) } @@ -30,11 +31,11 @@ func main() { 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) } diff --git a/pkg/sidecar/sidecar.go b/pkg/sidecar/sidecar.go index 67bd5e91..0e0b4cc8 100644 --- a/pkg/sidecar/sidecar.go +++ b/pkg/sidecar/sidecar.go @@ -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) @@ -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), diff --git a/pkg/sidecar/sidecar_test.go b/pkg/sidecar/sidecar_test.go index cf4d382f..3f4edf89 100644 --- a/pkg/sidecar/sidecar_test.go +++ b/pkg/sidecar/sidecar_test.go @@ -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") } @@ -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") }