config: add decode hook for the SANMatcher type (#4464)

This commit is contained in:
Kenneth Jenkins 2023-08-16 11:05:17 -07:00 committed by GitHub
parent a2539839d3
commit 1b3ee7ff8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View file

@ -38,4 +38,5 @@ var ViperPolicyHooks = viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
decodeJWTClaimHeadersHookFunc(),
decodeCodecTypeHookFunc(),
decodePPLPolicyHookFunc(),
decodeSANMatcherHookFunc(),
))

View file

@ -508,6 +508,26 @@ func parseJSONPB(src map[string]interface{}, dst proto.Message, opts protojson.U
return opts.Unmarshal(data, dst)
}
// decodeSANMatcherHookFunc returns a decode hook for the SANMatcher type.
func decodeSANMatcherHookFunc() mapstructure.DecodeHookFunc {
return func(f, t reflect.Type, data interface{}) (interface{}, error) {
if t != reflect.TypeOf(SANMatcher{}) {
return data, nil
}
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
var m SANMatcher
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
return m, nil
}
}
// serializable converts mapstructure nested map into map[string]interface{} that is serializable to JSON
func serializable(in interface{}) (interface{}, error) {
switch typed := in.(type) {

View file

@ -341,6 +341,27 @@ func Test_parsePolicyFile(t *testing.T) {
}
}
func Test_decodeSANMatcher(t *testing.T) {
// Verify that config file parsing will decode the SANMatcher type.
const yaml = `
downstream_mtls:
match_subject_alt_names:
- dns: 'example-1\..*'
- dns: '.*\.example-2'
`
cfg := filepath.Join(t.TempDir(), "config.yaml")
err := os.WriteFile(cfg, []byte(yaml), 0644)
require.NoError(t, err)
o, err := optionsFromViper(cfg)
require.NoError(t, err)
assert.Equal(t, []SANMatcher{
{Type: SANTypeDNS, Pattern: `example-1\..*`},
{Type: SANTypeDNS, Pattern: `.*\.example-2`},
}, o.DownstreamMTLS.MatchSubjectAltNames)
}
func Test_Checksum(t *testing.T) {
o := NewDefaultOptions()