-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.rs
More file actions
325 lines (281 loc) · 9.91 KB
/
config.rs
File metadata and controls
325 lines (281 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::{
fmt::Display,
net::SocketAddr,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
use rustls::{
pki_types::{pem::PemObject, ServerName},
version::TLS13,
};
use rustls_platform_verifier::Verifier;
use serde::Deserialize;
use tokio_rustls::{TlsAcceptor, TlsConnector};
use tracing::{info, warn};
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct Config {
pub server: NtsPoolKeConfig,
#[serde(default)]
pub observability: ObservabilityConfig,
}
#[derive(Debug)]
pub enum ConfigError {
Io(std::io::Error),
Toml(toml::de::Error),
}
impl From<std::io::Error> for ConfigError {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
impl From<toml::de::Error> for ConfigError {
fn from(value: toml::de::Error) -> Self {
Self::Toml(value)
}
}
impl Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(e) => write!(f, "io error while reading config: {e}"),
Self::Toml(e) => write!(f, "config toml parsing error: {e}"),
}
}
}
impl std::error::Error for ConfigError {}
impl Config {
pub fn check(&self) -> bool {
true
}
async fn from_file(file: impl AsRef<Path>) -> Result<Config, ConfigError> {
let meta = std::fs::metadata(&file)?;
let perm = meta.permissions();
const S_IWOTH: u32 = 2;
if perm.mode() & S_IWOTH != 0 {
warn!("Unrestricted config file permissions: Others can write.");
}
let contents = tokio::fs::read_to_string(file).await?;
Ok(toml::de::from_str(&contents)?)
}
pub async fn from_args(file: impl AsRef<Path>) -> Result<Config, ConfigError> {
let path = file.as_ref();
info!(?path, "using config file");
let config = Config::from_file(path).await?;
Ok(config)
}
}
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ObservabilityConfig {
#[serde(default)]
pub log_level: Option<super::daemon_tracing::LogLevel>,
}
#[derive(Debug, PartialEq, Eq, Clone, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct BareNtsPoolKeConfig {
/// Additional CAs used to validate the certificates of upstream servers
#[serde(default)]
upstream_cas: Option<PathBuf>,
/// Certificate chain for the key used by the server to identify itself during tls sessions
certificate_chain: PathBuf,
/// Private key used by the server to identify itself during tls sessions
private_key: PathBuf,
#[serde(default = "default_nts_ke_timeout")]
/// Timeout
key_exchange_timeout: u64,
/// Address for the server to listen on.
listen: SocketAddr,
/// Which upstream servers to use.
key_exchange_servers: Box<[KeyExchangeServer]>,
}
fn default_nts_ke_timeout() -> u64 {
1000
}
#[derive(Clone)]
pub struct NtsPoolKeConfig {
pub server_tls: TlsAcceptor,
pub upstream_tls: TlsConnector,
pub listen: SocketAddr,
pub key_exchange_servers: Box<[KeyExchangeServer]>,
pub key_exchange_timeout: Duration,
}
fn load_certificates(
path: impl AsRef<std::path::Path>,
) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>, rustls::pki_types::pem::Error> {
rustls::pki_types::CertificateDer::pem_file_iter(path)?.collect()
}
impl<'de> Deserialize<'de> for NtsPoolKeConfig {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bare = BareNtsPoolKeConfig::deserialize(deserializer)?;
let upstream_cas = bare
.upstream_cas
.map(|path| {
load_certificates(&path).map_err(|e| {
serde::de::Error::custom(format!(
"error reading additional upstream ca certificates from `{:?}`: {:?}",
path, e
))
})
})
.transpose()?;
let certificate_chain = load_certificates(&bare.certificate_chain).map_err(|e| {
serde::de::Error::custom(format!(
"error reading server's certificate chain from `{:?}`: {:?}",
bare.certificate_chain, e
))
})?;
let private_key = rustls::pki_types::PrivateKeyDer::from_pem_file(&bare.private_key)
.map_err(|e| {
serde::de::Error::custom(format!(
"error reading server's private key from `{:?}`: {:?}",
bare.private_key, e
))
})?;
let mut server_config = rustls::ServerConfig::builder_with_protocol_versions(&[&TLS13])
.with_no_client_auth()
.with_single_cert(certificate_chain.clone(), private_key.clone_key())
.map_err(serde::de::Error::custom)?;
server_config.alpn_protocols.clear();
server_config.alpn_protocols.push(b"ntske/1".to_vec());
let server_tls = TlsAcceptor::from(Arc::new(server_config));
let upstream_config_builder =
rustls::ClientConfig::builder_with_protocol_versions(&[&TLS13]);
let provider = upstream_config_builder.crypto_provider().clone();
let verifier = match upstream_cas {
Some(upstream_cas) => Verifier::new_with_extra_roots(upstream_cas.iter().cloned())
.map_err(serde::de::Error::custom)?
.with_provider(provider),
None => Verifier::new(),
};
let upstream_config = upstream_config_builder
.dangerous()
.with_custom_certificate_verifier(Arc::new(verifier))
.with_client_auth_cert(certificate_chain, private_key)
.map_err(serde::de::Error::custom)?;
let upstream_tls = TlsConnector::from(Arc::new(upstream_config));
Ok(NtsPoolKeConfig {
server_tls,
upstream_tls,
listen: bare.listen,
key_exchange_servers: bare.key_exchange_servers,
key_exchange_timeout: std::time::Duration::from_millis(bare.key_exchange_timeout),
})
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct KeyExchangeServer {
pub domain: String,
pub server_name: ServerName<'static>,
pub port: u16,
}
impl<'de> Deserialize<'de> for KeyExchangeServer {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct BareKeyExchangeServer {
domain: String,
port: u16,
}
let bare = BareKeyExchangeServer::deserialize(deserializer)?;
let Ok(server_name) = ServerName::try_from(bare.domain.clone()) else {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&bare.domain),
&"Domain name",
));
};
Ok(KeyExchangeServer {
domain: bare.domain.to_string(),
server_name,
port: bare.port,
})
}
}
#[cfg(test)]
mod tests {
use std::ops::Deref;
use super::*;
#[test]
fn test_deserialize_bare_config() {
let test: BareNtsPoolKeConfig = toml::from_str(
r#"
listen = "0.0.0.0:4460"
upstream-cas = "/foo/bar/ca.pem"
certificate-chain = "/foo/bar/baz.pem"
private-key = "spam.der"
key-exchange-servers = [
{ domain = "foo.bar", port = 1234 },
{ domain = "bar.foo", port = 4321 },
]
"#,
)
.unwrap();
let ca = PathBuf::from("/foo/bar/ca.pem");
assert_eq!(test.upstream_cas, Some(ca));
let chain = PathBuf::from("/foo/bar/baz.pem");
assert_eq!(test.certificate_chain, chain);
let private_key = PathBuf::from("spam.der");
assert_eq!(test.private_key, private_key);
assert_eq!(test.key_exchange_timeout, 1000,);
assert_eq!(test.listen, "0.0.0.0:4460".parse().unwrap(),);
assert_eq!(
test.key_exchange_servers.deref(),
[
KeyExchangeServer {
domain: String::from("foo.bar"),
server_name: ServerName::try_from("foo.bar").unwrap(),
port: 1234
},
KeyExchangeServer {
domain: String::from("bar.foo"),
server_name: ServerName::try_from("bar.foo").unwrap(),
port: 4321
},
]
.as_slice()
);
}
#[test]
fn test_deserialize_config() {
let test: Config = toml::from_str(
r#"
[server]
listen = "0.0.0.0:4460"
key-exchange-timeout = 500
upstream-cas = "testdata/testca.pem"
certificate-chain = "testdata/end.fullchain.pem"
private-key = "testdata/end.key"
key-exchange-servers = [
{ domain = "foo.bar", port = 1234 },
{ domain = "bar.foo", port = 4321 },
]
"#,
)
.unwrap();
assert_eq!(test.server.key_exchange_timeout, Duration::from_millis(500));
assert_eq!(test.server.listen, "0.0.0.0:4460".parse().unwrap(),);
assert_eq!(
test.server.key_exchange_servers.deref(),
[
KeyExchangeServer {
domain: String::from("foo.bar"),
server_name: ServerName::try_from("foo.bar").unwrap(),
port: 1234
},
KeyExchangeServer {
domain: String::from("bar.foo"),
server_name: ServerName::try_from("bar.foo").unwrap(),
port: 4321
},
]
.as_slice()
);
}
}