Skip to content

Commit

Permalink
Add granular control of SELinux labels for host mounts (#321)
Browse files Browse the repository at this point in the history
Add granular control of SELinux labels for host mounts

Currently when using the podman task driver plugin if the selinuxlabel = "z" option is present in the volume configuration, all the host volume mounts will be forced to use SELinux shared volume context.

There is no way for operators to use the csi-hostpath plugin or any type of mount that is not compliant with the SELinux security, or mount a private volume. This PR introduces a new configuration option that can be set per volume mount, allowing to set different SELinux context to different volumes.

Co-authored-by: Luiz Aoqui <[email protected]>
  • Loading branch information
Juanadelacuesta and lgfa29 authored Jun 11, 2024
1 parent 57be144 commit 775c215
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ IMPROVEMENTS:
* api: Address a backwards incompatible change in Podman 5.0 [[GH-332](https://github.com/hashicorp/nomad-driver-podman/issues/332)]
* config: Add `logging` options to the plugin configuration [[GH-285](https://github.com/hashicorp/nomad-driver-podman/pull/285)]
* build: Updated Nomad 1.8.0 [[GH-347](https://github.com/hashicorp/nomad-driver-podman/pull/347)]
* config: Add granular control of SELinux labels for host mounts [[GH-321]](https://github.com/hashicorp/nomad-driver-podman/pull/321)

## 0.5.2 (February 5, 2024)

Expand Down
27 changes: 20 additions & 7 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,14 @@ func (d *Driver) ExecTaskStreaming(ctx context.Context, taskID string, execOptio
return &exitResult, nil
}

func getSElinuxVolumeLabel(vc VolumeConfig, mc *drivers.MountConfig) string {
if mc.SELinuxLabel != vc.SelinuxLabel && mc.SELinuxLabel != "" {
return mc.SELinuxLabel
}

return vc.SelinuxLabel
}

func (d *Driver) containerMounts(task *drivers.TaskConfig, driverConfig *TaskConfig) ([]spec.Mount, error) {
var binds []spec.Mount
binds = append(binds, spec.Mount{Source: task.TaskDir().SharedAllocDir, Destination: task.Env[taskenv.AllocDir], Type: "bind"})
Expand All @@ -1318,6 +1326,13 @@ func (d *Driver) containerMounts(task *drivers.TaskConfig, driverConfig *TaskCon
// https://github.com/containers/libpod/pull/4548
taskLocalBindVolume := true

if selinuxLabel := d.config.Volumes.SelinuxLabel; selinuxLabel != "" && !driverConfig.Privileged {
// Apply SELinux Label to each volume
for i := range binds {
binds[i].Options = append(binds[i].Options, selinuxLabel)
}
}

for _, userbind := range driverConfig.Volumes {
src, dst, mode, err := parseVolumeSpec(userbind)
if err != nil {
Expand Down Expand Up @@ -1373,14 +1388,12 @@ func (d *Driver) containerMounts(task *drivers.TaskConfig, driverConfig *TaskCon
// If PropagationMode is something else or unset, Podman defaults to rprivate
}

binds = append(binds, bind)
}

if selinuxLabel := d.config.Volumes.SelinuxLabel; selinuxLabel != "" && !driverConfig.Privileged {
// Apply SELinux Label to each volume
for i := range binds {
binds[i].Options = append(binds[i].Options, selinuxLabel)
selinuxLabel := getSElinuxVolumeLabel(d.config.Volumes, m)
if selinuxLabel != "" && !driverConfig.Privileged {
bind.Options = append(bind.Options, selinuxLabel)
}

binds = append(binds, bind)
}

for _, dst := range driverConfig.Tmpfs {
Expand Down
40 changes: 22 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/hashicorp/nomad-driver-podman
go 1.21

replace (

// Fix error tidying due to Nomad downstream dependencies and the recent
// migration of the metrics library.
github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.5.3
Expand All @@ -21,7 +20,7 @@ require (
github.com/opencontainers/runtime-spec v1.1.0
github.com/ryanuber/go-glob v1.0.0
github.com/shoenig/test v1.8.0
golang.org/x/sync v0.6.0
golang.org/x/sync v0.7.0
)

require (
Expand All @@ -40,15 +39,17 @@ require (
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/brianvoe/gofakeit/v6 v6.28.0 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/container-storage-interface/spec v1.9.0 // indirect
github.com/containerd/containerd v1.7.15 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.9 // indirect
github.com/containers/storage v1.51.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/cli v25.0.2+incompatible // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v26.0.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v26.0.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.1 // indirect
Expand All @@ -69,10 +70,10 @@ require (
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/hashicorp/consul/api v1.27.0 // indirect
github.com/hashicorp/consul/api v1.28.2 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-bexpr v0.1.13 // indirect
github.com/hashicorp/go-bexpr v0.1.14 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 // indirect
Expand Down Expand Up @@ -106,15 +107,15 @@ require (
github.com/ishidawataru/sctp v0.0.0-20230406120618-7ff4192f6ff2 // indirect
github.com/jefferai/isbadcipher v0.0.0-20190226160619-51d2077c035f // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed // indirect
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.58 // indirect
github.com/miekg/dns v1.1.59 // indirect
github.com/mitchellh/cli v1.1.5 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -130,43 +131,46 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc6 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/shirou/gopsutil/v3 v3.24.1 // indirect
github.com/shoenig/go-landlock v1.2.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zclconf/go-cty v1.14.2 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014 // indirect
google.golang.org/grpc v1.61.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
Loading

0 comments on commit 775c215

Please sign in to comment.