-
Notifications
You must be signed in to change notification settings - Fork 86
Handlebars template validation and documentation #1030
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
Open
teresaromero
wants to merge
21
commits into
elastic:main
Choose a base branch
from
teresaromero:21-handlebars-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d6d204e
Add Handlebars template validation and corresponding tests
teresaromero ea6d4bd
add validator test cases with bad packages
teresaromero 67389f6
Add validation for Handlebars template files in input and integration…
teresaromero 97a32b2
Refactor Handlebars definition specifications for clarity and consist…
teresaromero f5854ea
Update changelog with link
teresaromero 32ebc29
Fix variable naming for handlebars template validation error
teresaromero e76c19a
Fix syntax error in sql_query definition across multiple Handlebars t…
teresaromero 49c9c5a
Reorder import statements for consistency in validate_hbs_templates.go
teresaromero 86281bb
update compliace gomod
teresaromero 3e48130
Refactor Handlebars validation logic to include linked files
teresaromero 32ab54d
Add bad integration Handlebars templates with linked files
teresaromero bc7eee5
Reorder import statements for consistency in validate_hbs_templates_t…
teresaromero 0f1ae93
use path instead of filepath
teresaromero d4898fe
Replace filepath.Join with path.Join in TestValidateHandlebarsFiles f…
teresaromero d6ee0cd
fix validateHandlebarsEntry to read file content from filesystem or a…
teresaromero ab1a541
Reorder logrus dependency in go.mod for consistency
teresaromero 0d298e7
Improve error messages in Handlebars validation for clarity
teresaromero e699759
Add link to Fleet implementation link for available Handlebars helper…
teresaromero 31644e8
Refactor Handlebars validation to return structured errors and update…
teresaromero 62679a5
Merge branch 'main' of github.com:elastic/package-spec into 21-handle…
teresaromero 17b18db
append errors instead of return
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
114 changes: 114 additions & 0 deletions
114
code/go/internal/validator/semantic/validate_hbs_templates.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,114 @@ | ||
| // 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" | ||
| "io/fs" | ||
| "os" | ||
| "path" | ||
|
|
||
| "github.com/mailgun/raymond/v2" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| "github.com/elastic/package-spec/v3/code/go/internal/linkedfiles" | ||
| "github.com/elastic/package-spec/v3/code/go/pkg/specerrors" | ||
| ) | ||
|
|
||
| var ( | ||
| errInvalidHandlebarsTemplate = errors.New("invalid handlebars template") | ||
| ) | ||
|
|
||
| // ValidateHandlebarsFiles validates all Handlebars (.hbs) files in the package filesystem. | ||
| // It returns a list of validation errors if any Handlebars files are invalid. | ||
| // hbs are located in both the package root and data stream directories under the agent folder. | ||
| func ValidateHandlebarsFiles(fsys fspath.FS) specerrors.ValidationErrors { | ||
| var errs specerrors.ValidationErrors | ||
|
|
||
| // template files are placed at /agent/input directory or | ||
| // at the datastream /agent/stream directory | ||
| inputDir := path.Join("agent", "input") | ||
| if inputErrs := validateTemplateDir(fsys, inputDir); inputErrs != nil { | ||
| errs = append(errs, inputErrs...) | ||
| } | ||
|
|
||
| datastreamEntries, err := fs.ReadDir(fsys, "data_stream") | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("error reading data_stream directory: %w", err), | ||
| } | ||
| } | ||
| for _, dsEntry := range datastreamEntries { | ||
| if !dsEntry.IsDir() { | ||
| continue | ||
| } | ||
| streamDir := path.Join("data_stream", dsEntry.Name(), "agent", "stream") | ||
| dsErrs := validateTemplateDir(fsys, streamDir) | ||
| if dsErrs != nil { | ||
| errs = append(errs, dsErrs...) | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| // validateTemplateDir validates all Handlebars files in the given directory. | ||
| func validateTemplateDir(fsys fspath.FS, dir string) specerrors.ValidationErrors { | ||
| entries, err := fs.ReadDir(fsys, dir) | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| return specerrors.ValidationErrors{ | ||
| specerrors.NewStructuredErrorf("error trying to read :%s", dir), | ||
| } | ||
| } | ||
| var errs specerrors.ValidationErrors | ||
| for _, entry := range entries { | ||
| if path.Ext(entry.Name()) == ".hbs" { | ||
| err := validateHandlebarsEntry(fsys, dir, entry.Name()) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf("%w: error validating %s: %w", errInvalidHandlebarsTemplate, path.Join(dir, entry.Name()), err)) | ||
| } | ||
| continue | ||
| } | ||
| if path.Ext(entry.Name()) == ".link" { | ||
| linkFilePath := path.Join(dir, entry.Name()) | ||
| linkFile, err := linkedfiles.NewLinkedFile(fsys.Path(linkFilePath)) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf("error reading linked file %s: %w", linkFilePath, err)) | ||
| continue | ||
| } | ||
| err = validateHandlebarsEntry(fsys, dir, linkFile.IncludedFilePath) | ||
| if err != nil { | ||
| errs = append(errs, specerrors.NewStructuredErrorf("%w: error validating %s: %w", errInvalidHandlebarsTemplate, path.Join(dir, linkFile.IncludedFilePath), err)) | ||
| } | ||
| } | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| // validateHandlebarsEntry validates a single Handlebars file located at filePath. | ||
| // it parses the file using the raymond library to check for syntax errors. | ||
| func validateHandlebarsEntry(fsys fspath.FS, dir, entryName string) error { | ||
| if entryName == "" { | ||
| return nil | ||
| } | ||
|
|
||
| var content []byte | ||
| var err error | ||
|
|
||
| // First try to read from filesystem (works for regular files and files within zip) | ||
| filePath := path.Join(dir, entryName) | ||
| if content, err = fs.ReadFile(fsys, filePath); err != nil { | ||
| // If fs.ReadFile fails (likely due to linked file path outside filesystem boundary), | ||
| // fall back to absolute path approach like linkedfiles.FS does | ||
| absolutePath := fsys.Path(filePath) | ||
| if content, err = os.ReadFile(absolutePath); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Parse from content string instead of file path | ||
| _, err = raymond.Parse(string(content)) | ||
| return err | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
code/go/internal/validator/semantic/validate_hbs_templates_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,125 @@ | ||
| // 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" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/elastic/package-spec/v3/code/go/internal/fspath" | ||
| ) | ||
|
|
||
| func TestValidateTemplateDir(t *testing.T) { | ||
| t.Run("empty directory", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pkgDir := path.Join(tmpDir, "package") | ||
| err := os.MkdirAll(pkgDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| templateDir := path.Join(pkgDir, "agent", "input") | ||
| err = os.MkdirAll(templateDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(pkgDir) | ||
| errs := validateTemplateDir(fsys, path.Join("agent", "input")) | ||
| require.Empty(t, errs) | ||
|
|
||
| }) | ||
| t.Run("valid handlebars file", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pkgDir := path.Join(tmpDir, "package") | ||
| err := os.MkdirAll(pkgDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| templateDir := path.Join(pkgDir, "agent", "input") | ||
| err = os.MkdirAll(templateDir, 0o755) | ||
| require.NoError(t, err) | ||
| hbsFilePath := path.Join(templateDir, "template.hbs") | ||
| hbsContent := `{{#if condition}}Valid Handlebars{{/if}}` | ||
| err = os.WriteFile(hbsFilePath, []byte(hbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(pkgDir) | ||
| errs := validateTemplateDir(fsys, path.Join("agent", "input")) | ||
| require.Empty(t, errs) | ||
| }) | ||
| t.Run("invalid handlebars file", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pkgDir := path.Join(tmpDir, "package") | ||
| err := os.MkdirAll(pkgDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| templateDir := path.Join(pkgDir, "agent", "input") | ||
| err = os.MkdirAll(templateDir, 0o755) | ||
| require.NoError(t, err) | ||
| hbsFilePath := path.Join(templateDir, "template.hbs") | ||
| hbsContent := `{{#if condition}}Valid Handlebars` | ||
| err = os.WriteFile(hbsFilePath, []byte(hbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(pkgDir) | ||
| errs := validateTemplateDir(fsys, path.Join("agent", "input")) | ||
| require.NotEmpty(t, errs) | ||
| assert.Len(t, errs, 1) | ||
| }) | ||
| t.Run("valid linked handlebars file", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pkgDir := path.Join(tmpDir, "package") | ||
| err := os.MkdirAll(pkgDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| pkgDirLinked := path.Join(tmpDir, "linked") | ||
| err = os.MkdirAll(pkgDirLinked, 0o755) | ||
| require.NoError(t, err) | ||
| linkedHbsFilePath := path.Join(pkgDirLinked, "linked_template.hbs") | ||
| linkedHbsContent := `{{#if condition}}Valid Linked Handlebars{{/if}}` | ||
| err = os.WriteFile(linkedHbsFilePath, []byte(linkedHbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| templateDir := path.Join(pkgDir, "agent", "input") | ||
| err = os.MkdirAll(templateDir, 0o755) | ||
| require.NoError(t, err) | ||
| hbsFilePath := path.Join(templateDir, "template.hbs.link") | ||
| hbsContent := `../../../linked/linked_template.hbs` | ||
| err = os.WriteFile(hbsFilePath, []byte(hbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(pkgDir) | ||
| errs := validateTemplateDir(fsys, path.Join("agent", "input")) | ||
| require.Empty(t, errs) | ||
|
|
||
| }) | ||
| t.Run("invalid linked handlebars file", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| pkgDir := path.Join(tmpDir, "package") | ||
| err := os.MkdirAll(pkgDir, 0o755) | ||
| require.NoError(t, err) | ||
|
|
||
| pkgDirLinked := path.Join(tmpDir, "linked") | ||
| err = os.MkdirAll(pkgDirLinked, 0o755) | ||
| require.NoError(t, err) | ||
| linkedHbsFilePath := path.Join(pkgDirLinked, "linked_template.hbs") | ||
| linkedHbsContent := `{{#if condition}}Valid Linked Handlebars` | ||
| err = os.WriteFile(linkedHbsFilePath, []byte(linkedHbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| templateDir := path.Join(pkgDir, "agent", "input") | ||
| err = os.MkdirAll(templateDir, 0o755) | ||
| require.NoError(t, err) | ||
| hbsFilePath := path.Join(templateDir, "template.hbs.link") | ||
| hbsContent := `../../../linked/linked_template.hbs` | ||
| err = os.WriteFile(hbsFilePath, []byte(hbsContent), 0o644) | ||
| require.NoError(t, err) | ||
|
|
||
| fsys := fspath.DirFS(pkgDir) | ||
| errs := validateTemplateDir(fsys, path.Join("agent", "input")) | ||
| require.NotEmpty(t, errs) | ||
| assert.Len(t, errs, 1) | ||
| }) | ||
| } |
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
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.
According to the comment, is this already done by the linkedfiles.FS ? If so, maybe it could be removed from here.
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.
this refers to the logic at
getLinkedFileChecksumwhere the file content is read to get the checksum. If the file is not within fsysfs.ReadFilefails, as is not in its scope. If this happens, we useos.ReadFile(like ingetLinkedFileChecksum) to read the file with the absolute path of it