From 5a592be5cb3bbf329b9b4f81698ab82578cd68b3 Mon Sep 17 00:00:00 2001 From: shubhamvernekar Date: Sat, 4 Nov 2023 21:17:03 +0530 Subject: [PATCH] fix run command error --- cmd/dolores/parse.go | 31 +++++++++++++++++++++++-------- cmd/dolores/run.go | 3 +++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/dolores/parse.go b/cmd/dolores/parse.go index ce0cd8f..d3a5fb2 100644 --- a/cmd/dolores/parse.go +++ b/cmd/dolores/parse.go @@ -5,31 +5,46 @@ import ( "net/url" "os" + "github.com/rs/zerolog/log" "github.com/scalescape/dolores" "github.com/scalescape/dolores/config" "github.com/scalescape/dolores/secrets" "github.com/urfave/cli/v2" ) -func parseDecryptConfig(ctx *cli.Context) (secrets.DecryptConfig, error) { - env := ctx.String("environment") - name := ctx.String("name") +func parseKeyConfig(ctx *cli.Context, cfg *secrets.DecryptConfig) error { + log.Trace().Msgf("parsing configuration required to decrypt config") + key := ctx.String("key") keyFile := ctx.String("key-file") - if keyFile == "" { d, err := config.LoadFromDisk() if err != nil { - return secrets.DecryptConfig{}, fmt.Errorf("dolores not initialized yet: %w", err) + return fmt.Errorf("dolores not initialized yet: %w", err) } - keyFile = d.Environments[env].KeyFile + keyFile = d.Environments[cfg.Environment].KeyFile + } + if keyFile == "" { + keyFile = os.Getenv("DOLORES_SECRETS_KEY_FILE") + } + if key == "" { + key = os.Getenv("DOLORES_SECRETS_KEY") } + cfg.KeyFile = keyFile + cfg.Key = key + return nil +} + +func parseDecryptConfig(ctx *cli.Context) (secrets.DecryptConfig, error) { + env := ctx.String("environment") + name := ctx.String("name") req := secrets.DecryptConfig{ Environment: env, Name: name, Out: os.Stdout, - KeyFile: keyFile, - Key: ctx.String("key"), + } + if err := parseKeyConfig(ctx, &req); err != nil { + return secrets.DecryptConfig{}, fmt.Errorf("unable to load key-file from config: %w", err) } if err := req.Valid(); err != nil { return secrets.DecryptConfig{}, fmt.Errorf("pass appropriate key or key-file to decrypt: %w", err) diff --git a/cmd/dolores/run.go b/cmd/dolores/run.go index e4fd87c..8adeeee 100644 --- a/cmd/dolores/run.go +++ b/cmd/dolores/run.go @@ -155,6 +155,9 @@ func (c *Runner) parse(ctx *cli.Context) error { Environment: c.environment, Out: c.configBuffer, } + if err := parseKeyConfig(ctx, &c.DecryptConfig); err != nil { + return err + } if err := c.DecryptConfig.Valid(); err != nil { return err }