containers_api/conn/
mod.rs

1//! Connection related items
2pub mod client;
3mod compat;
4mod headers;
5mod payload;
6pub mod transport;
7pub mod tty;
8
9pub use client::*;
10pub use headers::Headers;
11pub use payload::Payload;
12pub use transport::*;
13pub use tty::*;
14
15pub(crate) use compat::Compat;
16
17pub use http;
18pub use hyper;
19
20use hyper::client::HttpConnector;
21use hyper::StatusCode;
22use serde_json::Error as SerdeError;
23use thiserror::Error as ThisError;
24
25#[cfg(feature = "tls")]
26use {
27    hyper_openssl::HttpsConnector,
28    openssl::error::ErrorStack,
29    openssl::ssl::{SslConnector, SslFiletype, SslMethod},
30    std::path::Path,
31};
32
33/// Common result type used throughout this crate
34pub type Result<T> = std::result::Result<T, Error>;
35
36#[derive(Debug, ThisError)]
37/// All error variants that can happen during communication.
38pub enum Error {
39    #[error(transparent)]
40    SerdeJsonError(#[from] SerdeError),
41    #[error("The HTTP connection was not upgraded by the podman host")]
42    ConnectionNotUpgraded,
43    #[error(transparent)]
44    #[allow(clippy::upper_case_acronyms)]
45    IO(#[from] futures_util::io::Error),
46    #[error("error {code} - {message}")]
47    Fault { code: StatusCode, message: String },
48    #[error("Failed to parse uri - {0}")]
49    InvalidUri(http::uri::InvalidUri),
50    #[error(transparent)]
51    Hyper(#[from] hyper::Error),
52    #[error(transparent)]
53    Http(#[from] hyper::http::Error),
54    #[error(transparent)]
55    Encoding(#[from] std::string::FromUtf8Error),
56    #[cfg(feature = "tls")]
57    #[error(transparent)]
58    ErrorStack(#[from] ErrorStack),
59    #[error(transparent)]
60    Any(Box<dyn std::error::Error + 'static + Send + Sync>),
61}
62
63pub const AUTH_HEADER: &str = "X-Registry-Auth";
64
65pub fn get_http_connector() -> HttpConnector {
66    let mut http = HttpConnector::new();
67    http.enforce_http(false);
68
69    http
70}
71
72#[cfg(feature = "tls")]
73pub fn get_https_connector(
74    cert_path: &Path,
75    verify: bool,
76) -> Result<HttpsConnector<HttpConnector>> {
77    let mut ssl = SslConnector::builder(SslMethod::tls())?;
78    ssl.set_cipher_list("DEFAULT")?;
79    ssl.set_certificate_file(&cert_path.join("cert.pem"), SslFiletype::PEM)?;
80    ssl.set_private_key_file(&cert_path.join("key.pem"), SslFiletype::PEM)?;
81    verify.then(|| ssl.set_ca_file(&cert_path.join("ca.pem")));
82
83    HttpsConnector::with_connector(get_http_connector(), ssl).map_err(Error::from)
84}
85
86#[cfg(unix)]
87pub fn get_unix_connector() -> hyperlocal::UnixConnector {
88    hyperlocal::UnixConnector
89}