Skip to content

Commit

Permalink
Allow injecting certificate for tests
Browse files Browse the repository at this point in the history
Built on #609

When activating the `puffin-test-custom-ca-cert` feature, you can inject a custom ssl certificate by setting `PUFFIN_TEST_CA_CERT_PEM` to a pem file, e.g.

```bash
PUFFIN_TEST_CA_CERT_PEM=$(pwd)/mitmproxy-ca-cert.pem ./offlinepi record cargo test --features pypi --features puffin-test-custom-ca-cert -- --test-threads=1
```

This feature is off by default, so this is not possible in release builds.
  • Loading branch information
konstin committed Dec 12, 2023
1 parent 5565aef commit 38a13c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ cache-*

# python tmp files
__pycache__

scripts/offlinepi/mitmproxy-ca-cert.pem
scripts/offlinepi/responses.dat
3 changes: 3 additions & 0 deletions crates/puffin-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ url = { workspace = true }
[dev-dependencies]
anyhow = { workspace = true }
tokio = { workspace = true, features = ["fs", "macros"] }

[features]
puffin-test-custom-ca-cert = []
13 changes: 12 additions & 1 deletion crates/puffin-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,22 @@ impl RegistryClientBuilder {

pub fn build(self) -> RegistryClient {
let client_raw = {
let client_core = ClientBuilder::new()
let mut client_core = ClientBuilder::new()
.user_agent("puffin")
.pool_max_idle_per_host(20)
.timeout(std::time::Duration::from_secs(60 * 5));

if cfg!(feature = "puffin-test-custom-ca-cert") {
if let Some(cert) = std::env::var_os("PUFFIN_TEST_CA_CERT_PEM") {
client_core = client_core.add_root_certificate(
reqwest::Certificate::from_pem(
&fs_err::read(cert).expect("No PUFFIN_TEST_CA_CERT_PEM"),
)
.expect("Invalid certificate"),
)
}
}

client_core.build().expect("Fail to build HTTP client.")
};

Expand Down

0 comments on commit 38a13c7

Please sign in to comment.