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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2021 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Fixed configuration.
use crate::ConfigError;
use serde::de::Error;
use serde::{Deserialize, Deserializer};
use staged_builder::{staged_builder, Validate};
use std::env;
use std::path::{Path, PathBuf};
use std::time::Duration;

mod de;

/// The fixed configuration for a Witchcraft server.
#[derive(Clone, PartialEq, Debug)]
#[staged_builder]
#[builder(validate)]
pub struct InstallConfig {
    #[builder(into)]
    product_name: String,
    #[builder(into)]
    product_version: String,
    port: u16,
    #[builder(default)]
    keystore: KeystoreConfig,
    #[builder(default, into)]
    client_auth_truststore: Option<ClientAuthTruststoreConfig>,
    #[builder(into, default = "/".to_string())]
    context_path: String,
    #[builder(default = env::var_os("CONTAINER").is_some())]
    use_console_log: bool,
    #[builder(default)]
    server: ServerConfig,
}

impl Validate for InstallConfig {
    type Error = ConfigError;

    fn validate(&self) -> Result<(), Self::Error> {
        if !(self.context_path == "/"
            || (self.context_path.starts_with('/') && !self.context_path.ends_with('/')))
        {
            return Err(ConfigError(
                "context-path must either be `/` or start but not end with a `/`".to_string(),
            ));
        }

        Ok(())
    }
}

impl<'de> Deserialize<'de> for InstallConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = de::InstallConfig::deserialize(deserializer)?;
        let mut builder = InstallConfig::builder()
            .product_name(raw.product_name)
            .product_version(raw.product_version)
            .port(raw.port);
        if let Some(keystore) = raw.keystore {
            builder = builder.keystore(keystore);
        }
        if let Some(client_auth_truststore) = raw.client_auth_truststore {
            builder = builder.client_auth_truststore(client_auth_truststore);
        }
        if let Some(context_path) = raw.context_path {
            builder = builder.context_path(context_path);
        }
        if let Some(use_console_log) = raw.use_console_log {
            builder = builder.use_console_log(use_console_log);
        }
        if let Some(server) = raw.server {
            builder = builder.server(server);
        }

        builder.build().map_err(Error::custom)
    }
}

impl AsRef<InstallConfig> for InstallConfig {
    #[inline]
    fn as_ref(&self) -> &InstallConfig {
        self
    }
}

impl InstallConfig {
    /// Returns the service's name.
    ///
    /// Required.
    #[inline]
    pub fn product_name(&self) -> &str {
        &self.product_name
    }

    /// Returns the service's version.
    ///
    /// Required.
    #[inline]
    pub fn product_version(&self) -> &str {
        &self.product_version
    }

    /// Returns the port the server will listen on.
    ///
    /// Required.
    #[inline]
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Returns the server's TLS key configuration.
    #[inline]
    pub fn keystore(&self) -> &KeystoreConfig {
        &self.keystore
    }

    /// Returns the server's TLS client authentication truststore configuration.
    ///
    /// If set, the server will request (but not require) the client to authenticate itself during the TLS handshake.
    /// If a certificate is present and validated against the trust roots, all requests made over that connection will
    /// include a `ClientCertificate` extension.
    ///
    /// Defaults to `None`.
    #[inline]
    pub fn client_auth_truststore(&self) -> Option<&ClientAuthTruststoreConfig> {
        self.client_auth_truststore.as_ref()
    }

    /// Returns the server's context path.
    ///
    /// This must either be equal to `/` or start but not end with a `/`.
    ///
    /// Defaults to `/`.
    #[inline]
    pub fn context_path(&self) -> &str {
        &self.context_path
    }

    /// If `true`, the server will log to standard output rather than to files.
    ///
    /// Defaults to `true` if running in a container and false otherwise.
    #[inline]
    pub fn use_console_log(&self) -> bool {
        self.use_console_log
    }

    /// Returns advanced server settings.
    #[inline]
    pub fn server(&self) -> &ServerConfig {
        &self.server
    }
}

