Skip to content

Commit

Permalink
Merge pull request getsops#1394 from felixfontein/metadata-types
Browse files Browse the repository at this point in the history
INI, DotEnv stores: shamir_threshold is an integer
  • Loading branch information
felixfontein committed Dec 29, 2023
2 parents 2a70c24 + 56d765a commit 06db542
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
5 changes: 4 additions & 1 deletion stores/dotenv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (store *Store) LoadEncryptedFile(in []byte) (sops.Tree, error) {
}

stores.DecodeNewLines(mdMap)
stores.DecodeNonStrings(mdMap)
err = stores.DecodeNonStrings(mdMap)
if err != nil {
return sops.Tree{}, err
}
metadata, err := stores.UnflattenMetadata(mdMap)
if err != nil {
return sops.Tree{}, err
Expand Down
22 changes: 21 additions & 1 deletion stores/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,28 @@ func EncodeNewLines(m map[string]interface{}) {
}

// DecodeNonStrings will look for known metadata keys that are not strings and decode to the appropriate type
func DecodeNonStrings(m map[string]interface{}) {
func DecodeNonStrings(m map[string]interface{}) error {
if v, ok := m["mac_only_encrypted"]; ok {
m["mac_only_encrypted"] = false
if v == "true" {
m["mac_only_encrypted"] = true
}
}
if v, ok := m["shamir_threshold"]; ok {
switch val := v.(type) {
case string:
vInt, err := strconv.Atoi(val)
if err != nil {
return fmt.Errorf("shamir_threshold is not an integer: %s", err.Error())
}
m["shamir_threshold"] = vInt
case int:
m["shamir_threshold"] = val
default:
return fmt.Errorf("shamir_threshold is neither a string nor an integer, but %T", val)
}
}
return nil
}

// EncodeNonStrings will look for known metadata keys that are not strings and will encode it to strings
Expand All @@ -255,4 +270,9 @@ func EncodeNonStrings(m map[string]interface{}) {
}
}
}
if v, found := m["shamir_threshold"]; found {
if vInt, ok := v.(int); ok {
m["shamir_threshold"] = fmt.Sprintf("%d", vInt)
}
}
}
25 changes: 24 additions & 1 deletion stores/flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,44 @@ func TestDecodeNonStrings(t *testing.T) {
{map[string]interface{}{"mac_only_encrypted": "false"}, map[string]interface{}{"mac_only_encrypted": false}},
{map[string]interface{}{"mac_only_encrypted": "true"}, map[string]interface{}{"mac_only_encrypted": true}},
{map[string]interface{}{"mac_only_encrypted": "something-else"}, map[string]interface{}{"mac_only_encrypted": false}},
{map[string]interface{}{"shamir_threshold": "2"}, map[string]interface{}{"shamir_threshold": 2}},
{map[string]interface{}{"shamir_threshold": "002"}, map[string]interface{}{"shamir_threshold": 2}},
{map[string]interface{}{"shamir_threshold": "123"}, map[string]interface{}{"shamir_threshold": 123}},
{map[string]interface{}{"shamir_threshold": 123}, map[string]interface{}{"shamir_threshold": 123}},
}

for _, tt := range tests {
DecodeNonStrings(tt.input)
err := DecodeNonStrings(tt.input)
assert.Nil(t, err)
assert.Equal(t, tt.want, tt.input)
}
}

func TestDecodeNonStringsErrors(t *testing.T) {
tests := []struct {
input map[string]interface{}
want string
}{
{map[string]interface{}{"shamir_threshold": "foo"}, "shamir_threshold is not an integer: strconv.Atoi: parsing \"foo\": invalid syntax"},
{map[string]interface{}{"shamir_threshold": true}, "shamir_threshold is neither a string nor an integer, but bool"},
}

for _, tt := range tests {
err := DecodeNonStrings(tt.input)
assert.NotNil(t, err)
assert.Equal(t, tt.want, err.Error())
}
}

func TestEncodeNonStrings(t *testing.T) {
tests := []struct {
input map[string]interface{}
want map[string]interface{}
}{
{map[string]interface{}{"mac_only_encrypted": false}, map[string]interface{}{"mac_only_encrypted": "false"}},
{map[string]interface{}{"mac_only_encrypted": true}, map[string]interface{}{"mac_only_encrypted": "true"}},
{map[string]interface{}{"shamir_threshold": 2}, map[string]interface{}{"shamir_threshold": "2"}},
{map[string]interface{}{"shamir_threshold": 123}, map[string]interface{}{"shamir_threshold": "123"}},
}

for _, tt := range tests {
Expand Down
5 changes: 4 additions & 1 deletion stores/ini/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func (store *Store) iniSectionToMetadata(sopsSection *ini.Section) (stores.Metad
metadataHash[k] = v
}
stores.DecodeNewLines(metadataHash)
stores.DecodeNonStrings(metadataHash)
err := stores.DecodeNonStrings(metadataHash)
if err != nil {
return stores.Metadata{}, err
}
return stores.UnflattenMetadata(metadataHash)
}

Expand Down

0 comments on commit 06db542

Please sign in to comment.