From 79844cb1f7255254d09700b1ddd04f2b4da8a140 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 21 Jun 2023 18:59:15 +0000 Subject: [PATCH 1/5] e2e: add tests for using private registry with podman driver This PR adds e2e tests that stands up a private docker registry and has a podman tasks run a container from an image in that private registry. Tests - user:password set in task config - auth_soft_fail works for public images when auth is set in driver - credentials helper is set in driver auth config - config auth.json file is set in driver auth config --- e2e/podman/input/auth_basic.hcl | 76 ++++++++++ e2e/podman/input/auth_helper.hcl | 58 ++++++++ e2e/podman/input/auth_static.hcl | 68 +++++++++ .../input/{podman_basic.hcl => redis.hcl} | 9 +- e2e/podman/input/registry-auths.hcl | 120 +++++++++++++++ e2e/podman/input/registry.hcl | 137 ++++++++++++++++++ e2e/podman/podman_test.go | 117 ++++++++++++--- e2e/terraform/etc/nomad.d/client-linux.hcl | 4 + 8 files changed, 568 insertions(+), 21 deletions(-) create mode 100644 e2e/podman/input/auth_basic.hcl create mode 100644 e2e/podman/input/auth_helper.hcl create mode 100644 e2e/podman/input/auth_static.hcl rename e2e/podman/input/{podman_basic.hcl => redis.hcl} (66%) create mode 100644 e2e/podman/input/registry-auths.hcl create mode 100644 e2e/podman/input/registry.hcl diff --git a/e2e/podman/input/auth_basic.hcl b/e2e/podman/input/auth_basic.hcl new file mode 100644 index 000000000000..36e49fdb1204 --- /dev/null +++ b/e2e/podman/input/auth_basic.hcl @@ -0,0 +1,76 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +# This job runs a podman task using a container stored in a private registry +# configured with basic authentication. The registry.hcl job should be running +# and healthy before running this job. The registry_address and registry_port +# HCL variables must be provided. + +variable "registry_address" { + type = string + description = "The HTTP address of the local registry" + default = "localhost" +} + +variable "registry_port" { + type = number + description = "The HTTP port of the local registry" + default = "7511" +} + +variable "registry_username" { + type = string + description = "The Basic Auth username of the local registry" + default = "auth_basic_user" +} + +variable "registry_password" { + type = string + description = "The Basic Auth password of the local registry" + default = "auth_basic_pass" +} + +locals { + registry_auth = base64encode("${var.registry_username}:${var.registry_password}") +} + +job "auth_basic" { + type = "batch" + + constraint { + attribute = "${attr.kernel.name}" + value = "linux" + } + + group "basic" { + reschedule { + attempts = 0 + unlimited = false + } + + network { + mode = "host" + } + + task "echo" { + driver = "podman" + + config { + image = "${var.registry_address}:${var.registry_port}/docker.io/library/bash_auth_basic:private" + args = ["echo", "The auth basic test is OK!"] + auth_soft_fail = true + + auth { + username = "${var.registry_username}" + password = "${var.registry_password}" + tls_verify = false + } + } + + resources { + cpu = 100 + memory = 64 + } + } + } +} diff --git a/e2e/podman/input/auth_helper.hcl b/e2e/podman/input/auth_helper.hcl new file mode 100644 index 000000000000..f443eedf63ff --- /dev/null +++ b/e2e/podman/input/auth_helper.hcl @@ -0,0 +1,58 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +# This job runs a podman task using a container stored in a private registry +# configured with credentials helper authentication. The registry.hcl job should +# be running and healthy before running this job. + +variable "registry_address" { + type = string + description = "The HTTP address of the local registry" + default = "localhost" +} + +variable "registry_port" { + type = number + description = "The HTTP port of the local registry" + default = "7511" +} + +job "auth_static" { + type = "batch" + + constraint { + attribute = "${attr.kernel.name}" + value = "linux" + } + + group "helper" { + reschedule { + attempts = 0 + unlimited = false + } + + network { + mode = "host" + } + + task "echo" { + driver = "podman" + + config { + image = "${var.registry_address}:${var.registry_port}/docker.io/library/bash_auth_helper:private" + args = ["echo", "The credentials helper auth test is OK!"] + + auth { + # usename and password come from [docker-credential-]test.sh found on + # $PATH as specified by "helper=test.sh" in plugin config + tls_verify = false + } + } + + resources { + cpu = 100 + memory = 64 + } + } + } +} diff --git a/e2e/podman/input/auth_static.hcl b/e2e/podman/input/auth_static.hcl new file mode 100644 index 000000000000..4b187dc169f8 --- /dev/null +++ b/e2e/podman/input/auth_static.hcl @@ -0,0 +1,68 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +# This job runs a podman task using a container stored in a private registry +# configured with file config static authentication. The registry.hcl job should +# be running and healthy before running this job. + +variable "registry_address" { + type = string + description = "The HTTP address of the local registry" + default = "localhost" +} + +variable "registry_port" { + type = number + description = "The HTTP port of the local registry" + default = "7511" +} + +job "auth_static" { + type = "batch" + + constraint { + attribute = "${attr.kernel.name}" + value = "linux" + } + + group "static" { + reschedule { + attempts = 0 + unlimited = false + } + + network { + mode = "host" + } + + task "echo" { + driver = "podman" + + config { + image = "${var.registry_address}:${var.registry_port}/docker.io/library/bash_auth_static:private" + args = ["echo", "The static auth test is OK!"] + + auth { + # usename and password come from auth.json in plugin config + tls_verify = false + } + } + + resources { + cpu = 100 + memory = 64 + } + } + } +} + +# auth.json (must be pointed to by config=/auth.json) +# +# { +# "auths": { +# "127.0.0.1:7511/docker.io/library/bash_auth_static": { +# "auth": "YXV0aF9zdGF0aWNfdXNlcjphdXRoX3N0YXRpY19wYXNz" +# } +# } +# } + diff --git a/e2e/podman/input/podman_basic.hcl b/e2e/podman/input/redis.hcl similarity index 66% rename from e2e/podman/input/podman_basic.hcl rename to e2e/podman/input/redis.hcl index 0719cc097416..d7753e3515b7 100644 --- a/e2e/podman/input/podman_basic.hcl +++ b/e2e/podman/input/redis.hcl @@ -1,7 +1,9 @@ # Copyright (c) HashiCorp, Inc. # SPDX-License-Identifier: MPL-2.0 -job "podman_basic" { +# This is a simple redis job using the podman task driver. + +job "redis" { constraint { attribute = "${attr.kernel.name}" @@ -19,8 +21,9 @@ job "podman_basic" { driver = "podman" config { - image = "redis:7" - ports = ["db"] + image = "docker.io/library/redis:7" + ports = ["db"] + auth_soft_fail = true } resources { diff --git a/e2e/podman/input/registry-auths.hcl b/e2e/podman/input/registry-auths.hcl new file mode 100644 index 000000000000..3a8ce2128372 --- /dev/null +++ b/e2e/podman/input/registry-auths.hcl @@ -0,0 +1,120 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +# This job runs after the private registry is up and running, when we know +# address and port provided by the bridge network. It is a sysbatch job +# that writes these files on every linux client. +# - /usr/local/bin/docker-credential-test.sh +# - /etc/docker-registry-auth.json + +variable "registry_address" { + type = string + description = "The HTTP address of the local registry" +} + +variable "auth_dir" { + type = string + description = "The destination directory of the auth.json file." + default = "/tmp" +} + +variable "helper_dir" { + type = string + description = "The directory in which test.sh will be written." + default = "/tmp" +} + +variable "user" { + type = string + description = "The user to create files as. Should be root in e2e." + # no default because dealing with root files is annoying locally + # try -var=user=$USER for local development +} + +job "registry-auths" { + type = "sysbatch" + + constraint { + attribute = "${attr.kernel.name}" + value = "linux" + } + + group "create-files" { + reschedule { + attempts = 0 + unlimited = false + } + + # write out the test.sh file into var.helper_dir + task "create-helper-file" { + driver = "pledge" + user = "${var.user}" + + config { + command = "cp" + args = ["${NOMAD_TASK_DIR}/test.sh", "${var.helper_dir}/docker-credential-test.sh"] + promises = "stdio rpath wpath cpath" + unveil = ["r:${NOMAD_TASK_DIR}/test.sh", "rwc:${var.helper_dir}"] + } + template { + destination = "local/test.sh" + perms = "755" + data = <:/docker.io/library/bash_auth_basic:private +# +# Note that the
: is dynamic and can be found using NSD. +# Note that credentials are required (e.g. podman login), and are specific to +# each image, e.g. "auth_basic_user" and "auth_basic_pass". +# +# To add a new username/password credential, run this container command +# podman run --rm --entrypoint htpasswd registry:2.7.0 -Bbn +# and add : to the local/auth.txt file template below. + +job "registry" { + type = "service" + + constraint { + attribute = "${attr.kernel.name}" + value = "linux" + } + + group "registry-server" { + + update { + min_healthy_time = "4s" + } + + reschedule { + attempts = 0 + unlimited = false + } + + restart { + attempts = 0 + mode = "fail" + } + + network { + mode = "host" + port "registryhttp" {} + } + + service { + provider = "nomad" + name = "registry" + port = "registryhttp" + check { + name = "registry-http" + type = "http" + path = "/" + interval = "10s" + timeout = "3s" + } + } + + task "registry" { + driver = "podman" + + template { + data = < Date: Wed, 19 Jul 2023 09:24:37 -0500 Subject: [PATCH 2/5] packer: use nomad-driver-podman v0.5.0 --- .../packer/ubuntu-jammy-amd64/setup.sh | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh index 47262f16bfe3..c896b31c7c4a 100755 --- a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh +++ b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh @@ -43,8 +43,20 @@ sudo chown root:root /usr/local/bin/sockaddr sudo ufw disable || echo "ufw not installed" echo "Install HashiCorp apt repositories" -curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - -sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" +wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg +echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list +sudo apt-get update && sudo apt-get install nomad + +echo "Installing Docker apt repositories" +sudo install -m 0755 -d /etc/apt/keyrings +curl --insecure -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg +sudo chmod a+r /etc/apt/keyrings/docker.gpg +echo \ + "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + +echo "Refresh apt with third party repositories" sudo apt-get update echo "Install Consul and Nomad" @@ -67,16 +79,11 @@ mkdir_for_root /opt/nomad mkdir_for_root $NOMAD_PLUGIN_DIR sudo mv /tmp/linux/nomad.service /etc/systemd/system/nomad.service -echo "Installing third-party apt repositories" +echo "Installing third-party tools" +sudo apt-get install -y docker-ce docker-ce-cli # Docker -distro=$(lsb_release -si | tr '[:upper:]' '[:lower:]') -curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - -sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/${distro} $(lsb_release -cs) stable" - -# Docker -echo "Installing Docker" -sudo apt-get install -y docker-ce +sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli # Java echo "Installing Java" @@ -94,12 +101,12 @@ echo "Installing Podman" sudo apt-get -y install podman catatonit echo "Installing Podman Driver" -sudo hc-install install --path ${NOMAD_PLUGIN_DIR} --version 0.4.2 nomad-driver-podman +sudo hc-install install --path ${NOMAD_PLUGIN_DIR} --version 0.5.0 nomad-driver-podman # Pledge echo "Installing Pledge Driver" -curl -fsSL -o /tmp/pledge-driver.tar.gz https://github.com/shoenig/nomad-pledge-driver/releases/download/v0.2.3/nomad-pledge-driver_0.2.3_linux_amd64.tar.gz -curl -fsSL -o /tmp/pledge https://github.com/shoenig/nomad-pledge-driver/releases/download/pledge-1.8.com/pledge-1.8.com +curl -k -fsSL -o /tmp/pledge-driver.tar.gz https://github.com/shoenig/nomad-pledge-driver/releases/download/v0.2.3/nomad-pledge-driver_0.2.3_linux_amd64.tar.gz +curl -k -fsSL -o /tmp/pledge https://github.com/shoenig/nomad-pledge-driver/releases/download/pledge-1.8.com/pledge-1.8.com tar -C /tmp -xf /tmp/pledge-driver.tar.gz sudo mv /tmp/nomad-pledge-driver ${NOMAD_PLUGIN_DIR} sudo mv /tmp/pledge /usr/local/bin From 675ad7499e8e17a04249ffa0a0d4fb5298f84dcf Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 19 Jul 2023 15:51:57 -0500 Subject: [PATCH 3/5] e2e: eliminate unnecessary chmod Co-authored-by: Daniel Bennett --- e2e/podman/input/registry.hcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/podman/input/registry.hcl b/e2e/podman/input/registry.hcl index 92bdd123ee2a..f2e24bd93e7b 100644 --- a/e2e/podman/input/registry.hcl +++ b/e2e/podman/input/registry.hcl @@ -125,7 +125,7 @@ podman push --tls-verify=false --authfile=local/auth.json docker.io/library/bash config { command = "bash" - args = ["-c", "chmod +x local/script.sh && local/script.sh"] + args = ["local/script.sh"] } resources { From 5e2d6be5d31fe1291cf94d491242b35fc246b75e Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 19 Jul 2023 20:55:01 +0000 Subject: [PATCH 4/5] cr: no need to install nomad twice --- e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh index c896b31c7c4a..7b3d323bbe07 100755 --- a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh +++ b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh @@ -45,7 +45,6 @@ sudo ufw disable || echo "ufw not installed" echo "Install HashiCorp apt repositories" wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list -sudo apt-get update && sudo apt-get install nomad echo "Installing Docker apt repositories" sudo install -m 0755 -d /etc/apt/keyrings From 3777d6d961f59cf6fb556cf9caf85ded8fabbdc5 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 19 Jul 2023 20:59:04 +0000 Subject: [PATCH 5/5] cl: no need to install docker twice --- e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh index 7b3d323bbe07..4dcfe85c3cce 100755 --- a/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh +++ b/e2e/terraform/packer/ubuntu-jammy-amd64/setup.sh @@ -79,10 +79,10 @@ mkdir_for_root $NOMAD_PLUGIN_DIR sudo mv /tmp/linux/nomad.service /etc/systemd/system/nomad.service echo "Installing third-party tools" -sudo apt-get install -y docker-ce docker-ce-cli # Docker -sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli +echo "Installing Docker CE" +sudo apt-get install -y docker-ce docker-ce-cli # Java echo "Installing Java"