From e35d40db1519e76462a63b06049be7474422c7ab Mon Sep 17 00:00:00 2001 From: Zachary Gallagher Date: Thu, 4 Apr 2024 14:59:43 -0700 Subject: [PATCH 1/5] fix: permit absolute paths for bundle create --- src/pkg/bundle/common.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index 3e6bc704..ab9a65db 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -106,9 +106,11 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { // validate access to packages as well as components referenced in the package for idx, pkg := range bundle.Packages { - // if package path is set, make it relative to source directory + // Set relative to the source directory if not absolute if pkg.Path != "" { - pkg.Path = filepath.Join(b.cfg.CreateOpts.SourceDirectory, pkg.Path) + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(b.cfg.CreateOpts.SourceDirectory, pkg.Path) + } } spinner.Updatef("Validating Bundle Package: %s", pkg.Name) From 58cfa60cd93f54c56024ca937c5c240bd09bc2ca Mon Sep 17 00:00:00 2001 From: Zachary Gallagher Date: Fri, 5 Apr 2024 12:35:58 -0700 Subject: [PATCH 2/5] Moving snippet and adding test case --- src/pkg/bundle/common.go | 16 ++++++------- src/pkg/bundle/common_test.go | 42 +++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index ab9a65db..d3edaf3c 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -106,12 +106,6 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { // validate access to packages as well as components referenced in the package for idx, pkg := range bundle.Packages { - // Set relative to the source directory if not absolute - if pkg.Path != "" { - if !filepath.IsAbs(pkg.Path) { - pkg.Path = filepath.Join(b.cfg.CreateOpts.SourceDirectory, pkg.Path) - } - } spinner.Updatef("Validating Bundle Package: %s", pkg.Name) if pkg.Name == "" { @@ -160,7 +154,7 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { if utils.IsRegistryURL(b.cfg.CreateOpts.Output) { return fmt.Errorf("detected local Zarf package: %s, outputting to an OCI registry is not supported when using local Zarf packages", pkg.Name) } - path := getPkgPath(pkg, bundle.Metadata.Architecture) + path := getPkgPath(pkg, bundle.Metadata.Architecture, b.cfg.CreateOpts.SourceDirectory) bundle.Packages[idx].Path = path } @@ -214,9 +208,15 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { return nil } -func getPkgPath(pkg types.Package, arch string) string { +func getPkgPath(pkg types.Package, arch string, srcDir string) string { var fullPkgName string var path string + // Set path relative to the source directory if not absolute + if pkg.Path != "" { + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(srcDir, pkg.Path) + } + } if strings.HasSuffix(pkg.Path, ".tar.zst") { // use the provided pkg tarball path = pkg.Path diff --git a/src/pkg/bundle/common_test.go b/src/pkg/bundle/common_test.go index 7cdc3669..1c599655 100644 --- a/src/pkg/bundle/common_test.go +++ b/src/pkg/bundle/common_test.go @@ -149,8 +149,9 @@ func Test_validateOverrides(t *testing.T) { func Test_getPkgPath(t *testing.T) { type args struct { - pkg types.Package - arch string + pkg types.Package + arch string + srcDir string } tests := []struct { name string @@ -160,39 +161,52 @@ func Test_getPkgPath(t *testing.T) { { name: "init full path", args: args{ - pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"}, - arch: "fake64", + pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "../fake/path/custom-init.tar.zst", + want: "/mock/fake/path/custom-init.tar.zst", }, { name: "init directory only path", args: args{ - pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"}, - arch: "fake64", + pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "../fake/path/zarf-init-fake64-0.0.1.tar.zst", + want: "/mock/fake/path/zarf-init-fake64-0.0.1.tar.zst", }, { name: "full path", args: args{ - pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, - arch: "fake64", + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst", + want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", }, { name: "directory only path", args: args{ - pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake"}, - arch: "fake64", + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "fake/"}, + arch: "fake64", + srcDir: "/mock/source", + }, + want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", + }, + { + name: "absolute path", + args: args{ + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, want: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - path := getPkgPath(tt.args.pkg, tt.args.arch) + path := getPkgPath(tt.args.pkg, tt.args.arch, tt.args.srcDir) require.Equal(t, tt.want, path) }) } From ebb92aa731bef483541eb6965713a58cf8daf35f Mon Sep 17 00:00:00 2001 From: Zachary Gallagher Date: Tue, 9 Apr 2024 14:03:14 -0700 Subject: [PATCH 3/5] Removing unnecessary wrapper --- src/pkg/bundle/common.go | 6 ++---- src/pkg/bundle/common_test.go | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index d3edaf3c..4a54c214 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -212,10 +212,8 @@ func getPkgPath(pkg types.Package, arch string, srcDir string) string { var fullPkgName string var path string // Set path relative to the source directory if not absolute - if pkg.Path != "" { - if !filepath.IsAbs(pkg.Path) { - pkg.Path = filepath.Join(srcDir, pkg.Path) - } + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(srcDir, pkg.Path) } if strings.HasSuffix(pkg.Path, ".tar.zst") { // use the provided pkg tarball diff --git a/src/pkg/bundle/common_test.go b/src/pkg/bundle/common_test.go index 1c599655..daa5eadd 100644 --- a/src/pkg/bundle/common_test.go +++ b/src/pkg/bundle/common_test.go @@ -188,7 +188,7 @@ func Test_getPkgPath(t *testing.T) { { name: "directory only path", args: args{ - pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "fake/"}, + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "fake"}, arch: "fake64", srcDir: "/mock/source", }, From 4eff48118a2349a479d6ff01ec6a64fc3bcb056a Mon Sep 17 00:00:00 2001 From: Zachary Gallagher Date: Tue, 9 Apr 2024 15:44:36 -0700 Subject: [PATCH 4/5] Re-adding pkg.Path check --- src/pkg/bundle/common.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index 4a54c214..d3edaf3c 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -212,8 +212,10 @@ func getPkgPath(pkg types.Package, arch string, srcDir string) string { var fullPkgName string var path string // Set path relative to the source directory if not absolute - if !filepath.IsAbs(pkg.Path) { - pkg.Path = filepath.Join(srcDir, pkg.Path) + if pkg.Path != "" { + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(srcDir, pkg.Path) + } } if strings.HasSuffix(pkg.Path, ".tar.zst") { // use the provided pkg tarball From 00e748600ff905cc3ab1a891eb7b86addc50d75c Mon Sep 17 00:00:00 2001 From: Zachary Gallagher Date: Thu, 11 Apr 2024 10:32:40 -0700 Subject: [PATCH 5/5] Removing unnecessary pkg.Path check --- src/pkg/bundle/common.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index 23906f77..988b7580 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -212,10 +212,8 @@ func getPkgPath(pkg types.Package, arch string, srcDir string) string { var fullPkgName string var path string // Set path relative to the source directory if not absolute - if pkg.Path != "" { - if !filepath.IsAbs(pkg.Path) { - pkg.Path = filepath.Join(srcDir, pkg.Path) - } + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(srcDir, pkg.Path) } if strings.HasSuffix(pkg.Path, ".tar.zst") { // use the provided pkg tarball