Skip to content

Commit

Permalink
Add some more logging
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Harding <[email protected]>
  • Loading branch information
azdagron committed May 29, 2023
1 parent 6d1b687 commit 7c0cfad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
27 changes: 19 additions & 8 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ const (

var (
// We replace these in tests since bind mounting generally requires root.
bindMountRW = mount.BindMountRW
unmount = mount.Unmount
isMountPoint = mount.IsMountPoint
chcon = selinux.Chcon
bindMountRW = mount.BindMountRW
unmount = mount.Unmount
isMountPoint = mount.IsMountPoint
chcon = selinux.Chcon
seLinuxEnabled = selinux.GetEnabled
seLinuxEnforceMode = selinux.EnforceMode
)

// Config is the configuration for the driver
Expand Down Expand Up @@ -60,12 +62,21 @@ func New(config Config) (*Driver, error) {
// mount to be used within OpenShift, for example. This will fail if the
// Workload API socket directory is mounted read-only, but that will only
// result in a failure if SELinux is enabled and enforcing.
if err := chcon(config.WorkloadAPISocketDir, seLinuxContainerFileLabel, true); err != nil {
if selinux.GetEnabled() && selinux.EnforceMode() == selinux.Enforcing {
seLinuxEnabled := seLinuxEnabled()
seLinuxEnforceMode := seLinuxEnforceMode()
seLinuxProcessLabel, seLinuxFileLabel := selinux.ContainerLabels()
config.Log.Info("SELinux status",
"enabled", seLinuxEnabled,
"enforceMode", seLinuxEnforceMode,
"processLabel", seLinuxProcessLabel,
"fileLabel", seLinuxFileLabel,
)
if seLinuxEnabled && seLinuxEnforceMode == selinux.Enforcing {
if err := chcon(config.WorkloadAPISocketDir, seLinuxContainerFileLabel, true); err != nil {
return nil, fmt.Errorf("failed to set the container file label on the Workload API socket directory: %v", err)
} else {
config.Log.Info("Set the container file label on the Workload API socket directory")
}
} else {
config.Log.Info("Set the container file label on the Workload API socket directory")
}

return &Driver{
Expand Down
7 changes: 7 additions & 0 deletions pkg/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/go-logr/logr"
"github.com/opencontainers/selinux/go-selinux"
"github.com/spiffe/spiffe-csi/internal/version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -35,6 +36,12 @@ func init() {
return os.Remove(metaPath(dst))
}
chcon = writeSELinuxLabel
seLinuxEnabled = func() bool {
return true
}
seLinuxEnforceMode = func() int {
return selinux.Enforcing
}
}

func TestNew(t *testing.T) {
Expand Down

0 comments on commit 7c0cfad

Please sign in to comment.