Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: fix delimiter for selinux label for read-only volumes #23750

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

tgross
Copy link
Member

@tgross tgross commented Aug 6, 2024

The Docker driver's volume field to specify bind-mounts takes a list of strings that consist of three :-delimited fields: source, destination, and options. We append the SELinux label from the plugin configuration as the third field. But when the user has already specified the volume is read-only with :ro, we're incorrectly appending the SELinux label with another : instead of the required ,.

Combine the options into a single field value before appending them to the bind mounts configuration. Updated the tests to split out Windows behavior (which has a different volume specification format) and to ensure the test task has the expected environment for bind mounts.

Fixes: #23690

@tgross
Copy link
Member Author

tgross commented Aug 6, 2024

Tested end-to-end on Amazon Linux (which has SELinux) with the following agent configuration:

agent.hcl
bind_addr    = "0.0.0.0"
enable_debug = true
log_level    = "debug"
data_dir     = "/var/run/nomad"

server {
  enabled = true
}

client {
  enabled = true
}

plugin "docker" {
  config {
    allow_privileged = true
    volumes {
      enabled      = true
      selinuxlabel = "z"
    }
    gc {
      image = false
    }
  }
}

and the following jobspec:

example.nomad.hcl
job "example" {

  group "group" {

    task "task" {

      driver = "docker"

      config {
        image   = "busybox:1"
        command = "httpd"
        args    = ["-vv", "-f", "-p", "8001", "-h", "/local"]
        volumes = ["/etc/ssl/certs:/etc/ssl/certs:ro"]
      }

      resources {
        cpu    = 50
        memory = 50
      }

    }
  }
}

Logs from the client:

2024-08-06T17:31:39.726Z [DEBUG] client.driver_mgr.docker: binding directories: driver=docker task_name=task binds="[]string{"/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/alloc:/alloc:z", "/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/task/local:/local:z", "/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/task/secrets:/secrets:z", "/etc/ssl/certs:/etc/ssl/certs:ro,z"}"

And resulting mounts on the Docker container:

$ sudo docker inspect 34f5 | jq '.[0].Mounts'
[
  {
    "Type": "bind",
    "Source": "/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/alloc",
    "Destination": "/alloc",
    "Mode": "z",
    "RW": true,
    "Propagation": "rprivate"
  },
  {
    "Type": "bind",
    "Source": "/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/task/local",
    "Destination": "/local",
    "Mode": "z",
    "RW": true,
    "Propagation": "rprivate"
  },
  {
    "Type": "bind",
    "Source": "/run/nomad/data/alloc/69dbe88e-1409-24fa-29cf-f872ff1fc0ef/task/secrets",
    "Destination": "/secrets",
    "Mode": "z",
    "RW": true,
    "Propagation": "rprivate"
  },
  {
    "Type": "bind",
    "Source": "/etc/ssl/certs",
    "Destination": "/etc/ssl/certs",
    "Mode": "ro,z",
    "RW": false,
    "Propagation": "rprivate"
  }
]

The Docker driver's `volume` field to specify bind-mounts takes a list of
strings that consist of three `:`-delimited fields: source, destination, and
options. We append the SELinux label from the plugin configuration as the third
field. But when the user has already specified the volume is read-only with
`:ro`, we're incorrectly appending the SELinux label with another `:` instead of
the required `,`.

Combine the options into a single field value before appending them to the bind
mounts configuration. Updated the tests to split out Windows behavior (which
doesn't accept options) and to ensure the test task has the expected environment
for bind mounts.

Fixes: #23690
Copy link
Contributor

@pkazmierczak pkazmierczak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@tgross tgross merged commit 9543e74 into main Aug 8, 2024
19 checks passed
@tgross tgross deleted the selinux-label-docker-vols branch August 8, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/ent/1.6.x+ent Changes are backported to 1.6.x+ent backport/ent/1.7.x+ent Changes are backported to 1.7.x+ent backport/1.8.x backport to 1.8.x release line theme/driver/docker type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API error (500): invalid volume specification when specifying both volume flag & selinuxlabel
2 participants