Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INI, DotEnv stores: shamir_threshold is an integer #1394

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading