Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
04fb45d
Simple includes implementation
marc-gr Mar 18, 2025
f778462
Second iteration
marc-gr Mar 18, 2025
f24f948
Use link files instead of includes file
marc-gr Mar 19, 2025
52132f5
Collect included pipelines in tests and benchmarks
marc-gr Mar 20, 2025
fc8f054
Create complete path if not exist
marc-gr Mar 25, 2025
268e23e
Add test package
marc-gr Mar 25, 2025
4b6dcb0
Add links commands
marc-gr Mar 25, 2025
0fb950c
Improve links commands
marc-gr Mar 26, 2025
d4f4c36
List also if not in package
marc-gr Mar 26, 2025
f044059
Reorganize code
marc-gr Mar 27, 2025
e9196c0
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Mar 27, 2025
44a1d1a
go mod tidy
marc-gr Mar 27, 2025
afc4b26
Only read entire file when copying
marc-gr Mar 28, 2025
53eead0
Add unit tests
marc-gr Mar 31, 2025
c2cdf7b
Always copy file on build
marc-gr Mar 31, 2025
eb8b05c
remove unused function
marc-gr Mar 31, 2025
0fdd2df
Use package in spec
marc-gr Apr 2, 2025
a4322e5
Use package spec
marc-gr Apr 2, 2025
663ec29
Update links usage
marc-gr Apr 10, 2025
c10bdee
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Apr 10, 2025
6c94355
replace package-spec
marc-gr Apr 10, 2025
ed5a641
Update readme
marc-gr Apr 10, 2025
3438ac4
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 23, 2025
f30f574
Remove go.mod replace
marc-gr Jun 23, 2025
6526118
Secure linked files: fix path traversal vulnerabilities and improve t…
marc-gr Jun 26, 2025
1e50f26
Improve linkedfiles API with convenience functions and structured res…
marc-gr Jun 26, 2025
efd9c9f
fix test cleanup
marc-gr Jun 26, 2025
3c56a23
Use spec 3.4.0 for test package
marc-gr Jun 26, 2025
ebf7c56
Fix shared folder placement
marc-gr Jun 26, 2025
1880bcd
Close root usage on cleanup
marc-gr Jun 26, 2025
e0e8754
close root
marc-gr Jun 26, 2025
b450f33
handle paths better for linksfs
marc-gr Jun 27, 2025
83478ac
Enhance LinksFS security and path handling
marc-gr Jun 27, 2025
c091ed0
lint
marc-gr Jun 27, 2025
0d70806
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 27, 2025
1fb6aa5
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 30, 2025
12a0bc1
Restore LinksFS support lost in upstream merge
marc-gr Jun 30, 2025
bd5b9b8
Use LinksFS as the source for operations
marc-gr Jul 8, 2025
7668010
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jul 8, 2025
d4d7634
make linksfs work with relative paths from root and wd
marc-gr Jul 8, 2025
2ab1703
check
marc-gr Jul 8, 2025
70e6bc2
remove err declaration
marc-gr Jul 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve links commands
  • Loading branch information
marc-gr committed Mar 26, 2025
commit 0fb950c1a6d343bdf29488ebb0f124e333ec612c
14 changes: 10 additions & 4 deletions cmd/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
"github.com/elastic/elastic-package/internal/cobraext"
)

