-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconfig.rs
More file actions
189 lines (169 loc) · 5.55 KB
/
config.rs
File metadata and controls
189 lines (169 loc) · 5.55 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
use serde::Deserialize;
use std::net::SocketAddr;
const BIND_ADDR_VAR: &str = "VSS_BIND_ADDRESS";
const MAX_REQUEST_BODY_SIZE: &str = "VSS_MAX_REQUEST_BODY_SIZE";
const JWT_RSA_PEM_VAR: &str = "VSS_JWT_RSA_PEM";
const PSQL_USER_VAR: &str = "VSS_PSQL_USERNAME";
const PSQL_PASS_VAR: &str = "VSS_PSQL_PASSWORD";
const PSQL_ADDR_VAR: &str = "VSS_PSQL_ADDRESS";
const PSQL_DB_VAR: &str = "VSS_PSQL_DEFAULT_DB";
const PSQL_VSS_DB_VAR: &str = "VSS_PSQL_VSS_DB";
const PSQL_TLS_VAR: &str = "VSS_PSQL_TLS";
const PSQL_CERT_PEM_VAR: &str = "VSS_PSQL_CRT_PEM";
// The structure of the toml config file. Any settings specified therein can be overriden by the corresponding
// environment variable.
#[derive(Deserialize, Default)]
struct TomlConfig {
server_config: Option<ServerConfig>,
jwt_auth_config: Option<JwtAuthConfig>,
postgresql_config: Option<PostgreSQLConfig>,
}
#[derive(Deserialize)]
struct ServerConfig {
bind_address: Option<SocketAddr>,
maximum_request_body_size: Option<usize>,
}
#[derive(Deserialize)]
struct JwtAuthConfig {
rsa_pem: Option<String>,
}
#[derive(Deserialize)]
struct PostgreSQLConfig {
username: Option<String>,
password: Option<String>,
address: Option<SocketAddr>,
default_database: Option<String>,
vss_database: Option<String>,
tls: Option<TlsConfig>,
}
#[derive(Deserialize)]
struct TlsConfig {
crt_pem: Option<String>,
}
// Encapsulates the result of reading both the environment variables and the config file.
pub(crate) struct Configuration {
pub(crate) bind_address: SocketAddr,
pub(crate) maximum_request_body_size: Option<usize>,
pub(crate) rsa_pem: Option<String>,
pub(crate) postgresql_prefix: String,
pub(crate) default_db: String,
pub(crate) vss_db: String,
pub(crate) tls_config: Option<Option<String>>,
}
#[inline]
fn read_env(env_var: &str) -> Result<Option<String>, String> {
match std::env::var(env_var) {
Ok(env) => Ok(Some(env)),
Err(std::env::VarError::NotPresent) => Ok(None),
Err(e) => Err(format!("Failed to load the {} environment variable: {}", env_var, e)),
}
}
#[inline]
fn read_config<'a, T: std::fmt::Display>(
env: Option<T>, config: Option<T>, item: &str, var_name: &str,
) -> Result<T, String> {
env.or(config).ok_or(format!(
"{} must be provided in the configuration file or the environment variable {} must be set.",
item, var_name
))
}
pub(crate) fn load_configuration(config_file_path: Option<&str>) -> Result<Configuration, String> {
let TomlConfig { server_config, jwt_auth_config, postgresql_config } = match config_file_path {
Some(path) => {
let config_file = std::fs::read_to_string(path)
.map_err(|e| format!("Failed to read configuration file: {}", e))?;
toml::from_str(&config_file)
.map_err(|e| format!("Failed to parse configuration file: {}", e))?
},
None => TomlConfig::default(), // All fields are set to `None`
};
let (bind_address_config, max_request_body_size_config) = match server_config {
Some(c) => (c.bind_address, c.maximum_request_body_size),
None => (None, None),
};
let bind_address_env = read_env(BIND_ADDR_VAR)?
.map(|addr| {
addr.parse().map_err(|e| {
format!("Unable to parse the bind address environment variable: {}", e)
})
})
.transpose()?;
let bind_address = read_config(
bind_address_env,
bind_address_config,
"VSS server bind address",
BIND_ADDR_VAR,
)?;
let maximum_request_body_size_env = read_env(MAX_REQUEST_BODY_SIZE)?
.map(|mrbs| {
mrbs.parse::<usize>().map_err(|e| {
format!("Unable to parse the maximum request body size environment variable: {}", e)
})
})
.transpose()?;
let maximum_request_body_size = read_config(
maximum_request_body_size_env,
max_request_body_size_config,
"VSS server maximum request body size",
MAX_REQUEST_BODY_SIZE,
)?;
let rsa_pem_env = read_env(JWT_RSA_PEM_VAR)?;
let rsa_pem = rsa_pem_env.or(jwt_auth_config.and_then(|config| config.rsa_pem));
let username_env = read_env(PSQL_USER_VAR)?;
let password_env = read_env(PSQL_PASS_VAR)?;
let address_env: Option<SocketAddr> = read_env(PSQL_ADDR_VAR)?
.map(|address| {
address.parse().map_err(|e| {
format!("Unable to parse the postgresql address environment variable: {}", e)
})
})
.transpose()?;
let default_db_env = read_env(PSQL_DB_VAR)?;
let vss_db_env = read_env(PSQL_VSS_DB_VAR)?;
let tls_config_env = read_env(PSQL_TLS_VAR)?;
let crt_pem_env = read_env(PSQL_CERT_PEM_VAR)?;
let (
username_config,
password_config,
address_config,
default_db_config,
vss_db_config,
tls_config,
) = match postgresql_config {
Some(c) => (
c.username,
c.password,
c.address,
c.default_database,
c.vss_database,
c.tls.map(|tls| tls.crt_pem),
),
None => (None, None, None, None, None, None),
};
let username =
read_config(username_env, username_config, "PostgreSQL database username", PSQL_USER_VAR)?;
let password =
read_config(password_env, password_config, "PostgreSQL database password", PSQL_PASS_VAR)?;
let address =
read_config(address_env, address_config, "PostgreSQL service address", PSQL_ADDR_VAR)?;
let default_db = read_config(
default_db_env,
default_db_config,
"PostgreSQL default database name",
PSQL_DB_VAR,
)?;
let vss_db =
read_config(vss_db_env, vss_db_config, "PostgreSQL vss database name", PSQL_VSS_DB_VAR)?;
let tls_config =
crt_pem_env.map(|pem| Some(pem)).or(tls_config_env.map(|_| None)).or(tls_config);
let postgresql_prefix = format!("postgresql://{}:{}@{}", username, password, address);
Ok(Configuration {
bind_address,
maximum_request_body_size: Some(maximum_request_body_size),
rsa_pem,
postgresql_prefix,
default_db,
vss_db,
tls_config,
})
}