Skip to content

Commit

Permalink
Improve gpg binary detection and error handling (#522)
Browse files Browse the repository at this point in the history
Fixes #521
  • Loading branch information
dominikschulz authored Dec 15, 2017
1 parent 36b3fd4 commit 1e713cd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
8 changes: 7 additions & 1 deletion action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
gpgcli "github.com/justwatchcom/gopass/backend/gpg/cli"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store/root"
"github.com/justwatchcom/gopass/utils/out"
)

type gpger interface {
Expand Down Expand Up @@ -58,7 +59,7 @@ func New(ctx context.Context, cfg *config.Config, sv semver.Version) (*Action, e
Args: gpgOpts(),
})
if err != nil {
return nil, err
out.Red(ctx, "Warning: GPG not found: %s", err)
}

store, err := root.New(ctx, cfg, act.gpg)
Expand Down Expand Up @@ -94,3 +95,8 @@ func gpgOpts() []string {
func (s *Action) String() string {
return s.Store.String()
}

// HasGPG returns true if the GPG wrapper is initialized
func (s *Action) HasGPG() bool {
return s.gpg != nil
}
3 changes: 3 additions & 0 deletions action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
// Initialized returns an error if the store is not properly
// prepared.
func (s *Action) Initialized(ctx context.Context, c *cli.Context) error {
if s.gpg.Binary() == "" {
return s.exitError(ctx, ExitGPG, nil, "gpg not found but required")
}
if s.gpg.Version(ctx).LT(semver.Version{Major: 2, Minor: 0, Patch: 0}) {
out.Red(ctx, "Warning: Using GPG 1.x. Using GPG 2.0 or later is highly recommended")
}
Expand Down
11 changes: 10 additions & 1 deletion backend/gpg/cli/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/justwatchcom/gopass/backend/gpg"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -61,7 +62,12 @@ func New(cfg Config) (*GPG, error) {
binary: "gpg",
args: append(defaultArgs, cfg.Args...),
}
if err := g.detectBinary(cfg.Binary); err != nil {

ctx := context.Background()
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
ctx = ctxutil.WithDebug(ctx, true)
}
if err := g.detectBinary(ctx, cfg.Binary); err != nil {
return nil, err
}

Expand All @@ -70,6 +76,9 @@ func New(cfg Config) (*GPG, error) {

// Binary returns the GPG binary location
func (g *GPG) Binary() string {
if g == nil {
return ""
}
return g.binary
}

Expand Down
17 changes: 11 additions & 6 deletions backend/gpg/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/blang/semver"
"github.com/justwatchcom/gopass/utils/out"
)

type gpgBin struct {
Expand All @@ -33,7 +34,7 @@ func (v byVersion) Less(i, j int) bool {

// Version will returns GPG version information
func (g *GPG) Version(ctx context.Context) semver.Version {
return version(ctx, g.binary)
return version(ctx, g.Binary())
}

func version(ctx context.Context, binary string) semver.Version {
Expand All @@ -60,24 +61,28 @@ func version(ctx context.Context, binary string) semver.Version {
return v
}

func (g *GPG) detectBinary(bin string) error {
func (g *GPG) detectBinary(ctx context.Context, bin string) error {
bins, err := g.detectBinaryCandidates(bin)
if err != nil {
return err
}
bv := make(byVersion, 0, len(bins))
for _, b := range bins {
out.Debug(ctx, "gpg.detectBinary - Looking for '%s' ...", b)
if p, err := exec.LookPath(b); err == nil {
bv = append(bv, gpgBin{
gb := gpgBin{
path: p,
ver: version(context.Background(), p),
})
ver: version(ctx, p),
}
out.Debug(ctx, "gpg.detectBinary - Found '%s' at '%s' (%s)", b, p, gb.ver.String())
bv = append(bv, gb)
}
}
sort.Sort(bv)
if len(bv) < 1 {
return errors.New("no gpg binary found")
}
sort.Sort(bv)
g.binary = bv[len(bv)-1].path
out.Debug(ctx, "gpg.detectBinary - using '%s'", g.binary)
return nil
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ func main() {
Usage: "Initialize new password store.",
Description: "" +
"Initialize new password storage and use gpg-id for encryption.",
Before: func(c *cli.Context) error {
if !action.HasGPG() {
return fmt.Errorf("gpg not found")
}
return nil
},
Action: func(c *cli.Context) error {
return action.Init(withGlobalFlags(ctx, c), c)
},
Expand Down

0 comments on commit 1e713cd

Please sign in to comment.