alopex-server 0.7.0

Server component for Alopex DB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::net::{IpAddr, SocketAddr};
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;

use alopex_cluster::{
    ClusterId, ClusterIdentity, ClusterManager, ClusterManagerConfig, ClusterMode, Endpoint,
    MembershipSource, NodeId, NodeRole, NodeState,
};
use serde::Deserialize;

use crate::audit::AuditLogOutput;
use crate::auth::AuthMode;
use crate::error::{Result, ServerError};
use crate::tls::TlsConfig;

const MAX_ADMISSION_LIMIT: usize = 100_000;
const MAX_QUERY_TIMEOUT_MS: u128 = 300_000;

/// Server configuration options.
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct ServerConfig {
    /// HTTP bind address.
    pub http_bind: SocketAddr,
    /// gRPC bind address.
    pub grpc_bind: SocketAddr,
    /// Admin bind address.
    pub admin_bind: SocketAddr,
    /// Allowlist for admin API when non-loopback.
    pub admin_allowlist: Vec<IpAddr>,
    /// Data directory for storage.
    pub data_dir: PathBuf,
    /// API prefix for HTTP routes.
    pub api_prefix: String,
    /// Authentication mode.
    pub auth_mode: AuthMode,
    /// TLS configuration (optional).
    pub tls: Option<TlsConfig>,
    /// Query timeout.
    #[serde(with = "humantime_serde")]
    pub query_timeout: Duration,
    /// Maximum number of concurrent requests admitted.
    #[serde(alias = "max_connections")]
    pub max_concurrency: usize,
    /// Maximum number of queued requests under backpressure.
    pub max_queue_len: usize,
    /// Max request size in bytes.
    pub max_request_size: usize,
    /// Max response size in bytes.
    pub max_response_size: usize,
    /// Session TTL.
    #[serde(with = "humantime_serde")]
    pub session_ttl: Duration,
    /// Enable Prometheus metrics.
    pub metrics_enabled: bool,
    /// Enable tracing.
    pub tracing_enabled: bool,
    /// Enable audit logging.
    pub audit_log_enabled: bool,
    /// Audit log output.
    pub audit_log_output: AuditLogOutput,
    /// Cluster-aware startup configuration.
    pub cluster: ClusterServerConfig,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            http_bind: "127.0.0.1:8080".parse().unwrap(),
            grpc_bind: "127.0.0.1:9090".parse().unwrap(),
            admin_bind: "127.0.0.1:8081".parse().unwrap(),
            admin_allowlist: Vec::new(),
            data_dir: PathBuf::from("./data"),
            api_prefix: String::new(),
            auth_mode: AuthMode::None,
            tls: None,
            query_timeout: Duration::from_secs(30),
            max_concurrency: 64,
            max_queue_len: 256,
            max_request_size: 100 * 1024 * 1024,
            max_response_size: 100 * 1024 * 1024,
            session_ttl: Duration::from_secs(300),
            metrics_enabled: true,
            tracing_enabled: true,
            audit_log_enabled: true,
            audit_log_output: AuditLogOutput::Stdout,
            cluster: ClusterServerConfig::default(),
        }
    }
}

/// Cluster-aware server configuration.
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct ClusterServerConfig {
    /// Cluster operating mode.
    pub mode: ClusterMode,
    /// Stable local node identity for cluster-aware mode.
    pub node_id: Option<String>,
    /// Stable cluster identifier shared by configured nodes.
    pub cluster_id: Option<String>,
    /// Endpoint advertised to other cluster members.
    pub advertised_endpoint: Option<String>,
    /// Local node role.
    pub role: NodeRole,
    /// Local lifecycle state used when cluster-aware mode is enabled.
    pub lifecycle_state: NodeState,
    /// Membership metadata source to report and consume.
    pub membership_source: MembershipSource,
    /// Whether the membership source is available at startup.
    pub membership_source_available: bool,
}

impl Default for ClusterServerConfig {
    fn default() -> Self {
        Self {
            mode: ClusterMode::SingleNode,
            node_id: None,
            cluster_id: None,
            advertised_endpoint: None,
            role: NodeRole::Gateway,
            lifecycle_state: NodeState::Active,
            membership_source: MembershipSource::Chirps,
            membership_source_available: true,
        }
    }
}

impl ServerConfig {
    /// Load config from TOML and environment variables.
    ///
    /// Environment variables use `ALOPEX__` prefix with `__` separators.
    pub fn load(path: Option<&Path>) -> Result<Self> {
        let mut builder = config::Config::builder();
        if let Some(path) = path {
            builder = builder.add_source(config::File::from(path).required(false));
        } else {
            builder = builder.add_source(config::File::with_name("alopex").required(false));
        }
        builder = builder.add_source(config::Environment::with_prefix("ALOPEX").separator("__"));
        let mut config: ServerConfig = builder
            .build()
            .map_err(|err| ServerError::InvalidConfig(err.to_string()))?
            .try_deserialize()
            .map_err(|err| ServerError::InvalidConfig(err.to_string()))?;
        config.normalize()?;
        Ok(config)
    }

