Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass --show-protected to keepassxc-cli version 2.5.2 or later #553

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions cmd/secretkeepassxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/internal/chezmoi"
"golang.org/x/crypto/ssh/terminal"
Expand All @@ -34,10 +35,12 @@ type keePassXCAttributeCacheKey struct {
}

var (
keePassXCCache = make(map[string]map[string]string)
keePassXCAttributeCache = make(map[keePassXCAttributeCacheKey]string)
keePassXCPairRegexp = regexp.MustCompile(`^([^:]+): (.*)$`)
keePassXCPassword string
keePassXCVersion *semver.Version
keePassXCCache = make(map[string]map[string]string)
keePassXCAttributeCache = make(map[keePassXCAttributeCacheKey]string)
keePassXCPairRegexp = regexp.MustCompile(`^([^:]+): (.*)$`)
keePassXCPassword string
keePassXCNeedShowProtectedArgVersion = semver.Version{Major: 2, Minor: 5, Patch: 1}
)

func init() {
Expand All @@ -52,6 +55,24 @@ func (c *Config) runKeePassXCCmd(cmd *cobra.Command, args []string) error {
return c.run("", c.KeePassXC.Command, args...)
}

func (c *Config) getKeePassXCVersion() *semver.Version {
if keePassXCVersion != nil {
return keePassXCVersion
}
name := c.KeePassXC.Command
args := []string{"--version"}
cmd := exec.Command(name, args...)
output, err := c.mutator.IdempotentCmdOutput(cmd)
if err != nil {
panic(fmt.Errorf("keepassxc: %s %s: %w", name, chezmoi.ShellQuoteArgs(args), err))
}
keePassXCVersion, err = semver.NewVersion(string(bytes.TrimSpace(output)))
if err != nil {
panic(fmt.Errorf("keepassxc: cannot parse version %q: %w", output, err))
}
return keePassXCVersion
}

func (c *Config) keePassXCFunc(entry string) map[string]string {
if data, ok := keePassXCCache[entry]; ok {
return data
Expand All @@ -61,6 +82,9 @@ func (c *Config) keePassXCFunc(entry string) map[string]string {
}
name := c.KeePassXC.Command
args := []string{"show"}
if c.getKeePassXCVersion().Compare(keePassXCNeedShowProtectedArgVersion) >= 0 {
args = append(args, "--show-protected")
}
args = append(args, c.KeePassXC.Args...)
args = append(args, c.KeePassXC.Database, entry)
output, err := c.runKeePassXCCLICommand(name, args)
Expand Down Expand Up @@ -88,6 +112,9 @@ func (c *Config) keePassXCAttributeFunc(entry, attribute string) string {
}
name := c.KeePassXC.Command
args := []string{"show", "--attributes", attribute, "--quiet"}
if c.getKeePassXCVersion().Compare(keePassXCNeedShowProtectedArgVersion) >= 0 {
args = append(args, "--show-protected")
}
args = append(args, c.KeePassXC.Args...)
args = append(args, c.KeePassXC.Database, entry)
output, err := c.runKeePassXCCLICommand(name, args)
Expand Down