-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.rs
More file actions
189 lines (168 loc) · 5.75 KB
/
Copy pathconfig.rs
File metadata and controls
189 lines (168 loc) · 5.75 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 std::path::{Path, PathBuf};
use std::time::Duration;
use serde::Deserialize;
use crate::policy;
/// Per-identity byte-rate limiting configuration.
/// Set `bytes_per_second = 0` to disable (default).
#[derive(Debug, Deserialize, Clone, Copy)]
pub struct RateLimitConfig {
#[serde(default)]
pub bytes_per_second: u64,
#[serde(default = "default_burst_bytes")]
pub burst_bytes: u64,
}
fn default_burst_bytes() -> u64 {
1_048_576 // 1 MB
}
impl Default for RateLimitConfig {
fn default() -> Self {
Self {
bytes_per_second: 0,
burst_bytes: default_burst_bytes(),
}
}
}
#[derive(Debug, Deserialize)]
// Note: deny_unknown_fields removed from Config so the optional
// [rate_limit] section can be absent from existing config files.
pub struct Config {
pub server: ServerConfig,
pub observability: ObservabilityConfig,
pub policy: PolicyConfig,
#[serde(default)]
pub rate_limit: RateLimitConfig,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServerConfig {
pub listen_addr: String,
pub tls_cert_path: PathBuf,
pub tls_key_path: PathBuf,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservabilityConfig {
pub log_level: String,
pub otlp_endpoint: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PolicyConfig {
pub client_ext_oid: String,
pub database_url: Option<String>,
pub database_url_env: Option<String>,
pub max_connections: Option<u32>,
pub connect_timeout_ms: Option<u64>,
pub pool_acquire_timeout_ms: Option<u64>,
pub query_timeout_ms: Option<u64>,
}
impl Config {
/// Load, parse, and validate a TOML config file.
///
/// # Errors
///
/// Returns an error if the file cannot be read, the TOML cannot be parsed,
/// or any config value fails validation.
pub fn load(path: &Path) -> anyhow::Result<Self> {
let contents = std::fs::read_to_string(path)?;
let config: Config = toml::from_str(&contents)?;
config.validate()?;
Ok(config)
}
fn validate(&self) -> anyhow::Result<()> {
self.server
.listen_addr
.parse::<std::net::SocketAddr>()
.map_err(|e| anyhow::anyhow!("invalid server.listen_addr: {e}"))?;
policy::parse_client_ext_oid(&self.policy.client_ext_oid)?;
self.policy.validate()?;
Ok(())
}
}
impl PolicyConfig {
/// Resolve the configured database URL from the literal value or env var.
///
/// # Errors
///
/// Returns an error if no source is configured, both sources are configured,
/// the configured source is empty, or the environment variable cannot be read.
pub fn database_url(&self) -> anyhow::Result<String> {
match (&self.database_url, &self.database_url_env) {
(Some(url), None) => {
anyhow::ensure!(!url.is_empty(), "policy.database_url must not be empty");
Ok(url.clone())
}
(None, Some(env_name)) => {
anyhow::ensure!(
!env_name.is_empty(),
"policy.database_url_env must not be empty"
);
let url = std::env::var(env_name)
.map_err(|e| anyhow::anyhow!("reading database URL from ${env_name}: {e}"))?;
anyhow::ensure!(
!url.is_empty(),
"database URL from ${env_name} must not be empty"
);
Ok(url)
}
(None, None) => {
anyhow::bail!("policy.database_url or policy.database_url_env is required")
}
(Some(_), Some(_)) => {
anyhow::bail!("set only one of policy.database_url or policy.database_url_env")
}
}
}
#[must_use]
pub fn max_connections(&self) -> u32 {
self.max_connections.unwrap_or(5)
}
#[must_use]
pub fn connect_timeout(&self) -> Duration {
Duration::from_millis(self.connect_timeout_ms.unwrap_or(5_000))
}
#[must_use]
pub fn pool_acquire_timeout(&self) -> Duration {
Duration::from_millis(self.pool_acquire_timeout_ms.unwrap_or(1_000))
}
#[must_use]
pub fn query_timeout(&self) -> Duration {
Duration::from_millis(self.query_timeout_ms.unwrap_or(500))
}
fn validate(&self) -> anyhow::Result<()> {
anyhow::ensure!(
self.max_connections() > 0,
"policy.max_connections must be greater than zero"
);
ensure_positive_timeout(self.connect_timeout_ms, "policy.connect_timeout_ms")?;
ensure_positive_timeout(
self.pool_acquire_timeout_ms,
"policy.pool_acquire_timeout_ms",
)?;
ensure_positive_timeout(self.query_timeout_ms, "policy.query_timeout_ms")?;
match (&self.database_url, &self.database_url_env) {
(Some(url), None) => {
anyhow::ensure!(!url.is_empty(), "policy.database_url must not be empty");
}
(None, Some(env_name)) => {
anyhow::ensure!(
!env_name.is_empty(),
"policy.database_url_env must not be empty"
);
}
(None, None) => {
anyhow::bail!("policy.database_url or policy.database_url_env is required")
}
(Some(_), Some(_)) => {
anyhow::bail!("set only one of policy.database_url or policy.database_url_env")
}
}
Ok(())
}
}
fn ensure_positive_timeout(value: Option<u64>, field: &str) -> anyhow::Result<()> {
if let Some(value) = value {
anyhow::ensure!(value > 0, "{field} must be greater than zero");
}
Ok(())
}