/// TLS key configuration.
#[derive(Clone, PartialEq, Debug)]
#[staged_builder]
pub struct KeystoreConfig {
    #[builder(into, default = PathBuf::from("var/security/key.pem"))]
    key_path: PathBuf,
    #[builder(into, default = PathBuf::from("var/security/cert.cer"))]
    cert_path: PathBuf,
}

impl Default for KeystoreConfig {
    #[inline]
    fn default() -> Self {
        KeystoreConfig::builder().build()
    }
}

impl<'de> Deserialize<'de> for KeystoreConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = de::KeystoreConfig::deserialize(deserializer)?;
        let mut builder = KeystoreConfig::builder();
        if let Some(key_path) = raw.key_path {
            builder = builder.key_path(key_path);
        }
        if let Some(cert_path) = raw.cert_path {
            builder = builder.cert_path(cert_path);
        }
        Ok(builder.build())
    }
}

impl KeystoreConfig {
    /// Returns the path to the server's PEM-encoded private key.
    ///
    /// Defaults to `var/security/key.pem`.
    #[inline]
    pub fn key_path(&self) -> &Path {
        &self.key_path
    }

    /// Returns the path to the server's PEM-encoded certificate chain.
    ///
    /// The file should contain a sequence of certificates starting with the leaf certificate corresponding to the key
    /// in [`Self::key_path`] followed by the rest of the certificate chain up to a trusted root.
    ///
    /// Defaults to `var/security/cert.cer`.
    #[inline]
    pub fn cert_path(&self) -> &Path {
        &self.cert_path
    }
}

/// TLS client authentication configuration.
#[derive(Clone, PartialEq, Debug)]
#[staged_builder]
pub struct ClientAuthTruststoreConfig {
    #[builder(into, default = PathBuf::from("var/security/ca.cer"))]
    path: PathBuf,
}

impl Default for ClientAuthTruststoreConfig {
    #[inline]
    fn default() -> Self {
        ClientAuthTruststoreConfig::builder().build()
    }
}

impl<'de> Deserialize<'de> for ClientAuthTruststoreConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = de::ClientAuthTruststoreConfig::deserialize(deserializer)?;
        let mut builder = ClientAuthTruststoreConfig::builder();
        if let Some(path) = raw.path {
            builder = builder.path(path);
        }
        Ok(builder.build())
    }
}

impl ClientAuthTruststoreConfig {
    /// Returns the path to a file containg PEM-encoded certificates (i.e. blocks of `-----BEGIN CERTIFICATE-----`)
    /// that will act as the trust roots for validating the client's identity.
    ///
    /// Defaults to `var/security/ca.cer`.
    #[inline]
    pub fn path(&self) -> &Path {
        &self.path
    }
}

/// Advanced server configuration.
#[derive(Clone, PartialEq, Debug)]
#[staged_builder]
pub struct ServerConfig {
    #[builder(default = num_cpus::get())]
    processors: usize,
    #[builder(default, custom(type = usize, convert = Some))]
    min_threads: Option<usize>,
    #[builder(default, custom(type = usize, convert = Some))]
    max_threads: Option<usize>,
    #[builder(default, custom(type = usize, convert = Some))]
    max_connections: Option<usize>,
    #[builder(default, custom(type = usize, convert = Some))]
    io_threads: Option<usize>,
    #[builder(default = Duration::from_secs(5 * 60))]
    idle_thread_timeout: Duration,
    #[builder(default = Duration::from_secs(15))]
    shutdown_timeout: Duration,
    #[builder(default = true)]
    gzip: bool,
    #[builder(default = false)]
    http2: bool,
    #[builder(default, into)]
    idle_connection_timeout: Option<Duration>,
}

impl Default for ServerConfig {
    #[inline]
    fn default() -> Self {
        ServerConfig::builder().build()
    }
}

