| 1 | #!/usr/bin/env python3 |
| 2 | import json |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import tempfile |
| 7 | import unittest |
| 8 | |
| 9 | import snowglobe |
| 10 | |
| 11 | |
| 12 | class SecretFiles(unittest.TestCase): |
| 13 | @classmethod |
| 14 | def setUpClass(cls): |
| 15 | with tempfile.TemporaryDirectory(prefix="snowglobe-secret-template-") as temporary: |
| 16 | fixture = Path(temporary) / "fixture.pkl" |
| 17 | fixture.write_text('amends ' + json.dumps((snowglobe.REPO / "config/Service.pkl").as_uri()) + '\nname = "Fixture"\ncontainer { image = "localhost/fixture:pinned" }\n') |
| 18 | result = subprocess.run([os.environ.get("PKL", "pkl"), "eval", "-p", "uid=3132", "-p", "serviceId=fixture", str(fixture)], check=True, capture_output=True, text=True) |
| 19 | cls.fixture = json.loads(result.stdout) |
| 20 | cls.fixture.update(uid=3132, sourceId="fixture", inputs={}) |
| 21 | cls.fixture.pop("requirements") |
| 22 | |
| 23 | def setUp(self): |
| 24 | self.data = json.loads(json.dumps(self.fixture)) |
| 25 | self.task = self.data["containers"]["app"] |
| 26 | self.task["secretFiles"] = {"/run/secrets/oidc.json": '{{ with nomadVar "nomad/jobs/fixture/inputs/oidc" }}{"clientSecret":{{ .clientSecret.Value | toJSON }}}{{ end }}'} |
| 27 | |
| 28 | def test_private_allocation_template_and_read_only_file_mount(self): |
| 29 | rendered = snowglobe.render(self.data, {"fixture": self.data}) |
| 30 | self.assertIn('"secrets/oidc.json:/run/secrets/oidc.json:ro"', rendered) |
| 31 | self.assertIn('destination = "secrets/oidc.json"', rendered) |
| 32 | self.assertIn('perms = "0400"', rendered) |
| 33 | self.assertIn('uid = 3132', rendered) |
| 34 | self.assertIn('gid = 3132', rendered) |
| 35 | self.assertIn('error_on_missing_key = true', rendered) |
| 36 | self.assertIn(self.task["secretFiles"]["/run/secrets/oidc.json"], rendered) |
| 37 | self.assertNotIn('env = true', rendered) |
| 38 | self.assertNotIn('change_mode =', rendered) |
| 39 | |
| 40 | def test_services_without_files_gain_no_template_or_mount(self): |
| 41 | self.task["secretFiles"] = {} |
| 42 | rendered = snowglobe.render(self.data, {"fixture": self.data}) |
| 43 | self.assertNotIn('template {', rendered) |
| 44 | self.assertNotIn('secrets/', rendered) |
| 45 | |
| 46 | def test_read_only_root_is_opt_in_and_preserves_writable_data_mounts(self): |
| 47 | self.assertNotIn('readonly_rootfs', snowglobe.render(self.data, {"fixture": self.data})) |
| 48 | self.task["readOnlyRoot"] = True |
| 49 | self.task["volumes"] = {"/mirror": {"src": "/fixture/mirror", "readOnly": False}} |
| 50 | self.task["capAdd"] = ["FOWNER"] |
| 51 | rendered = snowglobe.render(self.data, {"fixture": self.data}) |
| 52 | self.assertIn('readonly_rootfs = true', rendered) |
| 53 | self.assertIn('cap_add = ["FOWNER"]', rendered) |
| 54 | self.assertIn('/fixture/mirror:/mirror"', rendered) |
| 55 | del self.task["readOnlyRoot"] |
| 56 | self.assertNotIn('readonly_rootfs', snowglobe.render(self.data, {"fixture": self.data})) |
| 57 | |
| 58 | def test_unsafe_and_colliding_targets_are_rejected(self): |
| 59 | for target in ("relative.json", "/run/../secret.json", "/run/./secret.json", "/run//secret.json", "/run/secrets/", "/secrets/file", "/secrets", "/run/app.env", "/run/app-secret.env", "/run/nomad_token", "/run/line\nbreak"): |
| 60 | with self.subTest(target=target): |
| 61 | self.task["secretFiles"] = {target: "{}"} |
| 62 | with self.assertRaisesRegex(ValueError, "invalid secret file target"): |
| 63 | snowglobe.render(self.data, {"fixture": self.data}) |
| 64 | self.task["secretFiles"] = {"/one/config.json": "{}", "/two/config.json": "{}"} |
| 65 | with self.assertRaisesRegex(ValueError, "invalid secret file target"): |
| 66 | snowglobe.render(self.data, {"fixture": self.data}) |
| 67 | self.task["secretFiles"] = {"/run/secrets/oidc.json": "{}"} |
| 68 | self.task["volumes"] = {"/run/secrets/oidc.json": {}} |
| 69 | with self.assertRaisesRegex(ValueError, "invalid secret file target"): |
| 70 | snowglobe.render(self.data, {"fixture": self.data}) |
| 71 | |
| 72 | def test_image_user_and_heredoc_injection_are_rejected(self): |
| 73 | self.task["imageUser"] = True |
| 74 | with self.assertRaisesRegex(ValueError, "service-owned"): |
| 75 | snowglobe.render(self.data, {"fixture": self.data}) |
| 76 | self.task["imageUser"] = False |
| 77 | for body in ("EOF", "{}\nEOF\nmalicious", "{}\nEOF\r\nmalicious"): |
| 78 | self.task["secretFiles"] = {"/run/secrets/oidc.json": body} |
| 79 | with self.assertRaisesRegex(ValueError, "invalid secret template delimiter"): |
| 80 | snowglobe.render(self.data, {"fixture": self.data}) |
| 81 | |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | unittest.main() |