From 2176d2f3fcfaa5dddfc763299f5ce6b1d70213b4 Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Thu, 12 Sep 2019 08:40:29 +0200 Subject: [PATCH] Validate packages to only use predefined categories (#100) This required to move the categories to util package. --- CHANGELOG.md | 1 + categories.go | 7 ++----- util/package.go | 11 +++++++++++ util/package_test.go | 10 ++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f2e691a1..72fe1ad6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Add validation check that Kibana min/max are valid semver versions. [#99](https://github.com/elastic/integrations-registry/pull/99) * Adding Cache-Control max-age headers to all http responses set to 1h. [#101](https://github.com/elastic/integrations-registry/pull/101) +* Validate packages to guarantee only predefined categories can be used. [#100](https://github.com/elastic/integrations-registry/pull/100) ### Changed diff --git a/categories.go b/categories.go index 72f2fc2bf..e6ca1aff3 100644 --- a/categories.go +++ b/categories.go @@ -9,10 +9,7 @@ import ( "github.com/elastic/integrations-registry/util" ) -var categoryTitles = map[string]string{ - "logs": "Logs", - "metrics": "Metrics", -} + type Category struct { Id string `yaml:"id" json:"id"` @@ -85,7 +82,7 @@ func getCategoriesOutput(categories map[string]*Category) ([]byte, error) { var outputCategories []*Category for _, k := range keys { c := categories[k] - if title, ok := categoryTitles[c.Title]; ok { + if title, ok := util.CategoryTitles[c.Title]; ok { c.Title = title } outputCategories = append(outputCategories, c) diff --git a/util/package.go b/util/package.go index de2990727..6722254fd 100644 --- a/util/package.go +++ b/util/package.go @@ -16,6 +16,11 @@ import ( const defaultType = "integration" +var CategoryTitles = map[string]string{ + "logs": "Logs", + "metrics": "Metrics", +} + type Package struct { Name string `yaml:"name" json:"name"` Title *string `yaml:"title,omitempty" json:"title,omitempty"` @@ -217,5 +222,11 @@ func (p *Package) Validate() error { } } + for _, c := range p.Categories { + if _, ok := CategoryTitles[c]; !ok { + return fmt.Errorf("invalid category: %s", c) + } + } + return nil } diff --git a/util/package_test.go b/util/package_test.go index 929435464..f271751b2 100644 --- a/util/package_test.go +++ b/util/package_test.go @@ -62,6 +62,16 @@ var packageTests = []struct { Max: "4.5.6", }, }, + Categories: []string{"metrics", "logs", "foo"}, + }, + false, + "invalid category ", + }, + { + Package{ + Title: &title, + Description: "my description", + Categories: []string{"metrics", "logs"}, }, true, "complete",