Skip to content

Commit

Permalink
Merge pull request #5294 from typeid/localize_absolute_paths
Browse files Browse the repository at this point in the history
feat: localize absolute paths
  • Loading branch information
k8s-ci-robot committed Apr 30, 2024
2 parents e7a1549 + 7b1eaf1 commit 49a645f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (lc *localizer) localizeFileWithContent(path string, content []byte) (strin
// 2. avoid paths that temporarily traverse outside the current root,
// i.e. ../../../scope/target/current-root. The localized file will be surrounded by
// different directories than its source, and so an uncleaned path may no longer be valid.
locPath = cleanFilePath(lc.fSys, lc.root, path)
locPath = cleanedRelativePath(lc.fSys, lc.root, path)
}
absPath := filepath.Join(lc.dst, locPath)
if err := lc.fSys.MkdirAll(filepath.Dir(absPath)); err != nil {
Expand Down
39 changes: 32 additions & 7 deletions api/internal/localizer/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ func TestLocalizeFileCleaned(t *testing.T) {
kind: Kustomization
patches:
- path: ../gamma/../../../alpha/beta/./gamma/patch.yaml
- path: /alpha/beta/../beta/./gamma/patch2.yaml
`,
"patch.yaml": podConfiguration,
"patch.yaml": podConfiguration,
"patch2.yaml": podConfiguration,
}
expected, actual := makeFileSystems(t, "/alpha/beta/gamma", kustAndPatch)

Expand All @@ -307,8 +309,10 @@ patches:
kind: Kustomization
patches:
- path: patch.yaml
- path: patch2.yaml
`,
"patch.yaml": podConfiguration,
"patch.yaml": podConfiguration,
"patch2.yaml": podConfiguration,
})
checkFSys(t, expected, actual)
}
Expand Down Expand Up @@ -1194,19 +1198,40 @@ func TestLocalizeResources(t *testing.T) {
kind: Kustomization
resources:
- pod.yaml
- /a/b/pod2.yaml
- ../../alpha
`,
"pod.yaml": podConfiguration,
"pod.yaml": podConfiguration,
"pod2.yaml": podConfiguration,
"../../alpha/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: my-
`,
}
expected, actual := makeFileSystems(t, "/a/b", kustAndResources)

checkRun(t, actual, "/a/b", "/", "/localized-b")
addFiles(t, expected, "/localized-b/a/b", kustAndResources)
checkFSys(t, expected, actual)
// Absolute path of `/a/b/pod2.yaml` is expected to be converted to a path
// relative to the kustomization root.
expectedKustAndResources := map[string]string{
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pod.yaml
- pod2.yaml
- ../../alpha
`,
"pod.yaml": podConfiguration,
"pod2.yaml": podConfiguration,
"../../alpha/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: my-
`,
}

expectedFs, actualFs := makeFileSystems(t, "/a/b", kustAndResources)

checkRun(t, actualFs, "/a/b", "/", "/localized-b")
addFiles(t, expectedFs, "/localized-b/a/b", expectedKustAndResources)
checkFSys(t, expectedFs, actualFs)
}

func TestLocalizePathError(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions api/internal/localizer/locloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ func (ll *Loader) Load(path string) ([]byte, error) {
if err != nil {
return nil, errors.WrapPrefixf(err, "invalid file reference")
}
if filepath.IsAbs(path) {
return nil, errors.Errorf("absolute paths not yet supported in alpha: file path %q is absolute", path)
}
if !loader.IsRemoteFile(path) && ll.local {
cleanPath := cleanFilePath(ll.fSys, filesys.ConfirmedDir(ll.Root()), path)
cleanPath := cleanedRelativePath(ll.fSys, filesys.ConfirmedDir(ll.Root()), path)
cleanAbs := filepath.Join(ll.Root(), cleanPath)
dir := filesys.ConfirmedDir(filepath.Dir(cleanAbs))
// target cannot reference newDir, as this load would've failed prior to localize;
Expand Down
12 changes: 5 additions & 7 deletions api/internal/localizer/locloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ func TestLoadFails(t *testing.T) {
checkNewLoader(req, ldr, &args, "/a", "/a", "/a/newDir", fSys)

cases := map[string]string{
"absolute path": "/a/pod.yaml",
"directory": "b",
"non-existent file": "kubectl.yaml",
"file outside root": "../alpha/beta/gamma/delta/deployment.yaml",
"inside dst": "newDir/pod.yaml",
"directory": "b",
"non-existent file": "kubectl.yaml",
"file outside root": "../alpha/beta/gamma/delta/deployment.yaml",
"inside dst": "newDir/pod.yaml",
"winding inside dst": "/a/test/../newDir/pod.yaml",
}
for name, file := range cases {
file := file
Expand All @@ -291,8 +291,6 @@ func TestLoadFails(t *testing.T) {
ldr, _, err := NewLoader("./a/../a", "/a/../a", "/a/newDir", fSys)
req.NoError(err)

req.NoError(fSys.WriteFile("/a/newDir/pod.yaml", []byte(podConfiguration)))

_, err = ldr.Load(file)
req.Error(err)
})
Expand Down
10 changes: 7 additions & 3 deletions api/internal/localizer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ func hasRef(repoURL string) bool {
return repoSpec.Ref != ""
}

// cleanFilePath returns file cleaned, where file is a relative path to root on fSys
func cleanFilePath(fSys filesys.FileSystem, root filesys.ConfirmedDir, file string) string {
abs := root.Join(file)
// cleanedRelativePath returns a cleaned relative path of file to root on fSys
func cleanedRelativePath(fSys filesys.FileSystem, root filesys.ConfirmedDir, file string) string {
abs := file
if !filepath.IsAbs(file) {
abs = root.Join(file)
}

dir, f, err := fSys.CleanedAbs(abs)
if err != nil {
log.Fatalf("cannot clean validated file path %q: %s", abs, err)
Expand Down
23 changes: 23 additions & 0 deletions api/internal/localizer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,26 @@ func TestLocRootPath_SymlinkPath(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, actual)
}

func TestCleanedRelativePath(t *testing.T) {
fSys := filesys.MakeFsInMemory()
require.NoError(t, fSys.MkdirAll("/root/test"))
require.NoError(t, fSys.WriteFile("/root/test/file.yaml", []byte("")))
require.NoError(t, fSys.WriteFile("/root/filetwo.yaml", []byte("")))

// Absolute path is cleaned to relative path
cleanedPath := cleanedRelativePath(fSys, "/root/", "/root/test/file.yaml")
require.Equal(t, "test/file.yaml", cleanedPath)

// Winding absolute path is cleaned to relative path
cleanedPath = cleanedRelativePath(fSys, "/root/", "/root/test/../filetwo.yaml")
require.Equal(t, "filetwo.yaml", cleanedPath)

// Already clean relative path stays the same
cleanedPath = cleanedRelativePath(fSys, "/root/", "test/file.yaml")
require.Equal(t, "test/file.yaml", cleanedPath)

// Winding relative path is cleaned
cleanedPath = cleanedRelativePath(fSys, "/root/", "test/../filetwo.yaml")
require.Equal(t, "filetwo.yaml", cleanedPath)
}

0 comments on commit 49a645f

Please sign in to comment.