diff --git a/haproxy.cfg b/haproxy.cfg index d61b1d4..a7a5084 100644 --- a/haproxy.cfg +++ b/haproxy.cfg @@ -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 } diff --git a/tests/test_service.py b/tests/test_service.py index 4724d3f..18992bd 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -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 = [