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

Allow the user to set a container as privileged #138

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ The ``[docker:container-name]`` section may contain the following directives:
This value is passed directly to Docker, and may be of any of the forms
that Docker accepts in eg ``docker run``.

``privileged``
A boolean (``true || false``) to set `runtime privilege
<https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities>`__.
This value is coerced to a boolean based of the same rules as
Python's default `ConfigParser
<https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.getboolean>`__.

``environment``
A multi-line list of ``KEY=value`` settings which is used to set
environment variables for the container. The variables are only available
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ports =
[docker:networking-two]
# along with networking-one, proves we can run multiple copies of the same image
image = ksdn117/tcp-udp-test
privileged = off
ports = 2345:1234/tcp
links = networking-one:linked_host

Expand All @@ -65,6 +66,7 @@ healthcheck_start_period = 1

[docker:healthcheck-custom]
image = docker.io/toxdocker/healthcheck:latest
privileged = true
healthcheck_cmd = touch /healthcheck/web/healthy
healthcheck_interval = 1
healthcheck_timeout = 1
Expand Down
2 changes: 2 additions & 0 deletions tox_docker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(
name: str,
image: Image,
stop: bool,
privileged: bool = False,
environment: Optional[Mapping[str, str]] = None,
healthcheck_cmd: Optional[str] = None,
healthcheck_interval: Optional[float] = None,
Expand All @@ -123,6 +124,7 @@ def __init__(
self.runas_name = runas_name(name)
self.image = image
self.stop = stop
self.privileged = privileged
self.environment: Mapping[str, str] = environment or {}
self.ports: Collection[Port] = ports or {}
self.links: Collection[Link] = links or {}
Expand Down
1 change: 1 addition & 0 deletions tox_docker/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def docker_run(
ports=ports,
publish_all_ports=len(ports) == 0,
mounts=container_config.mounts,
privileged=container_config.privileged,
)
container.reload() # TODO: why do we need this?
return container
Expand Down
19 changes: 19 additions & 0 deletions tox_docker/tests/test_privileged.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from tox_docker.tests.util import find_container


def test_can_grant_privileged() -> None:
container = find_container("healthcheck-custom")

assert container.attrs["HostConfig"]["Privileged"] is True


def test_can_deny_privileged() -> None:
container = find_container("networking-two")

assert container.attrs["HostConfig"]["Privileged"] is False


def test_not_privileged_by_default() -> None:
container = find_container("healthcheck-builtin")

assert container.attrs["HostConfig"]["Privileged"] is False
21 changes: 21 additions & 0 deletions tox_docker/tox3/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ def getint(reader: SectionReader, key: str) -> Optional[int]:
return val


def getboolean(reader: SectionReader, key: str) -> Optional[bool]:
val = reader.getstring(key)
if val is None:
return None

# Same implementation as:
# https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.getboolean
lower_val = val.lower().strip()
if lower_val in ("yes", "on", "true", "1"):
return True
if lower_val in ("no", "off", "false", "0"):
return False
msg = f"{val!r} is not a boolean (for {key} in [{reader.section_name}])"
raise ValueError(msg)


def getenvdict(reader: SectionReader, key: str) -> Mapping[str, str]:
environment = {}
for value in reader.getlist(key):
Expand Down Expand Up @@ -105,6 +121,10 @@ def parse_container_config(
"stop": container_name not in config.option.docker_dont_stop,
}

privileged = False
if reader.getstring("privileged"):
privileged = bool(getboolean(reader, "privileged"))

environment = None
if reader.getstring("environment"):
environment = getenvdict(reader, "environment")
Expand Down Expand Up @@ -137,6 +157,7 @@ def parse_container_config(
name=container_name,
image=Image(reader.getstring("image")),
stop=container_name not in config.option.docker_dont_stop,
privileged=privileged,
environment=environment,
healthcheck_cmd=hc_cmd,
healthcheck_interval=hc_interval,
Expand Down
7 changes: 7 additions & 0 deletions tox_docker/tox4/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def register_config(self) -> None:
desc="docker image to run",
post_process=image_required,
)
self.add_config(
keys=["privileged"],
of_type=bool,
default=False,
desc="give extended privileges to this container",
)
self.add_config(
keys=["environment"],
of_type=Dict[str, str],
Expand Down Expand Up @@ -93,6 +99,7 @@ def parse_container_config(docker_config: DockerConfigSet) -> ContainerConfig:
name=docker_config.name,
image=docker_config["image"],
stop=docker_config.name not in docker_config._conf.options.docker_dont_stop,
privileged=docker_config["privileged"],
environment=docker_config["environment"],
healthcheck_cmd=docker_config["healthcheck_cmd"],
healthcheck_interval=docker_config["healthcheck_interval"],
Expand Down