Skip to content

Commit b4a368e

Browse files
authored
feat(aggregation mode): add TLS support to Gateway (#2236)
1 parent 58b5eff commit b4a368e

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

aggregation_mode/Cargo.lock

Lines changed: 22 additions & 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: 6 additions & 2 deletions
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 }
@@ -14,11 +18,11 @@ db = { workspace = true }
1418
tracing = { version = "0.1", features = ["log"] }
1519
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
1620
bincode = "1.3.3"
17-
actix-web = "4"
21+
actix-web = { version = "4", features = ["rustls-0_23"] }
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: 59 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,28 @@ impl GatewayServer {
5662
}
5763
}
5864

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

71-
tracing::info!("Starting server at port {}", self.config.port);
72-
HttpServer::new(move || {
99+
#[cfg(feature = "tls")]
100+
let protocol = "https";
101+
#[cfg(not(feature = "tls"))]
102+
let protocol = "http";
103+
104+
tracing::info!(
105+
"Starting server at {}://{}:{}",
106+
protocol,
107+
self.config.ip,
108+
self.config.port
109+
);
110+
111+
let server = HttpServer::new(move || {
73112
App::new()
74113
.app_data(Data::new(state.clone()))
75114
.wrap(prometheus.clone())
@@ -79,12 +118,24 @@ impl GatewayServer {
79118
.route("/proof/sp1", web::post().to(Self::post_proof_sp1))
80119
.route("/proof/risc0", web::post().to(Self::post_proof_risc0))
81120
.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");
121+
});
122+
123+
#[cfg(feature = "tls")]
124+
let server = {
125+
let tls_config =
126+
Self::load_tls_config(&self.config.tls_cert_path, &self.config.tls_key_path)
127+
.expect("Failed to load TLS configuration");
128+
server
129+
.bind_rustls_0_23((self.config.ip.as_str(), port), tls_config)
130+
.expect("To bind socket correctly with TLS")
131+
};
132+
133+
#[cfg(not(feature = "tls"))]
134+
let server = server
135+
.bind((self.config.ip.as_str(), port))
136+
.expect("To bind socket correctly");
137+
138+
server.run().await.expect("Server to never end");
88139
}
89140

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

0 commit comments

Comments
 (0)