-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
- Loading branch information
1 parent
927a95e
commit d3ea135
Showing
7 changed files
with
254 additions
and
122 deletions.
There are no files selected for viewing
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,80 @@ | ||
package secret | ||
|
||
import ( | ||
"bufio" | ||
"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 docSep && err == nil && s.data != nil { | ||
return nil | ||
} | ||
} | ||
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 { | ||
return nil | ||
} | ||
} | ||
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()) | ||
} |
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
Oops, something went wrong.