/
settings.rs
274 lines (244 loc) · 9.66 KB
/
settings.rs
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
//! Application settings objects and initialization
use std::{cmp::min, env};
use config::{Config, ConfigError, Environment, File};
use serde::{de::Deserializer, Deserialize, Serialize};
use url::Url;
use crate::db::spanner::models::MAX_SPANNER_LOAD_SIZE;
use crate::error::ApiError;
use crate::web::auth::hkdf_expand_32;
static DEFAULT_PORT: u16 = 8000;
static KILOBYTE: u32 = 1024;
static MEGABYTE: u32 = KILOBYTE * KILOBYTE;
static DEFAULT_MAX_POST_BYTES: u32 = 2 * MEGABYTE;
static DEFAULT_MAX_POST_RECORDS: u32 = 100;
static DEFAULT_MAX_RECORD_PAYLOAD_BYTES: u32 = 2 * MEGABYTE;
static DEFAULT_MAX_REQUEST_BYTES: u32 = DEFAULT_MAX_POST_BYTES + 4 * KILOBYTE;
static DEFAULT_MAX_TOTAL_BYTES: u32 = 100 * DEFAULT_MAX_POST_BYTES;
static DEFAULT_MAX_TOTAL_RECORDS: u32 = 100 * DEFAULT_MAX_POST_RECORDS;
static PREFIX: &str = "sync";
#[derive(Clone, Debug, Deserialize)]
pub struct Settings {
pub debug: bool,
pub port: u16,
pub host: String,
pub database_url: String,
pub database_pool_max_size: Option<u32>,
pub database_pool_min_idle: Option<u32>,
#[cfg(test)]
pub database_use_test_transactions: bool,
pub actix_keep_alive: Option<u32>,
/// Server-enforced limits for request payloads.
pub limits: ServerLimits,
/// The master secret, from which are derived
/// the signing secret and token secret
/// that are used during Hawk authentication.
pub master_secret: Secrets,
pub human_logs: bool,
pub statsd_host: Option<String>,
pub statsd_port: u16,
pub statsd_label: String,
}
impl Default for Settings {
fn default() -> Settings {
Settings {
debug: false,
port: DEFAULT_PORT,
host: "127.0.0.1".to_string(),
database_url: "mysql://root@127.0.0.1/syncstorage".to_string(),
database_pool_max_size: None,
database_pool_min_idle: None,
#[cfg(test)]
database_use_test_transactions: false,
actix_keep_alive: None,
limits: ServerLimits::default(),
master_secret: Secrets::default(),
statsd_host: None,
statsd_port: 8125,
statsd_label: "syncstorage".to_string(),
human_logs: false,
}
}
}
impl Settings {
/// Load the settings from the config file if supplied, then the environment.
pub fn with_env_and_config_file(filename: &Option<String>) -> Result<Self, ConfigError> {
let mut s = Config::default();
// Set our defaults, this can be fixed up drastically later after:
// https://github.com/mehcode/config-rs/issues/60
s.set_default("debug", false)?;
s.set_default("port", i64::from(DEFAULT_PORT))?;
s.set_default("host", "127.0.0.1")?;
s.set_default("human_logs", false)?;
#[cfg(test)]
s.set_default("database_use_test_transactions", false)?;
s.set_default("master_secret", "")?;
s.set_default("limits.max_post_bytes", i64::from(DEFAULT_MAX_POST_BYTES))?;
s.set_default(
"limits.max_post_records",
i64::from(DEFAULT_MAX_POST_RECORDS),
)?;
s.set_default(
"limits.max_record_payload_bytes",
i64::from(DEFAULT_MAX_RECORD_PAYLOAD_BYTES),
)?;
s.set_default(
"limits.max_request_bytes",
i64::from(DEFAULT_MAX_REQUEST_BYTES),
)?;
s.set_default("limits.max_total_bytes", i64::from(DEFAULT_MAX_TOTAL_BYTES))?;
s.set_default(
"limits.max_total_records",
i64::from(DEFAULT_MAX_TOTAL_RECORDS),
)?;
s.set_default("statsd_host", "localhost")?;
s.set_default("statsd_port", 8125)?;
s.set_default("statsd_label", "syncstorage")?;
// Merge the config file if supplied
if let Some(config_filename) = filename {
s.merge(File::with_name(config_filename))?;
}
// Merge the environment overrides
// While the prefix is currently case insensitive, it's traditional that
// environment vars be UPPERCASE, this ensures that will continue should
// Environment ever change their policy about case insensitivity.
// This will accept environment variables specified as
// `SYNC_FOO__BAR_VALUE="gorp"` as `foo.bar_value = "gorp"`
s.merge(Environment::with_prefix(&PREFIX.to_uppercase()).separator("__"))?;
Ok(match s.try_into::<Self>() {
Ok(s) => {
// Adjust the max values if required.
if s.uses_spanner() {
let mut ms = s;
ms.limits.max_total_bytes =
min(ms.limits.max_total_bytes, MAX_SPANNER_LOAD_SIZE as u32);
return Ok(ms);
}
if !s.uses_spanner() {
if let Some(database_pool_max_size) = s.database_pool_max_size {
// Db backends w/ blocking calls block via
// actix-threadpool: grow its size to accommodate the
// full number of connections
let default = num_cpus::get() * 5;
if (database_pool_max_size as usize) > default {
env::set_var("ACTIX_THREADPOOL", database_pool_max_size.to_string());
}
}
}
s
}
Err(e) => match e {
// Configuration errors are not very sysop friendly, Try to make them
// a bit more 3AM useful.
ConfigError::Message(v) => {
println!("Bad configuration: {:?}", &v);
println!("Please set in config file or use environment variable.");
println!(
"For example to set `database_url` use env var `{}_DATABASE_URL`\n",
PREFIX.to_uppercase()
);
error!("Configuration error: Value undefined {:?}", &v);
return Err(ConfigError::NotFound(v));
}
_ => {
error!("Configuration error: Other: {:?}", &e);
return Err(e);
}
},
})
}
pub fn uses_spanner(&self) -> bool {
self.database_url.as_str().starts_with("spanner")
}
/// A simple banner for display of certain settings at startup
pub fn banner(&self) -> String {
let db = Url::parse(&self.database_url)
.map(|url| url.scheme().to_owned())
.unwrap_or_else(|_| "<invalid db>".to_owned());
format!("http://{}:{} ({})", self.host, self.port, db)
}
}
/// Server-enforced limits for request payloads.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ServerLimits {
/// Maximum combined size of BSO payloads for a single request, in bytes.
pub max_post_bytes: u32,
/// Maximum BSO count for a single request.
pub max_post_records: u32,
/// Maximum size of an individual BSO payload, in bytes.
pub max_record_payload_bytes: u32,
/// Maximum `Content-Length` for all incoming requests, in bytes.
///
/// Enforced externally to this repo, at the web server level.
/// It's important that nginx (or whatever)
/// really is configured to enforce exactly this limit,
/// otherwise client requests may fail with a 413
/// before even reaching the API.
pub max_request_bytes: u32,
/// Maximum combined size of BSO payloads across a batch upload, in bytes.
pub max_total_bytes: u32,
/// Maximum BSO count across a batch upload.
pub max_total_records: u32,
// ### debug_client - for testing client
pub debug_client: Option<String>,
}
impl Default for ServerLimits {
/// Create a default `ServerLimits` instance.
fn default() -> Self {
Self {
max_post_bytes: DEFAULT_MAX_POST_BYTES,
max_post_records: DEFAULT_MAX_POST_RECORDS,
max_record_payload_bytes: DEFAULT_MAX_RECORD_PAYLOAD_BYTES,
max_request_bytes: DEFAULT_MAX_REQUEST_BYTES,
max_total_bytes: DEFAULT_MAX_TOTAL_BYTES,
max_total_records: DEFAULT_MAX_TOTAL_RECORDS,
debug_client: None,
}
}
}
/// Secrets used during Hawk authentication.
#[derive(Clone, Debug)]
pub struct Secrets {
/// The master secret in byte array form.
///
/// The signing secret and token secret are derived from this.
pub master_secret: Vec<u8>,
/// The signing secret used during Hawk authentication.
pub signing_secret: [u8; 32],
}
impl Secrets {
/// Decode the master secret to a byte array
/// and derive the signing secret from it.
pub fn new(master_secret: &str) -> Result<Self, ApiError> {
let master_secret = master_secret.as_bytes().to_vec();
let signing_secret = hkdf_expand_32(
b"services.mozilla.com/tokenlib/v1/signing",
None,
&master_secret,
)?;
Ok(Self {
master_secret,
signing_secret,
})
}
}
impl Default for Secrets {
/// Create a (useless) default `Secrets` instance.
fn default() -> Self {
Self {
master_secret: vec![],
signing_secret: [0u8; 32],
}
}
}
impl<'d> Deserialize<'d> for Secrets {
/// Deserialize the master secret and signing secret byte arrays
/// from a single master secret string.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'d>,
{
let master_secret: String = Deserialize::deserialize(deserializer)?;
Secrets::new(&master_secret)
.map_err(|e| serde::de::Error::custom(format!("error: {:?}", e)))
}
}