From f10be074b4842c54fac2bd0c62648faa54449600 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:21:51 +0100 Subject: [PATCH] bake: handle root evaluation with available drives for windows entitlement Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- bake/entitlements.go | 18 +---------------- bake/entitlements_unix.go | 26 ++++++++++++++++++++++++ bake/entitlements_windows.go | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 bake/entitlements_unix.go create mode 100644 bake/entitlements_windows.go diff --git a/bake/entitlements.go b/bake/entitlements.go index b67e159b..8fe8b5b6 100644 --- a/bake/entitlements.go +++ b/bake/entitlements.go @@ -319,7 +319,7 @@ func isParentOrEqualPath(p, parent string) bool { if p == parent || parent == "/" { return true } - if strings.HasPrefix(p, parent+string(filepath.Separator)) { + if strings.HasPrefix(p, filepath.Clean(parent+string(filepath.Separator))) { return true } return false @@ -441,22 +441,6 @@ func removeCommonPaths(in, common []string) []string { return filtered } -func evaluatePaths(in []string) ([]string, error) { - out := make([]string, 0, len(in)) - for _, p := range in { - v, err := filepath.Abs(p) - if err != nil { - return nil, err - } - v, err = filepath.EvalSymlinks(v) - if err != nil { - return nil, errors.Wrapf(err, "failed to evaluate path %q", p) - } - out = append(out, v) - } - return out, nil -} - func evaluateToExistingPaths(in map[string]struct{}) (map[string]struct{}, error) { m := make(map[string]struct{}, len(in)) for p := range in { diff --git a/bake/entitlements_unix.go b/bake/entitlements_unix.go new file mode 100644 index 00000000..660bc6fe --- /dev/null +++ b/bake/entitlements_unix.go @@ -0,0 +1,26 @@ +//go:build !windows +// +build !windows + +package bake + +import ( + "path/filepath" + + "github.com/pkg/errors" +) + +func evaluatePaths(in []string) ([]string, error) { + out := make([]string, 0, len(in)) + for _, p := range in { + v, err := filepath.Abs(p) + if err != nil { + return nil, err + } + v, err = filepath.EvalSymlinks(v) + if err != nil { + return nil, errors.Wrapf(err, "failed to evaluate path %q", p) + } + out = append(out, v) + } + return out, nil +} diff --git a/bake/entitlements_windows.go b/bake/entitlements_windows.go new file mode 100644 index 00000000..c1cda8c0 --- /dev/null +++ b/bake/entitlements_windows.go @@ -0,0 +1,39 @@ +package bake + +import ( + "os" + "path/filepath" + + "github.com/pkg/errors" +) + +func evaluatePaths(in []string) ([]string, error) { + out := make([]string, 0, len(in)) + for _, p := range in { + if p == "/" { + out = append(out, getAllVolumes()...) + continue + } + v, err := filepath.Abs(p) + if err != nil { + return nil, err + } + v, err = filepath.EvalSymlinks(v) + if err != nil { + return nil, errors.Wrapf(err, "failed to evaluate path %q", p) + } + out = append(out, v) + } + return out, nil +} + +func getAllVolumes() []string { + var volumes []string + for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" { + p := string(drive) + ":" + string(filepath.Separator) + if _, err := os.Stat(p); !os.IsNotExist(err) { + volumes = append(volumes, p) + } + } + return volumes +}