BlueGit

projects-timfalken-com meteors

Files

meteors / ci / test_deploy_auth.py

test_deploy_auth.py

test_deploy_auth.py 4.2 KiB Download Raw
1#!/usr/bin/env python3
2"""Tests for writing data/config.php from the auth_client_secret CI secret.
3
4Fake secrets below are test fixtures only. The real secret is never used here.
5This file prints only pass/fail text, never a generated config or a secret.
6"""
7from __future__ import annotations
8
9import os
10import subprocess
11import sys
12import tempfile
13from pathlib import Path
14
15sys.path.insert(0, str(Path(__file__).resolve().parent))
16from deploy import ( # noqa: E402
17 auth_config_php,
18 deploy_commands,
19 php_single_quoted,
20)
21
22FAKE = 'test-secret-value-NOT-FOR-PRODUCTION'
23FAKE_QUOTED = "it's a \\secret with 'quotes'"
24ENV = {
25 'timftphost': 'ftp://example.test',
26 'timftpuser': 'u',
27 'timftppassword': 'p',
28}
29
30
31def check(cond, msg):
32 if not cond:
33 raise SystemExit('FAIL: ' + msg)
34
35
36def test_empty_secret():
37 check(auth_config_php({}) is None, 'unset → None')
38 check(auth_config_php({'auth_client_secret': ''}) is None, 'empty → None')
39 check(auth_config_php({'auth_client_secret': ' '}) is None, 'whitespace → None')
40
41
42def test_php_quoting():
43 check(php_single_quoted('hello') == "'hello'", 'plain')
44 check(php_single_quoted("it's") == r"'it\'s'", 'quote')
45 check(php_single_quoted('a\\b') == r"'a\\b'", 'backslash')
46
47
48def test_generated_php_evaluates():
49 php = auth_config_php({'auth_client_secret': FAKE_QUOTED, 'auth_client_id': ''})
50 check(php is not None, 'php generated')
51 check('projects.timfalken.com' in php, 'default client_id')
52 check('https://projects.timfalken.com/meteors' in php, 'base_url')
53 php2 = auth_config_php({
54 'auth_client_secret': FAKE,
55 'auth_client_id': 'other.example',
56 })
57 check('other.example' in php2, 'optional auth_client_id')
58
59 cfg = Path(tempfile.mkstemp(suffix='.php')[1])
60 wrp = Path(tempfile.mkstemp(suffix='.php')[1])
61 cfg.write_text(php)
62 # Wrapper prints only safe fields and a MATCH flag; never the secret.
63 wrp.write_text(
64 "<?php\n"
65 "$c = require $argv[1];\n"
66 "echo $c['client_id'], chr(10), $c['base_url'], chr(10), "
67 "strlen($c['client_secret']), chr(10), "
68 "($c['client_secret'] === $argv[2] ? 'MATCH' : 'MISMATCH'), chr(10);\n"
69 )
70 try:
71 result = subprocess.run(
72 ['php', str(wrp), str(cfg), FAKE_QUOTED],
73 capture_output=True,
74 text=True,
75 check=False,
76 )
77 finally:
78 cfg.unlink(missing_ok=True)
79 wrp.unlink(missing_ok=True)
80 if result.returncode != 0:
81 # Redact any accidental secret material before printing.
82 blob = (result.stdout + '\n' + result.stderr).replace(FAKE_QUOTED, '[FAKE]').replace(FAKE, '[FAKE]')
83 raise SystemExit('FAIL: php exit %s: %s' % (result.returncode, blob[:300]))
84 lines = result.stdout.strip().splitlines()
85 check(len(lines) >= 4, 'php output lines')
86 check(lines[0] == 'projects.timfalken.com', 'client_id eval')
87 check(lines[1] == 'https://projects.timfalken.com/meteors', 'base_url eval')
88 check(lines[3] == 'MATCH', 'secret round-trip')
89 check(FAKE_QUOTED not in result.stdout, 'php test stdout leaked secret')
90
91
92def test_lftp_script_has_no_secret():
93 cmds = '\n'.join(deploy_commands(ENV, '/var/www/meteors', Path('build')))
94 check('--delete' not in cmds, 'no --delete')
95 check('--exclude-glob "**/config.php"' in cmds, 'mirror excludes config')
96 check('--exclude-glob "data/**"' in cmds, 'mirror excludes data')
97 check(' -o data/config.php' not in cmds, 'no put without secret path')
98 check('data/.htaccess' in cmds, 'htaccess still put')
99
100 path = '/tmp/meteors-auth-XXXX/config.php'
101 cmds2 = '\n'.join(
102 deploy_commands(ENV, '/var/www/meteors', Path('build'), auth_config_path=path)
103 )
104 check(' -o data/config.php' in cmds2, 'put when path given')
105 check(path in cmds2, 'local path in put')
106 check(FAKE not in cmds2 and FAKE_QUOTED not in cmds2, 'secret not in lftp script')
107 check('--delete' not in cmds2, 'no --delete with config put')
108
109
110def main():
111 test_empty_secret()
112 test_php_quoting()
113 test_generated_php_evaluates()
114 test_lftp_script_has_no_secret()
115 print('auth-config ok')
116 return 0
117
118
119if __name__ == '__main__':
120 sys.exit(main())