diff --git a/CHANGELOG.md b/CHANGELOG.md index e378d2ea9f..247ac580f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,3 +44,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). [0.6.7]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.2...v0.6.7 + +## [0.6.9] + +### Changed + +- Update GenerateHints function to check supported list of hints + + +[0.6.9]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.8...v0.6.9 diff --git a/utils/hints.go b/utils/hints.go index f3b816b539..7cf427c8ee 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -202,8 +202,9 @@ func IsDisabled(hints mapstr.M, key string) bool { } // GenerateHints parses annotations based on a prefix and sets up hints that can be picked up by individual Beats. -func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { +func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, error) { hints := mapstr.M{} + returnerror := error(nil) if rawEntries, err := annotations.GetValue(prefix); err == nil { if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { @@ -212,6 +213,18 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { parts := strings.Split(key, "/") if len(parts) == 2 { hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1]) + //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints + found := false + for _, checksupported := range allSupportedHints { + if parts[1] == checksupported { + found = true + break + } + } + if !found { + returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1]) + } //End of check + // Insert only if there is no entry already. container level annotations take // higher priority. if _, err := hints.GetValue(hintKey); err != nil { @@ -248,7 +261,11 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { } } - return hints + if returnerror == nil { + return hints, nil + } + + return hints, returnerror } // GetHintsAsList gets a set of hints and tries to convert them into a list of hints diff --git a/utils/hints_test.go b/utils/hints_test.go index 624bab5456..2e40e2cdfa 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -50,6 +50,21 @@ func TestGetProcessors(t *testing.T) { } func TestGenerateHints(t *testing.T) { + const ( + integration = "package" + datastreams = "data_streams" + host = "host" + period = "period" + timeout = "timeout" + metricspath = "metrics_path" + username = "username" + password = "password" + stream = "stream" // this is the container stream: stdout/stderr + processors = "processors" + ) + + var allSupportedHints = []string{"enabled", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors} + tests := []struct { annotations map[string]string result mapstr.M @@ -219,7 +234,8 @@ func TestGenerateHints(t *testing.T) { continue } } - assert.Equal(t, test.result, GenerateHints(annMap, "foobar", "co.elastic")) + generateHints, _ := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) + assert.Equal(t, test.result, generateHints) } } func TestGetHintsAsList(t *testing.T) {