forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a simple, robust and predictable KV parser (gopasspw#659)
Fixes gopasspw#559 Fixes gopasspw#638
- Loading branch information
1 parent
7056192
commit 67faaef
Showing
10 changed files
with
274 additions
and
291 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package secret | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func (s *Secret) decodeKV() error { | ||
mayBeYAML := false | ||
scanner := bufio.NewScanner(strings.NewReader(s.body)) | ||
data := make(map[string]interface{}, strings.Count(s.body, "\n")) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "---") { | ||
mayBeYAML = true | ||
} | ||
parts := strings.SplitN(line, ":", 2) | ||
if len(parts) < 1 { | ||
continue | ||
} | ||
if len(parts) == 1 && strings.HasPrefix(parts[0], " ") { | ||
mayBeYAML = true | ||
} | ||
for i, part := range parts { | ||
parts[i] = strings.TrimSpace(part) | ||
} | ||
// preserve key only entries | ||
if len(parts) < 2 { | ||
data[parts[0]] = "" | ||
continue | ||
} | ||
if strings.HasPrefix(parts[1], "|") { | ||
mayBeYAML = true | ||
} | ||
data[parts[0]] = parts[1] | ||
} | ||
if mayBeYAML { | ||
docSep, err := s.decodeYAML() | ||
if debug { | ||
fmt.Printf("[DEBUG] decodeKV() - mayBeYAML - err: %s\n", err) | ||
} | ||
if docSep && err == nil && s.data != nil { | ||
return nil | ||
} | ||
} | ||
if debug { | ||
fmt.Printf("[DEBUG] decodeKV() - simple KV\n") | ||
} | ||
s.data = data | ||
return nil | ||
} | ||
|
||
func (s *Secret) encodeKV() error { | ||
if s.data == nil { | ||
return nil | ||
} | ||
keys := make([]string, 0, len(s.data)) | ||
for key := range s.data { | ||
keys = append(keys, key) | ||
} | ||
sort.Strings(keys) | ||
var buf strings.Builder | ||
mayBeYAML := false | ||
for _, key := range keys { | ||
sv, ok := s.data[key].(string) | ||
if !ok { | ||
mayBeYAML = true | ||
continue | ||
} | ||
_, _ = buf.WriteString(key) | ||
_, _ = buf.WriteString(": ") | ||
_, _ = buf.WriteString(sv) | ||
_, _ = buf.WriteString("\n") | ||
if strings.Contains(sv, "\n") { | ||
mayBeYAML = true | ||
} | ||
} | ||
if mayBeYAML { | ||
if err := s.encodeYAML(); err == nil { | ||
if debug { | ||
fmt.Printf("[DEBUG] encodeKV() - mayBeYAML - OK\n") | ||
} | ||
return nil | ||
} | ||
} | ||
if debug { | ||
fmt.Printf("[DEBUG] encodeKV() - simple KV\n") | ||
} | ||
s.body = buf.String() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package secret | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestYAMLFromHereDoc(t *testing.T) { | ||
t.Logf("Parse K/V w/ HereDoc as YAML, not K/V") | ||
mlValue := `somepw | ||
--- | ||
foo: | | ||
bar | ||
baz | ||
key: value | ||
` | ||
s, err := Parse([]byte(mlValue)) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, s) | ||
v, err := s.Value("foo") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "bar\nbaz\n", v) | ||
} | ||
|
||
func TestKVContentFromInvalidYAML(t *testing.T) { | ||
t.Logf("Retrieve content from invalid YAML (#375)") | ||
mlValue := `somepasswd | ||
--- | ||
Test / test.com | ||
username: [email protected] | ||
password: somepasswd | ||
url: http://www.test.com/` | ||
s, err := Parse([]byte(mlValue)) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, s) | ||
v, err := s.Value("Test / test.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "", v) | ||
|
||
// read back key | ||
assert.Equal(t, mlValue, s.String()) | ||
} |
Oops, something went wrong.