Skip to content

Commit

Permalink
cli-plugins: Fix searching inaccessible directories
Browse files Browse the repository at this point in the history
Fix a case where one inaccessible plugin search path stops the whole
search and prevents latter paths from being scanned.

Remove a preliminary `Stat` call that verifies whether path is an actual
directory and is accessible.
It's unneeded and doesn't actually check whether the directory can be
listed or not.
`os.ReadDir` will fail in such case anyway, so just attempt to do that
and ignore any encountered error, instead of erroring out the whole
plugin candidate listing.

Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Nov 28, 2024
1 parent 682cf57 commit 6de3d71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
10 changes: 3 additions & 7 deletions cli-plugins/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func getPluginDirs(cfg *configfile.ConfigFile) ([]string, error) {

func addPluginCandidatesFromDir(res map[string][]string, d string) error {
dentries, err := os.ReadDir(d)
// Silently ignore any directories which we cannot list (e.g. due to
// permissions or anything else) or which is not a directory
if err != nil {
return err
return nil
}
for _, dentry := range dentries {
switch dentry.Type() & os.ModeType {
Expand Down Expand Up @@ -106,12 +108,6 @@ func addPluginCandidatesFromDir(res map[string][]string, d string) error {
func listPluginCandidates(dirs []string) (map[string][]string, error) {
result := make(map[string][]string)
for _, d := range dirs {
// Silently ignore any directories which we cannot
// Stat (e.g. due to permissions or anything else) or
// which is not a directory.
if fi, err := os.Stat(d); err != nil || !fi.IsDir() {
continue
}
if err := addPluginCandidatesFromDir(result, d); err != nil {
// Silently ignore paths which don't exist.
if os.IsNotExist(err) {
Expand Down
24 changes: 24 additions & 0 deletions cli-plugins/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ func TestListPluginCandidates(t *testing.T) {
assert.DeepEqual(t, candidates, exp)
}

// Regression test for https://github.com/docker/cli/issues/5643.
// Check that inaccessible directories that come before accessible ones are ignored
// and do not prevent the latter from being processed.
func TestListPluginCandidatesInaccesibleDir(t *testing.T) {
dir := fs.NewDir(t, t.Name(),
fs.WithDir("no-perm", fs.WithMode(0)),
fs.WithDir("plugins",
fs.WithFile("docker-buildx", ""),
),
)
defer dir.Remove()

candidates, err := listPluginCandidates([]string{
dir.Join("no-perm"),
dir.Join("plugins"),
})
assert.NilError(t, err)
assert.DeepEqual(t, candidates, map[string][]string{
"buildx": {
dir.Join("plugins", "docker-buildx"),
},
})
}

func TestGetPlugin(t *testing.T) {
dir := fs.NewDir(t, t.Name(),
fs.WithFile("docker-bbb", `
Expand Down

0 comments on commit 6de3d71

Please sign in to comment.