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

Add support for YAML metadata parsing #114

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
50 changes: 34 additions & 16 deletions action/show.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package action

import (
"bytes"
"fmt"

"github.com/atotto/clipboard"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/qrcon"
"github.com/smallfish/simpleyaml"
"github.com/urfave/cli"
)

Expand All @@ -16,6 +16,7 @@ func (s *Action) Show(c *cli.Context) error {
clip := c.Bool("clip")
force := c.Bool("force")
qr := c.Bool("qr")
key := c.String("key")

if name == "" {
return fmt.Errorf("provide a secret name")
Expand All @@ -25,12 +26,42 @@ func (s *Action) Show(c *cli.Context) error {
return s.List(c)
}

if clip || qr {
content, err := s.Store.First(name)
content, err := s.Store.Get(name)
if err != nil {
return err
}

// load metadata if we want to parse the YAML (key is given) or we only want to display safe content (=metadata) and no clip/qr flag is given
if key != "" || (s.Store.SafeContent && !force && !(clip || qr)) {
content, err = s.Store.Metadata(name)
if err != nil {
return err
}
} else if clip || qr {
content, err = s.Store.First(name)
if err != nil {
return err
}
}

if key != "" {
yaml, err := simpleyaml.NewYaml(content)
if err != nil {
return fmt.Errorf("failed to load secret as yaml")
} else {
value, err := yaml.Get(key).String()
if err != nil {
keys, err := yaml.GetMapKeys()
if err == nil {
return fmt.Errorf("%s not available. Available keys are: %s\n", key, keys)
}
} else {
content = []byte(value)
}
}
}

if clip || qr {
if qr {
qr, err := qrcon.QRCode(string(content))
if err != nil {
Expand All @@ -42,19 +73,6 @@ func (s *Action) Show(c *cli.Context) error {
return s.copyToClipboard(name, content)
}

content, err := s.Store.Get(name)
if err != nil {
return err
}

if s.Store.SafeContent && !force {
lines := bytes.SplitN(content, []byte("\n"), 2)
if len(lines) < 2 || len(bytes.TrimSpace(lines[1])) == 0 {
return fmt.Errorf("no safe content to display, you can force display with show -f")
}
content = lines[1]
}

color.Yellow(string(content))

return nil
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ func main() {
Name: "force, f",
Usage: "Display the password even if safecontent is enabled",
},
cli.StringFlag{
Name: "key, k",
Usage: "Load secret metadata as YAML and display a single key",
},
},
},
{
Expand Down
13 changes: 13 additions & 0 deletions password/root_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ func (r *RootStore) First(name string) ([]byte, error) {
return bytes.TrimSpace(lines[0]), nil
}

// Metadata returns the content of a single entry except of the first line (the password)
func (r *RootStore) Metadata(name string) ([]byte, error) {
content, err := r.Get(name)
if err != nil {
return nil, err
}
lines := bytes.SplitN(content, []byte("\n"), 2)
if len(lines) < 2 || len(bytes.TrimSpace(lines[1])) == 0 {
return nil, fmt.Errorf("no metadata found")
}
return lines[1], nil
}

// Exists checks the existence of a single entry
func (r *RootStore) Exists(name string) (bool, error) {
store := r.getStore(name)
Expand Down