Skip to content

Commit 8005863

Browse files
test(testkit): harden host server TLS and name environment ports _PORT
Add the authority key identifier to server certificates, which strict verifiers such as Python 3.13 require, time out TLS handshakes, report CA load failures as such, and keep test CA files in temporary directories. The contract table now names environment-chosen ports OPENSHELL_TEST_<SERVICE>_PORT, matching HttpRecorder's port option. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 8b1fcef commit 8005863

5 files changed

Lines changed: 38 additions & 17 deletions

File tree

‎tests/support/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/support/README.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ purpose: unit-test child-process markers, the re-entry markers in
3737
`tests/support/environment.rs`, Nix guest-cache settings, `install.sh` test
3838
hooks and crate test settings.
3939

40-
### Addresses the environment chooses
40+
### Ports the environment chooses
4141

42-
When a test must listen on an address the environment picked, because the
43-
gateway or another service is configured to call it, the environment
44-
publishes it as `OPENSHELL_TEST_<SERVICE>_LISTEN` and the test binds it, for
45-
example with `HttpRecorder::builder().port(port)` from `host-server-support`.
46-
The test never picks the address itself. Tests that bind the same address can't
47-
run at the same time.
42+
When a test must listen on a port the environment picked, because the gateway
43+
or another service is configured to call it, the environment publishes it as
44+
`OPENSHELL_TEST_<SERVICE>_PORT` and the test binds that port on every host
45+
address, for example with `HttpRecorder::builder().port(port)` from
46+
`host-server-support`. The test never picks the port itself. Tests that bind
47+
the same port can't run at the same time.

‎tests/support/host-server/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ tokio-rustls = { version = "0.26", default-features = false, features = ["tls12"
2222
tungstenite = "0.26"
2323

2424
[dev-dependencies]
25+
tempfile = "3"
2526
tokio = { version = "1.43", features = ["macros", "rt"] }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ impl HttpRecorder {
225225
match tls {
226226
None => respond(stream, local_addr, handler, requests).await,
227227
Some(tls) => {
228-
if let Ok(stream) = tls.accept(stream).await {
228+
let handshake = tokio::time::timeout(READ_TIMEOUT, tls.accept(stream));
229+
if let Ok(Ok(stream)) = handshake.await {
229230
respond(stream, local_addr, handler, requests).await;
230231
}
231232
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl TestCa {
9494
.with_no_client_auth()
9595
.with_single_cert(vec![der.clone()], PrivateKeyDer::Pkcs8(key_der.into()))
9696
.map_err(failed(format!(
97-
"the CA key {} does not match the CA certificate {}",
97+
"load the CA key {} with the CA certificate {}",
9898
key.display(),
9999
cert.display()
100100
)))?;
@@ -121,6 +121,8 @@ impl TestCa {
121121
params.distinguished_name.push(DnType::CommonName, first);
122122
params.key_usages = vec![KeyUsagePurpose::DigitalSignature];
123123
params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ServerAuth];
124+
// Strict verifiers, such as Python 3.13 and later, require it.
125+
params.use_authority_key_identifier_extension = true;
124126
let key = pki::generate_keypair().map_err(failed("generate a server key"))?;
125127
let cert = pki::signed_by_issuer(params, &key, &self.issuer)
126128
.map_err(failed(format!("sign a server certificate for {names:?}")))?;
@@ -192,6 +194,7 @@ mod tests {
192194
use std::path::PathBuf;
193195

194196
use rustls::pki_types::ServerName;
197+
use tempfile::TempDir;
195198
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
196199
use tokio::net::TcpStream;
197200
use tokio_rustls::TlsConnector;
@@ -231,12 +234,10 @@ mod tests {
231234
String::from_utf8_lossy(&response).into_owned()
232235
}
233236

234-
fn write_ca(dir: &str) -> (PathBuf, PathBuf, String) {
237+
fn write_ca(dir: &TempDir) -> (PathBuf, PathBuf, String) {
235238
let key = pki::generate_keypair().unwrap();
236239
let cert = pki::self_signed(ca_params(), &key).unwrap();
237-
let dir = std::env::temp_dir().join(format!("{dir}-{}", std::process::id()));
238-
std::fs::create_dir_all(&dir).unwrap();
239-
let (cert_path, key_path) = (dir.join("ca.pem"), dir.join("ca-key.pem"));
240+
let (cert_path, key_path) = (dir.path().join("ca.pem"), dir.path().join("ca-key.pem"));
240241
std::fs::write(&cert_path, cert.pem()).unwrap();
241242
std::fs::write(&key_path, key.serialize_pem().unwrap()).unwrap();
242243
(cert_path, key_path, cert.pem())
@@ -283,7 +284,8 @@ mod tests {
283284

284285
#[tokio::test]
285286
async fn from_vars_loads_the_environment_ca() {
286-
let (cert, key, cert_pem) = write_ca("host-server-env-ca");
287+
let dir = TempDir::new().unwrap();
288+
let (cert, key, cert_pem) = write_ca(&dir);
287289
let ca = TestCa::from_vars(|name| match name {
288290
ADDITIONAL_CA_CERT_ENV => Some(cert.display().to_string()),
289291
ADDITIONAL_CA_KEY_ENV => Some(key.display().to_string()),
@@ -317,10 +319,26 @@ mod tests {
317319

318320
#[test]
319321
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 (a, b) = (TempDir::new().unwrap(), TempDir::new().unwrap());
323+
let (cert, _, _) = write_ca(&a);
324+
let (_, other_key, _) = write_ca(&b);
322325
let error = TestCa::from_files(&cert, &other_key).err().unwrap();
323-
assert!(error.to_string().contains("does not match"), "{error}");
326+
assert!(
327+
error.to_string().contains("with the CA certificate"),
328+
"{error}"
329+
);
330+
}
331+
332+
#[test]
333+
fn leaf_names_its_issuer_key() {
334+
const AUTHORITY_KEY_IDENTIFIER: &[u8] = &[0x06, 0x03, 0x55, 0x1d, 0x23];
335+
let leaf = TestCa::new().unwrap().leaf(&["localhost"]).unwrap();
336+
assert!(
337+
leaf.cert_der
338+
.windows(AUTHORITY_KEY_IDENTIFIER.len())
339+
.any(|window| window == AUTHORITY_KEY_IDENTIFIER),
340+
"the server certificate has no authority key identifier"
341+
);
324342
}
325343

326344
#[test]

0 commit comments

Comments
 (0)