mirror of
https://github.com/containers/podman.git
synced 2025-05-23 18:17:53 +08:00

...to work for specific edge cases with a simpler solution. Re-reads hooks directories after any changes are detected by the watchers. Added monitoring test for adding a different invalid hook to primary directory. Some issues with prior code: - ReadDir would stop when it encounters an invalid hook, rather than registering an error but continuing to read the valid hook. - Wouldn’t account for Rename and Chmod events. - After doing a mv of the hooks file instead of rm, it would still think the hooks file is in the directory, but it has been moved to another location. - If a hook file was renamed, it would register the renamed file as a separate hook and not delete the original, so it would then execute the hook twice - once for the renamed file, and once for the original name which it did not delete. Signed-off-by: samc24 <sam.chaturvedi24@gmail.com>
99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
// Package hooks implements CRI-O's hook handling.
|
|
package hooks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
current "github.com/containers/libpod/pkg/hooks/1.0.0"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type reader func(content []byte) (*current.Hook, error)
|
|
|
|
var (
|
|
// ErrNoJSONSuffix represents hook-add attempts where the filename
|
|
// does not end in '.json'.
|
|
ErrNoJSONSuffix = errors.New("hook filename does not end in '.json'")
|
|
|
|
// Readers registers per-version hook readers.
|
|
Readers = map[string]reader{}
|
|
)
|
|
|
|
// Read reads a hook JSON file, verifies it, and returns the hook configuration.
|
|
func Read(path string, extensionStages []string) (*current.Hook, error) {
|
|
if !strings.HasSuffix(path, ".json") {
|
|
return nil, ErrNoJSONSuffix
|
|
}
|
|
content, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hook, err := read(content)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "parsing hook %q", path)
|
|
}
|
|
err = hook.Validate(extensionStages)
|
|
return hook, err
|
|
}
|
|
|
|
func read(content []byte) (hook *current.Hook, err error) {
|
|
var ver version
|
|
if err := json.Unmarshal(content, &ver); err != nil {
|
|
return nil, errors.Wrap(err, "version check")
|
|
}
|
|
reader, ok := Readers[ver.Version]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unrecognized hook version: %q", ver.Version)
|
|
}
|
|
|
|
hook, err = reader(content)
|
|
if err != nil {
|
|
return hook, errors.Wrap(err, ver.Version)
|
|
}
|
|
return hook, err
|
|
}
|
|
|
|
// ReadDir reads hook JSON files from a directory into the given map,
|
|
// clobbering any previous entries with the same filenames.
|
|
func ReadDir(path string, extensionStages []string, hooks map[string]*current.Hook) error {
|
|
logrus.Debugf("reading hooks from %s", path)
|
|
files, err := ioutil.ReadDir(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res := err
|
|
for _, file := range files {
|
|
filePath := filepath.Join(path, file.Name())
|
|
hook, err := Read(filePath, extensionStages)
|
|
if err != nil {
|
|
if err == ErrNoJSONSuffix {
|
|
continue
|
|
}
|
|
if os.IsNotExist(err) {
|
|
if err2, ok := err.(*os.PathError); ok && err2.Path == filePath {
|
|
continue
|
|
}
|
|
}
|
|
if res == nil {
|
|
res = err
|
|
} else {
|
|
res = errors.Wrapf(res, "%v", err)
|
|
}
|
|
continue
|
|
}
|
|
hooks[file.Name()] = hook
|
|
logrus.Debugf("added hook %s", filePath)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func init() {
|
|
Readers[current.Version] = current.Read
|
|
}
|