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.
master
Dustin 2022-02-12 11:32:29 -06:00
parent dc0c987358
commit d690cf0982
3 changed files with 101 additions and 0 deletions

11
Cargo.lock generated
View File

@ -59,6 +59,7 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
name = "burp_exporter" name = "burp_exporter"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"gethostname",
"openssl", "openssl",
"prometheus_exporter_base", "prometheus_exporter_base",
"serde", "serde",
@ -216,6 +217,16 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "gethostname"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4addc164932852d066774c405dbbdb7914742d2b39e39e1a7ca949c856d054d1"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
openssl = "^0.10.38" openssl = "^0.10.38"
gethostname = "^0.2.2"
serde_json = "1.0.78" serde_json = "1.0.78"
tokio-native-tls = "^0.3.0" tokio-native-tls = "^0.3.0"
tracing = "^0.1.30" tracing = "^0.1.30"

89
src/config.rs Normal file
View File

@ -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<String>,
/// ca certificate file
#[serde(default)]
pub ca_cert: Option<String>,
/// client identity file
#[serde(default)]
pub identity: Option<String>,
/// client identity file password
#[serde(default)]
pub identity_password: Option<String>,
/// client name (default: *local host name*)
#[serde(default = "default_client_name")]
pub client_name: String,
/// client password
#[serde(default)]
pub password: Option<String>,
}
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()
}