Skip to content

Commit

Permalink
Merge pull request #30 from ccojocar/optional-host-pid
Browse files Browse the repository at this point in the history
Add an option to skip the check that a container runs into the host PID namespace
  • Loading branch information
pjbgf authored Nov 7, 2024
2 parents 995a10b + a287207 commit 1375e5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/apparmor/apparmor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (a *AppArmor) LoadPolicy(fileName string) error {

err = cmd.Run()
if err != nil {
return err
return fmt.Errorf("parsing profile: %w", err)
}

runtime.LockOSThread()
Expand Down
17 changes: 9 additions & 8 deletions pkg/hostop/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewMountHostOp(opts ...HostOpOption) HostOp {
o := hostOpOpts{
logger: logr.Discard(),
insideContainer: InsideContainer,
hostPidCheck: func() bool { return true },
}

o.applyOpts(opts...)
Expand All @@ -50,16 +51,16 @@ func NewMountHostOp(opts ...HostOpOption) HostOp {
func (m *mountHostOp) Do(action func() error) error {
if m.opts.insideContainer() {
m.opts.logger.V(2).Info("running inside container")
if m.opts.hostPidCheck() {
hostPidNs, err := HostPidNamespace()
if err != nil {
return fmt.Errorf("identifying pid namespace: %w", err)
}

hostPidNs, err := HostPidNamespace()
if err != nil {
return fmt.Errorf("identifying pid namespace: %w", err)
}

if !hostPidNs {
return fmt.Errorf("must run within host PID namespace: %w", err)
if !hostPidNs {
return fmt.Errorf("must run within host PID namespace: %w", err)
}
}

return m.containerDo(action)
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/hostop/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type hostOpOpts struct {
logger logr.Logger
insideContainer func() bool
hostPidCheck func() bool
}

type HostOpOption func(*hostOpOpts)
Expand Down Expand Up @@ -40,3 +41,11 @@ func WithAssumeHost() HostOpOption {
o.insideContainer = func() bool { return false }
}
}

// WithAssumeHostPidNamespace ensures that HostOp always assume that the container
// runs into the host PID namespace.
func WithAssumeHostPidNamespace() HostOpOption {
return func(o *hostOpOpts) {
o.hostPidCheck = func() bool { return false }
}
}

0 comments on commit 1375e5e

Please sign in to comment.