Skip to content

Commit d8aa49b

Browse files
test(testkit): reject a mismatched additional CA key at load time
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 41184f2 commit d8aa49b

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎tests/support/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ tests to read. A tmachine environment publishes its variables with
1212
`tests/tmachine/ansible/playbooks/test-env.yaml`, and `openshell-test-runner`
1313
passes them to the tests. To run a suite against your own deployment, set them
1414
yourself. A fixture fails the test when a variable it needs is missing; it
15-
never skips it. Add a row when you add a variable.
15+
never skips it. Add a row when you add a variable. The runner also sets
16+
`OPENSHELL_GATEWAY` and `OPENSHELL_BIN`, which testkit reads; see
17+
[Running against a gateway](../testkit/README.md#running-against-a-gateway).
1618

1719
| Variable | Set by | Read by | Meaning |
1820
|---|---|---|---|
@@ -23,7 +25,7 @@ never skips it. Add a row when you add a variable.
2325
| `OPENSHELL_TEST_KEYCLOAK_CLIENT_ID` | `<base>-keycloak` | `keycloak-support` | A client in that realm that allows password grants. |
2426
| `OPENSHELL_TEST_KEYCLOAK_USERNAME` | `<base>-keycloak` | `keycloak-support` | The realm user that tests sign in as. |
2527
| `OPENSHELL_TEST_KEYCLOAK_PASSWORD` | `<base>-keycloak` | `keycloak-support` | That user's password. |
26-
| `OPENSHELL_TEST_ADDITIONAL_CA_CERT` | The additional-CA environment, which configures the gateway's `additional_ca_bundle` (added by #5729) | `host-server-support` (`TestCa::from_env`) | The path of the CA certificate, in PEM, that the gateway trusts. |
28+
| `OPENSHELL_TEST_ADDITIONAL_CA_CERT` | The additional-CA environment, which sets the driver's `additional_ca_bundle` (added by #5729) | `host-server-support` (`TestCa::from_env`) | The path of the CA certificate, in PEM, that the supervisor trusts for sandbox egress. |
2729
| `OPENSHELL_TEST_ADDITIONAL_CA_KEY` | The additional-CA environment | `host-server-support` (`TestCa::from_env`) | The path of that CA's private key, in PEM, readable by the test user. Tests sign server certificates with it. |
2830
| `OPENSHELL_TEST_BASE_IMAGE` | Optional; you, for example to use a mirror | `testkit::base_image()` | The default sandbox image. Unset, it is `nvcr.io/nvidia/base/ubuntu:24.04`. |
2931
| `OPENSHELL_TEST_PYTHON_IMAGE` | Optional; `e2e/rust/e2e-kubernetes.sh` and `e2e-vm.sh` set a pullable image | `testkit::python_image()` | The workload image for tests that run `python3`, curl, git or openssl. Unset, it is `openshell/e2e-python:dev`, which the tmachine installers load. |

‎tests/support/host-server/src/tls.rs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! - [`TestCa::new`] makes a CA that only the test knows, so nothing trusts it
1010
//! unless the test hands over [`TestCa::cert_pem`].
1111
//! - [`TestCa::from_env`] loads the environment's additional CA, which the
12-
//! gateway is configured to trust (`additional_ca_bundle`). The environment
12+
//! supervisor trusts for sandbox egress (`additional_ca_bundle`). The environment
1313
//! publishes the paths of its PEM certificate and key in
1414
//! `OPENSHELL_TEST_ADDITIONAL_CA_CERT` and `OPENSHELL_TEST_ADDITIONAL_CA_KEY`.
1515
@@ -82,9 +82,23 @@ impl TestCa {
8282
"parse the CA certificate {}",
8383
cert.display()
8484
)))?;
85-
let key = KeyPair::from_pem(&key_pem)
85+
let key_pair = KeyPair::from_pem(&key_pem)
8686
.map_err(failed(format!("parse the CA key {}", key.display())))?;
87-
let issuer = pki::issuer_from_der(&der, key)
87+
// issuer_from_der reads the certificate's metadata only, so check the
88+
// key here rather than at the first client handshake.
89+
let key_der = key_pair
90+
.serialize_der()
91+
.map_err(failed(format!("export the CA key {}", key.display())))?;
92+
openshell_crypto::tls::try_server_builder()
93+
.map_err(failed("build a TLS server configuration"))?
94+
.with_no_client_auth()
95+
.with_single_cert(vec![der.clone()], PrivateKeyDer::Pkcs8(key_der.into()))
96+
.map_err(failed(format!(
97+
"the CA key {} does not match the CA certificate {}",
98+
key.display(),
99+
cert.display()
100+
)))?;
101+
let issuer = pki::issuer_from_der(&der, key_pair)
88102
.map_err(failed(format!("load the CA {}", cert.display())))?;
89103
Ok(Self { cert_pem, issuer })
90104
}
@@ -301,6 +315,14 @@ mod tests {
301315
);
302316
}
303317

318+
#[test]
319+
fn from_files_rejects_a_key_from_another_ca() {
320+
let (cert, _, _) = write_ca("host-server-mismatch-a");
321+
let (_, other_key, _) = write_ca("host-server-mismatch-b");
322+
let error = TestCa::from_files(&cert, &other_key).err().unwrap();
323+
assert!(error.to_string().contains("does not match"), "{error}");
324+
}
325+
304326
#[test]
305327
fn leaf_needs_a_name() {
306328
assert!(TestCa::new().unwrap().leaf(&[]).is_err());

0 commit comments

Comments
 (0)