2 This test runs podman as a backend for the Docker CLI.
4 import ../make-test-python.nix (
7 let gen-ca = pkgs.writeScript "gen-ca" ''
9 PATH="${pkgs.openssl}/bin:$PATH"
10 openssl genrsa -out ca-key.pem 4096
11 openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -subj '/C=NL/ST=Zuid-Holland/L=The Hague/O=Stevige Balken en Planken B.V./OU=OpSec/CN=Certificate Authority' -out ca.pem
14 openssl genrsa -out podman-key.pem 4096
15 openssl req -subj '/CN=podman' -sha256 -new -key podman-key.pem -out service.csr
16 echo subjectAltName = DNS:podman,IP:127.0.0.1 >> extfile.cnf
17 echo extendedKeyUsage = serverAuth >> extfile.cnf
18 openssl x509 -req -days 365 -sha256 -in service.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out podman-cert.pem -extfile extfile.cnf
21 openssl genrsa -out client-key.pem 4096
22 openssl req -subj '/CN=client' -new -key client-key.pem -out client.csr
23 echo extendedKeyUsage = clientAuth > extfile-client.cnf
24 openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem -extfile extfile-client.cnf
27 PATH="${pkgs.openssl}/bin:$PATH"
28 openssl genrsa -out ca-2-key.pem 4096
29 openssl req -new -x509 -days 365 -key ca-2-key.pem -sha256 -subj '/C=NL/ST=Zuid-Holland/L=The Hague/O=Stevige Balken en Planken B.V./OU=OpSec/CN=Certificate Authority' -out ca-2.pem
31 # Create client signed by CA 2
32 openssl genrsa -out client-2-key.pem 4096
33 openssl req -subj '/CN=client' -new -key client-2-key.pem -out client-2.csr
34 echo extendedKeyUsage = clientAuth > extfile-client.cnf
35 openssl x509 -req -days 365 -sha256 -in client-2.csr -CA ca-2.pem -CAkey ca-2-key.pem -CAcreateserial -out client-2-cert.pem -extfile extfile-client.cnf
40 name = "podman-tls-ghostunnel";
42 maintainers = lib.teams.podman.members ++ [ lib.maintainers.roberth ];
49 virtualisation.podman.enable = true;
50 virtualisation.podman.dockerSocket.enable = true;
51 virtualisation.podman.networkSocket = {
54 server = "ghostunnel";
55 tls.cert = "/root/podman-cert.pem";
56 tls.key = "/root/podman-key.pem";
57 tls.cacert = "/root/ca.pem";
60 environment.systemPackages = [
67 description = "Alice Foobar";
68 extraGroups = ["podman"];
74 environment.systemPackages = [
75 # Installs the docker _client_ only
76 # Normally, you'd want `virtualisation.docker.enable = true;`.
79 environment.variables.DOCKER_HOST = "podman:2376";
80 environment.variables.DOCKER_TLS_VERIFY = "1";
88 def su_cmd(user, cmd):
89 cmd = shlex.quote(cmd)
90 return f"su {user} -l -c {cmd}"
94 r = os.system(command)
96 raise Exception(f"Command {command} failed with exit code {r}")
101 podman.copy_from_host("ca.pem", "/root/ca.pem")
102 podman.copy_from_host("podman-cert.pem", "/root/podman-cert.pem")
103 podman.copy_from_host("podman-key.pem", "/root/podman-key.pem")
105 client.copy_from_host("ca.pem", "/root/.docker/ca.pem")
106 # client.copy_from_host("podman-cert.pem", "/root/podman-cert.pem")
107 client.copy_from_host("client-cert.pem", "/root/.docker/cert.pem")
108 client.copy_from_host("client-key.pem", "/root/.docker/key.pem")
110 # TODO (ghostunnel): add file watchers so the restart isn't necessary
111 podman.succeed("systemctl reset-failed && systemctl restart ghostunnel-server-podman-socket.service")
113 podman.wait_for_unit("sockets.target")
114 podman.wait_for_unit("ghostunnel-server-podman-socket.service")
116 with subtest("Create default network"):
117 podman.succeed("docker network create default")
119 with subtest("Root docker cli also works"):
120 podman.succeed("docker version")
122 with subtest("A podman member can also still use the docker cli"):
123 podman.succeed(su_cmd("alice", "docker version"))
125 with subtest("Run container remotely via docker cli"):
126 client.succeed("docker version")
128 # via socket would be nicer
129 podman.succeed("tar cv --files-from /dev/null | podman import - scratchimg")
132 "docker run -d --name=sleeping -v /nix/store:/nix/store -v /run/current-system/sw/bin:/bin localhost/scratchimg /bin/sleep 10"
134 client.succeed("docker ps | grep sleeping")
135 podman.succeed("docker ps | grep sleeping")
136 client.succeed("docker stop sleeping")
137 client.succeed("docker rm sleeping")
139 with subtest("Clients without cert will be denied"):
140 client.succeed("rm /root/.docker/{cert,key}.pem")
141 client.fail("docker version")
143 with subtest("Clients with wrong cert will be denied"):
144 client.copy_from_host("client-2-cert.pem", "/root/.docker/cert.pem")
145 client.copy_from_host("client-2-key.pem", "/root/.docker/key.pem")
146 client.fail("docker version")