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

Add exitOnWait flag #126

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
MarcosDY marked this conversation as resolved.
Show resolved Hide resolved
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
Loading