mirror of
https://codeberg.org/icewind/nextcloud-config-parser.git
synced 2026-06-03 08:34:13 +02:00
rework redis options
This commit is contained in:
parent
e4bb72fd00
commit
88b64b402d
5 changed files with 86 additions and 213 deletions
129
Cargo.lock
generated
129
Cargo.lock
generated
|
|
@ -165,22 +165,6 @@ version = "0.9.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.17"
|
||||
|
|
@ -680,6 +664,15 @@ version = "1.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.14"
|
||||
|
|
@ -846,6 +839,7 @@ name = "nextcloud-config-parser"
|
|||
version = "0.13.0"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"itertools",
|
||||
"miette",
|
||||
"php-literal-parser",
|
||||
"redis",
|
||||
|
|
@ -926,12 +920,6 @@ version = "1.21.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "openssl-probe"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
||||
|
||||
[[package]]
|
||||
name = "owo-colors"
|
||||
version = "4.1.0"
|
||||
|
|
@ -1134,8 +1122,6 @@ dependencies = [
|
|||
"itoa",
|
||||
"num-bigint",
|
||||
"percent-encoding",
|
||||
"rustls",
|
||||
"rustls-native-certs",
|
||||
"ryu",
|
||||
"sha1_smol",
|
||||
"socket2",
|
||||
|
|
@ -1180,20 +1166,6 @@ version = "0.8.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "rsa"
|
||||
version = "0.9.8"
|
||||
|
|
@ -1246,93 +1218,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "ryu"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "serde"
|
||||
version = "1.0.217"
|
||||
|
|
@ -1916,12 +1813,6 @@ version = "0.1.14"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.4"
|
||||
|
|
|
|||
|
|
@ -15,8 +15,9 @@ php-literal-parser = "0.6.2"
|
|||
miette = "7.4.0"
|
||||
urlencoding = "2.1.3"
|
||||
form_urlencoded = "1.2.1"
|
||||
itertools = "0.14.0"
|
||||
|
||||
[dev-dependencies]
|
||||
miette = { version = "7.4.0", features = ["fancy"] }
|
||||
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"
|
||||
|
|
|
|||
97
src/lib.rs
97
src/lib.rs
|
|
@ -1,6 +1,7 @@
|
|||
mod nc;
|
||||
|
||||
use form_urlencoded::Serializer;
|
||||
use itertools::Either;
|
||||
use miette::Diagnostic;
|
||||
use std::iter::once;
|
||||
use std::path::PathBuf;
|
||||
|
|
@ -19,50 +20,42 @@ pub struct Config {
|
|||
#[derive(Debug)]
|
||||
pub enum RedisConfig {
|
||||
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)]
|
||||
pub enum RedisConnectionAddr {
|
||||
Tcp {
|
||||
host: String,
|
||||
port: u16,
|
||||
},
|
||||
TcpTls {
|
||||
host: String,
|
||||
port: u16,
|
||||
tls_params: Option<RedisTlsParams>,
|
||||
},
|
||||
Unix {
|
||||
path: PathBuf,
|
||||
},
|
||||
Tcp { host: String, port: u16 },
|
||||
Unix { path: PathBuf },
|
||||
}
|
||||
|
||||
impl RedisConnectionAddr {
|
||||
pub fn with_tls(self, tls_params: RedisTlsParams) -> Self {
|
||||
match self {
|
||||
RedisConnectionAddr::Tcp { host, port }
|
||||
| RedisConnectionAddr::TcpTls { host, port, .. } => RedisConnectionAddr::TcpTls {
|
||||
host,
|
||||
port,
|
||||
tls_params: Some(tls_params),
|
||||
},
|
||||
unix => unix,
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RedisClusterConnectionInfo {
|
||||
pub addr: Vec<RedisConnectionAddr>,
|
||||
pub db: i64,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub tls_params: Option<RedisTlsParams>,
|
||||
}
|
||||
|
||||
pub fn with_tls_opt(self, tls_params: Option<RedisTlsParams>) -> Self {
|
||||
if let Some(params) = tls_params {
|
||||
self.with_tls(params)
|
||||
} else {
|
||||
match self {
|
||||
RedisConnectionAddr::Tcp { host, port }
|
||||
| RedisConnectionAddr::TcpTls { host, port, .. } => {
|
||||
RedisConnectionAddr::Tcp { host, port }
|
||||
}
|
||||
unix => unix,
|
||||
}
|
||||
}
|
||||
impl RedisClusterConnectionInfo {
|
||||
pub fn iter(&self) -> impl Iterator<Item = RedisConnectionInfo> + '_ {
|
||||
self.addr.iter().cloned().map(|addr| RedisConnectionInfo {
|
||||
addr,
|
||||
db: self.db,
|
||||
username: self.username.clone(),
|
||||
password: self.password.clone(),
|
||||
tls_params: self.tls_params.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,9 +65,10 @@ pub struct RedisConnectionInfo {
|
|||
pub db: i64,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub tls_params: Option<RedisTlsParams>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct RedisTlsParams {
|
||||
pub local_cert: Option<PathBuf>,
|
||||
pub local_pk: Option<PathBuf>,
|
||||
|
|
@ -85,44 +79,37 @@ pub struct RedisTlsParams {
|
|||
|
||||
impl RedisConfig {
|
||||
pub fn addr(&self) -> impl Iterator<Item = &RedisConnectionAddr> {
|
||||
let boxed: Box<dyn Iterator<Item = &RedisConnectionAddr>> = match self {
|
||||
RedisConfig::Single(conn) => Box::new(once(&conn.addr)),
|
||||
RedisConfig::Cluster(conns) => Box::new(conns.iter().map(|conn| &conn.addr)),
|
||||
};
|
||||
boxed
|
||||
match self {
|
||||
RedisConfig::Single(conn) => Either::Left(once(&conn.addr)),
|
||||
RedisConfig::Cluster(cluster) => Either::Right(cluster.addr.iter()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn db(&self) -> i64 {
|
||||
match self {
|
||||
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> {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => conn.username.as_deref(),
|
||||
RedisConfig::Cluster(conns) => conns
|
||||
.first()
|
||||
.map(|conn| conn.username.as_deref())
|
||||
.unwrap_or_default(),
|
||||
RedisConfig::Cluster(cluster) => cluster.username.as_deref(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn passwd(&self) -> Option<&str> {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => conn.password.as_deref(),
|
||||
RedisConfig::Cluster(conns) => conns
|
||||
.first()
|
||||
.map(|conn| conn.password.as_deref())
|
||||
.unwrap_or_default(),
|
||||
RedisConfig::Cluster(cluster) => cluster.password.as_deref(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_vec(self) -> Vec<RedisConnectionInfo> {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
RedisConfig::Single(conn) => vec![conn],
|
||||
RedisConfig::Cluster(vec) => vec,
|
||||
RedisConfig::Single(_) => false,
|
||||
RedisConfig::Cluster(cluster) => cluster.addr.is_empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
src/nc.rs
26
src/nc.rs
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
Config, Database, DbConnect, DbError, Error, NotAConfigError, PhpParseError,
|
||||
RedisConnectionInfo, RedisTlsParams, Result, SslOptions,
|
||||
RedisClusterConnectionInfo, RedisConnectionInfo, RedisTlsParams, Result, SslOptions,
|
||||
};
|
||||
use crate::{RedisConfig, RedisConnectionAddr};
|
||||
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_pk: ssl_options["local_pk"].as_str().map(From::from),
|
||||
ca_file: ssl_options["cafile"].as_str().map(From::from),
|
||||
accept_invalid_hostname: ssl_options["allow_self_signed"] == true
|
||||
|| ssl_options["verify_peer_name"] == false,
|
||||
accept_invalid_hostname: ssl_options["verify_peer_name"] == false,
|
||||
insecure: ssl_options["verify_peer "] == false,
|
||||
})
|
||||
} else {
|
||||
|
|
@ -363,22 +362,19 @@ fn parse_redis_options(parsed: &Value) -> RedisConfig {
|
|||
|
||||
match address {
|
||||
RedisAddress::Single(addr) => RedisConfig::Single(RedisConnectionInfo {
|
||||
addr: addr.with_tls_opt(tls_params),
|
||||
addr,
|
||||
db,
|
||||
username,
|
||||
password,
|
||||
tls_params,
|
||||
}),
|
||||
RedisAddress::Cluster(addr) => RedisConfig::Cluster(RedisClusterConnectionInfo {
|
||||
addr,
|
||||
db,
|
||||
username,
|
||||
password,
|
||||
tls_params,
|
||||
}),
|
||||
RedisAddress::Cluster(addresses) => RedisConfig::Cluster(
|
||||
addresses
|
||||
.into_iter()
|
||||
.map(|addr| RedisConnectionInfo {
|
||||
addr: addr.with_tls_opt(tls_params.clone()),
|
||||
db,
|
||||
username: username.clone(),
|
||||
password: password.clone(),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,12 +24,9 @@ fn config_from_file(path: &str) -> Config {
|
|||
fn parse_redis(cfg: &str) -> RedisConnectionInfo {
|
||||
let redis = ConnectionInfo::from_str(cfg).unwrap();
|
||||
let addr = match redis.addr {
|
||||
ConnectionAddr::Tcp(host, port) => RedisConnectionAddr::Tcp { host, port },
|
||||
ConnectionAddr::TcpTls { host, port, .. } => RedisConnectionAddr::TcpTls {
|
||||
host,
|
||||
port,
|
||||
tls_params: None,
|
||||
},
|
||||
ConnectionAddr::Tcp(host, port) | ConnectionAddr::TcpTls { host, port, .. } => {
|
||||
RedisConnectionAddr::Tcp { host, port }
|
||||
}
|
||||
ConnectionAddr::Unix(path) => RedisConnectionAddr::Unix { path },
|
||||
};
|
||||
RedisConnectionInfo {
|
||||
|
|
@ -37,6 +34,7 @@ fn parse_redis(cfg: &str) -> RedisConnectionInfo {
|
|||
db: redis.redis.db,
|
||||
username: redis.redis.username,
|
||||
password: redis.redis.password,
|
||||
tls_params: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -108,20 +106,20 @@ fn test_parse_redis_tls() {
|
|||
let config = config_from_file("tests/configs/redis_tls.php");
|
||||
assert_debug_equal(
|
||||
RedisConfig::Single(RedisConnectionInfo {
|
||||
addr: RedisConnectionAddr::TcpTls {
|
||||
addr: RedisConnectionAddr::Tcp {
|
||||
host: "127.0.0.1".into(),
|
||||
port: 6379,
|
||||
tls_params: Some(RedisTlsParams {
|
||||
local_cert: Some("/certs/redis.crt".into()),
|
||||
local_pk: Some("/certs/redis.key".into()),
|
||||
ca_file: Some("/certs/ca.crt".into()),
|
||||
insecure: false,
|
||||
accept_invalid_hostname: false,
|
||||
}),
|
||||
},
|
||||
db: 0,
|
||||
username: None,
|
||||
password: None,
|
||||
tls_params: Some(RedisTlsParams {
|
||||
local_cert: Some("/certs/redis.crt".into()),
|
||||
local_pk: Some("/certs/redis.key".into()),
|
||||
ca_file: Some("/certs/ca.crt".into()),
|
||||
insecure: false,
|
||||
accept_invalid_hostname: false,
|
||||
}),
|
||||
}),
|
||||
config.redis,
|
||||
);
|
||||
|
|
@ -301,18 +299,18 @@ fn test_parse_postgres_socket_folder() {
|
|||
#[test]
|
||||
fn test_parse_redis_cluster() {
|
||||
let config = config_from_file("tests/configs/redis.cluster.php");
|
||||
let mut conns = config.redis.into_vec();
|
||||
conns.sort_by(|a, b| format!("{:?}", a.addr).cmp(&format!("{:?}", b.addr)));
|
||||
let mut addresses = config.redis.addr().cloned().collect::<Vec<_>>();
|
||||
addresses.sort_by(|a, b| format!("{:?}", a).cmp(&format!("{:?}", b)));
|
||||
assert_debug_equal(
|
||||
vec![
|
||||
parse_redis("redis://:xxx@db1:6380"),
|
||||
parse_redis("redis://:xxx@db1:6381"),
|
||||
parse_redis("redis://:xxx@db1:6382"),
|
||||
parse_redis("redis://:xxx@db2:6380"),
|
||||
parse_redis("redis://:xxx@db2:6381"),
|
||||
parse_redis("redis://:xxx@db2:6382"),
|
||||
parse_redis("redis://:xxx@db1:6380").addr,
|
||||
parse_redis("redis://:xxx@db1:6381").addr,
|
||||
parse_redis("redis://:xxx@db1:6382").addr,
|
||||
parse_redis("redis://:xxx@db2:6380").addr,
|
||||
parse_redis("redis://:xxx@db2:6381").addr,
|
||||
parse_redis("redis://:xxx@db2:6382").addr,
|
||||
],
|
||||
conns,
|
||||
addresses,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue