Skip to content

Commit

Permalink
load key-file from dolores.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamvernekar committed Nov 4, 2023
1 parent a7d7442 commit a3991a0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
8 changes: 2 additions & 6 deletions cmd/dolores/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/dolores/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ var (

type CtxKey string

var (
EnvKey CtxKey = "ctx_environment"
)
var EnvKey CtxKey = "ctx_environment"

var (
HomePath = os.Getenv("HOME")
Expand Down
2 changes: 1 addition & 1 deletion secrets/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down

0 comments on commit a3991a0

Please sign in to comment.