Skip to content

Commit

Permalink
Implement config options per mount (#330)
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
dominikschulz committed Sep 15, 2017
1 parent 5463f2e commit a00560b
Show file tree
Hide file tree
Showing 28 changed files with 535 additions and 300 deletions.
2 changes: 1 addition & 1 deletion action/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *Action) binaryCopy(ctx context.Context, from, to string, deleteSource b
case fsutil.IsFile(from) && fsutil.IsFile(to):
// copying from on file to another file is not supported
return errors.New("ambiquity detected. Only from or to can be a file")
case s.Store.Exists(from) && s.Store.Exists(to):
case s.Store.Exists(ctx, from) && s.Store.Exists(ctx, to):
// copying from one secret to another secret is not supported
return errors.New("ambiquity detected. Either from or to must be a file")
case fsutil.IsFile(from) && !fsutil.IsFile(to):
Expand Down
54 changes: 42 additions & 12 deletions action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,49 @@ func (s *Action) Config(ctx context.Context, c *cli.Context) error {
return s.exitError(ctx, ExitUsage, nil, "Usage: %s config key value", s.Name)
}

if err := s.setConfigValue(ctx, c.Args()[0], c.Args()[1]); err != nil {
if err := s.setConfigValue(ctx, c.String("store"), c.Args()[0], c.Args()[1]); err != nil {
return s.exitError(ctx, ExitUnknown, err, "Error setting config value")
}
return nil
}

func (s *Action) printConfigValues(filter ...string) error {
m := s.cfg.ConfigMap()
out := make([]string, 0, len(m))
for k := range m {
if !contains(filter, k) {
func (s *Action) printConfigValues(needles ...string) error {
prefix := ""
if len(needles) < 1 {
fmt.Printf("root store config:\n")
prefix = " "
}
m := s.cfg.Root.ConfigMap()
for _, k := range filter(m, needles) {
fmt.Printf("%s%s: %s\n", prefix, k, m[k])
}
for mp, sc := range s.cfg.Mounts {
if len(needles) < 1 {
fmt.Printf("mount '%s' config:\n", mp)
mp = " "
} else {
mp += "/"
}
sm := sc.ConfigMap()
for _, k := range filter(sm, needles) {
if sm[k] != m[k] {
fmt.Printf("%s%s: %s\n", mp, k, sm[k])
}
}
}
return nil
}

func filter(haystack map[string]string, needles []string) []string {
out := make([]string, 0, len(haystack))
for k := range haystack {
if !contains(needles, k) {
continue
}
out = append(out, k)
}
sort.Strings(out)
for _, k := range out {
fmt.Printf("%s: %s\n", k, m[k])
}
return nil
return out
}

func contains(haystack []string, needle string) bool {
Expand All @@ -63,9 +86,16 @@ func contains(haystack []string, needle string) bool {
return false
}

func (s *Action) setConfigValue(ctx context.Context, key, value string) error {
if err := s.cfg.SetConfigValue(key, value); err != nil {
func (s *Action) setConfigValue(ctx context.Context, store, key, value string) error {
if err := s.cfg.SetConfigValue(store, key, value); err != nil {
return errors.Wrapf(err, "failed to set config value '%s'", key)
}
return s.printConfigValues(key)
}

// ConfigComplete will print the list of valid config keys
func (s *Action) ConfigComplete(c *cli.Context) {
for k := range s.cfg.Root.ConfigMap() {
fmt.Println(k)
}
}
4 changes: 2 additions & 2 deletions action/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func (s *Action) Copy(ctx context.Context, c *cli.Context) error {
from := c.Args()[0]
to := c.Args()[1]

if !s.Store.Exists(from) {
if !s.Store.Exists(ctx, from) {
return s.exitError(ctx, ExitNotFound, nil, "%s does not exist", from)
}

if !force {
if s.Store.Exists(to) && !s.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {
if s.Store.Exists(ctx, to) && !s.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {
return s.exitError(ctx, ExitAborted, nil, "not overwriting your current secret")
}
}
Expand Down
4 changes: 2 additions & 2 deletions action/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *Action) Delete(ctx context.Context, c *cli.Context) error {
if recursive {
recStr = "recursively "
}
if (s.Store.Exists(name) || s.Store.IsDir(name)) && key == "" && !s.AskForConfirmation(ctx, fmt.Sprintf("Are you sure you would like to %sdelete %s?", recStr, name)) {
if (s.Store.Exists(ctx, name) || s.Store.IsDir(ctx, name)) && key == "" && !s.AskForConfirmation(ctx, fmt.Sprintf("Are you sure you would like to %sdelete %s?", recStr, name)) {
return nil
}
}
Expand All @@ -38,7 +38,7 @@ func (s *Action) Delete(ctx context.Context, c *cli.Context) error {
return nil
}

if s.Store.IsDir(name) {
if s.Store.IsDir(ctx, name) {
return errors.Errorf("Cannot remove '%s': Is a directory. Use 'gopass rm -r %s' to delete", name, name)
}

Expand Down
4 changes: 2 additions & 2 deletions action/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *Action) Edit(ctx context.Context, c *cli.Context) error {

var content []byte
var changed bool
if s.Store.Exists(name) {
if s.Store.Exists(ctx, name) {
sec, err := s.Store.Get(ctx, name)
if err != nil {
return errors.Errorf("failed to decrypt %s: %v", name, err)
Expand All @@ -37,7 +37,7 @@ func (s *Action) Edit(ctx context.Context, c *cli.Context) error {
if err != nil {
return errors.Errorf("failed to decode %s: %v", name, err)
}
} else if tmpl, found := s.Store.LookupTemplate(name); found {
} else if tmpl, found := s.Store.LookupTemplate(ctx, name); found {
changed = true
// load template if it exists
content = pwgen.GeneratePassword(defaultLength, false)
Expand Down
4 changes: 2 additions & 2 deletions action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
}

if !force { // don't check if it's force anyway
if s.Store.Exists(name) && key == "" && !s.AskForConfirmation(ctx, fmt.Sprintf("An entry already exists for %s. Overwrite the current password?", name)) {
if s.Store.Exists(ctx, name) && key == "" && !s.AskForConfirmation(ctx, fmt.Sprintf("An entry already exists for %s. Overwrite the current password?", name)) {
return s.exitError(ctx, ExitAborted, nil, "user aborted. not overwriting your current password")
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
if err := s.Store.Set(sub.WithReason(ctx, "Generated password for YAML key"), name, sec); err != nil {
return s.exitError(ctx, ExitEncrypt, err, "failed to set key '%s' of '%s': %s", key, name, err)
}
} else if s.Store.Exists(name) {
} else if s.Store.Exists(ctx, name) {
sec, err := s.Store.Get(ctx, name)
if err != nil {
return s.exitError(ctx, ExitEncrypt, err, "failed to set key '%s' of '%s': %s", key, name, err)
Expand Down
8 changes: 4 additions & 4 deletions action/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Action) Insert(ctx context.Context, c *cli.Context) error {
}

sec := secret.New("", "")
if s.Store.Exists(name) {
if s.Store.Exists(ctx, name) {
var err error
sec, err = s.Store.Get(ctx, name)
if err != nil {
Expand Down Expand Up @@ -84,15 +84,15 @@ func (s *Action) Insert(ctx context.Context, c *cli.Context) error {
}

if !force { // don't check if it's force anyway
if s.Store.Exists(name) && !s.AskForConfirmation(ctx, fmt.Sprintf("An entry already exists for %s. Overwrite it?", name)) {
if s.Store.Exists(ctx, name) && !s.AskForConfirmation(ctx, fmt.Sprintf("An entry already exists for %s. Overwrite it?", name)) {
return s.exitError(ctx, ExitAborted, nil, "not overwriting your current secret")
}
}

// if multi-line input is requested start an editor
if multiline && ctxutil.IsInteractive(ctx) {
buf := []byte{}
if s.Store.Exists(name) {
if s.Store.Exists(ctx, name) {
var err error
sec, err := s.Store.Get(ctx, name)
if err != nil {
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *Action) Insert(ctx context.Context, c *cli.Context) error {
}

var sec *secret.Secret
if s.Store.Exists(name) {
if s.Store.Exists(ctx, name) {
var err error
sec, err = s.Store.Get(ctx, name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion action/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *Action) MountAdd(ctx context.Context, c *cli.Context) error {
keys = append(keys, k)
}

if s.Store.Exists(alias) {
if s.Store.Exists(ctx, alias) {
fmt.Printf(color.YellowString("WARNING: shadowing %s entry\n"), alias)
}

Expand Down
2 changes: 1 addition & 1 deletion action/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *Action) Move(ctx context.Context, c *cli.Context) error {
to := c.Args()[1]

if !force {
if s.Store.Exists(to) && !s.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {
if s.Store.Exists(ctx, to) && !s.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {
return s.exitError(ctx, ExitAborted, nil, "not overwriting your current secret")
}
}
Expand Down
4 changes: 2 additions & 2 deletions action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func (s *Action) show(ctx context.Context, c *cli.Context, name, key string, cli
return s.exitError(ctx, ExitUsage, nil, "Usage: %s show [name]", s.Name)
}

if s.Store.IsDir(name) {
if s.Store.IsDir(ctx, name) {
return s.List(ctx, c)
}

// auto-fallback to binary files with b64 suffix, if unique
if !s.Store.Exists(name) && s.Store.Exists(name+BinarySuffix) {
if !s.Store.Exists(ctx, name) && s.Store.Exists(ctx, name+BinarySuffix) {
name += BinarySuffix
}

Expand Down
12 changes: 6 additions & 6 deletions action/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Action) TemplatesPrint(ctx context.Context, c *cli.Context) error {
func (s *Action) TemplatePrint(ctx context.Context, c *cli.Context) error {
name := c.Args().First()

content, err := s.Store.GetTemplate(name)
content, err := s.Store.GetTemplate(ctx, name)
if err != nil {
return s.exitError(ctx, ExitIO, err, "failed to retrieve template: %s", err)
}
Expand All @@ -59,9 +59,9 @@ func (s *Action) TemplateEdit(ctx context.Context, c *cli.Context) error {
}

var content []byte
if s.Store.HasTemplate(name) {
if s.Store.HasTemplate(ctx, name) {
var err error
content, err = s.Store.GetTemplate(name)
content, err = s.Store.GetTemplate(ctx, name)
if err != nil {
return s.exitError(ctx, ExitIO, err, "failed to retrieve template: %s", err)
}
Expand All @@ -79,7 +79,7 @@ func (s *Action) TemplateEdit(ctx context.Context, c *cli.Context) error {
return nil
}

return s.Store.SetTemplate(name, nContent)
return s.Store.SetTemplate(ctx, name, nContent)
}

// TemplateRemove will remove a single template
Expand All @@ -89,11 +89,11 @@ func (s *Action) TemplateRemove(ctx context.Context, c *cli.Context) error {
return s.exitError(ctx, ExitUsage, nil, "usage: %s templates remove [name]", s.Name)
}

if !s.Store.HasTemplate(name) {
if !s.Store.HasTemplate(ctx, name) {
return s.exitError(ctx, ExitNotFound, nil, "template '%s' not found", name)
}

return s.Store.RemoveTemplate(name)
return s.Store.RemoveTemplate(ctx, name)
}

// TemplatesComplete prints a list of all templates for bash completion
Expand Down

0 comments on commit a00560b

Please sign in to comment.