Docker Authorization

Docker’s out-of-the-box authorization model is all or nothing. But many users require finer-grained access control and Docker’s plugin infrastructure allows us to do so.

This is an excellent opportunity to see how to policy enable an existing service.

Goals

This tutorial helps you get started with OPA and introduces you to core concepts in OPA.

Policy enabling an application decouples the policy implementation from the business logic so that administrators can define policy without changing the application while still keeping up with the size, complexity, and dynamic nature of modern applications.

For the purpose of this tutorial, we want to use OPA to enforce a policy that prevents users from running insecure containers.

This tutorial illustrates two key concepts:

  1. OPA policy definition is decoupled from the implementation of the service (in this case Docker). The administrator is empowered to define and manage policies without requiring changes to any of the apps.

  2. Both the data relevant to policy and the policy definitions themselves can change rapidly.

Prerequisites

This tutorial requires:

  • Docker Engine 18.06.0-ce or newer
  • Docker API version 1.38 or newer
  • root or sudo access

The tutorial has been tested on the following platforms:

  • Ubuntu 18.04 (64-bit)

If you are using a different distro, OS, or architecture, the steps will be the same. However, there may be slight differences in the commands you need to run.

Steps

Several of the steps below require root or sudo access. When you are modifying files under /etc/docker or signalling the Docker daemon to restart, you will need root access.

1. Create an empty policy definition that will allow all requests.

  1. mkdir -p /etc/docker/policies
  2. cat >/etc/docker/policies/authz.rego <<EOF
  3. package docker.authz
  4. allow = true
  5. EOF

This policy defines a single rule named allow that always produces the decision true. Once all of the components are running, we will come back to the policy.

2. Install the opa-docker-authz plugin.

  1. docker plugin install openpolicyagent/opa-docker-authz-v2:0.4 opa-args="-policy-file /opa/policies/authz.rego"

You need to configure the Docker daemon to use the plugin for authorization.

  1. cat > /etc/docker/daemon.json <<EOF
  2. {
  3. "authorization-plugins": ["openpolicyagent/opa-docker-authz-v2:0.4"]
  4. }
  5. EOF

Signal the Docker daemon to reload the configuration file.

  1. kill -HUP $(pidof dockerd)

4. Run a simple Docker command to make sure everything is still working.

  1. docker ps

If everything is setup correctly, the command should exit successfully. You can expect to see log messages from OPA and the plugin.

5. Test that the policy definition is working.

Let’s modify our policy to deny all requests:

  1. cat >/etc/docker/policies/authz.rego <<EOF
  2. package docker.authz
  3. allow = false
  4. EOF

In OPA, rules defines the content of documents. Documents be boolean values (true/false) or they can represent more complex structures using arrays, objects, strings, etc.

In the example above we modified the policy to always return false so that requests will be rejected.

  1. docker ps

The output should be:

  1. Error response from daemon: authorization denied by plugin opa-docker-authz: request rejected by administrative policy

To learn more about how rules define the content of documents, see: How Does OPA Work?

With this policy in place, users will not be able to run any Docker commands. Go ahead and try other commands such as docker run or docker pull. They will all be rejected.

Now let’s change the policy so that it’s a bit more useful.

6. Update the policy to reject requests with the unconfined seccomp profile:

  1. cat >/etc/docker/policies/authz.rego <<EOF
  2. package docker.authz
  3. default allow = false
  4. allow {
  5. not deny
  6. }
  7. deny {
  8. seccomp_unconfined
  9. }
  10. seccomp_unconfined {
  11. # This expression asserts that the string on the right-hand side is equal
  12. # to an element in the array SecurityOpt referenced on the left-hand side.
  13. input.Body.HostConfig.SecurityOpt[_] == "seccomp:unconfined"
  14. }
  15. EOF

7. Test the policy is working by running a simple container:

  1. docker run hello-world

Now try running the same container but disable seccomp (which should be prevented by the policy):

  1. docker run --security-opt seccomp:unconfined hello-world

Congratulations! You have successfully prevented containers from running without seccomp!

The rest of the tutorial shows how you can grant fine grained access to specific clients.

8. Identify the user in Docker requests.

Back up your existing Docker configuration, just in case. You can replace your original configuration after you are done with the tutorial.

  1. mkdir -p ~/.docker
  2. cp ~/.docker/config.json ~/.docker/config.json~

To identify the user, include an HTTP header in all of the requests sent to the Docker daemon:

  1. cat >~/.docker/config.json <<EOF
  2. {
  3. "HttpHeaders": {
  4. "Authz-User": "bob"
  5. }
  6. }
  7. EOF

Docker does not currently provide a way to authenticate clients. But in Docker 1.12, clients can be authenticated using TLS and there are plans to include other means of authentication. For the purpose of this tutorial, we assume that an authentication system is place.

9. Update the policy to include basic user access controls.

  1. cat >/etc/docker/policies/authz.rego <<EOF
  2. package docker.authz
  3. default allow = false
  4. # allow if the user is granted read/write access.
  5. allow {
  6. user_id := input.Headers["Authz-User"]
  7. user := users[user_id]
  8. not user.readOnly
  9. }
  10. # allow if the user is granted read-only access and the request is a GET.
  11. allow {
  12. user_id := input.Headers["Authz-User"]
  13. users[user_id].readOnly
  14. input.Method == "GET"
  15. }
  16. # users defines permissions for the user. In this case, we define a single
  17. # attribute 'readOnly' that controls the kinds of commands the user can run.
  18. users = {
  19. "bob": {"readOnly": true},
  20. "alice": {"readOnly": false},
  21. }
  22. EOF

10. Attempt to run a container.

Because the configured user is "bob", the request is rejected:

  1. docker run hello-world

11. Change the user to “alice” and re-run the container.

  1. cat > ~/.docker/config.json <<EOF
  2. {
  3. "HttpHeaders": {
  4. "Authz-User": "alice"
  5. }
  6. }
  7. EOF

Because the configured user is "alice", the request will succeed:

  1. docker run hello-world

That’s it!