-
Notifications
You must be signed in to change notification settings - Fork 124
Simple includes implementation #2483
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
marc-gr
wants to merge
35
commits into
elastic:main
Choose a base branch
from
marc-gr:feat/includes
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
35 commits
Select commit
Hold shift + click to select a range
04fb45d
Simple includes implementation
marc-gr f778462
Second iteration
marc-gr f24f948
Use link files instead of includes file
marc-gr 52132f5
Collect included pipelines in tests and benchmarks
marc-gr fc8f054
Create complete path if not exist
marc-gr 268e23e
Add test package
marc-gr 4b6dcb0
Add links commands
marc-gr 0fb950c
Improve links commands
marc-gr d4f4c36
List also if not in package
marc-gr f044059
Reorganize code
marc-gr e9196c0
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr 44a1d1a
go mod tidy
marc-gr afc4b26
Only read entire file when copying
marc-gr 53eead0
Add unit tests
marc-gr c2cdf7b
Always copy file on build
marc-gr eb8b05c
remove unused function
marc-gr 0fdd2df
Use package in spec
marc-gr a4322e5
Use package spec
marc-gr 663ec29
Update links usage
marc-gr c10bdee
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr 6c94355
replace package-spec
marc-gr ed5a641
Update readme
marc-gr 3438ac4
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr f30f574
Remove go.mod replace
marc-gr 6526118
Secure linked files: fix path traversal vulnerabilities and improve t…
marc-gr 1e50f26
Improve linkedfiles API with convenience functions and structured res…
marc-gr efd9c9f
fix test cleanup
marc-gr 3c56a23
Use spec 3.4.0 for test package
marc-gr ebf7c56
Fix shared folder placement
marc-gr 1880bcd
Close root usage on cleanup
marc-gr e0e8754
close root
marc-gr b450f33
handle paths better for linksfs
marc-gr 83478ac
Enhance LinksFS security and path handling
marc-gr c091ed0
lint
marc-gr 0d70806
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr 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
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,144 @@ | ||
// 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 cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/elastic-package/internal/cobraext" | ||
"github.com/elastic/elastic-package/internal/files" | ||
) | ||
|
||
const ( | ||
linksLongDescription = `Use this command to manage linked files in the repository.` | ||
linksCheckLongDescription = `Use this command to check if linked files references inside the current directory are up to date.` | ||
linksUpdateLongDescription = `Use this command to update all linked files references inside the current directory.` | ||
linksListLongDescription = `Use this command to list all packages that have linked file references that include the current directory.` | ||
) | ||
|
||
func setupLinksCommand() *cobraext.Command { | ||
cmd := &cobra.Command{ | ||
Use: "links", | ||
Short: "Manage linked files", | ||
Long: linksLongDescription, | ||
RunE: func(parent *cobra.Command, args []string) error { | ||
return cobraext.ComposeCommandsParentContext(parent, args, parent.Commands()...) | ||
}, | ||
} | ||
|
||
cmd.AddCommand(getLinksCheckCommand()) | ||
cmd.AddCommand(getLinksUpdateCommand()) | ||
cmd.AddCommand(getLinksListCommand()) | ||
|
||
return cobraext.NewCommand(cmd, cobraext.ContextGlobal) | ||
} | ||
|
||
func getLinksCheckCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "check", | ||
Short: "Check for linked files changes", | ||
Long: linksCheckLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksCheckCommandAction, | ||
} | ||
return cmd | ||
} | ||
|
||
func linksCheckCommandAction(cmd *cobra.Command, args []string) error { | ||
cmd.Printf("Check for linked files changes\n") | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
linkedFiles, err := files.CheckLinkedFiles(pwd) | ||
if err != nil { | ||
return fmt.Errorf("checking linked files are up-to-date failed: %w", err) | ||
} | ||
for _, f := range linkedFiles { | ||
if !f.UpToDate { | ||
cmd.Printf("%s is outdated.\n", filepath.Join(f.WorkDir, f.LinkFilePath)) | ||
} | ||
} | ||
if len(linkedFiles) > 0 { | ||
return fmt.Errorf("linked files are outdated") | ||
} | ||
return nil | ||
} | ||
|
||
func getLinksUpdateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update linked files checksums if needed.", | ||
Long: linksUpdateLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksUpdateCommandAction, | ||
} | ||
return cmd | ||
} | ||
|
||
func linksUpdateCommandAction(cmd *cobra.Command, args []string) error { | ||
cmd.Printf("Update linked files checksums if needed.\n") | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
updatedLinks, err := files.UpdateLinkedFiles(pwd) | ||
if err != nil { | ||
return fmt.Errorf("updating linked files checksums failed: %w", err) | ||
} | ||
|
||
for _, l := range updatedLinks { | ||
cmd.Printf("%s was updated.\n", l.LinkFilePath) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getLinksListCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List packages linking files from this path", | ||
Long: linksListLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksListCommandAction, | ||
} | ||
cmd.Flags().BoolP(cobraext.PackagesFlagName, "", false, cobraext.PackagesFlagDescription) | ||
return cmd | ||
} | ||
|
||
func linksListCommandAction(cmd *cobra.Command, args []string) error { | ||
onlyPackages, err := cmd.Flags().GetBool(cobraext.PackagesFlagName) | ||
if err != nil { | ||
return cobraext.FlagParsingError(err, cobraext.PackagesFlagName) | ||
} | ||
|
||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
byPackage, err := files.ListLinkedFilesByPackage(pwd) | ||
if err != nil { | ||
return fmt.Errorf("listing linked packages failed: %w", err) | ||
} | ||
|
||
for _, pkg := range byPackage { | ||
if onlyPackages { | ||
cmd.Println(pkg.PackageName) | ||
continue | ||
} | ||
for _, link := range pkg.Links { | ||
cmd.Println(link) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,110 @@ | ||
# HOWTO: Use links to reuse common files. | ||
|
||
## Introduction | ||
|
||
Many packages have files that are equal between them. This is more common in pipelines, | ||
input configurations, and field definitions. | ||
|
||
In order to help developers, there is the ability to define links, so a file that might be reused needs to only be defined once, and can be reused from any other packages. | ||
|
||
|
||
# Links | ||
|
||
Currently, there are some specific places where links can be defined: | ||
|
||
- `elasticsearch/ingest_pipeline` | ||
- `data_stream/**/elasticsearch/ingest_pipeline` | ||
- `agent/input` | ||
- `data_stream/**/agent/stream` | ||
- `data_stream/**/fields` | ||
|
||
A link consists of a file with a `.link` extension that contains a path, relative to its location, to the file that it will be replaced with. It also consists of a checksum to validate the linked file is up to date with the package expectations. | ||
|
||
`data_stream/foo/elasticsearch/ingest_pipeline/default.yml.link` | ||
|
||
``` | ||
../../../../../testpackage/data_stream/test/elasticsearch/ingest_pipeline/default.yml f7c5f0c03aca8ef68c379a62447bdafbf0dcf32b1ff2de143fd6878ee01a91ad | ||
``` | ||
|
||
This will use the contents of the linked file during validation, tests, and building of the package, so functionally nothing changes from the package point of view. | ||
|
||
## The `_dev/shared` folder | ||
|
||
As a convenience, shared files can be placed under `_dev/shared` if they are going to be | ||
reused from several places. They can even be added outside of any package, in any place in the repository. | ||
|
||
## Managing Links with elastic-package | ||
|
||
The `elastic-package` tool provides several subcommands to help manage linked files: | ||
|
||
### `elastic-package links check` | ||
|
||
Check if all linked files in the current directory and its subdirectories are up to date. This command verifies that the checksums in link files match the actual content of the included files. | ||
|
||
```bash | ||
elastic-package links check | ||
``` | ||
|
||
This command will: | ||
- Scan for all `.link` files in the current directory tree | ||
- Validate that each linked file's checksum matches the included file's current content | ||
- Report any outdated link files that need updating | ||
- Exit with an error if any link files are outdated | ||
|
||
### `elastic-package links update` | ||
|
||
Update the checksums of all outdated linked files in the current directory and its subdirectories. | ||
|
||
```bash | ||
elastic-package links update | ||
``` | ||
|
||
This command will: | ||
- Find all `.link` files that have outdated checksums | ||
- Calculate new checksums for the included files | ||
- Update the `.link` files with the new checksums | ||
- Report which link files were updated | ||
|
||
### `elastic-package links list` | ||
|
||
List all packages that have linked files referencing content from the current directory. | ||
|
||
```bash | ||
# List all linked file paths | ||
elastic-package links list | ||
|
||
# List only package names (without individual file paths) | ||
elastic-package links list --packages | ||
``` | ||
|
||
This command will: | ||
- Find all `.link` files in the repository that reference files in the current directory | ||
- Group the results by package name | ||
- Display either the full file paths or just the package names (with `--packages` flag) | ||
|
||
## Workflow | ||
|
||
A typical workflow for managing linked files: | ||
|
||
1. **Create a shared file** in a central location (e.g., `_dev/shared/` or in a reference package) | ||
|
||
2. **Create link files** in packages that need to reference the shared file: | ||
```bash | ||
echo "../../_dev/shared/common-pipeline.yml" > data_stream/logs/elasticsearch/ingest_pipeline/default.yml.link | ||
``` | ||
|
||
3. **Update checksums** to make the link valid: | ||
```bash | ||
elastic-package links update | ||
``` | ||
|
||
4. **Check links regularly** to ensure they stay up to date: | ||
```bash | ||
elastic-package links check | ||
``` | ||
|
||
5. **When modifying shared files**, update all dependent links: | ||
```bash | ||
# After editing a shared file, update all links that reference it | ||
elastic-package links update | ||
``` |
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.