    /// Validate config invariants.
    pub fn validate(&self) -> Result<()> {
        if !self.admin_bind.ip().is_loopback() && self.admin_allowlist.is_empty() {
            return Err(ServerError::InvalidConfig(
                "admin_allowlist is required for non-loopback admin_bind".into(),
            ));
        }
        if !self.api_prefix.is_empty() && !self.api_prefix.starts_with('/') {
            return Err(ServerError::InvalidConfig(
                "api_prefix must start with '/' or be empty".into(),
            ));
        }
        if self.max_response_size == 0 {
            return Err(ServerError::InvalidConfig(
                "max_response_size must be greater than 0".into(),
            ));
        }
        if self.max_request_size == 0 {
            return Err(ServerError::InvalidConfig(
                "max_request_size must be greater than 0".into(),
            ));
        }
        if self.max_concurrency == 0 {
            return Err(ServerError::InvalidConfig(
                "max_concurrency must be greater than 0".into(),
            ));
        }
        if self.max_concurrency > MAX_ADMISSION_LIMIT {
            return Err(ServerError::InvalidConfig(format!(
                "max_concurrency must be <= {MAX_ADMISSION_LIMIT}"
            )));
        }
        if self.max_queue_len == 0 {
            return Err(ServerError::InvalidConfig(
                "max_queue_len must be greater than 0".into(),
            ));
        }
        if self.max_queue_len > MAX_ADMISSION_LIMIT {
            return Err(ServerError::InvalidConfig(format!(
                "max_queue_len must be <= {MAX_ADMISSION_LIMIT}"
            )));
        }
        let query_timeout_ms = self.query_timeout.as_millis();
        if query_timeout_ms == 0 {
            return Err(ServerError::InvalidConfig(
                "query_timeout must be greater than 0ms".into(),
            ));
        }
        if query_timeout_ms > MAX_QUERY_TIMEOUT_MS {
            return Err(ServerError::InvalidConfig(format!(
                "query_timeout must be <= {MAX_QUERY_TIMEOUT_MS}ms"
            )));
        }
        let cluster_config = self.cluster_manager_config()?;
        ClusterManager::new(cluster_config)
            .map_err(|err| ServerError::InvalidConfig(err.to_string()))?;
        Ok(())
    }

    /// Build cluster manager configuration from server configuration.
    pub fn cluster_manager_config(&self) -> Result<ClusterManagerConfig> {
        self.cluster.to_manager_config()
    }

    fn normalize(&mut self) -> Result<()> {
        if self.api_prefix == "/" {
            self.api_prefix.clear();
        } else if self.api_prefix.ends_with('/') {
            while self.api_prefix.ends_with('/') {
                self.api_prefix.pop();
            }
        }
        self.validate()
    }
}

impl ClusterServerConfig {
    fn to_manager_config(&self) -> Result<ClusterManagerConfig> {
        match self.mode {
            ClusterMode::SingleNode => Ok(ClusterManagerConfig::single_node()),
            ClusterMode::ClusterAware => {
                let mut config =
                    ClusterManagerConfig::cluster_aware(self.cluster_aware_identity()?);
                config.membership_source = self.membership_source;
                config.membership_source_available = self.membership_source_available;
                Ok(config)
            }
        }
    }

    fn cluster_aware_identity(&self) -> Result<ClusterIdentity> {
        let node_id = required_cluster_value(self.node_id.as_deref(), "cluster.node_id")?;
        let cluster_id = required_cluster_value(self.cluster_id.as_deref(), "cluster.cluster_id")?;
        let endpoint = required_cluster_value(
            self.advertised_endpoint.as_deref(),
            "cluster.advertised_endpoint",
        )?;

        Ok(ClusterIdentity {
            node_id: NodeId::new(node_id),
            cluster_id: Some(ClusterId::new(cluster_id)),
            advertised_endpoint: Some(Endpoint::new(endpoint)),
            role: self.role,
            lifecycle_state: self.lifecycle_state,
            metadata_schema_version: alopex_cluster::CLUSTER_METADATA_SCHEMA_VERSION,
            update_epoch: alopex_cluster::INITIAL_UPDATE_EPOCH,
        })
    }
}

