Skip to content
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

fix: allow absolute file paths on create #3518

Merged
merged 8 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions src/internal/packager2/layout/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,14 @@ func assemblePackageComponent(ctx context.Context, component v1alpha1.ZarfCompon
return fmt.Errorf(lang.ErrFileExtract, file.ExtractPath, file.Source, err.Error())
}
} else {
if err := helpers.CreatePathAndCopy(filepath.Join(packagePath, file.Source), dst); err != nil {
return fmt.Errorf("unable to copy file %s: %w", file.Source, err)
if filepath.IsAbs(file.Source) {
if err := helpers.CreatePathAndCopy(file.Source, dst); err != nil {
return fmt.Errorf("unable to copy file %s: %w", file.Source, err)
}
} else {
if err := helpers.CreatePathAndCopy(filepath.Join(packagePath, file.Source), dst); err != nil {
return fmt.Errorf("unable to copy file %s: %w", file.Source, err)
}
}
}
}
Expand Down
110 changes: 110 additions & 0 deletions src/internal/packager2/layout/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"testing"

"github.com/defenseunicorns/pkg/helpers/v2"
goyaml "github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"

"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/test/testutil"
Expand Down Expand Up @@ -224,3 +226,111 @@ func TestLoadPackageErrorWithoutCompatibleFlavor(t *testing.T) {
_, err := LoadPackage(context.Background(), filepath.Join("testdata", "package-with-flavors"), "non-existent-flavor", map[string]string{})
require.EqualError(t, err, fmt.Sprintf("package validation failed: %s", lint.PkgValidateErrNoComponents))
}

func TestCreateAbsolutePathFileSource(t *testing.T) {
t.Parallel()
lint.ZarfSchema = testutil.LoadSchema(t, "../../../../zarf.schema.json")

createFileToImport := func(t *testing.T, dir string) string {
t.Helper()
absoluteFilePath, err := filepath.Abs(filepath.Join(dir, "file.txt"))
require.NoError(t, err)
_, err = os.Create(absoluteFilePath)
require.NoError(t, err)
return absoluteFilePath
}

writePackageToDisk := func(t *testing.T, pkg v1alpha1.ZarfPackage, dir string) {
t.Helper()
b, err := goyaml.Marshal(pkg)
require.NoError(t, err)
path := filepath.Join(dir, "zarf.yaml")
err = os.WriteFile(path, b, 0700)
require.NoError(t, err)
}

t.Run("test a standard package can use absolute file paths", func(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
absoluteFilePath := createFileToImport(t, tmpdir)
pkg := v1alpha1.ZarfPackage{
Kind: v1alpha1.ZarfPackageConfig,
Metadata: v1alpha1.ZarfMetadata{
Name: "standard",
},
Components: []v1alpha1.ZarfComponent{
{
Name: "file",
Files: []v1alpha1.ZarfFile{
{
Source: absoluteFilePath,
Target: "file.txt",
},
},
},
},
}
// Create the zarf.yaml file in the tmpdir
writePackageToDisk(t, pkg, tmpdir)

pkgLayout, err := CreatePackage(context.Background(), tmpdir, CreateOptions{})
require.NoError(t, err)

// Ensure the components have the correct file
fileComponent, err := pkgLayout.GetComponentDir(tmpdir, "file", FilesComponentDir)
require.NoError(t, err)
require.FileExists(t, filepath.Join(fileComponent, "0", "file.txt"))
})

t.Run("test that imports handle absolute paths properly", func(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
absoluteFilePath := createFileToImport(t, tmpdir)
parentPkg := v1alpha1.ZarfPackage{
Kind: v1alpha1.ZarfPackageConfig,
Metadata: v1alpha1.ZarfMetadata{
Name: "parent",
},
Components: []v1alpha1.ZarfComponent{
{
Name: "file-import",
Import: v1alpha1.ZarfComponentImport{
Path: "child",
},
},
},
}
// Create package using absolute file path set to be import
childPkg := v1alpha1.ZarfPackage{
Kind: v1alpha1.ZarfPackageConfig,
Metadata: v1alpha1.ZarfMetadata{
Name: "child",
},
Components: []v1alpha1.ZarfComponent{
{
Name: "file-import",
Files: []v1alpha1.ZarfFile{
{
Source: absoluteFilePath,
Target: "file.txt",
},
},
},
},
}
// Create zarf.yaml files in the tempdir
writePackageToDisk(t, parentPkg, tmpdir)
childDir := filepath.Join(tmpdir, "child")
err := os.Mkdir(childDir, 0700)
require.NoError(t, err)
writePackageToDisk(t, childPkg, childDir)
// create the package
pkgLayout, err := CreatePackage(context.Background(), tmpdir, CreateOptions{})
require.NoError(t, err)

// Ensure the component has the correct file
importedFileComponent, err := pkgLayout.GetComponentDir(tmpdir, "file-import", FilesComponentDir)
require.NoError(t, err)
require.FileExists(t, filepath.Join(importedFileComponent, "0", "file.txt"))
})
}
3 changes: 3 additions & 0 deletions src/internal/packager2/layout/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func makePathRelativeTo(path, relativeTo string) string {
if helpers.IsURL(path) {
return path
}
if filepath.IsAbs(path) {
return path
}
return filepath.Join(relativeTo, path)
}

Expand Down
Loading