Run Infrahub as a non-root user
This guide explains how to run the Infrahub server, the task workers and the task manager as a non-root user. The Infrahub Docker image runs as root by default. It also contains an infrahub user (user ID 1000, group ID 0, home directory /home/infrahub), which owns every directory Infrahub writes to at runtime, and group 0 has the same permissions on them. You can therefore run the containers as:
- user ID
1000, with any group ID - any other user ID with group ID
0, for example on platforms that assign a random user ID, such as OpenShift
Any other combination, such as a random user ID with a group ID other than 0, cannot write to these directories.
What you'll accomplish​
By following this guide, you'll learn how to:
- Make the data in existing volumes writable for the non-root user
- Run the Infrahub containers as a non-root user with Docker Compose, Podman, Helm or OpenShift
- Verify that the containers run as the non-root user
Prerequisites​
Before starting, ensure you have:
- A running Infrahub instance deployed with Docker Compose or Helm, or a new deployment
- Infrahub 1.12.2 or later. In earlier images, the directories Infrahub writes to belong to root and group
0cannot write to them. With Helm or OpenShift, check that your values file does not pin an earlier version inglobal.infrahubTag. - Command-line access to the host or cluster running Infrahub
- For production deployments, a maintenance window, as the containers restart
Step 1: Make existing volumes writable​
New volumes need no preparation. On an existing deployment, the files in the storage and workflow volumes belong to root. Give them to the infrahub user and give group 0 the same permissions as the owner before you switch users.
- Docker Compose
- Helm
- OpenShift
Run a one-off container as root that mounts the same volumes as the server:
docker compose run --rm --no-deps --user 0 infrahub-server \
sh -c 'chown -R 1000:0 /opt/infrahub/storage /opt/infrahub/workflow && chmod -R g=u /opt/infrahub/storage /opt/infrahub/workflow'
Set the pod security context group to 0 in the next step. For volume types that support it, Kubernetes then makes the volume writable for group 0 when the pod starts.
Some volume types, such as NFS, ignore the pod security context group. On such a volume, change the permissions of the existing files once, from a pod that runs as root and mounts the volume:
chown -R 1000:0 /opt/infrahub/storage && chmod -R g=u /opt/infrahub/storage
These steps cover a new deployment. An existing release keeps Neo4j and PostgreSQL data that belong to the user IDs it ran with, and PostgreSQL refuses to start when its data directory belongs to another user. This page does not describe how to move such a release to these values.
New volumes need no preparation when their type supports the pod's file system group. OpenShift assigns each pod a file system group from the project's range, and Kubernetes makes the volume writable for that group when the pod starts.
Some volume types, such as NFS, ignore the file system group. Before you deploy, make such a volume writable for the project's group, the first group ID in the project's openshift.io/sa.scc.supplemental-groups annotation.
Step 2: Run the containers as the non-root user​
- Docker Compose
- Podman
- Helm
- OpenShift
Create a docker-compose.override.yml file next to docker-compose.yml:
services:
infrahub-server:
user: "1000:0"
task-worker:
user: "1000:0"
task-manager:
user: "1000:0"
Restart Infrahub:
docker compose up -d
With named volumes, use the same docker-compose.override.yml as for Docker Compose.
With directories of the host bind-mounted into the containers, map your host user to user ID 1000 instead, so that the containers can write to the files you own. Create a docker-compose.override.yml file next to docker-compose.yml:
x-podman:
in_pod: false
services:
infrahub-server:
userns_mode: "keep-id:uid=1000"
task-worker:
userns_mode: "keep-id:uid=1000"
task-manager:
userns_mode: "keep-id:uid=1000"
podman-compose runs the services in a pod by default, and Podman 4.9 refuses a separate user namespace for a container in a pod (--userns and --pod cannot be set together). in_pod: false turns the pod off, so the same file works on Podman 4.9 as well as on Podman 6.1, which accepts either setting. Without podman-compose, pass --userns=keep-id:uid=1000 to podman run. Plain --userns=keep-id runs the containers as your host user ID, which cannot write to the runtime directories unless it is 1000.
Restart Infrahub:
podman-compose up -d
Add a security context for the server and the task workers to your values file:
infrahubServer:
podSecurityContext:
runAsUser: 1000
runAsGroup: 0
fsGroup: 0
runAsNonRoot: true
infrahubServer:
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
infrahubTaskWorker:
podSecurityContext:
runAsUser: 1000
runAsGroup: 0
fsGroup: 0
runAsNonRoot: true
infrahubTaskWorker:
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
The task manager is deployed by the Prefect chart, which runs it as a non-root user by default.
Upgrade the release:
helm upgrade infrahub opsmill/infrahub -f values.yaml
OpenShift's default security context constraint, restricted-v2, runs each pod with a user ID from the project's range and group ID 0. It refuses a pod that sets a user ID or a file system group outside the project's ranges, so it refuses the values of the Helm tab.
Do not set a security context for the Infrahub server and the task workers: OpenShift assigns one. The Neo4j, Prefect and PostgreSQL charts included in the Infrahub chart set fixed user IDs, which restricted-v2 also refuses. Remove these user IDs in your values file:
global:
compatibility:
openshift:
adaptSecurityContext: force
neo4j:
securityContext:
runAsNonRoot: true
runAsUser: null
runAsGroup: null
fsGroup: null
containerSecurityContext:
runAsNonRoot: true
runAsUser: null
runAsGroup: null
capabilities:
drop: ["ALL"]
prefect-server:
server:
podSecurityContext:
runAsUser: null
fsGroup: null
containerSecurityContext:
runAsUser: null
postgresql:
primary:
podSecurityContext:
enabled: false
containerSecurityContext:
enabled: false
The global.compatibility.openshift setting makes the Redis and RabbitMQ charts leave out their fixed user IDs. With the default value, auto, they leave them out only when Helm detects OpenShift on the cluster. force also leaves them out when Helm renders the chart without a cluster, for example with helm template.
Install the release, or upgrade a release that already uses these values:
helm upgrade --install infrahub oci://registry.opsmill.io/opsmill/chart/infrahub -f values.yaml
At the first start of a new deployment, the Infrahub server and the task manager can restart a few times, with connection errors in their logs, until Neo4j, PostgreSQL and RabbitMQ accept connections. These restarts are not permission errors.
Step 3: Verify​
Check the user of each container:
- Docker Compose
- Helm
- OpenShift
docker compose exec infrahub-server id -u
docker compose exec task-worker id -u
docker compose exec task-manager id -u
Each command prints 1000.
kubectl exec deploy/infrahub-infrahub-server -- id -u
kubectl exec deploy/infrahub-infrahub-task-worker -- id -u
Each command prints 1000.
oc exec deploy/infrahub-infrahub-server -- id
oc exec deploy/infrahub-infrahub-task-worker -- id
Each command prints a user ID from the project's range and group ID 0, for example uid=1000710000(1000710000) gid=0(root) groups=0(root),1000710000.
Then check the logs of the task workers for permission errors. At startup, each task worker writes its git configuration and logs Set git config file to /opt/infrahub/.gitconfig. A message that starts with Failed to set git means that the user cannot write to /opt/infrahub.
Finally, create a branch to confirm that the task workers run tasks. infrahubctl reads the address of Infrahub from INFRAHUB_ADDRESS and an API token from INFRAHUB_API_TOKEN.
On OpenShift, if Infrahub is not reachable from your computer, forward a local port to the Infrahub server and set INFRAHUB_ADDRESS to http://localhost:8000. The command runs until you stop it, so run infrahubctl in another terminal:
oc port-forward svc/infrahub-infrahub-server 8000:8000
Create and delete a branch:
infrahubctl branch create non-root-check
infrahubctl branch delete non-root-check
Related resources​
- Production deployment - Hardened configuration checklist
- Trust a private CA - Mount a CA bundle into the Infrahub containers
- How to configure Infrahub - Setting environment variables per deployment method