-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass file secrets to "podman build" via parameter "--secret"
to make them available for "RUN --mount=type=secret" statements inside the Dockerfile. Keep using --volume to pass file secrets to "podman run". Signed-off-by: wiehe <[email protected]>
- Loading branch information
Showing
6 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM busybox | ||
|
||
RUN --mount=type=secret,required=true,id=build_secret \ | ||
ls -l /run/secrets/ && cat /run/secrets/build_secret | ||
|
||
RUN --mount=type=secret,required=true,id=build_secret,target=/tmp/secret \ | ||
ls -l /run/secrets/ /tmp/ && cat /tmp/secret | ||
|
||
CMD [ 'echo', 'nothing here' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: "3.8" | ||
|
||
services: | ||
test: | ||
image: test | ||
secrets: | ||
- run_secret # implicitly mount to /run/secrets/run_secret | ||
- source: run_secret | ||
target: /tmp/run_secret2 # explicit mount point | ||
|
||
build: | ||
context: . | ||
secrets: | ||
- build_secret # can be mounted in Dockerfile with "RUN --mount=type=secret,id=build_secret" | ||
- source: build_secret | ||
target: build_secret2 # rename to build_secret2 | ||
|
||
secrets: | ||
build_secret: | ||
file: ./my_secret | ||
run_secret: | ||
file: ./my_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: "3.8" | ||
|
||
services: | ||
test: | ||
image: test | ||
build: | ||
context: . | ||
secrets: | ||
# invalid target argument | ||
# | ||
# According to https://github.com/compose-spec/compose-spec/blob/master/build.md, target is | ||
# supposed to be the "name of a *file* to be mounted in /run/secrets/". Not a path. | ||
- source: build_secret | ||
target: /build_secret | ||
|
||
secrets: | ||
build_secret: | ||
file: ./my_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
important-secret-is-important |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# SPDX-License-Identifier: GPL-2.0 | ||
|
||
|
||
"""Test how secrets in files are passed to podman.""" | ||
|
||
import os | ||
import subprocess | ||
import unittest | ||
|
||
from .test_podman_compose import podman_compose_path | ||
from .test_podman_compose import test_path | ||
|
||
|
||
def compose_yaml_path(): | ||
""" "Returns the path to the compose file used for this test module""" | ||
return os.path.join(test_path(), "build_secrets") | ||
|
||
|
||
class TestComposeBuildSecrets(unittest.TestCase): | ||
def test_run_secret(self): | ||
"""podman run should receive file secrets as --volume | ||
See build_secrets/docker-compose.yaml for secret names and mount points (aka targets) | ||
""" | ||
cmd = ( | ||
"coverage", | ||
"run", | ||
podman_compose_path(), | ||
"--dry-run", | ||
"--verbose", | ||
"-f", | ||
os.path.join(compose_yaml_path(), "docker-compose.yaml"), | ||
"run", | ||
"test", | ||
) | ||
p = subprocess.run( | ||
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True | ||
) | ||
self.assertEqual(p.returncode, 0) | ||
secret_path = os.path.join(compose_yaml_path(), "my_secret") | ||
self.assertIn(f"--volume {secret_path}:/run/secrets/run_secret:ro,rprivate,rbind", p.stdout) | ||
self.assertIn(f"--volume {secret_path}:/tmp/run_secret2:ro,rprivate,rbind", p.stdout) | ||
|
||
def test_build_secret(self): | ||
"""podman build should receive secrets as --secret, so that they can be used inside the | ||
Dockerfile in "RUN --mount=type=secret ..." commands. | ||
""" | ||
cmd = ( | ||
"coverage", | ||
"run", | ||
podman_compose_path(), | ||
"--dry-run", | ||
"--verbose", | ||
"-f", | ||
os.path.join(compose_yaml_path(), "docker-compose.yaml"), | ||
"build", | ||
) | ||
p = subprocess.run( | ||
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True | ||
) | ||
self.assertEqual(p.returncode, 0) | ||
secret_path = os.path.join(compose_yaml_path(), "my_secret") | ||
self.assertIn(f"--secret id=build_secret,src={secret_path}", p.stdout) | ||
self.assertIn(f"--secret id=build_secret2,src={secret_path}", p.stdout) | ||
|
||
def test_invalid_build_secret(self): | ||
"""build secrets in docker-compose file can only have a target argument without directory | ||
component | ||
""" | ||
cmd = ( | ||
"coverage", | ||
"run", | ||
podman_compose_path(), | ||
"--dry-run", | ||
"--verbose", | ||
"-f", | ||
os.path.join(compose_yaml_path(), "docker-compose.yaml.invalid"), | ||
"build", | ||
) | ||
p = subprocess.run( | ||
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True | ||
) | ||
self.assertNotEqual(p.returncode, 0) | ||
self.assertIn( | ||
'ValueError: ERROR: Build secret "build_secret" has invalid target "/build_secret"', | ||
p.stdout, | ||
) |