-
Notifications
You must be signed in to change notification settings - Fork 87
Add validation for template_path at input packages
#1000
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 9 commits into
elastic:main
from
teresaromero:703-input-packages-template-path-validation
Oct 22, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f3a6998
Add validation for input package policy templates and corresponding t…
teresaromero 6711aca
Add input package policy template validation and update test manifests
teresaromero 4b7eefd
Add template_path to required properties in input package manifest
teresaromero 33763f1
Update doc for function and add changelog
teresaromero 97f7f13
Fix import order in validate_input_policy_template_template_path_test.go
teresaromero 203245d
Enhance policy template validation to check for templates in multiple…
teresaromero 677b592
Revert "Enhance policy template validation to check for templates in …
teresaromero e079f63
Add test package for failed case
teresaromero 7e723e3
Refactor package type definition to use string instead of custom type
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
100 changes: 100 additions & 0 deletions
100
code/go/internal/validator/semantic/validate_input_policy_template_template_path.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,100 @@ | ||
| // 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 ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path" | ||
|
|
||
| "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 ( | ||
| inputPackageType packageType = "input" | ||
| ) | ||
|
|
||
| var ( | ||
| errRequiredTemplatePath = errors.New("template_path is required for input type packages") | ||
| errFailedToReadManifest = errors.New("failed to read manifest") | ||
| errFailedToParseManifest = errors.New("failed to parse manifest") | ||
| errTemplateNotFound = errors.New("template file not found") | ||
| errInvalidPackageType = errors.New("invalid package type") | ||
| ) | ||
|
|
||
| type packageType string | ||
|
|
||
| type inputPolicyTemplate struct { | ||
| Name string `yaml:"name"` | ||
| TemplatePath string `yaml:"template_path"` // input type packages require this field | ||
| } | ||
|
|
||
| type inputPackageManifest struct { // package manifest | ||
| Type packageType `yaml:"type"` | ||
teresaromero marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| PolicyTemplates []inputPolicyTemplate `yaml:"policy_templates"` | ||
| } | ||
|
|
||
| // ValidateInputPackagesPolicyTemplates validates the policy template entries of an input package | ||
| func ValidateInputPackagesPolicyTemplates(fsys fspath.FS) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
|
|
||
| manifestPath := "manifest.yml" | ||
| data, err := fs.ReadFile(fsys, manifestPath) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("file \"%s\" is invalid: %ww", fsys.Path(manifestPath), errFailedToReadManifest)} | ||
| } | ||
|
|
||
| var manifest inputPackageManifest | ||
| err = yaml.Unmarshal(data, &manifest) | ||
| if err != nil { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("file \"%s\" is invalid: %w", fsys.Path(manifestPath), errFailedToParseManifest)} | ||
| } | ||
|
|
||
| if manifest.Type != inputPackageType { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("file \"%s\" is invalid: expected package type \"%s\", got \"%s\": %w", | ||
| fsys.Path(manifestPath), inputPackageType, manifest.Type, errInvalidPackageType)} | ||
| } | ||
|
|
||
| for _, policyTemplate := range manifest.PolicyTemplates { | ||
| err := validateInputPackagePolicyTemplate(fsys, policyTemplate) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf( | ||
| "file \"%s\" is invalid: policy template \"%s\" references template_path \"%s\": %w", | ||
| fsys.Path(manifestPath), policyTemplate.Name, policyTemplate.TemplatePath, err)) | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| // validateInputPackagePolicyTemplate validates the template_path at the policy template level for input type packages | ||
| // if the template_path is empty, it returns an error as this field is required for input type packages | ||
| func validateInputPackagePolicyTemplate(fsys fspath.FS, policyTemplate inputPolicyTemplate) error { | ||
| if policyTemplate.TemplatePath == "" { | ||
| return errRequiredTemplatePath | ||
| } | ||
| return validateAgentInputTemplatePath(fsys, policyTemplate.TemplatePath) | ||
| } | ||
|
|
||
| func validateAgentInputTemplatePath(fsys fspath.FS, tmplPath string) error { | ||
| templatePath := path.Join("agent", "input", tmplPath) | ||
| _, err := fs.Stat(fsys, templatePath) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return errTemplateNotFound | ||
| } | ||
| return fmt.Errorf("failed to stat template file %s: %w", fsys.Path(templatePath), err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
code/go/internal/validator/semantic/validate_input_policy_template_template_path_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,97 @@ | ||
| // 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 TestValidateInputPackagesPolicyTemplates(t *testing.T) { | ||
|
|
||
| t.Run("policy_templates_have_template_path", func(t *testing.T) { | ||
| d := t.TempDir() | ||
|
|
||
| err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` | ||
| type: input | ||
| policy_templates: | ||
| - name: udp | ||
| template_path: udp.yml.hbs | ||
| `), 0o644) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(d, "agent", "input", "udp.yml.hbs"), []byte("# UDP template"), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) | ||
| require.Empty(t, errs, "expected no validation errors") | ||
|
|
||
| }) | ||
|
|
||
| t.Run("policy_templates_empty_template_path", func(t *testing.T) { | ||
| d := t.TempDir() | ||
|
|
||
| err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` | ||
| type: input | ||
| policy_templates: | ||
| - name: udp | ||
| `), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) | ||
| require.NotEmpty(t, errs, "expected no validation errors") | ||
|
|
||
| assert.Len(t, errs, 1) | ||
| assert.ErrorIs(t, errs[0], errRequiredTemplatePath) | ||
| }) | ||
|
|
||
| t.Run("policy_templates_missing_template_path", func(t *testing.T) { | ||
| d := t.TempDir() | ||
|
|
||
| err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` | ||
| type: input | ||
| policy_templates: | ||
| - name: udp | ||
| template_path: missing.yml.hbs | ||
| `), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) | ||
| require.NotEmpty(t, errs, "expected validation errors") | ||
| assert.Len(t, errs, 1) | ||
| assert.ErrorIs(t, errs[0], errTemplateNotFound) | ||
| }) | ||
|
|
||
| t.Run("not_input_package_type", func(t *testing.T) { | ||
| d := t.TempDir() | ||
|
|
||
| err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) | ||
| require.NoError(t, err) | ||
| err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` | ||
| type: integration | ||
| policy_templates: | ||
| - name: udp | ||
| template_path: missing.yml.hbs | ||
| `), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) | ||
| require.NotEmpty(t, errs, "expected validation errors") | ||
| assert.Len(t, errs, 1) | ||
| assert.ErrorIs(t, errs[0], errInvalidPackageType) | ||
| }) | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.