Skip to content

Commit 9acf7c3

Browse files
authored
Merge pull request #202 from Dstack-TEE/config-tls-ver
gw: Configurable TLS version and crypto provider
2 parents b4b835b + 32a5927 commit 9acf7c3

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

gateway/gateway.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ endpoint = "10.0.2.2:51820"
4949
[core.proxy]
5050
cert_chain = "/etc/rproxy/certs/cert.pem"
5151
cert_key = "/etc/rproxy/certs/key.pem"
52+
tls_crypto_provider = "aws-lc-rs"
53+
tls_versions = ["1.2"]
5254
base_domain = "app.localhost"
5355
listen_addr = "0.0.0.0"
5456
listen_port = 8443

gateway/src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,28 @@ fn validate(ip: Ipv4Net, reserved_net: &[Ipv4Net], client_ip_range: Ipv4Net) ->
4747
Ok(())
4848
}
4949

50+
#[derive(Debug, Clone, Deserialize)]
51+
pub enum CryptoProvider {
52+
#[serde(rename = "aws-lc-rs")]
53+
AwsLcRs,
54+
#[serde(rename = "ring")]
55+
Ring,
56+
}
57+
58+
#[derive(Debug, Clone, Deserialize)]
59+
pub enum TlsVersion {
60+
#[serde(rename = "1.2")]
61+
Tls12,
62+
#[serde(rename = "1.3")]
63+
Tls13,
64+
}
65+
5066
#[derive(Debug, Clone, Deserialize)]
5167
pub struct ProxyConfig {
5268
pub cert_chain: String,
5369
pub cert_key: String,
70+
pub tls_crypto_provider: CryptoProvider,
71+
pub tls_versions: Vec<TlsVersion>,
5472
pub base_domain: String,
5573
pub listen_addr: Ipv4Addr,
5674
pub listen_port: u16,

gateway/src/proxy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ pub async fn run(config: &ProxyConfig, app_state: Proxy) -> Result<()> {
155155
Arc::new(format!(".{base_domain}"))
156156
};
157157
let tls_terminate_proxy =
158-
TlsTerminateProxy::new(&app_state, &config.cert_chain, &config.cert_key)
159-
.context("failed to create tls terminate proxy")?;
158+
TlsTerminateProxy::new(&app_state).context("failed to create tls terminate proxy")?;
160159
let tls_terminate_proxy = Arc::new(tls_terminate_proxy);
161160

162161
let listener = TcpListener::bind((config.listen_addr, config.listen_port))

gateway/src/proxy/tls_terminate.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::io;
2-
use std::path::Path;
32
use std::pin::Pin;
43
use std::sync::Arc;
54
use std::task::{Context, Poll};
@@ -8,12 +7,14 @@ use anyhow::{Context as _, Result};
87
use fs_err as fs;
98
use rustls::pki_types::pem::PemObject;
109
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
10+
use rustls::version::{TLS12, TLS13};
1111
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
1212
use tokio::net::TcpStream;
1313
use tokio::time::timeout;
1414
use tokio_rustls::{rustls, TlsAcceptor};
1515
use tracing::debug;
1616

17+
use crate::config::{CryptoProvider, ProxyConfig, TlsVersion};
1718
use crate::main_service::Proxy;
1819

1920
use super::io_bridge::bridge;
@@ -91,25 +92,43 @@ pub struct TlsTerminateProxy {
9192
acceptor: TlsAcceptor,
9293
}
9394

94-
impl TlsTerminateProxy {
95-
pub fn new(app_state: &Proxy, cert: impl AsRef<Path>, key: impl AsRef<Path>) -> Result<Self> {
96-
let cert_pem = fs::read(cert.as_ref()).context("failed to read certificate")?;
97-
let key_pem = fs::read(key.as_ref()).context("failed to read private key")?;
98-
let certs = CertificateDer::pem_slice_iter(cert_pem.as_slice())
99-
.collect::<Result<Vec<_>, _>>()
100-
.context("failed to parse certificate")?;
101-
let key = PrivateKeyDer::from_pem_slice(key_pem.as_slice())
102-
.context("failed to parse private key")?;
95+
fn create_acceptor(config: &ProxyConfig) -> Result<TlsAcceptor> {
96+
let cert_pem = fs::read(&config.cert_chain).context("failed to read certificate")?;
97+
let key_pem = fs::read(&config.cert_key).context("failed to read private key")?;
98+
let certs = CertificateDer::pem_slice_iter(cert_pem.as_slice())
99+
.collect::<Result<Vec<_>, _>>()
100+
.context("failed to parse certificate")?;
101+
let key =
102+
PrivateKeyDer::from_pem_slice(key_pem.as_slice()).context("failed to parse private key")?;
103+
104+
let provider = match config.tls_crypto_provider {
105+
CryptoProvider::AwsLcRs => rustls::crypto::aws_lc_rs::default_provider(),
106+
CryptoProvider::Ring => rustls::crypto::ring::default_provider(),
107+
};
108+
let supported_versions = config
109+
.tls_versions
110+
.iter()
111+
.map(|v| match v {
112+
TlsVersion::Tls12 => &TLS12,
113+
TlsVersion::Tls13 => &TLS13,
114+
})
115+
.collect::<Vec<_>>();
116+
let config = rustls::ServerConfig::builder_with_provider(Arc::new(provider))
117+
.with_protocol_versions(&supported_versions)
118+
.context("Failed to build TLS config")?
119+
.with_no_client_auth()
120+
.with_single_cert(certs, key)?;
103121

104-
let config = rustls::ServerConfig::builder()
105-
.with_no_client_auth()
106-
.with_single_cert(certs, key)?;
122+
let acceptor = TlsAcceptor::from(Arc::new(config));
107123

108-
let acceptor = TlsAcceptor::from(Arc::new(config));
124+
Ok(acceptor)
125+
}
109126

127+
impl TlsTerminateProxy {
128+
pub fn new(app_state: &Proxy) -> Result<Self> {
110129
Ok(Self {
111130
app_state: app_state.clone(),
112-
acceptor,
131+
acceptor: create_acceptor(&app_state.config.proxy)?,
113132
})
114133
}
115134

0 commit comments

Comments
 (0)