open-consul/command/config/write/decode_shim.go
R.B. Boyer 6bf4255cdc
command: ensure that the json form of config entries can be submitted with 'consul config write' (#6290)
The json decoder inside of the HCLv1 hcl.Decode function behaves
unexpectedly when decoding generically into a map[string]interface{} as
is done for 'consul config write' pre-submit decoding.

This results in some subtle (service-router Match and Destinations being
separated) and some not so subtle (service-resolver subsets and failover
panic if multiple subsets are referenced) bugs when subsequently passed
through mapstructure to finish decoding.

Given that HCLv1 is basically frozen and the HCL part of it is fine
instead of trying to figure out what the underlying bug is in the json
decoder for our purposes just sniff the byte slice and selectively use
the stdlib json decoder for JSON and hcl decoder for HCL.
2019-08-07 16:41:33 -05:00

109 lines
2.2 KiB
Go

package write
import (
"encoding/json"
"unicode"
"unicode/utf8"
"github.com/hashicorp/hcl"
)
// hclDecode is a modified version of hcl.Decode just for the super general
// purposes here. There's some strange bug in how hcl.Decode decodes json where
//
// { "sub" : { "v1" : { "field" : "value1" }, "v2" : { "field" : "value2" } } }
//
// hcl.Decode-s into:
//
// map[string]interface {}{
// "sub":[]map[string]interface {}{
// map[string]interface {}{
// "v1":[]map[string]interface {}{
// map[string]interface {}{
// "field":"value1"
// }
// }
// },
// map[string]interface {}{
// "v2":[]map[string]interface {}{
// map[string]interface {}{
// "field":"value2"
// }
// }
// }
// }
// }
//
// but json.Unmarshal-s into the more expected:
//
// map[string]interface {}{
// "sub":map[string]interface {}{
// "v1":map[string]interface {}{
// "field":"value1"
// },
// "v2":map[string]interface {}{
// "field":"value2"
// }
// }
// }
//
// The strange part is that the following HCL:
//
// sub { "v1" = { field = "value1" }, "v2" = { field = "value2" } }
//
// hcl.Decode-s into:
//
// map[string]interface {}{
// "sub":[]map[string]interface {}{
// map[string]interface {}{
// "v1":[]map[string]interface {}{
// map[string]interface {}{
// "field":"value1"
// }
// },
// "v2":[]map[string]interface {}{
// map[string]interface {}{
// "field":"value2"
// }
// }
// }
// }
// }
//
// Which is the "correct" value assuming you did the patch-slice-of-maps correction.
//
// Given that HCLv1 is basically frozen and the HCL part of it is fine instead
// of trying to track down a weird bug we'll bypass the weird JSON decoder and just use
// the stdlib one.
func hclDecode(out interface{}, in string) error {
data := []byte(in)
if isHCL(data) {
return hcl.Decode(out, in)
}
return json.Unmarshal(data, out)
}
// this is an inlined variant of hcl.lexMode()
func isHCL(v []byte) bool {
var (
r rune
w int
offset int
)
for {
r, w = utf8.DecodeRune(v[offset:])
offset += w
if unicode.IsSpace(r) {
continue
}
if r == '{' {
return false
}
break
}
return true
}