|
| 1 | +""" |
| 2 | +Integration tests for truststore inheritance and merging. |
| 3 | +
|
| 4 | +Tests that CodeQL can connect to HTTPS servers with custom CA certificates: |
| 5 | +1. test_buildless: Buildless mode inherits truststore from MAVEN_OPTS |
| 6 | +2. test_autobuild_merge_trust_store: Autobuild merges system truststore with |
| 7 | + CODEQL_PROXY_CA_CERTIFICATE (fixes github/codeql-team#4482) |
| 8 | +""" |
1 | 9 | import subprocess |
2 | 10 | import os |
| 11 | +import pytest |
3 | 12 | import runs_on |
| 13 | +from contextlib import contextmanager |
4 | 14 |
|
5 | 15 |
|
6 | | -def test(codeql, java, cwd): |
7 | | - # This serves the "repo" directory on https://locahost:4443 |
| 16 | +@contextmanager |
| 17 | +def _https_server(cwd): |
| 18 | + """Start an HTTPS server serving the repo/ directory on https://localhost:4443.""" |
8 | 19 | command = ["python3", "../server.py"] |
9 | 20 | if runs_on.github_actions and runs_on.posix: |
10 | 21 | # On GitHub Actions, we saw the server timing out while running in parallel with other tests |
11 | 22 | # we work around that by running it with higher permissions |
12 | 23 | command = ["sudo"] + command |
13 | 24 | repo_server_process = subprocess.Popen(command, cwd="repo") |
14 | | - certspath = cwd / "jdk8_shipped_cacerts_plus_cert_pem" |
15 | | - # If we override MAVEN_OPTS, we'll break cross-test maven isolation, so we need to append to it instead |
16 | | - maven_opts = os.environ["MAVEN_OPTS"] + f" -Djavax.net.ssl.trustStore={certspath}" |
17 | | - |
18 | 25 | try: |
| 26 | + yield |
| 27 | + finally: |
| 28 | + repo_server_process.kill() |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.ql_test(expected=".buildless.expected") |
| 32 | +def test_buildless(codeql, java, cwd, check_diagnostics, check_buildless_fetches): |
| 33 | + """Test that buildless mode inherits truststore from MAVEN_OPTS.""" |
| 34 | + # Use buildless-specific expected file suffixes |
| 35 | + check_diagnostics.expected_suffix = ".buildless.expected" |
| 36 | + check_buildless_fetches.expected_suffix = ".buildless.expected" |
| 37 | + |
| 38 | + with _https_server(cwd): |
| 39 | + certspath = cwd / "jdk8_shipped_cacerts_plus_cert_pem" |
| 40 | + # If we override MAVEN_OPTS, we'll break cross-test maven isolation, so we need to append to it instead |
| 41 | + maven_opts = os.environ["MAVEN_OPTS"] + f" -Djavax.net.ssl.trustStore={certspath}" |
| 42 | + |
19 | 43 | codeql.database.create( |
20 | 44 | extractor_option="buildless=true", |
21 | 45 | _env={ |
22 | 46 | "MAVEN_OPTS": maven_opts, |
23 | 47 | "CODEQL_JAVA_EXTRACTOR_TRUST_STORE_PATH": str(certspath), |
24 | 48 | }, |
25 | 49 | ) |
26 | | - finally: |
27 | | - repo_server_process.kill() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.ql_test(expected=".autobuild.expected") |
| 53 | +def test_autobuild_merge_trust_store(codeql, java, cwd, check_diagnostics): |
| 54 | + """ |
| 55 | + Test that autobuild merges system truststore with CODEQL_PROXY_CA_CERTIFICATE. |
| 56 | +
|
| 57 | + This tests the fix for github/codeql-team#4482 where autobuild was overriding |
| 58 | + JAVA_TOOL_OPTIONS truststore with a new one containing only the proxy CA, |
| 59 | + causing PKIX failures when connecting to internal HTTPS servers. |
| 60 | + """ |
| 61 | + # Use autobuild-specific expected file suffix |
| 62 | + check_diagnostics.expected_suffix = ".autobuild.expected" |
| 63 | + |
| 64 | + with _https_server(cwd): |
| 65 | + certspath = cwd / "jdk8_shipped_cacerts_plus_cert_pem" |
| 66 | + |
| 67 | + # Read the certificate to use as CODEQL_PROXY_CA_CERTIFICATE |
| 68 | + with open(cwd / "cert.pem", "r") as f: |
| 69 | + proxy_ca_cert = f.read() |
| 70 | + |
| 71 | + # Set JAVA_TOOL_OPTIONS to use our custom truststore |
| 72 | + # This is the key setting that was being overridden before the fix |
| 73 | + java_tool_options = f"-Djavax.net.ssl.trustStore={certspath}" |
| 74 | + |
| 75 | + # Run autobuild with the truststore configured |
| 76 | + # Before the fix: autobuild would create a new truststore with ONLY the proxy CA, |
| 77 | + # losing the custom CA from JAVA_TOOL_OPTIONS, causing PKIX failures |
| 78 | + # After the fix: autobuild merges the system truststore + proxy CA |
| 79 | + codeql.database.create( |
| 80 | + build_mode="autobuild", |
| 81 | + _env={ |
| 82 | + "JAVA_TOOL_OPTIONS": java_tool_options, |
| 83 | + "CODEQL_PROXY_CA_CERTIFICATE": proxy_ca_cert, |
| 84 | + }, |
| 85 | + ) |
0 commit comments