From a3991a07d1b4b98dd2a546d14f830f1d66663ce2 Mon Sep 17 00:00:00 2001 From: shubhamvernekar Date: Sat, 4 Nov 2023 20:50:17 +0530 Subject: [PATCH] load key-file from dolores.json file --- cmd/dolores/config.go | 8 ++------ cmd/dolores/parse.go | 10 ++++++++++ config/config.go | 4 +--- secrets/manager.go | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/dolores/config.go b/cmd/dolores/config.go index c064459..5508a06 100644 --- a/cmd/dolores/config.go +++ b/cmd/dolores/config.go @@ -34,14 +34,13 @@ func NewConfig(client GetClient) *ConfigCommand { } func (c *ConfigCommand) editAction(ctx *cli.Context) error { - rcli := c.rcli(ctx.Context) env := ctx.String("environment") log := c.log.With().Str("cmd", "config.edit").Str("environment", env).Logger() dcfg, err := parseDecryptConfig(ctx) if err != nil { return err } - sec := secrets.NewSecretsManager(log, rcli) + sec := secrets.NewSecretsManager(log, c.rcli(ctx.Context)) cfg := secrets.EditConfig{DecryptConfig: dcfg} if err := sec.Edit(cfg); err != nil { log.Error().Msgf("error editing file: %v", err) @@ -65,16 +64,13 @@ func (c *ConfigCommand) encryptAction(ctx *cli.Context) error { } func (c *ConfigCommand) decryptAction(ctx *cli.Context) error { - rcli := c.rcli(ctx.Context) - - ctx.Set("key-file", rcli.GetKeyFile()) req, err := parseDecryptConfig(ctx) if err != nil { return err } log := c.log.With().Str("cmd", "config.dencrypt").Str("environment", req.Environment).Logger() log.Trace().Str("cmd", "config.decrypt").Msgf("running decryption") - sec := secrets.NewSecretsManager(log, rcli) + sec := secrets.NewSecretsManager(log, c.rcli(ctx.Context)) return sec.Decrypt(req) } diff --git a/cmd/dolores/parse.go b/cmd/dolores/parse.go index 13f9c28..ce0cd8f 100644 --- a/cmd/dolores/parse.go +++ b/cmd/dolores/parse.go @@ -15,11 +15,21 @@ func parseDecryptConfig(ctx *cli.Context) (secrets.DecryptConfig, error) { env := ctx.String("environment") name := ctx.String("name") 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) + } + keyFile = d.Environments[env].KeyFile + } + req := secrets.DecryptConfig{ Environment: env, Name: name, Out: os.Stdout, KeyFile: keyFile, + Key: ctx.String("key"), } if err := req.Valid(); err != nil { return secrets.DecryptConfig{}, fmt.Errorf("pass appropriate key or key-file to decrypt: %w", err) diff --git a/config/config.go b/config/config.go index 9e1c6be..ea81703 100644 --- a/config/config.go +++ b/config/config.go @@ -19,9 +19,7 @@ var ( type CtxKey string -var ( - EnvKey CtxKey = "ctx_environment" -) +var EnvKey CtxKey = "ctx_environment" var ( HomePath = os.Getenv("HOME") diff --git a/secrets/manager.go b/secrets/manager.go index 42d2645..51b9f8e 100644 --- a/secrets/manager.go +++ b/secrets/manager.go @@ -91,7 +91,7 @@ func (c DecryptConfig) Output() io.Writer { } func (c DecryptConfig) Valid() error { - if c.KeyFile == "" { + if c.KeyFile == "" && c.Key == "" { return ErrInvalidKeyFile } if c.Name == "" {