From 8e10f2f2266c705844da4ea4c3096a80236d2a08 Mon Sep 17 00:00:00 2001 From: Kevin Fox Date: Sat, 6 Jan 2024 08:50:08 -0800 Subject: [PATCH 1/3] 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 --- README.md | 2 ++ cmd/spiffe-helper/main.go | 7 ++++--- pkg/sidecar/sidecar.go | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d4f6cdc..34f7d5dc 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 54d7b70f..dc406204 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), From d215134febd5bbab5daab938c17c07332cff9859 Mon Sep 17 00:00:00 2001 From: Kevin Fox Date: Sat, 6 Jan 2024 08:55:48 -0800 Subject: [PATCH 2/3] Fix test Signed-off-by: Kevin Fox --- pkg/sidecar/sidecar_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/sidecar/sidecar_test.go b/pkg/sidecar/sidecar_test.go index cf4d382f..7e778801 100644 --- a/pkg/sidecar/sidecar_test.go +++ b/pkg/sidecar/sidecar_test.go @@ -194,14 +194,14 @@ 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 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 +210,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") } From 870a21aa4c47f7f39347a48c6e1e5ae12f3a58d0 Mon Sep 17 00:00:00 2001 From: Kevin Fox Date: Wed, 10 Jan 2024 09:16:05 -0800 Subject: [PATCH 3/3] Add test Signed-off-by: Kevin Fox --- pkg/sidecar/sidecar_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/sidecar/sidecar_test.go b/pkg/sidecar/sidecar_test.go index 7e778801..3f4edf89 100644 --- a/pkg/sidecar/sidecar_test.go +++ b/pkg/sidecar/sidecar_test.go @@ -198,6 +198,14 @@ func TestDefaultAgentAddress(t *testing.T) { 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()