fn required_cluster_value(value: Option<&str>, field: &'static str) -> Result<String> {
    let Some(value) = value else {
        return Err(ServerError::InvalidConfig(format!(
            "{field} is required when cluster.mode is cluster_aware"
        )));
    };
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(ServerError::InvalidConfig(format!(
            "{field} must be non-empty when cluster.mode is cluster_aware"
        )));
    }
    Ok(trimmed.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_admission_control_values_match_v06_policy() {
        let cfg = ServerConfig::default();
        assert_eq!(cfg.max_concurrency, 64);
        assert_eq!(cfg.max_queue_len, 256);
        assert_eq!(cfg.query_timeout.as_millis(), 30_000);
    }

    #[test]
    fn validate_rejects_zero_or_excessive_admission_values() {
        let cfg = ServerConfig {
            max_concurrency: 0,
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());

        let cfg = ServerConfig {
            max_queue_len: 0,
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());

        let cfg = ServerConfig {
            max_concurrency: MAX_ADMISSION_LIMIT + 1,
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());

        let cfg = ServerConfig {
            max_queue_len: MAX_ADMISSION_LIMIT + 1,
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());

        let cfg = ServerConfig {
            query_timeout: Duration::from_millis(0),
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());

        let cfg = ServerConfig {
            query_timeout: Duration::from_millis((MAX_QUERY_TIMEOUT_MS + 1) as u64),
            ..ServerConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn load_supports_legacy_max_connections_alias() {
        let raw = r#"
http_bind = "127.0.0.1:8080"
grpc_bind = "127.0.0.1:9090"
admin_bind = "127.0.0.1:8081"
data_dir = "./data"
api_prefix = ""
query_timeout = "30s"
max_connections = 77
max_queue_len = 256
max_request_size = 1048576
max_response_size = 1048576
session_ttl = "300s"
metrics_enabled = true
tracing_enabled = true
audit_log_enabled = false

[auth_mode]
type = "none"

[audit_log_output]
type = "stdout"
"#;

        let built = config::Config::builder()
            .add_source(config::File::from_str(raw, config::FileFormat::Toml))
            .build()
            .expect("build config");
        let cfg: ServerConfig = built.try_deserialize().expect("deserialize");
        assert_eq!(cfg.max_concurrency, 77);
    }

    #[test]
    fn default_cluster_config_is_single_node_non_degraded() {
        let cfg = ServerConfig::default();
        let manager = ClusterManager::new(cfg.cluster_manager_config().unwrap()).unwrap();
        let snapshot = manager.status_snapshot();

        assert_eq!(snapshot.mode, ClusterMode::SingleNode);
        assert_eq!(snapshot.identity.node_id.as_str(), "local");
        assert_eq!(snapshot.identity.lifecycle_state, NodeState::Unconfigured);
        assert_eq!(snapshot.membership.source, MembershipSource::LocalDefault);
        assert!(!snapshot.degraded);
        assert!(snapshot.diagnostics.is_empty());
    }

    #[test]
    fn cluster_aware_config_builds_configured_identity() {
        let cfg = ServerConfig {
            cluster: ClusterServerConfig {
                mode: ClusterMode::ClusterAware,
                node_id: Some("node-a".to_string()),
                cluster_id: Some("cluster-a".to_string()),
                advertised_endpoint: Some("127.0.0.1:7001".to_string()),
                role: NodeRole::Worker,
                lifecycle_state: NodeState::Joining,
                membership_source_available: false,
                ..ClusterServerConfig::default()
            },
            ..ServerConfig::default()
        };

        let manager = ClusterManager::new(cfg.cluster_manager_config().unwrap()).unwrap();
        let snapshot = manager.status_snapshot();

        assert_eq!(snapshot.mode, ClusterMode::ClusterAware);
        assert_eq!(snapshot.identity.node_id.as_str(), "node-a");
        assert_eq!(snapshot.identity.cluster_id.unwrap().as_str(), "cluster-a");
        assert_eq!(
            snapshot.identity.advertised_endpoint.unwrap().as_str(),
            "127.0.0.1:7001"
        );
        assert_eq!(snapshot.identity.role, NodeRole::Worker);
        assert_eq!(snapshot.identity.lifecycle_state, NodeState::Joining);
        assert_eq!(snapshot.membership.source, MembershipSource::Chirps);
        assert!(snapshot.degraded);
    }

    #[test]
    fn validate_rejects_invalid_cluster_identity() {
        let cfg = ServerConfig {
            cluster: ClusterServerConfig {
                mode: ClusterMode::ClusterAware,
                node_id: Some("".to_string()),
                cluster_id: Some("cluster-a".to_string()),
                advertised_endpoint: Some("127.0.0.1:7001".to_string()),
                ..ClusterServerConfig::default()
            },
            ..ServerConfig::default()
        };

        let err = cfg.validate().unwrap_err();
        assert!(err.to_string().contains("cluster.node_id"));
    }
}