-
Notifications
You must be signed in to change notification settings - Fork 90
Add support for inputs with qualified names #1135
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
79 changes: 79 additions & 0 deletions
79
code/go/internal/validator/semantic/validate_integration_input_qualifier.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,79 @@ | ||
| // 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" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" | ||
| ) | ||
|
|
||
| type integrationInputQualifier struct { | ||
| Name string `yaml:"name"` | ||
| Type string `yaml:"type"` | ||
| } | ||
|
|
||
| type integrationPolicyTemplateQualifier struct { | ||
| Name string `yaml:"name"` | ||
| Inputs []integrationInputQualifier `yaml:"inputs"` | ||
| } | ||
|
|
||
| type integrationPackageManifestQualifier struct { | ||
| Type string `yaml:"type"` | ||
| PolicyTemplates []integrationPolicyTemplateQualifier `yaml:"policy_templates"` | ||
| } | ||
|
|
||
| // ValidateIntegrationInputQualifier checks that if a policy template contains | ||
| // multiple inputs of the same type, all of them must have a name set. Without | ||
| // names, Fleet cannot distinguish them, leading to the ambiguity this field | ||
| // aims to solve. | ||
| func ValidateIntegrationInputQualifier(fsys fspath.FS) specerrors.ValidationErrors { | ||
| manifestPath := "manifest.yml" | ||
| data, err := fs.ReadFile(fsys, manifestPath) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("file \"%s\" is invalid: failed to read manifest: %w", fsys.Path(manifestPath), err)} | ||
| } | ||
|
|
||
| var manifest integrationPackageManifestQualifier | ||
| if err := yaml.Unmarshal(data, &manifest); err != nil { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("file \"%s\" is invalid: failed to parse manifest: %w", fsys.Path(manifestPath), err)} | ||
| } | ||
|
|
||
| if manifest.Type != integrationPackageType { | ||
| return nil | ||
| } | ||
|
|
||
| return validateInputQualifiers(fsys, manifest, manifestPath) | ||
| } | ||
|
|
||
| func validateInputQualifiers(fsys fspath.FS, manifest integrationPackageManifestQualifier, manifestPath string) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
|
|
||
| for _, policyTemplate := range manifest.PolicyTemplates { | ||
| typeCounts := make(map[string]int) | ||
| for _, input := range policyTemplate.Inputs { | ||
| typeCounts[input.Type]++ | ||
| } | ||
|
|
||
| reported := make(map[string]bool) | ||
| for _, input := range policyTemplate.Inputs { | ||
| if typeCounts[input.Type] > 1 && input.Name == "" && !reported[input.Type] { | ||
| reported[input.Type] = true | ||
| errs = append(errs, specerrors.NewStructuredError( | ||
| fmt.Errorf("file \"%s\" is invalid: policy template \"%s\": input with type \"%s\" must have a name when multiple inputs of the same type are present", | ||
| fsys.Path(manifestPath), policyTemplate.Name, input.Type), | ||
| specerrors.CodeIntegrationInputQualifierRequired)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } |
134 changes: 134 additions & 0 deletions
134
code/go/internal/validator/semantic/validate_integration_input_qualifier_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 ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" | ||
| ) | ||
|
|
||
| func TestValidateIntegrationInputQualifier(t *testing.T) { | ||
| tests := map[string]struct { | ||
| manifest string | ||
| expectedErrs []string | ||
| expectedCodes []string | ||
| }{ | ||
| "single_input_per_type_no_name": { | ||
| manifest: ` | ||
| type: integration | ||
| policy_templates: | ||
| - name: nginx | ||
| inputs: | ||
| - type: logfile | ||
| title: Logs | ||
| description: Collect logs | ||
| - type: httpjson | ||
| title: Metrics | ||
| description: Collect metrics | ||
| `, | ||
| }, | ||
| "multiple_inputs_same_type_with_names": { | ||
| manifest: ` | ||
| type: integration | ||
| policy_templates: | ||
| - name: nginx | ||
| inputs: | ||
| - name: filelog_otel | ||
| type: otelcol | ||
| title: Logs | ||
| description: Collect logs | ||
| - name: nginx_otel | ||
| type: otelcol | ||
| title: Metrics | ||
| description: Collect metrics | ||
| `, | ||
| }, | ||
| "multiple_inputs_same_type_no_names": { | ||
| manifest: ` | ||
| type: integration | ||
| policy_templates: | ||
| - name: nginx | ||
| inputs: | ||
| - type: otelcol | ||
| title: Logs | ||
| description: Collect logs | ||
| - type: otelcol | ||
| title: Metrics | ||
| description: Collect metrics | ||
| `, | ||
| expectedErrs: []string{ | ||
| `policy template "nginx": input with type "otelcol" must have a name when multiple inputs of the same type are present`, | ||
| }, | ||
| expectedCodes: []string{ | ||
| specerrors.CodeIntegrationInputQualifierRequired, | ||
| }, | ||
| }, | ||
| "multiple_inputs_same_type_partial_names": { | ||
| manifest: ` | ||
| type: integration | ||
| policy_templates: | ||
| - name: nginx | ||
| inputs: | ||
| - name: filelog_otel | ||
| type: otelcol | ||
| title: Logs | ||
| description: Collect logs | ||
| - type: otelcol | ||
| title: Metrics | ||
| description: Collect metrics | ||
| `, | ||
| expectedErrs: []string{ | ||
| `policy template "nginx": input with type "otelcol" must have a name when multiple inputs of the same type are present`, | ||
| }, | ||
| expectedCodes: []string{ | ||
| specerrors.CodeIntegrationInputQualifierRequired, | ||
| }, | ||
| }, | ||
| "non_integration_package": { | ||
| manifest: ` | ||
| type: input | ||
| policy_templates: | ||
| - name: sample | ||
| inputs: | ||
| - type: logfile | ||
| title: Logs | ||
| description: Collect logs | ||
| - type: logfile | ||
| title: More logs | ||
| description: Collect more logs | ||
| `, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| d := t.TempDir() | ||
| err := os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(tc.manifest), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| errs := ValidateIntegrationInputQualifier(fspath.DirFS(d)) | ||
|
|
||
| if len(tc.expectedErrs) == 0 { | ||
| require.Empty(t, errs) | ||
| return | ||
| } | ||
|
|
||
| require.Len(t, errs, len(tc.expectedErrs)) | ||
| for i, expected := range tc.expectedErrs { | ||
| assert.True(t, strings.Contains(errs[i].Error(), expected), | ||
| "error %q does not contain %q", errs[i].Error(), expected) | ||
| assert.Equal(t, tc.expectedCodes[i], errs[i].Code()) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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,5 @@ | ||
| - version: 0.0.1 | ||
| changes: | ||
| - description: Initial release | ||
| type: enhancement | ||
| link: https://github.com/elastic/package-spec/pull/1234 |
15 changes: 15 additions & 0 deletions
15
test/packages/bad_input_qualifier_ambiguous/data_stream/access/agent/stream/stream.yml.hbs
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,15 @@ | ||
| receivers: | ||
| filelog: | ||
| include: | ||
| {{#each paths}} | ||
| - {{this}} | ||
| {{/each}} | ||
| exporters: | ||
| {{#each exporters}} | ||
| {{this}} | ||
| {{/each}} | ||
| service: | ||
| pipelines: | ||
| {{#each pipelines}} | ||
| {{this}} | ||
| {{/each}} |
18 changes: 18 additions & 0 deletions
18
...ad_input_qualifier_ambiguous/data_stream/access/elasticsearch/ingest_pipeline/default.yml
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,18 @@ | ||
| --- | ||
| description: Pipeline for processing Nginx access logs | ||
| processors: | ||
| - set: | ||
| tag: set_data_stream_type | ||
| field: data_stream.type | ||
| value: logs | ||
| 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 }}}' |
12 changes: 12 additions & 0 deletions
12
test/packages/bad_input_qualifier_ambiguous/data_stream/access/fields/fields.yml
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,12 @@ | ||
| - name: data_stream.type | ||
| type: constant_keyword | ||
| description: Data stream type. | ||
| - name: data_stream.dataset | ||
| type: constant_keyword | ||
| description: Data stream dataset. | ||
| - name: data_stream.namespace | ||
| type: constant_keyword | ||
| description: Data stream namespace. | ||
| - name: '@timestamp' | ||
| type: date | ||
| description: Event timestamp. |
15 changes: 15 additions & 0 deletions
15
test/packages/bad_input_qualifier_ambiguous/data_stream/access/manifest.yml
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,15 @@ | ||
| title: Nginx Access Logs | ||
| type: logs | ||
| streams: | ||
| - input: filelog_otel | ||
| title: Collect Nginx access logs via filelog OTel receiver | ||
| description: Collecting Nginx access logs via filelog OpenTelemetry receiver. | ||
| vars: | ||
| - name: paths | ||
| type: text | ||
| title: Log file paths | ||
| multi: true | ||
| required: true | ||
| show_user: true | ||
| default: | ||
| - /var/log/nginx/access.log |
13 changes: 13 additions & 0 deletions
13
...packages/bad_input_qualifier_ambiguous/data_stream/stubstatus/agent/stream/stream.yml.hbs
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,13 @@ | ||
| receivers: | ||
| nginx: | ||
| endpoint: http://127.0.0.1/nginx_status | ||
| collection_interval: {{period}} | ||
| exporters: | ||
| {{#each exporters}} | ||
| {{this}} | ||
| {{/each}} | ||
| service: | ||
| pipelines: | ||
| {{#each pipelines}} | ||
| {{this}} | ||
| {{/each}} |
18 changes: 18 additions & 0 deletions
18
...nput_qualifier_ambiguous/data_stream/stubstatus/elasticsearch/ingest_pipeline/default.yml
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,18 @@ | ||
| --- | ||
| description: Pipeline for processing Nginx stub status metrics | ||
| processors: | ||
| - set: | ||
| tag: set_data_stream_type | ||
| field: data_stream.type | ||
| value: metrics | ||
| 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 }}}' |
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.
would this be 3.6.1?
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.
I would like to allow this from 3.6.0.