From 67b7517f06719ced05fa0cf41b3f0128988bfd81 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 26 Jan 2024 09:51:47 -0800 Subject: [PATCH 1/4] Fix context bug which could hang forever --- provider/image.go | 2 +- provider/provider.go | 4 ++-- provider/provider_test.go | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/provider/image.go b/provider/image.go index f0ca9355..1dabdeda 100644 --- a/provider/image.go +++ b/provider/image.go @@ -898,7 +898,7 @@ func processLogLine(jm jsonmessage.JSONMessage, info += "failed to parse aux message: " + err.Error() } if err := (&resp).Unmarshal(infoBytes); err != nil { - info += "failed to parse aux message: " + err.Error() + info += "failed to parse info bytes: " + err.Error() } for _, vertex := range resp.Vertexes { info += fmt.Sprintf("digest: %+v\n", vertex.Digest) diff --git a/provider/provider.go b/provider/provider.go index 71e52098..7ae3c43c 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -582,7 +582,7 @@ func (accumulator *contextHashAccumulator) hashPath( if err != nil { return fmt.Errorf("could not copy symlink path %s to hash: %w", filePath, err) } - } else { + } else if fileMode.IsRegular() { // For regular files, we can hash their content. // TODO: consider only hashing file metadata to improve performance f, err := os.Open(filePath) @@ -737,7 +737,7 @@ func setConfiguration(configVars map[string]string) map[string]string { } func marshalBuildOnPreview(inputs resource.PropertyMap) bool { - //set default if not set + // set default if not set if inputs["buildOnPreview"].IsNull() || inputs["buildOnPreview"].ContainsUnknowns() { return false } diff --git a/provider/provider_test.go b/provider/provider_test.go index 35ee78fc..dbd24af6 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -238,6 +238,24 @@ func TestHashDeepSymlinks(t *testing.T) { assert.NoError(t, err) } +func TestIgnoreIrregularFiles(t *testing.T) { + dir := t.TempDir() + + // Create a Dockerfile + dockerfile := filepath.Join(dir, "Dockerfile") + err := os.WriteFile(dockerfile, []byte{}, 0o600) + require.NoError(t, err) + + // Create a pipe which should be ignored. + // err = syscall.Mkfifo(filepath.Join(dir, "pipe"), 0o666) + // require.NoError(t, err) + + hash, err := hashContext(dir, dockerfile) + assert.NoError(t, err) + // Expected hash of just our Dockerfile. + assert.Equal(t, "8b1ab751cc04d1d3ada38b648a764e9d10a20ca73981bc46555ef1f955d6964f", hash) +} + func TestHashUnignoredDirs(t *testing.T) { step1Dir := "./testdata/unignores/basedir" baseResult, err := hashContext(step1Dir, filepath.Join(step1Dir, defaultDockerfile)) From dc2c9d090344b4b30c9285a506d101382827cf57 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 26 Jan 2024 09:55:47 -0800 Subject: [PATCH 2/4] fix test --- provider/provider_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/provider/provider_test.go b/provider/provider_test.go index dbd24af6..c60a8e69 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "syscall" "testing" "github.com/docker/distribution/reference" @@ -247,8 +248,8 @@ func TestIgnoreIrregularFiles(t *testing.T) { require.NoError(t, err) // Create a pipe which should be ignored. - // err = syscall.Mkfifo(filepath.Join(dir, "pipe"), 0o666) - // require.NoError(t, err) + err = syscall.Mkfifo(filepath.Join(dir, "pipe"), 0o666) + require.NoError(t, err) hash, err := hashContext(dir, dockerfile) assert.NoError(t, err) From b79cd4bec49535bb1a0d6ff65bfb432f45ec736a Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 26 Jan 2024 09:57:16 -0800 Subject: [PATCH 3/4] actually fix test --- provider/provider_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/provider/provider_test.go b/provider/provider_test.go index c60a8e69..5c18f3a7 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -247,14 +247,13 @@ func TestIgnoreIrregularFiles(t *testing.T) { err := os.WriteFile(dockerfile, []byte{}, 0o600) require.NoError(t, err) - // Create a pipe which should be ignored. + // Create a pipe which should be ignored. (We will time out trying to read + // it if it's not.) err = syscall.Mkfifo(filepath.Join(dir, "pipe"), 0o666) require.NoError(t, err) - hash, err := hashContext(dir, dockerfile) + _, err = hashContext(dir, dockerfile) assert.NoError(t, err) - // Expected hash of just our Dockerfile. - assert.Equal(t, "8b1ab751cc04d1d3ada38b648a764e9d10a20ca73981bc46555ef1f955d6964f", hash) } func TestHashUnignoredDirs(t *testing.T) { From 75705f54f8b2af20d1465909bb50da6a5bd87630 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 26 Jan 2024 10:10:01 -0800 Subject: [PATCH 4/4] assert irregularity --- provider/provider_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/provider/provider_test.go b/provider/provider_test.go index 5c18f3a7..abaf19e1 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -249,8 +249,13 @@ func TestIgnoreIrregularFiles(t *testing.T) { // Create a pipe which should be ignored. (We will time out trying to read // it if it's not.) - err = syscall.Mkfifo(filepath.Join(dir, "pipe"), 0o666) + pipe := filepath.Join(dir, "pipe") + err = syscall.Mkfifo(pipe, 0o666) require.NoError(t, err) + // Confirm it's irregular. + fi, err := os.Stat(pipe) + require.NoError(t, err) + assert.False(t, fi.Mode().IsRegular()) _, err = hashContext(dir, dockerfile) assert.NoError(t, err)