-
Notifications
You must be signed in to change notification settings - Fork 87
Validate kibana tags duplicates #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
teresaromero
merged 11 commits into
elastic:main
from
teresaromero:977-avoid-tag-duplicates
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
82bd903
Implement validation for duplicate Kibana tags in YAML and JSON files
teresaromero 6fe6465
Handle non-existent directory errors gracefully in JSON tag validation
teresaromero 8d7dc40
add validation for duplicate Kibana tag names
teresaromero 3b17a29
add copyright notice to validate_kibana_tag_duplicates_test.go
teresaromero 2d25032
Apply tag duplicate validation to all packages
teresaromero f1b5bea
Refactor tag validation to handle duplicates in Kibana tags and updat…
teresaromero 1673c25
Update validations and changelog for Kibana tag duplicate validation
teresaromero 99a973e
Refactor import statements in validate_kibana_tag_duplicates.go and v…
teresaromero 60a9a0e
Refactor path usage in tag validation functions to use filepath package
teresaromero 031791d
Merge branch 'main' of github.com:elastic/package-spec into 977-avoid…
teresaromero ae229cb
change changelog entry to next patch
teresaromero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
code/go/internal/validator/semantic/validate_kibana_tag_duplicates.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package semantic | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "path" | ||
| "slices" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" | ||
| ) | ||
|
|
||
| const ( | ||
| tagType = "tag" | ||
| ) | ||
|
|
||
| type packageSpecTag struct { | ||
| Attributes struct { | ||
| Name string `yaml:"name"` | ||
| } `yaml:"attributes"` | ||
| Type string `yaml:"type"` | ||
| } | ||
|
|
||
| type sharedTagYML struct { | ||
| Name string `yaml:"text"` | ||
| } | ||
|
|
||
| // ValidateKibanaTagDuplicates checks for duplicate Kibana tag names | ||
| // between the kibana/tags.yml file and the tags defined in the package's kibana/tag/*.json files. | ||
| // It returns a list of validation errors if any duplicates are found. | ||
| func ValidateKibanaTagDuplicates(fsys fspath.FS) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
| sharedTagNames, verr := getValidatedSharedKibanaTags(fsys) | ||
| if len(verr) > 0 { | ||
| errs = append(errs, verr...) | ||
| } | ||
|
|
||
| verr = validateKibanaPackageTagsDuplicates(fsys, sharedTagNames) | ||
| if len(verr) > 0 { | ||
| errs = append(errs, verr...) | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| // getValidatedSharedKibanaTags reads the kibana/tags.yml file and returns a slice of tag names defined in it. | ||
| // It also returns any validation errors encountered during the process if tags are duplicated within the file. | ||
| func getValidatedSharedKibanaTags(fsys fspath.FS) ([]string, specerrors.ValidationErrors) { | ||
| tagsPath := path.Join("kibana", "tags.yml") | ||
| // Collect all tags defined in the kibana/tags.yml file. | ||
| b, err := fs.ReadFile(fsys, tagsPath) | ||
| if err != nil { | ||
| // if the file does not exist, return an empty slice without error | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return nil, nil | ||
| } | ||
| return nil, specerrors.ValidationErrors{specerrors.NewStructuredErrorf("error reading file %s: %v", tagsPath, err)} | ||
| } | ||
| var sharedKibanaTags []sharedTagYML | ||
| err = yaml.Unmarshal(b, &sharedKibanaTags) | ||
| if err != nil { | ||
| return nil, specerrors.ValidationErrors{specerrors.NewStructuredErrorf("error unmarshaling file %s: %v", tagsPath, err)} | ||
| } | ||
|
|
||
| tags := make([]string, 0) | ||
| errs := make(specerrors.ValidationErrors, 0) | ||
| // Check for duplicate tag names in the kibana/tags.yml file. | ||
| for _, tag := range sharedKibanaTags { | ||
| if slices.Contains(tags, tag.Name) { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file \"%s\" is invalid: duplicate tag name '%s' found", fsys.Path(tagsPath), tag.Name), specerrors.CodeKibanaTagDuplicates)) | ||
| continue | ||
| } | ||
| tags = append(tags, tag.Name) | ||
| } | ||
| return tags, errs | ||
| } | ||
|
|
||
| func validateKibanaPackageTagsDuplicates(fsys fspath.FS, sharedTagNames []string) specerrors.ValidationErrors { | ||
| entries, err := fs.ReadDir(fsys, path.Join("kibana", "tag")) | ||
| if err != nil { | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return nil | ||
| } | ||
| return specerrors.ValidationErrors{specerrors.NewStructuredErrorf("error reading kibana/tag directory: %v", err)} | ||
| } | ||
|
|
||
| tags := make([]string, 0) | ||
| errs := make(specerrors.ValidationErrors, 0) | ||
| for _, entry := range entries { | ||
| if entry.IsDir() || path.Ext(entry.Name()) != ".json" { | ||
| // skip non-json files and directories | ||
| continue | ||
| } | ||
| filePath := path.Join("kibana", "tag", entry.Name()) | ||
| b, err := fs.ReadFile(fsys, filePath) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf("error reading file %s: %v", fsys.Path(filePath), err)) | ||
| continue | ||
| } | ||
| var pkgTag packageSpecTag | ||
| err = json.Unmarshal(b, &pkgTag) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf("error unmarshaling file %s: %v", fsys.Path(filePath), err)) | ||
| continue | ||
| } | ||
| // skip non-tag types | ||
| if pkgTag.Type != tagType { | ||
| continue | ||
| } | ||
|
|
||
| // validate if the tag is already defined in other json file | ||
| if slices.Contains(tags, pkgTag.Attributes.Name) { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file \"%s\" is invalid: duplicate package tag name '%s' found", fsys.Path(filePath), pkgTag.Attributes.Name), specerrors.CodeKibanaTagDuplicates)) | ||
| continue | ||
| } | ||
| if slices.Contains(sharedTagNames, pkgTag.Attributes.Name) { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file \"%s\" is invalid: tag name '%s' is already defined in tags.yml", fsys.Path(filePath), pkgTag.Attributes.Name), specerrors.CodeKibanaTagDuplicates)) | ||
| continue | ||
| } | ||
| tags = append(tags, pkgTag.Attributes.Name) | ||
| } | ||
| return errs | ||
| } |
140 changes: 140 additions & 0 deletions
140
code/go/internal/validator/semantic/validate_kibana_tag_duplicates_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package semantic | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| ) | ||
|
|
||
| func TestGetValidatedSharedKibanaTags(t *testing.T) { | ||
| t.Run("no tags.yml file", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| fsys := fspath.DirFS(tmpDir) | ||
|
|
||
| tags, errs := getValidatedSharedKibanaTags(fsys) | ||
| require.Empty(t, errs) | ||
| assert.Empty(t, tags) | ||
| }) | ||
|
|
||
| t.Run("with tags.yml file and duplicates", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| kibanaDir := filepath.Join(tmpDir, "kibana") | ||
| err := os.MkdirAll(kibanaDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| tagsYMLPath := filepath.Join(kibanaDir, "tags.yml") | ||
| tagsYMLContent := `- text: tag1 | ||
| - text: tag2 | ||
| - text: tag1 | ||
| ` | ||
| err = os.WriteFile(tagsYMLPath, []byte(tagsYMLContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(tmpDir) | ||
| tags, errs := getValidatedSharedKibanaTags(fsys) | ||
| require.Len(t, errs, 1) | ||
| assert.Contains(t, errs[0].Error(), "duplicate tag name 'tag1' found (SVR00007)") | ||
| require.Len(t, tags, 2) | ||
| assert.Contains(t, tags, "tag1") | ||
| assert.Contains(t, tags, "tag2") | ||
| }) | ||
| } | ||
|
|
||
| func TestValidateKibanaPackageTagsDuplicates(t *testing.T) { | ||
| t.Run("with duplicate tags in JSON files", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| kibanaTagDir := filepath.Join(tmpDir, "kibana", "tag") | ||
| err := os.MkdirAll(kibanaTagDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| tag1Path := filepath.Join(kibanaTagDir, "tag1.json") | ||
| tag1Content := `{ | ||
| "attributes": { | ||
| "name": "tagA" | ||
| }, | ||
| "type": "tag" | ||
| }` | ||
| err = os.WriteFile(tag1Path, []byte(tag1Content), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| tag2Path := filepath.Join(kibanaTagDir, "tag2.json") | ||
| tag2Content := `{ | ||
| "attributes": { | ||
| "name": "tagA" | ||
| }, | ||
| "type": "tag" | ||
| }` | ||
| err = os.WriteFile(tag2Path, []byte(tag2Content), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(tmpDir) | ||
| tags := []string{"tagB"} | ||
| errs := validateKibanaPackageTagsDuplicates(fsys, tags) | ||
| require.Len(t, errs, 1) | ||
| assert.Contains(t, errs[0].Error(), "duplicate package tag name 'tagA'") | ||
| }) | ||
|
|
||
| t.Run("with tag in JSON already defined in tags.yml", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| kibanaTagDir := filepath.Join(tmpDir, "kibana", "tag") | ||
| err := os.MkdirAll(kibanaTagDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| tag1Path := filepath.Join(kibanaTagDir, "tag1.json") | ||
| tag1Content := `{ | ||
| "attributes": { | ||
| "name": "tagB" | ||
| }, | ||
| "type": "tag" | ||
| }` | ||
| err = os.WriteFile(tag1Path, []byte(tag1Content), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(tmpDir) | ||
| tags := []string{"tagB"} | ||
| errs := validateKibanaPackageTagsDuplicates(fsys, tags) | ||
| require.Len(t, errs, 1) | ||
| assert.Contains(t, errs[0].Error(), "tag name 'tagB' is already defined in tags.yml (SVR00007)") | ||
| }) | ||
|
|
||
| t.Run("with unique tags in JSON files", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| kibanaTagDir := filepath.Join(tmpDir, "kibana", "tag") | ||
| err := os.MkdirAll(kibanaTagDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| tag1Path := filepath.Join(kibanaTagDir, "tag1.json") | ||
| tag1Content := `{ | ||
| "attributes": { | ||
| "name": "tagA" | ||
| }, | ||
| "type": "tag" | ||
| }` | ||
| err = os.WriteFile(tag1Path, []byte(tag1Content), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| tag2Path := filepath.Join(kibanaTagDir, "tag2.json") | ||
| tag2Content := `{ | ||
| "attributes": { | ||
| "name": "tagB" | ||
| }, | ||
| "type": "tag" | ||
| }` | ||
| err = os.WriteFile(tag2Path, []byte(tag2Content), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(tmpDir) | ||
| tags := []string{"tagC"} | ||
| errs := validateKibanaPackageTagsDuplicates(fsys, tags) | ||
| require.Empty(t, errs) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be some conflicts with this other PR that is also adding the same validation code errors: #1038
https://github.com/elastic/package-spec/pull/1038/files#diff-d8846111760decbd765dc97b91c095ed8264c3f87d42449304e4735930437d1fR25-R26