Skip to content

Commit

Permalink
Add gopass fix to add YAML document separators
Browse files Browse the repository at this point in the history
Fixes #468
  • Loading branch information
dominikschulz committed Nov 18, 2017
1 parent 4802d7f commit 653fd58
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
70 changes: 70 additions & 0 deletions action/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package action

import (
"context"
"errors"
"strings"

"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)

func (s *Action) Fix(ctx context.Context, c *cli.Context) error {
if c.IsSet("force") {
ctx = sub.WithFsckForce(ctx, c.Bool("force"))
}
check := c.Bool("check")

if !s.AskForConfirmation(ctx, "Do you want to introduce YAML document separators to all secrets that can be parsed as valid YAML?") {
return errors.New("user aborted")
}

t, err := s.Store.Tree()
if err != nil {
return s.exitError(ctx, ExitList, err, "failed to list store: %s", err)
}

pwList := t.List(0)
num := 0

for _, name := range pwList {
sec, err := s.Store.Get(ctx, name)
if err != nil {
out.Red(ctx, "Failed to decode secret %s: %s", name, err)
continue
}
if sec.Data() != nil {
// valid YAML already
out.Debug(ctx, "%s - not fixing - valid YAML", name)
continue
}
body := sec.Body()
if strings.HasPrefix(body, "---\n") {
// invalid YAML, but doc-sep, avoid duplicating the doc-sep
out.Debug(ctx, "%s - not fixing - invalid YAML w/ doc-sep", name)
continue
}
if strings.Trim(body, "\n\r\t ") == "" {
// body contains only whitespaces
out.Debug(ctx, "%s - not fixing - only whitepaces", name)
continue
}
out.Debug(ctx, "Secret %s before fix: %s", name, sec.String())
if err := sec.SetBody("---\n" + body); err != nil {
out.Red(ctx, "Failed to add doc-sep to %s: %s", name, err)
continue
}
out.Debug(ctx, "Secret after fix: %s", sec.String())
num++
if check {
continue
}
if err := s.Store.Set(ctx, name, sec); err != nil {
out.Red(ctx, "Failed to save secret %s: %s", name, err)
}
}
out.Green(ctx, "Fixed %d secrets", num)

return nil
}
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,24 @@ func main() {
},
},
},
{
Name: "fix",
Usage: "Upgrade secrets",
Before: func(c *cli.Context) error { return action.Initialized(withGlobalFlags(ctx, c), c) },
Action: func(c *cli.Context) error {
return action.Fix(withGlobalFlags(ctx, c), c)
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "check, c",
Usage: "Only report",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Auto-correct any errors, do not ask",
},
},
},
{
Name: "fsck",
Usage: "Check inconsistencies (ALPHA)",
Expand Down

0 comments on commit 653fd58

Please sign in to comment.