-
Notifications
You must be signed in to change notification settings - Fork 87
feat: Add validators for pipeline on_failure handler #1038
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 7 commits into
elastic:main
from
taylor-swanson:feat/validate-on-failure
Jan 12, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0accc4a
feat: Add validators for pipeline on_failure handler
taylor-swanson 6708d07
Changelog
taylor-swanson d479c4c
Merge branch 'main' into feat/validate-on-failure
taylor-swanson e5c0aa9
Include non-data-stream pipelines in listPipelineFiles
taylor-swanson c32065b
Merge branch 'main' into feat/validate-on-failure
taylor-swanson 78cb727
Merge branch 'main' into feat/validate-on-failure
taylor-swanson 8b331f7
add comment about empty dir
taylor-swanson 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
141 changes: 141 additions & 0 deletions
141
code/go/internal/validator/semantic/validate_pipeline_on_failure.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,141 @@ | ||
| // 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 ( | ||
| "fmt" | ||
| "io/fs" | ||
| "strings" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" | ||
| ) | ||
|
|
||
| var requiredMessageValues = []string{ | ||
| "_ingest.on_failure_processor_type", | ||
| "_ingest.on_failure_processor_tag", | ||
| "_ingest.on_failure_message", | ||
| "_ingest.pipeline", | ||
| } | ||
|
|
||
| // ValidatePipelineOnFailure validates ingest pipeline global on_failure handlers. | ||
| func ValidatePipelineOnFailure(fsys fspath.FS) specerrors.ValidationErrors { | ||
| dataStreams, err := listDataStreams(fsys) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{specerrors.NewStructuredError(err, specerrors.UnassignedCode)} | ||
| } | ||
|
|
||
| var errs specerrors.ValidationErrors | ||
| for _, dataStream := range dataStreams { | ||
| pipelineFiles, err := listPipelineFiles(fsys, dataStream) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{specerrors.NewStructuredError(err, specerrors.UnassignedCode)} | ||
| } | ||
|
|
||
| for _, pipelineFile := range pipelineFiles { | ||
| content, err := fs.ReadFile(fsys, pipelineFile.filePath) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{specerrors.NewStructuredError(err, specerrors.UnassignedCode)} | ||
| } | ||
|
|
||
| var pipeline ingestPipeline | ||
| if err = yaml.Unmarshal(content, &pipeline); err != nil { | ||
| return specerrors.ValidationErrors{specerrors.NewStructuredError(err, specerrors.UnassignedCode)} | ||
| } | ||
|
|
||
| if vErrs := validatePipelineOnFailure(&pipeline, pipelineFile.fullFilePath); len(vErrs) > 0 { | ||
| errs = append(errs, vErrs...) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| func validatePipelineOnFailure(pipeline *ingestPipeline, filename string) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
|
|
||
| if e := checkSetEventKind(pipeline, filename); len(e) > 0 { | ||
| errs = append(errs, e...) | ||
| } | ||
| if e := checkSetErrorMessage(pipeline, filename); len(e) > 0 { | ||
| errs = append(errs, e...) | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| func checkSetEventKind(pipeline *ingestPipeline, filename string) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
| var found bool | ||
|
|
||
| for _, proc := range pipeline.OnFailure { | ||
| if proc.Type != "set" { | ||
| continue | ||
| } | ||
| if s, ok := proc.GetAttributeString("field"); !ok || s != "event.kind" { | ||
| continue | ||
| } | ||
|
|
||
| found = true | ||
|
|
||
| if s, ok := proc.GetAttributeString("value"); !ok || s != "pipeline_error" { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file %q is invalid: pipeline on_failure handler must set event.kind to \"pipeline_error\"", filename), | ||
| specerrors.CodePipelineOnFailureEventKind), | ||
| ) | ||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| if !found { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file %q is invalid: pipeline on_failure handler must set event.kind to \"pipeline_error\"", filename), | ||
| specerrors.CodePipelineOnFailureEventKind), | ||
| ) | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| func checkSetErrorMessage(pipeline *ingestPipeline, filename string) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
| var found bool | ||
|
|
||
| for _, proc := range pipeline.OnFailure { | ||
| if proc.Type != "set" && proc.Type != "append" { | ||
| continue | ||
| } | ||
| if s, ok := proc.GetAttributeString("field"); !ok || s != "error.message" { | ||
| continue | ||
| } | ||
|
|
||
| found = true | ||
|
|
||
| value, _ := proc.GetAttributeString("value") | ||
| for _, reqMessageValue := range requiredMessageValues { | ||
| if !strings.Contains(value, reqMessageValue) { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file %q is invalid: pipeline on_failure error.message must include %q", filename, reqMessageValue), | ||
| specerrors.CodePipelineOnFailureMessage), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| if !found { | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file %q is invalid: pipeline on_failure handler must set error.message", filename), | ||
| specerrors.CodePipelineOnFailureMessage), | ||
| ) | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
134 changes: 134 additions & 0 deletions
134
code/go/internal/validator/semantic/validate_pipeline_on_failure_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,134 @@ | ||
| // 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 ( | ||
| "testing" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidatePipelineOnFailure(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| pipeline string | ||
| errors []string | ||
| }{ | ||
| { | ||
| name: "good-set", | ||
| pipeline: ` | ||
| on_failure: | ||
| - set: | ||
| field: event.kind | ||
| value: pipeline_error | ||
| - set: | ||
| field: error.message | ||
| value: >- | ||
| Processor '{{{ _ingest.on_failure_processor_type }}}' | ||
| with tag '{{{ _ingest.on_failure_processor_tag }}}' | ||
| in pipeline '{{{ _ingest.pipeline }}}' | ||
| failed with message '{{{ _ingest.on_failure_message }}}' | ||
| `, | ||
| }, | ||
| { | ||
| name: "good-append", | ||
| pipeline: ` | ||
| on_failure: | ||
| - set: | ||
| field: event.kind | ||
| value: pipeline_error | ||
| - append: | ||
| field: error.message | ||
| value: >- | ||
| Processor '{{{ _ingest.on_failure_processor_type }}}' | ||
| with tag '{{{ _ingest.on_failure_processor_tag }}}' | ||
| in pipeline '{{{ _ingest.pipeline }}}' | ||
| failed with message '{{{ _ingest.on_failure_message }}}' | ||
| `, | ||
| }, | ||
| { | ||
| name: "bad-event-kind-missing", | ||
| pipeline: ` | ||
| on_failure: | ||
| - append: | ||
| field: error.message | ||
| value: >- | ||
| Processor '{{{ _ingest.on_failure_processor_type }}}' | ||
| with tag '{{{ _ingest.on_failure_processor_tag }}}' | ||
| in pipeline '{{{ _ingest.pipeline }}}' | ||
| failed with message '{{{ _ingest.on_failure_message }}}' | ||
| `, | ||
| errors: []string{ | ||
| `file "default.yml" is invalid: pipeline on_failure handler must set event.kind to "pipeline_error" (SVR00007)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "bad-event-kind-wrong-value", | ||
| pipeline: ` | ||
| on_failure: | ||
| - set: | ||
| field: event.kind | ||
| value: event | ||
| - append: | ||
| field: error.message | ||
| value: >- | ||
| Processor '{{{ _ingest.on_failure_processor_type }}}' | ||
| with tag '{{{ _ingest.on_failure_processor_tag }}}' | ||
| in pipeline '{{{ _ingest.pipeline }}}' | ||
| failed with message '{{{ _ingest.on_failure_message }}}' | ||
| `, | ||
| errors: []string{ | ||
| `file "default.yml" is invalid: pipeline on_failure handler must set event.kind to "pipeline_error" (SVR00007)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "bad-error-message-missing", | ||
| pipeline: ` | ||
| on_failure: | ||
| - set: | ||
| field: event.kind | ||
| value: pipeline_error | ||
| `, | ||
| errors: []string{ | ||
| `file "default.yml" is invalid: pipeline on_failure handler must set error.message (SVR00008)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "bad-error-message-wrong-value", | ||
| pipeline: ` | ||
| on_failure: | ||
| - set: | ||
| field: event.kind | ||
| value: pipeline_error | ||
| - set: | ||
| field: error.message | ||
| value: Pipeline failed | ||
| `, | ||
| errors: []string{ | ||
| `file "default.yml" is invalid: pipeline on_failure error.message must include "_ingest.on_failure_processor_type" (SVR00008)`, | ||
| `file "default.yml" is invalid: pipeline on_failure error.message must include "_ingest.on_failure_processor_tag" (SVR00008)`, | ||
| `file "default.yml" is invalid: pipeline on_failure error.message must include "_ingest.on_failure_message" (SVR00008)`, | ||
| `file "default.yml" is invalid: pipeline on_failure error.message must include "_ingest.pipeline" (SVR00008)`, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var pipeline ingestPipeline | ||
| err := yaml.Unmarshal([]byte(tc.pipeline), &pipeline) | ||
| require.NoError(t, err) | ||
|
|
||
| errors := validatePipelineOnFailure(&pipeline, "default.yml") | ||
| assert.Len(t, errors, len(tc.errors)) | ||
| for _, err := range errors { | ||
| assert.Contains(t, tc.errors, err.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.