From d690cf09822c54f019ca63a4cfbc9af2e319d354 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 12 Feb 2022 11:32:29 -0600 Subject: [PATCH] config: Add application configuration Naturally, we need a configuration mechanism in order to specify connection parameters for the BURP stats socket. Using the `serde::Deserialize` trait will make this relatively straightforward and allows us to choose from a wide variety of data formats as our configuration language. We will probably use TOML since it is pretty simple to read and write for both humans and machines, and our data model is very simple. --- Cargo.lock | 11 +++++++ Cargo.toml | 1 + src/config.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 64530b9..f0adec9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,7 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" name = "burp_exporter" version = "0.1.0" dependencies = [ + "gethostname", "openssl", "prometheus_exporter_base", "serde", @@ -216,6 +217,16 @@ dependencies = [ "slab", ] +[[package]] +name = "gethostname" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4addc164932852d066774c405dbbdb7914742d2b39e39e1a7ca949c856d054d1" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "hermit-abi" version = "0.1.19" diff --git a/Cargo.toml b/Cargo.toml index e37c411..e5ad5fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] openssl = "^0.10.38" +gethostname = "^0.2.2" serde_json = "1.0.78" tokio-native-tls = "^0.3.0" tracing = "^0.1.30" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..c875848 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,89 @@ +//! Application configuration +use gethostname::gethostname; +use serde::Deserialize; + +/// Configuration options +#[derive(Clone, Debug, Deserialize)] +pub struct Config { + /// http listen address (default: `::`) + #[serde(default = "default_listen_host")] + pub listen_host: String, + + /// http listen port (default: `9645`) + #[serde(default = "default_listen_port")] + pub listen_port: u16, + + /// server hostname or ip address (default: `localhost`) + #[serde(default = "default_server")] + pub server: String, + + /// server port (default: `4972`) + #[serde(default = "default_port")] + pub port: u16, + + /// server name (for certificate validation) + #[serde(default)] + pub server_name: Option, + + /// ca certificate file + #[serde(default)] + pub ca_cert: Option, + + /// client identity file + #[serde(default)] + pub identity: Option, + + /// client identity file password + #[serde(default)] + pub identity_password: Option, + + /// client name (default: *local host name*) + #[serde(default = "default_client_name")] + pub client_name: String, + + /// client password + #[serde(default)] + pub password: Option, +} + +impl Default for Config { + fn default() -> Self { + Self { + listen_host: default_listen_host(), + listen_port: default_listen_port(), + server: default_server(), + port: default_port(), + server_name: None, + ca_cert: None, + identity: None, + identity_password: None, + client_name: default_client_name(), + password: None, + } + } +} + +#[doc(hidden)] +fn default_listen_host() -> String { + "::".into() +} + +#[doc(hidden)] +fn default_listen_port() -> u16 { + 9645 +} + +#[doc(hidden)] +fn default_server() -> String { + "localhost".into() +} + +#[doc(hidden)] +fn default_port() -> u16 { + 4972 +} + +#[doc(hidden)] +fn default_client_name() -> String { + gethostname().to_string_lossy().into() +}