Skip to content

Commit 2fe1684

Browse files
authored
Merge pull request #1899 from felixfontein/shamir
Fix Shamir threshold encoding for INI and ENV files
2 parents 7d77e8a + df09e2c commit 2fe1684

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

stores/flatten.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,14 @@ func DecodeNonStrings(m map[string]interface{}) error {
248248
case string:
249249
vInt, err := strconv.Atoi(val)
250250
if err != nil {
251-
return fmt.Errorf("shamir_threshold is not an integer: %s", err.Error())
251+
// Older versions of SOPS stored shamir_threshold as a floating point representation
252+
// of the actual integer. Try to parse a floating point number and see whether it
253+
// can be converted without loss to an integer.
254+
vFloat, floatErr := strconv.ParseFloat(val, 64)
255+
vInt = int(vFloat)
256+
if floatErr != nil || float64(vInt) != vFloat {
257+
return fmt.Errorf("shamir_threshold is not an integer: %s", err.Error())
258+
}
252259
}
253260
m["shamir_threshold"] = vInt
254261
case int:
@@ -274,5 +281,11 @@ func EncodeNonStrings(m map[string]interface{}) {
274281
if vInt, ok := v.(int); ok {
275282
m["shamir_threshold"] = fmt.Sprintf("%d", vInt)
276283
}
284+
// FlattenMetadata serializes the input as JSON and then deserializes it.
285+
// The JSON unserializer treats every number as a float, so the above 'if'
286+
// never applies in that situation.
287+
if vFloat, ok := v.(float64); ok {
288+
m["shamir_threshold"] = fmt.Sprintf("%.0f", vFloat)
289+
}
277290
}
278291
}

0 commit comments

Comments
 (0)