cexy 0.1.0-dev.16

Official Rust SDK for the CEXY.io exchange API (REST + WebSocket)
Documentation
//! The one TLS configuration used by both the REST client and the WebSocket client: rustls with
//! the `ring` crypto provider and the Mozilla root store from `webpki-roots` (the same roots on
//! every platform, no OpenSSL, no system trust store, no C toolchain needed to build).

use std::sync::Arc;
#[cfg(not(test))]
use std::sync::OnceLock;

use rustls::{ClientConfig, RootCertStore};

/// Extra trust anchors for the tests' local TLS servers. Test builds only.
#[cfg(test)]
pub(crate) static TEST_ROOTS: std::sync::Mutex<Vec<rustls::pki_types::CertificateDer<'static>>> =
    std::sync::Mutex::new(Vec::new());

fn build() -> Arc<ClientConfig> {
    let mut roots = RootCertStore::empty();
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    #[cfg(test)]
    for c in TEST_ROOTS.lock().unwrap().iter() {
        roots.add(c.clone()).expect("test root");
    }
    #[cfg(cexy_test_extra_root)]
    extra_test_roots(&mut roots);
    let config =
        ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
            .with_safe_default_protocol_versions()
            .expect("ring supports the default protocol versions")
            .with_root_certificates(roots)
            .with_no_client_auth();
    Arc::new(config)
}

/// Only in builds compiled with `RUSTFLAGS="--cfg cexy_test_extra_root"` (never a normal build):
/// also trust the PEM certificates in `CEXY_TEST_EXTRA_ROOT_PEM`. The repository's `ci/consumer`
/// check uses it to reach a local TLS test server through the real, non-test client.
///
/// It is a rustc cfg, not a Cargo feature, on purpose: features unify across a dependency graph, so
/// any dependency could enable one silently, and whoever controls the environment of such a build
/// could then add a trusted root and intercept API traffic. A cfg can only come from the top-level
/// build's own flags. Both opt-ins are needed: the cfg at compile time and the variable at run time.
#[cfg(cexy_test_extra_root)]
fn extra_test_roots(roots: &mut RootCertStore) {
    use rustls::pki_types::{CertificateDer, pem::PemObject};
    if let Ok(pem) = std::env::var("CEXY_TEST_EXTRA_ROOT_PEM") {
        for cert in CertificateDer::pem_slice_iter(pem.as_bytes()).flatten() {
            let _ = roots.add(cert);
        }
    }
}

/// The shared rustls client configuration.
pub(crate) fn client_config() -> Arc<ClientConfig> {
    #[cfg(not(test))]
    {
        static CONFIG: OnceLock<Arc<ClientConfig>> = OnceLock::new();
        CONFIG.get_or_init(build).clone()
    }
    #[cfg(test)]
    build()
}