| 1 | #!/usr/bin/env python3 |
| 2 | """Tests for writing data/config.php from the auth_client_secret CI secret. |
| 3 | |
| 4 | Fake secrets below are test fixtures only. The real secret is never used here. |
| 5 | This file prints only pass/fail text, never a generated config or a secret. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | import tempfile |
| 13 | from pathlib import Path |
| 14 | |
| 15 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 16 | from deploy import ( # noqa: E402 |
| 17 | auth_config_php, |
| 18 | deploy_commands, |
| 19 | php_single_quoted, |
| 20 | ) |
| 21 | |
| 22 | FAKE = 'test-secret-value-NOT-FOR-PRODUCTION' |
| 23 | FAKE_QUOTED = "it's a \\secret with 'quotes'" |
| 24 | ENV = { |
| 25 | 'timftphost': 'ftp://example.test', |
| 26 | 'timftpuser': 'u', |
| 27 | 'timftppassword': 'p', |
| 28 | } |
| 29 | |
| 30 | |
| 31 | def check(cond, msg): |
| 32 | if not cond: |
| 33 | raise SystemExit('FAIL: ' + msg) |
| 34 | |
| 35 | |
| 36 | def 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 | |
| 42 | def 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 | |
| 48 | def 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 | |
| 92 | def 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 | |
| 110 | def 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 | |
| 119 | if __name__ == '__main__': |
| 120 | sys.exit(main()) |
projects-timfalken-com meteors