Skip to content

Commit 947e410

Browse files
wkingrh-atomic-bot
authored andcommitted
hooks: Fail ReadDir if a configured hook executable is missing
The continue here is from 5676597 (hooks/read: Ignore IsNotExist for JSON files in ReadDir, 2018-04-27, #686), where it was intended to silently ignore missing JSON files. However, the old logic was also silently ignoring not-exist errors from the os.Stat(hook.Hook.Path) from 68eb128 (pkg/hooks: Version the hook structure and add 1.0.0 hooks, 2018-04-27, #686). This commit adjusts the check so JSON not-exist errors continue to be silently ignored while hook executable not-exist errors become fatal. Signed-off-by: W. Trevor King <[email protected]> Closes: #887 Approved by: rhatdan
1 parent cae49fc commit 947e410

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/hooks/read.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ func ReadDir(path string, extensionStages []string, hooks map[string]*current.Ho
6767
}
6868

6969
for _, file := range files {
70-
hook, err := Read(filepath.Join(path, file.Name()), extensionStages)
70+
filePath := filepath.Join(path, file.Name())
71+
hook, err := Read(filePath, extensionStages)
7172
if err != nil {
7273
if err == ErrNoJSONSuffix {
7374
continue
7475
}
7576
if os.IsNotExist(err) {
76-
continue
77+
if err2, ok := err.(*os.PathError); ok && err2.Path == filePath {
78+
continue
79+
}
7780
}
7881
return err
7982
}

pkg/hooks/read_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,24 @@ func TestBadDir(t *testing.T) {
191191
}
192192
assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error())
193193
}
194+
195+
func TestHookExecutableDoesNotExit(t *testing.T) {
196+
dir, err := ioutil.TempDir("", "hooks-test-")
197+
if err != nil {
198+
t.Fatal(err)
199+
}
200+
defer os.RemoveAll(dir)
201+
202+
jsonPath := filepath.Join(dir, "hook.json")
203+
err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/does/not/exist\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"), 0644)
204+
if err != nil {
205+
t.Fatal(err)
206+
}
207+
208+
hooks := map[string]*current.Hook{}
209+
err = ReadDir(dir, []string{}, hooks)
210+
if err == nil {
211+
t.Fatal("unexpected success")
212+
}
213+
assert.Regexp(t, "^stat /does/not/exist: no such file or directory$", err.Error())
214+
}

0 commit comments

Comments
 (0)