rework redis options

This commit is contained in:
Robin Appelman 2025-04-30 18:17:43 +02:00
commit 88b64b402d
5 changed files with 86 additions and 213 deletions

129
Cargo.lock generated
View file

@ -165,22 +165,6 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "core-foundation"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.17" version = "0.2.17"
@ -680,6 +664,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45"
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.14" version = "1.0.14"
@ -846,6 +839,7 @@ name = "nextcloud-config-parser"
version = "0.13.0" version = "0.13.0"
dependencies = [ dependencies = [
"form_urlencoded", "form_urlencoded",
"itertools",
"miette", "miette",
"php-literal-parser", "php-literal-parser",
"redis", "redis",
@ -926,12 +920,6 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "openssl-probe"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
[[package]] [[package]]
name = "owo-colors" name = "owo-colors"
version = "4.1.0" version = "4.1.0"
@ -1134,8 +1122,6 @@ dependencies = [
"itoa", "itoa",
"num-bigint", "num-bigint",
"percent-encoding", "percent-encoding",
"rustls",
"rustls-native-certs",
"ryu", "ryu",
"sha1_smol", "sha1_smol",
"socket2", "socket2",
@ -1180,20 +1166,6 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.16",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "rsa" name = "rsa"
version = "0.9.8" version = "0.9.8"
@ -1246,93 +1218,18 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "rustls"
version = "0.23.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df51b5869f3a441595eac5e8ff14d486ff285f7b8c0df8770e49c3b56351f0f0"
dependencies = [
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-native-certs"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcff2dd52b58a8d98a70243663a0d234c4e2b79235637849d15913394a247d3"
dependencies = [
"openssl-probe",
"rustls-pki-types",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pki-types"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c"
[[package]]
name = "rustls-webpki"
version = "0.103.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fef8b8769aaccf73098557a87cd1816b4f9c7c16811c9c77142aa695c16f2c03"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.18" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "schannel"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d"
dependencies = [
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "security-framework"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "271720403f46ca04f7ba6f55d438f8bd878d6b8ca0a1046e8228c4145bcbb316"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.217" version = "1.0.217"
@ -1916,12 +1813,6 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]] [[package]]
name = "url" name = "url"
version = "2.5.4" version = "2.5.4"

View file

@ -15,8 +15,9 @@ php-literal-parser = "0.6.2"
miette = "7.4.0" miette = "7.4.0"
urlencoding = "2.1.3" urlencoding = "2.1.3"
form_urlencoded = "1.2.1" form_urlencoded = "1.2.1"
itertools = "0.14.0"
[dev-dependencies] [dev-dependencies]
miette = { version = "7.4.0", features = ["fancy"] } miette = { version = "7.4.0", features = ["fancy"] }
sqlx = { version = "0.8.3", default-features = false, features = ["any", "mysql", "sqlite", "postgres"] } sqlx = { version = "0.8.3", default-features = false, features = ["any", "mysql", "sqlite", "postgres"] }
redis = { version = "0.30.0", features = ["tls-rustls", "tls-rustls-insecure"] } redis = "0.30.0"

View file

@ -1,6 +1,7 @@
mod nc; mod nc;
use form_urlencoded::Serializer; use form_urlencoded::Serializer;
use itertools::Either;
use miette::Diagnostic; use miette::Diagnostic;
use std::iter::once; use std::iter::once;
use std::path::PathBuf; use std::path::PathBuf;
@ -19,50 +20,42 @@ pub struct Config {
#[derive(Debug)] #[derive(Debug)]
pub enum RedisConfig { pub enum RedisConfig {
Single(RedisConnectionInfo), Single(RedisConnectionInfo),
Cluster(Vec<RedisConnectionInfo>), Cluster(RedisClusterConnectionInfo),
}
impl RedisConfig {
pub fn as_single(&self) -> Option<RedisConnectionInfo> {
match self {
RedisConfig::Single(single) => Some(single.clone()),
RedisConfig::Cluster(cluster) => cluster.iter().next(),
}
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum RedisConnectionAddr { pub enum RedisConnectionAddr {
Tcp { Tcp { host: String, port: u16 },
host: String, Unix { path: PathBuf },
port: u16,
},
TcpTls {
host: String,
port: u16,
tls_params: Option<RedisTlsParams>,
},
Unix {
path: PathBuf,
},
} }
impl RedisConnectionAddr { #[derive(Clone, Debug)]
pub fn with_tls(self, tls_params: RedisTlsParams) -> Self { pub struct RedisClusterConnectionInfo {
match self { pub addr: Vec<RedisConnectionAddr>,
RedisConnectionAddr::Tcp { host, port } pub db: i64,
| RedisConnectionAddr::TcpTls { host, port, .. } => RedisConnectionAddr::TcpTls { pub username: Option<String>,
host, pub password: Option<String>,
port, pub tls_params: Option<RedisTlsParams>,
tls_params: Some(tls_params), }
},
unix => unix,
}
}
pub fn with_tls_opt(self, tls_params: Option<RedisTlsParams>) -> Self { impl RedisClusterConnectionInfo {
if let Some(params) = tls_params { pub fn iter(&self) -> impl Iterator<Item = RedisConnectionInfo> + '_ {
self.with_tls(params) self.addr.iter().cloned().map(|addr| RedisConnectionInfo {
} else { addr,
match self { db: self.db,
RedisConnectionAddr::Tcp { host, port } username: self.username.clone(),
| RedisConnectionAddr::TcpTls { host, port, .. } => { password: self.password.clone(),
RedisConnectionAddr::Tcp { host, port } tls_params: self.tls_params.clone(),
} })
unix => unix,
}
}
} }
} }
@ -72,9 +65,10 @@ pub struct RedisConnectionInfo {
pub db: i64, pub db: i64,
pub username: Option<String>, pub username: Option<String>,
pub password: Option<String>, pub password: Option<String>,
pub tls_params: Option<RedisTlsParams>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub struct RedisTlsParams { pub struct RedisTlsParams {
pub local_cert: Option<PathBuf>, pub local_cert: Option<PathBuf>,
pub local_pk: Option<PathBuf>, pub local_pk: Option<PathBuf>,
@ -85,44 +79,37 @@ pub struct RedisTlsParams {
impl RedisConfig { impl RedisConfig {
pub fn addr(&self) -> impl Iterator<Item = &RedisConnectionAddr> { pub fn addr(&self) -> impl Iterator<Item = &RedisConnectionAddr> {
let boxed: Box<dyn Iterator<Item = &RedisConnectionAddr>> = match self { match self {
RedisConfig::Single(conn) => Box::new(once(&conn.addr)), RedisConfig::Single(conn) => Either::Left(once(&conn.addr)),
RedisConfig::Cluster(conns) => Box::new(conns.iter().map(|conn| &conn.addr)), RedisConfig::Cluster(cluster) => Either::Right(cluster.addr.iter()),
}; }
boxed
} }
pub fn db(&self) -> i64 { pub fn db(&self) -> i64 {
match self { match self {
RedisConfig::Single(conn) => conn.db, RedisConfig::Single(conn) => conn.db,
RedisConfig::Cluster(conns) => conns.first().map(|conn| conn.db).unwrap_or_default(), RedisConfig::Cluster(cluster) => cluster.db,
} }
} }
pub fn username(&self) -> Option<&str> { pub fn username(&self) -> Option<&str> {
match self { match self {
RedisConfig::Single(conn) => conn.username.as_deref(), RedisConfig::Single(conn) => conn.username.as_deref(),
RedisConfig::Cluster(conns) => conns RedisConfig::Cluster(cluster) => cluster.username.as_deref(),
.first()
.map(|conn| conn.username.as_deref())
.unwrap_or_default(),
} }
} }
pub fn passwd(&self) -> Option<&str> { pub fn passwd(&self) -> Option<&str> {
match self { match self {
RedisConfig::Single(conn) => conn.password.as_deref(), RedisConfig::Single(conn) => conn.password.as_deref(),
RedisConfig::Cluster(conns) => conns RedisConfig::Cluster(cluster) => cluster.password.as_deref(),
.first()
.map(|conn| conn.password.as_deref())
.unwrap_or_default(),
} }
} }
pub fn into_vec(self) -> Vec<RedisConnectionInfo> { pub fn is_empty(&self) -> bool {
match self { match self {
RedisConfig::Single(conn) => vec![conn], RedisConfig::Single(_) => false,
RedisConfig::Cluster(vec) => vec, RedisConfig::Cluster(cluster) => cluster.addr.is_empty(),
} }
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError, Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError,
RedisConnectionInfo, RedisTlsParams, Result, SslOptions, RedisClusterConnectionInfo, RedisConnectionInfo, RedisTlsParams, Result, SslOptions,
}; };
use crate::{RedisConfig, RedisConnectionAddr}; use crate::{RedisConfig, RedisConnectionAddr};
use php_literal_parser::Value; use php_literal_parser::Value;
@ -343,8 +343,7 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
local_cert: ssl_options["local_cert"].as_str().map(From::from), local_cert: ssl_options["local_cert"].as_str().map(From::from),
local_pk: ssl_options["local_pk"].as_str().map(From::from), local_pk: ssl_options["local_pk"].as_str().map(From::from),
ca_file: ssl_options["cafile"].as_str().map(From::from), ca_file: ssl_options["cafile"].as_str().map(From::from),
accept_invalid_hostname: ssl_options["allow_self_signed"] == true accept_invalid_hostname: ssl_options["verify_peer_name"] == false,
|| ssl_options["verify_peer_name"] == false,
insecure: ssl_options["verify_peer "] == false, insecure: ssl_options["verify_peer "] == false,
}) })
} else { } else {
@ -363,22 +362,19 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
match address { match address {
RedisAddress::Single(addr) => RedisConfig::Single(RedisConnectionInfo { RedisAddress::Single(addr) => RedisConfig::Single(RedisConnectionInfo {
addr: addr.with_tls_opt(tls_params), addr,
db, db,
username, username,
password, password,
tls_params,
}), }),
RedisAddress::Cluster(addresses) => RedisConfig::Cluster( RedisAddress::Cluster(addr) => RedisConfig::Cluster(RedisClusterConnectionInfo {
addresses addr,
.into_iter()
.map(|addr| RedisConnectionInfo {
addr: addr.with_tls_opt(tls_params.clone()),
db, db,
username: username.clone(), username,
password: password.clone(), password,
}) tls_params,
.collect(), }),
),
} }
} }

View file

@ -24,12 +24,9 @@ fn config_from_file(path: &str) -> Config {
fn parse_redis(cfg: &str) -> RedisConnectionInfo { fn parse_redis(cfg: &str) -> RedisConnectionInfo {
let redis = ConnectionInfo::from_str(cfg).unwrap(); let redis = ConnectionInfo::from_str(cfg).unwrap();
let addr = match redis.addr { let addr = match redis.addr {
ConnectionAddr::Tcp(host, port) => RedisConnectionAddr::Tcp { host, port }, ConnectionAddr::Tcp(host, port) | ConnectionAddr::TcpTls { host, port, .. } => {
ConnectionAddr::TcpTls { host, port, .. } => RedisConnectionAddr::TcpTls { RedisConnectionAddr::Tcp { host, port }
host, }
port,
tls_params: None,
},
ConnectionAddr::Unix(path) => RedisConnectionAddr::Unix { path }, ConnectionAddr::Unix(path) => RedisConnectionAddr::Unix { path },
}; };
RedisConnectionInfo { RedisConnectionInfo {
@ -37,6 +34,7 @@ fn parse_redis(cfg: &str) -> RedisConnectionInfo {
db: redis.redis.db, db: redis.redis.db,
username: redis.redis.username, username: redis.redis.username,
password: redis.redis.password, password: redis.redis.password,
tls_params: None,
} }
} }
@ -108,9 +106,13 @@ fn test_parse_redis_tls() {
let config = config_from_file("tests/configs/redis_tls.php"); let config = config_from_file("tests/configs/redis_tls.php");
assert_debug_equal( assert_debug_equal(
RedisConfig::Single(RedisConnectionInfo { RedisConfig::Single(RedisConnectionInfo {
addr: RedisConnectionAddr::TcpTls { addr: RedisConnectionAddr::Tcp {
host: "127.0.0.1".into(), host: "127.0.0.1".into(),
port: 6379, port: 6379,
},
db: 0,
username: None,
password: None,
tls_params: Some(RedisTlsParams { tls_params: Some(RedisTlsParams {
local_cert: Some("/certs/redis.crt".into()), local_cert: Some("/certs/redis.crt".into()),
local_pk: Some("/certs/redis.key".into()), local_pk: Some("/certs/redis.key".into()),
@ -118,10 +120,6 @@ fn test_parse_redis_tls() {
insecure: false, insecure: false,
accept_invalid_hostname: false, accept_invalid_hostname: false,
}), }),
},
db: 0,
username: None,
password: None,
}), }),
config.redis, config.redis,
); );
@ -301,18 +299,18 @@ fn test_parse_postgres_socket_folder() {
#[test] #[test]
fn test_parse_redis_cluster() { fn test_parse_redis_cluster() {
let config = config_from_file("tests/configs/redis.cluster.php"); let config = config_from_file("tests/configs/redis.cluster.php");
let mut conns = config.redis.into_vec(); let mut addresses = config.redis.addr().cloned().collect::<Vec<_>>();
conns.sort_by(|a, b| format!("{:?}", a.addr).cmp(&format!("{:?}", b.addr))); addresses.sort_by(|a, b| format!("{:?}", a).cmp(&format!("{:?}", b)));
assert_debug_equal( assert_debug_equal(
vec![ vec![
parse_redis("redis://:xxx@db1:6380"), parse_redis("redis://:xxx@db1:6380").addr,
parse_redis("redis://:xxx@db1:6381"), parse_redis("redis://:xxx@db1:6381").addr,
parse_redis("redis://:xxx@db1:6382"), parse_redis("redis://:xxx@db1:6382").addr,
parse_redis("redis://:xxx@db2:6380"), parse_redis("redis://:xxx@db2:6380").addr,
parse_redis("redis://:xxx@db2:6381"), parse_redis("redis://:xxx@db2:6381").addr,
parse_redis("redis://:xxx@db2:6382"), parse_redis("redis://:xxx@db2:6382").addr,
], ],
conns, addresses,
); );
} }