diff --git a/go/caller_test.go b/go/caller_test.go index 1759eec..d08b88f 100644 --- a/go/caller_test.go +++ b/go/caller_test.go @@ -145,6 +145,13 @@ func TestACallerInTheArgumentsIsNeverTheCaller(t *testing.T) { if d, _ := ParseVerbCallers(" ", nil, VerbCallers{"publish": {"x"}}); len(d["publish"]) != 1 { t.Error("an empty value is not the holder's defaults") } + // As mesh-config resolves the value: the object itself, or written as a string; null is the defaults. + for _, raw := range []string{`{"publish":["a"]}`, `"{\"publish\":[\"a\"]}"`, `null`} { + c, err := parseConfiguredVerbCallers(json.RawMessage(raw), []string{"publish"}, VerbCallers{"publish": {"a"}}) + if err != nil || c.Allowed("publish", "ace.a") != nil { + t.Errorf("the config value %s read as %v, %v", raw, c, err) + } + } if ModuleOf("ace.mesh-build-go") != "mesh-build-go" { t.Error("ModuleOf does not read the module of a proved caller") } diff --git a/go/callers.go b/go/callers.go index 67a8a38..6fe6a49 100644 --- a/go/callers.go +++ b/go/callers.go @@ -110,3 +110,22 @@ func ModuleOf(proved string) string { } return proved } + +// ConfiguredVerbCallers is the holder's list as mesh-config resolved its config value key for this machine (Config): +// a JSON object of verb to callers, or that object written as a string; an unset or empty value is defaults. A value +// that cannot be read is an error, never the defaults, so a broken list is said, not widened. +func ConfiguredVerbCallers(key string, verbs []string, defaults VerbCallers) (VerbCallers, error) { + raw, err := Config(key) + if err != nil { + return nil, fmt.Errorf("the config value %s cannot be read: %w", key, err) + } + return parseConfiguredVerbCallers(raw, verbs, defaults) +} + +func parseConfiguredVerbCallers(raw json.RawMessage, verbs []string, defaults VerbCallers) (VerbCallers, error) { + var text string + if err := json.Unmarshal(raw, &text); err == nil || len(bytes.TrimSpace(raw)) == 0 || string(bytes.TrimSpace(raw)) == "null" { + return ParseVerbCallers(text, verbs, defaults) + } + return ParseVerbCallers(string(raw), verbs, defaults) +}