Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Research/prototype code to talk to a Docker Engine with Rust and Hyper
extern crate hyper;
extern crate openssl;
use hyper::{Client, Url};
use hyper::client::pool::{Config, Pool};
use hyper::http::h1::Http11Protocol;
use hyper::net::{HttpsConnector, Openssl};
use openssl::ssl::{SslContext, SslMethod};
use openssl::x509::X509FileType;
use std::env;
use std::io;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
macro_rules! println_stderr(
($($arg:tt)*) => (
match writeln!(&mut ::std::io::stderr(), $($arg)*) {
Ok(_) => {},
Err(x) => panic!("Unable to write to stderr: {}", x),
}
)
);
#[derive(Debug)]
struct CertFiles {
ca_file: String,
private_key_file: String,
certificate_file: String,
}
fn make_tls_client(certs: CertFiles) -> Client {
let config = Config::default();
let mut ssl_context = SslContext::new(SslMethod::Sslv23).unwrap();
ssl_context.set_CA_file(certs.ca_file).unwrap();
ssl_context.set_private_key_file(certs.private_key_file, X509FileType::PEM).unwrap();
ssl_context.set_certificate_file(certs.certificate_file, X509FileType::PEM).unwrap();
let connector = HttpsConnector::new(Openssl {
context: Arc::new(ssl_context)
});
let pool = Pool::with_connector(config, connector);
Client::with_protocol(Http11Protocol::with_connector(pool))
}
fn main() {
let mut docker_host = match env::var("DOCKER_HOST") {
Ok(val) => Url::parse(&val).unwrap(),
Err(e) => panic!("DOCKER_HOST must be set ({})", e)
};
docker_host.scheme = "https".to_owned();
let docker_cert_path = match env::var("DOCKER_CERT_PATH") {
Ok(val) => val,
Err(e) => panic!("DOCKER_CERT_PATH must be set ({})", e)
};
let cert_files = CertFiles {
ca_file: Path::new(&docker_cert_path).
join("ca.pem").to_str().unwrap().to_owned(),
private_key_file: Path::new(&docker_cert_path).
join("key.pem").to_str().unwrap().to_owned(),
certificate_file: Path::new(&docker_cert_path).
join("cert.pem").to_str().unwrap().to_owned(),
};
let url = format!("{}/info", docker_host);
let client = make_tls_client(cert_files);
let mut res = client.get(&url).send().unwrap();
println_stderr!("URL: {}", res.url);
println_stderr!("Response: {}", res.status);
println_stderr!("Headers:\n{}", res.headers);
io::copy(&mut res, &mut io::stdout()).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.