Skip to content

Commit

Permalink
Make cli connect to tower server via tls
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Jun 3, 2022
1 parent 45a1b91 commit b9ae451
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions teos/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ use serde_json::to_string_pretty as pretty_json;
use std::fs;
use std::str::FromStr;
use structopt::StructOpt;
use tonic::Request;
use tonic::{
transport::{Channel, ClientTlsConfig},
Request,
};

use teos::cli_config::{Command, Config, Opt};
use teos::config;
use teos::protos as msgs;
use teos::protos::private_tower_services_client::PrivateTowerServicesClient;
use teos::tls::Identity;
use teos_common::UserId;

#[tokio::main]
Expand All @@ -27,17 +31,37 @@ async fn main() {
let mut conf = config::from_file::<Config>(path.join("teos.toml"));
conf.patch_with_options(opt);

// Create gRPC client and send request
let mut client =
PrivateTowerServicesClient::connect(format!("http://{}:{}", conf.rpc_bind, conf.rpc_port))
.await
.unwrap_or_else(|e| {
eprintln!("Cannot connect to the tower. Connection refused");
if conf.debug {
eprintln!("{:?}", e);
}
std::process::exit(1);
});
let client_key_path = path.join("client-key.pem");
let client_cert_path = path.join("client.pem");
let ca_cert_path = path.join("ca.pem");
let key = std::fs::read(&client_key_path).expect("unable to read client key from disk");
let certificate =
std::fs::read(client_cert_path).expect("unable to read client cert from disk");
let client_id = Identity { certificate, key };
let ca_cert = std::fs::read(ca_cert_path).expect("unable to read ca cert from disk");
let ca_cert = tonic::transport::Certificate::from_pem(ca_cert);

let tls = ClientTlsConfig::new()
.domain_name("localhost")
.ca_certificate(ca_cert)
.identity(client_id.to_tonic_identity());

let endpoint = format!("http://{}:{}", conf.rpc_bind, conf.rpc_port);
let channel = Channel::from_shared(endpoint)
.expect("Cannot create channel from endpoint")
.tls_config(tls)
.unwrap_or_else(|e| {
eprintln!("Could not configure tls: {:?}", e);
std::process::exit(1);
})
.connect()
.await
.unwrap_or_else(|e| {
eprintln!("Could not connect to tower: {:?}", e);
std::process::exit(1);
});

let mut client = PrivateTowerServicesClient::new(channel);

match command {
Command::GetAllAppointments => {
Expand Down

0 comments on commit b9ae451

Please sign in to comment.