mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-31 09:18:28 +00:00
fix(runtime): remove high-impact unwrap paths (#3755)
* fix(runtime): remove high-impact unwrap paths * fix(runtime): propagate managed SSE metadata errors * fix(runtime): add typed OPA config errors * fix(runtime): harden config and credential helpers * fix(runtime): remove SSE hmac unwraps * fix(runtime): complete SSE helper error propagation * fix(trusted-proxies): avoid legacy global init panics * test(credentials): allow deprecated rpc token check * fix(storage): harden object lock retention parsing * chore(checks): refresh layer dependency baseline * chore(checks): refresh layer dependency baseline * Update layer-dependency-baseline.txt Signed-off-by: houseme <housemecn@gmail.com> * test(context): avoid clone on copy boot time --------- Signed-off-by: houseme <housemecn@gmail.com> Co-authored-by: Zhengchao An <anzhengchao@gmail.com> Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
@@ -31,9 +31,30 @@ static METRICS: OnceLock<Option<ProxyMetrics>> = OnceLock::new();
|
||||
/// Global instance of the trusted proxy layer.
|
||||
static PROXY_LAYER: OnceLock<LegacyTrustedProxyLayer> = OnceLock::new();
|
||||
|
||||
/// Disabled fallback layer used when legacy trusted proxies are not enabled.
|
||||
static DISABLED_PROXY_LAYER: OnceLock<LegacyTrustedProxyLayer> = OnceLock::new();
|
||||
|
||||
/// Global flag indicating if the trusted proxy middleware is enabled.
|
||||
static ENABLED: OnceLock<bool> = OnceLock::new();
|
||||
|
||||
fn load_config() -> &'static Arc<AppConfig> {
|
||||
CONFIG.get_or_init(|| Arc::new(ConfigLoader::from_env_or_default()))
|
||||
}
|
||||
|
||||
fn load_metrics(config: &AppConfig, enabled: bool) -> &'static Option<ProxyMetrics> {
|
||||
METRICS.get_or_init(|| {
|
||||
if config.monitoring.metrics_enabled {
|
||||
Some(default_proxy_metrics(enabled))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn disabled_layer() -> &'static LegacyTrustedProxyLayer {
|
||||
DISABLED_PROXY_LAYER.get_or_init(LegacyTrustedProxyLayer::disabled)
|
||||
}
|
||||
|
||||
/// Initializes the global trusted proxy system.
|
||||
///
|
||||
/// This function should be called once at the start of the application.
|
||||
@@ -54,24 +75,11 @@ pub fn init() {
|
||||
return;
|
||||
}
|
||||
|
||||
let config = CONFIG.get_or_init(|| Arc::new(ConfigLoader::from_env_or_default())).clone();
|
||||
let config = load_config().clone();
|
||||
let metrics = load_metrics(&config, enabled).clone();
|
||||
|
||||
METRICS.get_or_init(|| {
|
||||
if config.monitoring.metrics_enabled {
|
||||
Some(default_proxy_metrics(enabled))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
PROXY_LAYER.get_or_init(|| {
|
||||
LegacyTrustedProxyLayer::with_cache_config(
|
||||
config.proxy.clone(),
|
||||
config.cache.clone(),
|
||||
METRICS.get().and_then(|m| m.clone()),
|
||||
enabled,
|
||||
)
|
||||
});
|
||||
PROXY_LAYER
|
||||
.get_or_init(|| LegacyTrustedProxyLayer::with_cache_config(config.proxy.clone(), config.cache.clone(), metrics, enabled));
|
||||
|
||||
tracing::info!(
|
||||
event = "trusted_proxies.lifecycle",
|
||||
@@ -90,25 +98,27 @@ pub fn init() {
|
||||
/// Returns a reference to the global trusted proxy layer.
|
||||
///
|
||||
/// This layer can be used to wrap Axum services or other Tower-compatible services.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `init()` has not been called.
|
||||
pub fn layer() -> &'static LegacyTrustedProxyLayer {
|
||||
PROXY_LAYER
|
||||
.get()
|
||||
.expect("Trusted proxy system not initialized. Call init() first.")
|
||||
if let Some(layer) = PROXY_LAYER.get() {
|
||||
return layer;
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
if let Some(layer) = PROXY_LAYER.get() {
|
||||
return layer;
|
||||
}
|
||||
|
||||
disabled_layer()
|
||||
}
|
||||
|
||||
/// Returns a reference to the global configuration.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `init()` has not been called.
|
||||
pub fn config() -> &'static AppConfig {
|
||||
CONFIG
|
||||
.get()
|
||||
.expect("Trusted proxy system not initialized. Call init() first.")
|
||||
if CONFIG.get().is_none() {
|
||||
init();
|
||||
}
|
||||
|
||||
load_config().as_ref()
|
||||
}
|
||||
|
||||
/// Returns a reference to the global metrics collector, if enabled.
|
||||
@@ -120,3 +130,23 @@ pub fn metrics() -> Option<&'static ProxyMetrics> {
|
||||
pub fn is_enabled() -> bool {
|
||||
*ENABLED.get_or_init(|| rustfs_utils::get_env_bool(ENV_TRUSTED_PROXY_ENABLED, DEFAULT_TRUSTED_PROXY_ENABLED))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn legacy_layer_is_available_without_explicit_init() {
|
||||
let layer = layer();
|
||||
assert_eq!(layer.is_enabled(), is_enabled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_config_is_available_without_explicit_init() {
|
||||
let expected = ConfigLoader::from_env_or_default();
|
||||
let config = config();
|
||||
|
||||
assert_eq!(config.server_addr, expected.server_addr);
|
||||
assert_eq!(config.monitoring.metrics_enabled, expected.monitoring.metrics_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user