Skip to content

Commit 4df37e9

Browse files
committed
feat(gateway): add TLS
1 parent ba483d3 commit 4df37e9

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

aggregation_mode/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation_mode/gateway/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "gateway"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[features]
7+
default = []
8+
tls = ["dep:rustls"]
9+
610
[dependencies]
711
serde = { workspace = true }
812
serde_json = { workspace = true }
@@ -17,8 +21,8 @@ bincode = "1.3.3"
1721
actix-web = "4"
1822
actix-multipart = "0.7.2"
1923
actix-web-prometheus = "0.1.2"
24+
rustls = { version = "0.23", optional = true, default-features = false, features = ["std", "aws-lc-rs"] }
2025
alloy = { workspace = true }
2126
tokio = { version = "1", features = ["time", "macros", "rt-multi-thread"]}
22-
# TODO: enable tls
2327
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal" ] }
2428
hex = "0.4"

aggregation_mode/gateway/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ pub struct Config {
1010
pub network: String,
1111
pub max_daily_proofs_per_user: i64,
1212
pub gateway_metrics_port: u16,
13+
#[cfg(feature = "tls")]
14+
pub tls_cert_path: String,
15+
#[cfg(feature = "tls")]
16+
pub tls_key_path: String,
1317
}
1418

1519
impl Config {

aggregation_mode/gateway/src/http.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ use std::{
44
time::{Instant, SystemTime, UNIX_EPOCH},
55
};
66

7+
#[cfg(feature = "tls")]
8+
use rustls::{
9+
pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer},
10+
ServerConfig,
11+
};
12+
713
use actix_multipart::form::MultipartForm;
814
use actix_web::{
915
web::{self, Data},
@@ -56,6 +62,25 @@ impl GatewayServer {
5662
}
5763
}
5864

65+
#[cfg(feature = "tls")]
66+
fn load_tls_config(cert_path: &str, key_path: &str) -> Result<ServerConfig, Box<dyn std::error::Error>> {
67+
// Install the default crypto provider
68+
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
69+
70+
// Load certificate chain
71+
let certs: Vec<CertificateDer> = CertificateDer::pem_file_iter(cert_path)?
72+
.collect::<Result<Vec<_>, _>>()?;
73+
74+
// Load private key
75+
let private_key = PrivateKeyDer::from_pem_file(key_path)?;
76+
77+
let config = ServerConfig::builder()
78+
.with_no_client_auth()
79+
.with_single_cert(certs, private_key)?;
80+
81+
Ok(config)
82+
}
83+
5984
pub async fn start(&self) {
6085
// Note: GatewayServer is thread safe so we can just clone it (no need to add mutexes)
6186
let port = self.config.port;
@@ -68,8 +93,14 @@ impl GatewayServer {
6893
.build()
6994
.unwrap();
7095

71-
tracing::info!("Starting server at port {}", self.config.port);
72-
HttpServer::new(move || {
96+
#[cfg(feature = "tls")]
97+
let protocol = "https";
98+
#[cfg(not(feature = "tls"))]
99+
let protocol = "http";
100+
101+
tracing::info!("Starting server at {}://{}:{}", protocol, self.config.ip, self.config.port);
102+
103+
let server = HttpServer::new(move || {
73104
App::new()
74105
.app_data(Data::new(state.clone()))
75106
.wrap(prometheus.clone())
@@ -79,12 +110,24 @@ impl GatewayServer {
79110
.route("/proof/sp1", web::post().to(Self::post_proof_sp1))
80111
.route("/proof/risc0", web::post().to(Self::post_proof_risc0))
81112
.route("/quotas/{address}", web::get().to(Self::get_quotas))
82-
})
83-
.bind((self.config.ip.as_str(), port))
84-
.expect("To bind socket correctly")
85-
.run()
86-
.await
87-
.expect("Server to never end");
113+
});
114+
115+
#[cfg(feature = "tls")]
116+
let server = {
117+
let tls_config = Self::load_tls_config(&self.config.tls_cert_path, &self.config.tls_key_path)
118+
.expect("Failed to load TLS configuration");
119+
server.bind_rustls_0_23((self.config.ip.as_str(), port), tls_config)
120+
.expect("To bind socket correctly with TLS")
121+
};
122+
123+
#[cfg(not(feature = "tls"))]
124+
let server = server.bind((self.config.ip.as_str(), port))
125+
.expect("To bind socket correctly");
126+
127+
server
128+
.run()
129+
.await
130+
.expect("Server to never end");
88131
}
89132

90133
// Returns an OK response (code 200), no matters what receives in the request

0 commit comments

Comments
 (0)