Skip to content
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
9 changes: 6 additions & 3 deletions haproxy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ backend docker-events

frontend dockerfrontend
bind ${BIND_CONFIG}
http-request deny unless METH_GET || { env(POST) -m bool }

# Allowed endpoints
# Granular container actions are POST requests, so they must be allowed
# before the POST gate below, or they only work with POST=1 (#101).
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/((stop)|(restart)|(kill)) } { env(ALLOW_RESTARTS) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/start } { env(ALLOW_START) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/stop } { env(ALLOW_STOP) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/pause } { env(ALLOW_PAUSE) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/unpause } { env(ALLOW_UNPAUSE) -m bool }

http-request deny unless METH_GET || { env(POST) -m bool }

# Allowed endpoints
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/auth } { env(AUTH) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/build } { env(BUILD) -m bool }
http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/commit } { env(COMMIT) -m bool }
Expand Down
37 changes: 37 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,43 @@ def test_network_post_permissions(proxy_factory):
_check_permissions(allowed_calls, forbidden_calls)


@pytest.fixture
def target_container():
"""A container to act on, started directly against the docker socket."""
container_id = docker(
"container", "run", "--detach", "alpine", "sleep", "300"
).strip()
try:
yield container_id
finally:
docker("container", "rm", "-f", container_id)


def test_granular_permissions_without_post(proxy_factory, target_container):
"""The ALLOW_* container actions work on their own, without POST=1."""
with proxy_factory(
ALLOW_START=1,
ALLOW_STOP=1,
ALLOW_RESTARTS=1,
ALLOW_PAUSE=1,
ALLOW_UNPAUSE=1,
):
allowed_calls = [
("pause", target_container),
("unpause", target_container),
("restart", "--time", "0", target_container),
("stop", "--time", "0", target_container),
("start", target_container),
("kill", target_container),
]
forbidden_calls = [
("rm", "-f", target_container),
("run", "--rm", "alpine"),
("network", "create", "foobar"),
]
_check_permissions(allowed_calls, forbidden_calls)


def test_exec_permissions(proxy_factory):
with proxy_factory(CONTAINERS=1, EXEC=1, POST=1) as container_id:
allowed_calls = [
Expand Down