impl<'de> Deserialize<'de> for ServerConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = de::ServerConfig::deserialize(deserializer)?;
        let mut builder = ServerConfig::builder();
        if let Some(processors) = raw.processors {
            builder = builder.processors(processors);
        }
        if let Some(min_threads) = raw.min_threads {
            builder = builder.min_threads(min_threads);
        }
        if let Some(max_threads) = raw.max_threads {
            builder = builder.max_threads(max_threads);
        }
        if let Some(max_connections) = raw.max_connections {
            builder = builder.max_connections(max_connections);
        }
        if let Some(io_threads) = raw.io_threads {
            builder = builder.io_threads(io_threads);
        }
        if let Some(idle_thread_timeout) = raw.idle_thread_timeout {
            builder = builder.idle_thread_timeout(idle_thread_timeout);
        }
        if let Some(shutdown_timeout) = raw.shutdown_timeout {
            builder = builder.shutdown_timeout(shutdown_timeout);
        }
        if let Some(gzip) = raw.gzip {
            builder = builder.gzip(gzip);
        }
        if let Some(http2) = raw.http2 {
            builder = builder.http2(http2);
        }
        if let Some(idle_connection_timeout) = raw.idle_connection_timeout {
            builder = builder.idle_connection_timeout(idle_connection_timeout);
        }

        Ok(builder.build())
    }
}

impl ServerConfig {
    /// Returns the number of processors the server is allocated.
    ///
    /// This is only used to derive default values for other settings in this type.
    ///
    /// Defaults to the number of logical CPUs.
    #[inline]
    pub fn processors(&self) -> usize {
        self.processors
    }

    /// Returns the minimum number of threads in the pool used to process blocking endpoints.
    ///
    /// Defaults to 8 times the number of processors.
    #[inline]
    pub fn min_threads(&self) -> usize {
        self.min_threads.unwrap_or(self.processors * 8)
    }

    /// Returns the maximum number of threads in the pool used to process blocking endpoints.
    ///
    /// Defaults to the maximum of 32 times the number of processors and 256.
    #[inline]
    pub fn max_threads(&self) -> usize {
        self.max_threads
            .unwrap_or_else(|| usize::max(self.processors * 32, 256))
    }

    /// Returns the maximum number of live TCP connections the server will allow at any time.
    ///
    /// Defaults to 10 times the value of [`Self::max_threads`].
    #[inline]
    pub fn max_connections(&self) -> usize {
        self.max_connections
            .unwrap_or_else(|| self.max_threads() * 10)
    }

    /// Returns the number of threads used for nonblocking operations in the server's Tokio runtime.
    ///
    /// Defaults to half the number of processors.
    #[inline]
    pub fn io_threads(&self) -> usize {
        self.io_threads
            .unwrap_or_else(|| usize::max(1, self.processors / 2))
    }

    /// Returns the amount of time a thread in the blocking request pool will sit idle before shutting down.
    ///
    /// Defaults to 5 minutes.
    #[inline]
    pub fn idle_thread_timeout(&self) -> Duration {
        self.idle_thread_timeout
    }

    /// Returns the amount of time the server will wait for pending requests to complete when shutting down.
    ///
    /// Defaults to 15 seconds.
    #[inline]
    pub fn shutdown_timeout(&self) -> Duration {
        self.shutdown_timeout
    }

    /// Determines if responses larger than 1 MiB will be compressed with gzip.
    ///
    /// Defaults to `true`.
    #[inline]
    pub fn gzip(&self) -> bool {
        self.gzip
    }

    /// Determines if the server will support the HTTP2 protocol.
    ///
    /// Defaults to `false`.
    #[inline]
    pub fn http2(&self) -> bool {
        self.http2
    }

    /// Returns the amount of time the server allows TCP connections to remain idle before shutting them down.
    ///
    /// If `None`, defaults to 1 minute. If `Some`, the time will be included in HTTP responses in a `Keep-Alive`
    /// header.
    #[inline]
    pub fn idle_connection_timeout(&self) -> Option<Duration> {
        self.idle_connection_timeout
    }
}