Skip to content

Commit c544c7e

Browse files
authored
Add defaults when parsing toml (#279)
* Update config.rs * remove unused stuff * fmt
1 parent 366bbc3 commit c544c7e

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

example-config.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ http_port = 8080
66
# port the gRPC server will listen on
77
grpc_port = 50051
88

9-
# gRPC SSL configuration
10-
# provide certificate and key to connect to gRPC server with HTTPS
11-
# https://defguard.gitbook.io/defguard/features/setting-up-your-instance/docker-compose#grpc-ssl-setup
12-
# Optional: path to cert file
13-
# grpc_cert: proxy.crt
14-
# Optional: path to key file
15-
# grpc_key: proxy.key
169
log_level = "info"
1710
rate_limit_per_second = 0
1811
rate_limit_burst = 0
19-
url = "http://localhost:8080"
2012
acme_staging = false

src/config.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ use clap::Parser;
44
use log::LevelFilter;
55
use serde::Deserialize;
66

7+
fn default_http_port() -> u16 {
8+
8080
9+
}
10+
11+
fn default_grpc_port() -> u16 {
12+
50051
13+
}
14+
15+
fn default_log_level() -> LevelFilter {
16+
LevelFilter::Info
17+
}
18+
19+
fn default_cert_dir() -> PathBuf {
20+
PathBuf::from("/etc/defguard/certs")
21+
}
22+
23+
fn default_https_port() -> u16 {
24+
443
25+
}
26+
727
fn default_adoption_timeout() -> u64 {
828
10
929
}
@@ -18,19 +38,24 @@ pub struct EnvConfig {
1838
env = "DEFGUARD_PROXY_HTTP_PORT",
1939
default_value_t = 8080
2040
)]
41+
#[serde(default = "default_http_port")]
2142
pub http_port: u16,
2243

2344
// port the API server will listen on
2445
#[arg(long, env = "DEFGUARD_PROXY_GRPC_PORT", default_value_t = 50051)]
46+
#[serde(default = "default_grpc_port")]
2547
pub grpc_port: u16,
2648

2749
#[arg(long, env = "DEFGUARD_PROXY_LOG_LEVEL", default_value_t = LevelFilter::Info)]
50+
#[serde(default = "default_log_level")]
2851
pub log_level: LevelFilter,
2952

3053
#[arg(long, env = "DEFGUARD_PROXY_RATELIMIT_PERSECOND", default_value_t = 0)]
54+
#[serde(default)]
3155
pub rate_limit_per_second: u64,
3256

3357
#[arg(long, env = "DEFGUARD_PROXY_RATELIMIT_BURST", default_value_t = 0)]
58+
#[serde(default)]
3459
pub rate_limit_burst: u32,
3560

3661
/// Configuration file path
@@ -39,9 +64,11 @@ pub struct EnvConfig {
3964
config_path: Option<PathBuf>,
4065

4166
#[arg(long, env = "DEFGUARD_HTTP_BIND_ADDRESS")]
67+
#[serde(default)]
4268
pub http_bind_address: Option<IpAddr>,
4369

4470
#[arg(long, env = "DEFGUARD_GRPC_BIND_ADDRESS")]
71+
#[serde(default)]
4572
pub grpc_bind_address: Option<IpAddr>,
4673

4774
// TODO: On different platforms this may be different
@@ -50,15 +77,18 @@ pub struct EnvConfig {
5077
env = "DEFGUARD_PROXY_CERT_DIR",
5178
default_value = "/etc/defguard/certs"
5279
)]
80+
#[serde(default = "default_cert_dir")]
5381
pub cert_dir: PathBuf,
5482

5583
/// Port for the HTTPS server. When Core sends TLS certificates over gRPC, the HTTP
5684
/// server is restarted on this port using those certificates.
5785
#[arg(long, env = "DEFGUARD_PROXY_HTTPS_PORT", default_value_t = 443)]
86+
#[serde(default = "default_https_port")]
5887
pub https_port: u16,
5988

6089
/// Use Let's Encrypt staging environment for ACME issuance.
6190
#[arg(long, env = "DEFGUARD_PROXY_ACME_STAGING", default_value_t = false)]
91+
#[serde(default)]
6292
pub acme_staging: bool,
6393

6494
/// Time limit in minutes for the auto-adoption process.

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ pub(crate) mod generated {
4040
}
4141

4242
pub(crate) mod proto {
43-
pub(crate) use crate::generated::defguard::client_types::*;
44-
pub(crate) use crate::generated::defguard::common::v2::*;
45-
pub(crate) use crate::generated::defguard::proxy::v2::*;
43+
pub(crate) use crate::generated::defguard::{client_types::*, common::v2::*, proxy::v2::*};
4644
}
4745

4846
#[macro_use]

src/tests/mtls.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ use tonic::{
2222
transport::{Certificate, Channel, ClientTlsConfig, Endpoint, Identity},
2323
};
2424

25-
use crate::grpc::{ProxyServer, TlsConfig};
26-
use crate::proto::proxy_client::ProxyClient;
25+
use crate::{
26+
grpc::{ProxyServer, TlsConfig},
27+
proto::proxy_client::ProxyClient,
28+
};
2729

2830
struct TestCerts {
2931
/// PEM-encoded CA certificate (used as the trust root for both server and client validation).

0 commit comments

Comments
 (0)