const linksLongDescription = ``
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{
Expand All @@ -37,6 +42,7 @@ func getLinksCheckCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check for linked files changes",
Long: linksCheckLongDescription,
Args: cobra.NoArgs,
RunE: linksCheckCommandAction,
}
Expand All @@ -56,7 +62,7 @@ func linksCheckCommandAction(cmd *cobra.Command, args []string) error {
}
for _, f := range linkedFiles {
if !f.UpToDate {
cmd.Printf("%s is outdated.\n", f.Path)
cmd.Printf("%s is outdated.\n", f.LinkFilePath)
}
}
if len(linkedFiles) > 0 {
Expand All @@ -69,6 +75,7 @@ func getLinksUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update linked files checksums if needed.",
Long: linksUpdateLongDescription,
Args: cobra.NoArgs,
RunE: linksUpdateCommandAction,
}
Expand All @@ -89,7 +96,7 @@ func linksUpdateCommandAction(cmd *cobra.Command, args []string) error {

for _, f := range linkedFiles {
if !f.UpToDate {
cmd.Printf("%s is outdated.\n", f.Path)
cmd.Printf("%s is outdated.\n", f.LinkFilePath)
}
}

Expand All @@ -107,7 +114,6 @@ func getLinksListCommand() *cobra.Command {
}

func linksListCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("List packages linking files from this path.\n")
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("reading current working directory failed: %w", err)
Expand Down
97 changes: 58 additions & 39 deletions internal/builder/linked_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import (
)

type Link struct {
Path string
Checksum string
LinkPackageName string
LinkPackagePath string
LinkFilePath string
LinkChecksum string

TargetFilePath string

IncludedPackageName string
IncludedPackagePath string
IncludedFilePath string
IncludedFileContents []byte
IncludedFileContentsChecksum string
Expand All @@ -44,7 +48,7 @@ func AreLinkedFilesUpToDate(fromDir string) ([]Link, error) {

var outdated []Link
for _, l := range links {
logger.Debugf("Check if %s is up-to-date", l.Path)
logger.Debugf("Check if %s is up-to-date", l.LinkFilePath)
if !l.UpToDate {
outdated = append(outdated, l)
}
Expand All @@ -65,9 +69,10 @@ func IncludeLinkedFiles(fromDir, toDir string) ([]Link, error) {
}
if !l.UpToDate {
newContent := fmt.Sprintf("%v %v", l.IncludedFilePath, l.IncludedFileContentsChecksum)
if err := writeFile(l.Path, []byte(newContent)); err != nil {
return nil, fmt.Errorf("could not update checksum for file %v: %w", l.Path, err)
if err := writeFile(l.LinkFilePath, []byte(newContent)); err != nil {
return nil, fmt.Errorf("could not update checksum for file %v: %w", l.LinkFilePath, err)
}
logger.Debugf("%v updated", l.LinkFilePath)
}
logger.Debugf("%v included in package", l.TargetFilePath)
}
Expand All @@ -84,19 +89,19 @@ func UpdateLinkedFilesChecksums(fromDir string) ([]Link, error) {
for _, l := range links {
if !l.UpToDate {
newContent := fmt.Sprintf("%v %v", l.IncludedFilePath, l.IncludedFileContentsChecksum)
if err := writeFile(l.Path, []byte(newContent)); err != nil {
return nil, fmt.Errorf("could not update checksum for file %v: %w", l.Path, err)
if err := writeFile(l.LinkFilePath, []byte(newContent)); err != nil {
return nil, fmt.Errorf("could not update checksum for file %v: %w", l.LinkFilePath, err)
}
logger.Debugf("%v updated", l.Path)
logger.Debugf("%v updated", l.LinkFilePath)
}
}

return links, nil
}

func ListPackagesWithLinkedFilesFrom(includedPath string) ([]string, error) {
func ListPackagesWithLinkedFilesFrom(fromPath string) ([]string, error) {
defer func() {
if err := os.Chdir(filepath.Dir(includedPath)); err != nil {
if err := os.Chdir(filepath.Dir(fromPath)); err != nil {
logger.Errorf("could not change directory: %w", err)
}
}()
Expand All @@ -108,30 +113,18 @@ func ListPackagesWithLinkedFilesFrom(includedPath string) ([]string, error) {

links, err := collectLinkedFiles(rootPath, "")
if err != nil {
return nil, fmt.Errorf("updating linked files failed: %w", err)
}

dirRoot, err := os.OpenRoot(rootPath)
if err != nil {
return nil, fmt.Errorf("could not open root: %w", err)
return nil, fmt.Errorf("collect linked files failed: %w", err)
}

packagePath, _, _ := packages.FindPackageRoot()
packageName := filepath.Base(packagePath)
m := map[string]struct{}{}
for _, l := range links {
if _, err := dirRoot.Stat(l.IncludedFilePath); os.IsNotExist(err) {
continue
}
if err := os.Chdir(filepath.Dir(l.Path)); err != nil {
return nil, fmt.Errorf("could not change directory: %w", err)
}
p, found, err := packages.FindPackageRoot()
if !found || err != nil {
if err != nil {
logger.Errorf("could not find package root directory: %w", err)
}
if l.LinkPackageName == "" ||
l.LinkPackageName == l.IncludedPackageName ||
l.IncludedPackageName != packageName {
continue
}
m[filepath.Base(p)] = struct{}{}
m[l.LinkPackageName] = struct{}{}
}

packages := make([]string, 0, len(m))
Expand All @@ -154,7 +147,7 @@ func collectLinkedFiles(fromDir, toDir string) ([]Link, error) {
if err != nil {
return nil, fmt.Errorf("could not collect file %v: %w", l.IncludedFilePath, err)
}
if l.Checksum == cs {
if l.LinkChecksum == cs {
links[i].UpToDate = true
}
links[i].IncludedFileContents = b
Expand All @@ -164,7 +157,17 @@ func collectLinkedFiles(fromDir, toDir string) ([]Link, error) {
return links, nil
}

func getLinksFrom(fromDir, toDir string) ([]Link, error) {
func getLinksFrom(fromDir, toDir, rootPath string) ([]Link, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("reading current working directory failed: %w", err)
}
defer func() {
if err := os.Chdir(pwd); err != nil {
logger.Errorf("could not change directory: %w", err)
}
}()

var linkFiles []string
if err := filepath.Walk(fromDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -186,7 +189,7 @@ func getLinksFrom(fromDir, toDir string) ([]Link, error) {
if err != nil {
return nil, err
}
links[i].Path = f
links[i].LinkFilePath = f
links[i].TargetFilePath = strings.TrimSuffix(f, ".link")
// if a destination dir is set we replace the source dir with the destination dir
if toDir != "" {
Expand All @@ -200,15 +203,36 @@ func getLinksFrom(fromDir, toDir string) ([]Link, error) {
fields := strings.Fields(firstLine)
links[i].IncludedFilePath = fields[0]
if len(fields) == 2 {
links[i].Checksum = fields[1]
links[i].LinkChecksum = fields[1]
}

if err := os.Chdir(filepath.Dir(filepath.Join(rootPath, links[i].IncludedFilePath))); err != nil {
return nil, fmt.Errorf("could not change directory: %w", err)
}

p, _, _ := packages.FindPackageRoot()
links[i].IncludedPackageName = filepath.Base(p)
links[i].IncludedPackagePath = p

if err := os.Chdir(filepath.Dir(links[i].LinkFilePath)); err != nil {
return nil, fmt.Errorf("could not change directory: %w", err)
}

p, _, _ = packages.FindPackageRoot()
links[i].LinkPackageName = filepath.Base(p)
links[i].LinkPackagePath = p
}

return links, nil
}

func getLinksAndRoot(fromDir, toDir string) ([]Link, fs.ReadFileFS, error) {
links, err := getLinksFrom(fromDir, toDir)
rootPath, err := files.FindRepositoryRootDirectory()
if err != nil {
return nil, nil, fmt.Errorf("root not found: %w", err)
}

links, err := getLinksFrom(fromDir, toDir, rootPath)
if err != nil {
return nil, nil, fmt.Errorf("could not list link files: %w", err)
}
Expand All @@ -219,11 +243,6 @@ func getLinksAndRoot(fromDir, toDir string) ([]Link, fs.ReadFileFS, error) {

logger.Debugf("Package has linked files defined")

rootPath, err := files.FindRepositoryRootDirectory()
if err != nil {
return nil, nil, fmt.Errorf("root not found: %w", err)
}

// scope any possible operation to the repository folder
dirRoot, err := os.OpenRoot(rootPath)
if err != nil {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/packages/other/pipeline_tests/data_stream/test/elasticsearch/ingest_pipeline/default.yml f7c5f0c03aca8ef68c379a62447bdafbf0dcf32b1ff2de143fd6878ee01a91ad