From 44525d25a24f47137a1141249c3f625dfcec7ab5 Mon Sep 17 00:00:00 2001 From: peak-stephen <121192940+peak-stephen@users.noreply.github.com> Date: Sun, 15 Jan 2023 21:58:18 +0100 Subject: [PATCH 1/2] Do not try to hash non-regular files If the build context directory contains named pipes, unix sockets, or other special files, the provider will try to read them and hang. --- cmd/pulumi-resource-docker-buildkit/main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/pulumi-resource-docker-buildkit/main.go b/cmd/pulumi-resource-docker-buildkit/main.go index 4551796..745b9ba 100644 --- a/cmd/pulumi-resource-docker-buildkit/main.go +++ b/cmd/pulumi-resource-docker-buildkit/main.go @@ -475,9 +475,12 @@ func hashContext(contextPath string, dockerfile string) (string, error) { if err != nil { return fmt.Errorf("determining mode for %q: %w", path, err) } - err = ch.hashPath(path, info.Mode()) - if err != nil { - return fmt.Errorf("hashing %q: %w", path, err) + mode := info.Mode() + if mode.IsRegular() || mode == fs.ModeSymlink { + err = ch.hashPath(path, info.Mode()) + if err != nil { + return fmt.Errorf("hashing %q: %w", path, err) + } } return nil }) From 3220455989134111b59642bab213ed9f37ad3069 Mon Sep 17 00:00:00 2001 From: peak-stephen <121192940+peak-stephen@users.noreply.github.com> Date: Sun, 15 Jan 2023 22:57:39 +0100 Subject: [PATCH 2/2] Improve hashing of symlinks --- cmd/pulumi-resource-docker-buildkit/main.go | 42 ++++++++++++--------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/pulumi-resource-docker-buildkit/main.go b/cmd/pulumi-resource-docker-buildkit/main.go index 745b9ba..54ae401 100644 --- a/cmd/pulumi-resource-docker-buildkit/main.go +++ b/cmd/pulumi-resource-docker-buildkit/main.go @@ -397,19 +397,30 @@ func newContextHash(contextPath string) *contextHash { } func (ch *contextHash) hashPath(path string, fileMode fs.FileMode) error { - f, err := os.Open(filepath.Join(ch.contextPath, path)) - if err != nil { - return fmt.Errorf("open %s: %w", path, err) - } - defer f.Close() - h := sha256.New() - _, err = io.Copy(h, f) - if err != nil { - return fmt.Errorf("read %s: %w", path, err) - } ch.input.Write([]byte(path)) ch.input.Write([]byte(fileMode.String())) - ch.input.Write(h.Sum(nil)) + + fullpath := filepath.Join(ch.contextPath, path) + if fileMode.IsRegular() { + f, err := os.Open(fullpath) + if err != nil { + return fmt.Errorf("open %s: %w", path, err) + } + defer f.Close() + h := sha256.New() + _, err = io.Copy(h, f) + if err != nil { + return fmt.Errorf("read %s: %w", path, err) + } + ch.input.Write(h.Sum(nil)) + } else if fileMode&fs.ModeSymlink != 0 { + dest, err := os.Readlink(fullpath) + if err != nil { + return fmt.Errorf("readlink %s:") + } + ch.input.Write([]byte(dest)) + } + ch.input.WriteByte(0) return nil } @@ -475,12 +486,9 @@ func hashContext(contextPath string, dockerfile string) (string, error) { if err != nil { return fmt.Errorf("determining mode for %q: %w", path, err) } - mode := info.Mode() - if mode.IsRegular() || mode == fs.ModeSymlink { - err = ch.hashPath(path, info.Mode()) - if err != nil { - return fmt.Errorf("hashing %q: %w", path, err) - } + err = ch.hashPath(path, info.Mode()) + if err != nil { + return fmt.Errorf("hashing %q: %w", path, err) } return nil })