Skip to content

Commit 1c1500f

Browse files
committed
Optionally configure bind address via env var
This allows users to configure VSS-Server entirely through environment variables, without a configuration file. We hence remove the requirement that a path to a configuration file always be passed as an argument.
1 parent b7198cb commit 1c1500f

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

rust/server/src/main.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ mod vss_service;
2828

2929
fn main() {
3030
let args: Vec<String> = std::env::args().collect();
31-
if args.len() != 2 {
32-
eprintln!("Usage: {} <config-file-path>", args[0]);
33-
std::process::exit(1);
34-
}
3531

36-
let config = util::config::load_configuration(&args[1]).unwrap_or_else(|e| {
37-
eprintln!("Failed to load configuration: {}", e);
38-
std::process::exit(-1);
39-
});
32+
let config =
33+
util::config::load_configuration(args.get(1).map(|s| s.as_str())).unwrap_or_else(|e| {
34+
eprintln!("Failed to load configuration: {}", e);
35+
std::process::exit(-1);
36+
});
4037

4138
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
4239
Ok(runtime) => Arc::new(runtime),

rust/server/src/util/config.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use serde::Deserialize;
22
use std::net::SocketAddr;
33

4+
const BIND_ADDR_VAR: &str = "VSS_BIND_ADDRESS";
45
const JWT_RSA_PEM_VAR: &str = "VSS_JWT_RSA_PEM";
56
const PSQL_USER_VAR: &str = "VSS_PSQL_USERNAME";
67
const PSQL_PASS_VAR: &str = "VSS_PSQL_PASSWORD";
@@ -10,16 +11,16 @@ const PSQL_VSS_DB_VAR: &str = "VSS_PSQL_VSS_DB";
1011
const PSQL_TLS_VAR: &str = "VSS_PSQL_TLS";
1112
const PSQL_CERT_PEM_VAR: &str = "VSS_PSQL_CRT_PEM";
1213

13-
#[derive(Deserialize)]
14+
#[derive(Deserialize, Default)]
1415
struct Config {
15-
server_config: ServerConfig,
16+
server_config: Option<ServerConfig>,
1617
jwt_auth_config: Option<JwtAuthConfig>,
1718
postgresql_config: Option<PostgreSQLConfig>,
1819
}
1920

2021
#[derive(Deserialize)]
2122
struct ServerConfig {
22-
bind_address: SocketAddr,
23+
bind_address: Option<SocketAddr>,
2324
}
2425

2526
#[derive(Deserialize)]
@@ -51,12 +52,19 @@ pub(crate) struct Configuration {
5152
pub(crate) tls_config: Option<Option<String>>,
5253
}
5354

54-
pub(crate) fn load_configuration(config_file_path: &str) -> Result<Configuration, String> {
55-
let config_file = std::fs::read_to_string(config_file_path)
56-
.map_err(|e| format!("Failed to read configuration file: {}", e))?;
57-
let Config { server_config: ServerConfig { bind_address }, jwt_auth_config, postgresql_config } =
55+
pub(crate) fn load_configuration(config_file_path: Option<&str>) -> Result<Configuration, String> {
56+
let Config { server_config, jwt_auth_config, postgresql_config } = if config_file_path.is_some()
57+
&& std::fs::exists(config_file_path.expect("config file path is some"))
58+
.map_err(|e| format!("Failed to check presence of configuration file: {}", e))?
59+
{
60+
let config_file =
61+
std::fs::read_to_string(config_file_path.expect("config file path is some"))
62+
.map_err(|e| format!("Failed to read configuration file: {}", e))?;
5863
toml::from_str(&config_file)
59-
.map_err(|e| format!("Failed to parse configuration file: {}", e))?;
64+
.map_err(|e| format!("Failed to parse configuration file: {}", e))?
65+
} else {
66+
Config::default() // All fields are set to `None`
67+
};
6068

6169
macro_rules! read_env {
6270
($env_var:expr) => {
@@ -82,6 +90,20 @@ pub(crate) fn load_configuration(config_file_path: &str) -> Result<Configuration
8290
};
8391
}
8492

93+
let bind_address_env = read_env!(BIND_ADDR_VAR)
94+
.map(|addr| {
95+
addr.parse().map_err(|e| {
96+
format!("Unable to parse the bind address environment variable: {}", e)
97+
})
98+
})
99+
.transpose()?;
100+
let bind_address = read_config!(
101+
bind_address_env,
102+
server_config.and_then(|c| c.bind_address),
103+
"VSS server bind address",
104+
BIND_ADDR_VAR
105+
);
106+
85107
let rsa_pem_env = read_env!(JWT_RSA_PEM_VAR);
86108
let rsa_pem = rsa_pem_env.or(jwt_auth_config.and_then(|config| config.rsa_pem));
87109

rust/server/vss-server-config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[server_config]
2-
bind_address = "127.0.0.1:8080"
2+
bind_address = "127.0.0.1:8080" # Optional in TOML, can be overridden by env var `VSS_BIND_ADDRESS`
33

44
# Uncomment the table below to verify JWT tokens in the HTTP Authorization header against the given RSA public key,
55
# can be overridden by env var `VSS_JWT_RSA_PEM`

0 commit comments

Comments
 (0)