Skip to content

Commit

Permalink
Add checks for CNIBinDir ownership/permissions. (#606)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Nashwan Azhari <nazhari@cloudbasesolutions.com>
  • Loading branch information
aznashwan committed Aug 28, 2024
1 parent 991e48e commit f269968
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:

- name: go test
working-directory: src/k8s
run: make go.unit
# NOTE: there are a handful of tests checking/setting
# root ownership so the tests must be run as root:
run: sudo make go.unit

test-binary:
name: Binaries
Expand Down
54 changes: 53 additions & 1 deletion src/k8s/pkg/k8sd/setup/directories.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package setup

import (
"errors"
"fmt"
"io/fs"
"os"
"syscall"

"github.com/canonical/k8s/pkg/log"
"github.com/canonical/k8s/pkg/snap"
)

// EnsureAllDirectories ensures all required configuration and state directories are created.
func EnsureAllDirectories(snap snap.Snap) error {
if err := ensureCniBinDir(snap.CNIBinDir()); err != nil {
return err
}

for _, dir := range []string{
snap.CNIBinDir(),
snap.CNIConfDir(),
snap.ContainerdConfigDir(),
snap.ContainerdExtraConfigDir(),
Expand All @@ -32,3 +39,48 @@ func EnsureAllDirectories(snap snap.Snap) error {
}
return nil
}

// Ensures that the provided path is a directory with the appropriate
// ownership/permissions for it to be used as the CNI binary directory.
// https://github.com/canonical/k8s-snap/issues/567
// https://github.com/cilium/cilium/issues/23838
func ensureCniBinDir(cniBinDir string) error {
l := log.L().WithValues("cniBinDir", cniBinDir)
if cniBinDir == "" {
l.V(1).Info("Skipping creation of cni bin directory since it was not set")
return nil
}

var stat syscall.Stat_t
if err := syscall.Stat(cniBinDir, &stat); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to syscall.Stat(%q): %w", cniBinDir, err)
}

l.Info("Creating cni bin directory")
if err := os.MkdirAll(cniBinDir, 0o0700); err != nil {
return fmt.Errorf("failed to os.MkdirAll(%s): %w", cniBinDir, err)
}

if err := syscall.Stat(cniBinDir, &stat); err != nil {
return fmt.Errorf("failed to syscall.Stat(%q) newly-created cni bin dir: %w", cniBinDir, err)
}
}

if stat.Uid != 0 || stat.Gid != 0 {
l.Info("Ensuring ownership of cni bin directory")
if err := os.Chown(cniBinDir, 0, 0); err != nil {
return fmt.Errorf("failed to os.Chown(%q, 0, 0): %w", cniBinDir, err)
}
}

if (stat.Mode & 0o700) != 0o700 {
l.Info("Ensuring permissions of cni bin directory")
mode := os.FileMode(stat.Mode | 0o700)
if err := os.Chmod(cniBinDir, mode); err != nil {
return fmt.Errorf("failed to os.Chmod(%q, %o): %w", cniBinDir, mode, err)
}
}

return nil
}

0 comments on commit f269968

Please sign in to comment.