Skip to content

Commit

Permalink
Try to leave secret content untouched (gopasspw#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Dec 11, 2017
1 parent fe171e9 commit 0fa7c27
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
35 changes: 12 additions & 23 deletions store/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package secret

import (
"bytes"
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -55,24 +54,24 @@ func (s *Secret) decodeYAML() (bool, error) {
return true, err
}
s.data = d
s.body = ""
return true, nil
}

func (s *Secret) encodeYAML() error {
// update body
yb, err := yaml.Marshal(s.data)
if err != nil {
return err
}
s.body = "---\n" + string(yb)
return nil
}

// Bytes encodes an secret
func (s *Secret) Bytes() ([]byte, error) {
buf := &bytes.Buffer{}
_, _ = buf.WriteString(s.password)
_, _ = buf.WriteString("\n")
if s.data != nil {
yb, err := yaml.Marshal(s.data)
if err != nil {
return nil, err
}
_, _ = buf.WriteString("---\n")
_, _ = buf.Write(yb)
return buf.Bytes(), nil
}
_, _ = buf.WriteString(s.body)
return buf.Bytes(), nil
}
Expand All @@ -82,16 +81,6 @@ func (s *Secret) String() string {
buf := &bytes.Buffer{}
_, _ = buf.WriteString(s.password)
_, _ = buf.WriteString("\n")
if s.data != nil {
yb, err := yaml.Marshal(s.data)
if err != nil {
_, _ = buf.WriteString(fmt.Sprintf("YAML-Encoding Error: %s\n%+v\n", err, s.data))
return buf.String()
}
_, _ = buf.WriteString("---\n")
_, _ = buf.Write(yb)
return buf.String()
}
_, _ = buf.WriteString(s.body)
return buf.String()
}
Expand Down Expand Up @@ -177,7 +166,7 @@ func (s *Secret) SetValue(key, value string) error {
return store.ErrYAMLNoMark
}
s.data[key] = value
return nil
return s.encodeYAML()
}

// DeleteKey key will delete a single key from an decoded map
Expand All @@ -189,7 +178,7 @@ func (s *Secret) DeleteKey(key string) error {
return store.ErrYAMLNoMark
}
delete(s.data, key)
return nil
return s.encodeYAML()
}

// Equal returns true if two secrets are equal
Expand Down
10 changes: 8 additions & 2 deletions store/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ key2: value2`),
"key1": "value1",
"key2": "value2",
},
Body: `---
key1: value1
key2: value2`,
},
{
Desc: "YAML only Secret",
Expand All @@ -55,6 +58,9 @@ key2: value2`),
"key1": "value1",
"key2": "value2",
},
Body: `---
key1: value1
key2: value2`,
},
{
Desc: "invalid YAML Secret",
Expand Down Expand Up @@ -90,10 +96,10 @@ key2: value2`,
continue
}
if sec.Password() != tc.Password {
t.Errorf("Wrong password")
t.Errorf("[%s] Wrong password", tc.Desc)
}
if sec.Body() != tc.Body {
t.Errorf("Wrong body")
t.Errorf("[%s] Wrong body: %s - %s", tc.Desc, sec.Body(), tc.Body)
}
for k, v := range tc.Data {
rv, err := sec.Value(k)
Expand Down

0 comments on commit 0fa7c27

Please sign in to comment.