Skip to content

Commit

Permalink
Merge pull request #36 from hhollenstain/exit-early
Browse files Browse the repository at this point in the history
feat: exit-early flag when provider or secret not found
  • Loading branch information
alexei-led authored Sep 6, 2023
2 parents ff49794 + 3650278 commit faf0ad4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func main() {
Usage: "supported secrets manager provider ['aws', 'google']",
Value: "aws",
},
&cli.BoolFlag{
Name: "exit-early",
Usage: "exit when a provider fails or a secret is not found",
EnvVars: []string{"EXIT_EARLY"},
&cli.StringFlag{
Name: "google-project",
Usage: "the google cloud project for secrets without a project prefix",
Expand Down Expand Up @@ -137,11 +141,14 @@ func mainCmd(c *cli.Context) error {
}
if err != nil {
log.WithField("provider", c.String("provider")).WithError(err).Error("failed to initialize secrets provider")
if c.Bool("exit-early") {
os.Exit(1)
}
}

// Launch main command
var childPid int
childPid, err = run(ctx, provider, c.Args().Slice())
childPid, err = run(ctx, provider, c.Bool("exit-early"), c.Args().Slice())
if err != nil {
log.WithError(err).Error("failed to run")
os.Exit(1)
Expand Down Expand Up @@ -181,7 +188,7 @@ func removeZombies(childPid int) {
}

// run passed command
func run(ctx context.Context, provider secrets.Provider, commandSlice []string) (childPid int, err error) {
func run(ctx context.Context, provider secrets.Provider, exitEarly bool, commandSlice []string) (childPid int, err error) {
var commandStr string
var argsSlice []string

Expand Down Expand Up @@ -213,6 +220,10 @@ func run(ctx context.Context, provider secrets.Provider, commandSlice []string)
cmd.Env, err = provider.ResolveSecrets(ctx, os.Environ())
if err != nil {
log.WithError(err).Error("failed to resolve secrets")
if exitEarly {
log.Error("Exiting early unable to retrieve secrets")
os.Exit(1)
}
}
} else {
log.Warn("no secrets provider available; using environment without resolving secrets")
Expand Down

0 comments on commit faf0ad4

Please sign in to comment.