-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathlib.rs
More file actions
304 lines (275 loc) · 11 KB
/
lib.rs
File metadata and controls
304 lines (275 loc) · 11 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
#[macro_use]
extern crate slog_scope;
use config::{Config, ConfigError, Environment, File};
use serde::{Deserialize, Deserializer};
use syncserver_common::{
X_LAST_MODIFIED, X_VERIFY_CODE, X_WEAVE_BYTES, X_WEAVE_NEXT_OFFSET, X_WEAVE_RECORDS,
X_WEAVE_TIMESTAMP, X_WEAVE_TOTAL_BYTES, X_WEAVE_TOTAL_RECORDS,
};
use syncstorage_settings::Settings as SyncstorageSettings;
use tokenserver_settings::Settings as TokenserverSettings;
use url::Url;
static PREFIX: &str = "sync";
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct Settings {
pub port: u16,
pub host: String,
/// public facing URL of the server
pub public_url: Option<String>,
/// Keep-alive header value (seconds)
pub actix_keep_alive: Option<u32>,
/// 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,
/// Whether to include the hostname in metrics, which increases cardinality significantly in
/// prod.
pub include_hostname_tag: bool,
/// Environment of Sync application (Stage, Prod, Dev, etc).
pub environment: String,
/// Cors Settings
pub cors_allowed_origin: Option<String>,
pub cors_max_age: Option<usize>,
pub cors_allowed_methods: Option<Vec<String>>,
pub cors_allowed_headers: Option<Vec<String>>,
/// The maximum number of blocking threads that can be used by the worker.
/// Note, we don't want "Option" here because we use this as part of the
/// metric periodic reporter. The default value is 512.
pub worker_max_blocking_threads: usize,
// TOOD: Eventually, the below settings will be enabled or disabled via Cargo features
pub syncstorage: SyncstorageSettings,
pub tokenserver: TokenserverSettings,
}
impl Settings {
/// Load the settings from the config file if supplied, then the environment.
pub fn with_env_and_config_file(filename: Option<&str>) -> Result<Self, ConfigError> {
let mut s = Config::default();
// 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("__"))?;
match s.try_into::<Self>() {
Ok(mut s) => {
s.syncstorage.normalize();
if s.worker_max_blocking_threads == 0 {
// Db backends w/ blocking calls block via
// actix-threadpool: grow its size to accommodate the
// full number of connections
let total_db_pool_size = {
let syncstorage_pool_max_size =
if s.syncstorage.uses_spanner() || !s.syncstorage.enabled {
0
} else {
s.syncstorage.database_pool_max_size
};
let tokenserver_pool_max_size = if s.tokenserver.enabled {
s.tokenserver.database_pool_max_size
} else {
0
};
syncstorage_pool_max_size + tokenserver_pool_max_size
};
let fxa_threads = if s.tokenserver.enabled
&& s.tokenserver.fxa_oauth_primary_jwk.is_none()
&& s.tokenserver.fxa_oauth_secondary_jwk.is_none()
{
s.tokenserver
.additional_blocking_threads_for_fxa_requests
.ok_or_else(|| {
println!(
"If the Tokenserver OAuth JWK is not cached, additional blocking \
threads must be used to handle the requests to FxA."
);
let setting_name =
"tokenserver.additional_blocking_threads_for_fxa_requests";
ConfigError::NotFound(String::from(setting_name))
})?
} else {
0
};
s.worker_max_blocking_threads =
(total_db_pool_size + fxa_threads).max(num_cpus::get() as u32 * 5) as usize;
}
Ok(s)
}
// Configuration errors are not very sysop friendly, Try to make them
// a bit more 3AM useful.
Err(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);
Err(ConfigError::NotFound(v))
}
Err(e) => {
error!("Configuration error: Other: {:?}", &e);
Err(e)
}
}
}
#[cfg(debug_assertions)]
pub fn test_settings() -> Self {
let mut settings =
Self::with_env_and_config_file(None).expect("Could not get Settings in test_settings");
settings.port = 8000;
settings.syncstorage.database_pool_max_size = 1;
settings.syncstorage.database_use_test_transactions = true;
settings.syncstorage.database_spanner_use_mutations = false;
settings.syncstorage.database_pool_connection_max_idle = Some(300);
settings.syncstorage.database_pool_connection_lifespan = Some(300);
settings
}
pub fn banner(&self) -> String {
let quota = if self.syncstorage.enable_quota {
format!(
"Quota: {} bytes ({}enforced)",
self.syncstorage.limits.max_quota_limit,
if !self.syncstorage.enforce_quota {
"un"
} else {
""
}
)
} else {
"No quota".to_owned()
};
let db = Url::parse(&self.syncstorage.database_url)
.map(|url| url.scheme().to_owned())
.unwrap_or_else(|_| "<invalid db>".to_owned());
let parallelism = format!(
"available_parallelism: {:?} num_cpus: {} num_cpus (phys): {}",
std::thread::available_parallelism(),
num_cpus::get(),
num_cpus::get_physical()
);
format!(
"http://{}:{} ({db}) ({parallelism}) {quota}",
self.host, self.port
)
}
}
impl Default for Settings {
fn default() -> Settings {
Settings {
port: 8000,
host: "127.0.0.1".to_string(),
public_url: None,
actix_keep_alive: None,
master_secret: Secrets::default(),
statsd_host: Some("localhost".to_owned()),
statsd_port: 8125,
include_hostname_tag: false,
environment: "dev".to_owned(),
human_logs: false,
cors_allowed_origin: Some("*".to_owned()),
cors_allowed_methods: Some(
["DELETE", "GET", "POST", "PUT"]
.into_iter()
.map(String::from)
.collect(),
),
cors_allowed_headers: Some(
[
"Authorization",
"Content-Type",
"User-Agent",
X_LAST_MODIFIED,
X_WEAVE_TIMESTAMP,
X_WEAVE_NEXT_OFFSET,
X_WEAVE_RECORDS,
X_WEAVE_BYTES,
X_WEAVE_TOTAL_RECORDS,
X_WEAVE_TOTAL_BYTES,
X_VERIFY_CODE,
"TEST_IDLES",
]
.into_iter()
.map(String::from)
.collect(),
),
cors_max_age: Some(1728000),
worker_max_blocking_threads: 512,
syncstorage: SyncstorageSettings::default(),
tokenserver: TokenserverSettings::default(),
}
}
}
/// 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, String> {
let master_secret = master_secret.as_bytes().to_vec();
let signing_secret = syncserver_common::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)))
}
}
#[cfg(test)]
mod test {
use std::env;
use super::*;
#[test]
fn test_environment_variable_prefix() {
// Setting an environment variable with the correct prefix correctly sets the setting
// (note that the default value for the settings.tokenserver.enabled setting is false)
env::set_var("SYNC_TOKENSERVER__ENABLED", "true");
let settings = Settings::with_env_and_config_file(None).unwrap();
assert!(settings.tokenserver.enabled);
// Setting an environment variable with the incorrect prefix does not set the setting
env::remove_var("SYNC_TOKENSERVER__ENABLED");
env::set_var("SYNC__TOKENSERVER__ENABLED", "true");
let settings = Settings::with_env_and_config_file(None).unwrap();
assert!(!settings.tokenserver.enabled);
}
}