mirror of
https://codeberg.org/icewind/nextcloud-config-parser.git
synced 2026-06-03 16:44:09 +02:00
also disable ssl when connecting to pgsql using ip
This commit is contained in:
parent
87dd4c570b
commit
26a212cb9b
5 changed files with 131 additions and 27 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "nextcloud-config-parser"
|
name = "nextcloud-config-parser"
|
||||||
description = "Rust parser for nextcloud config files"
|
description = "Rust parser for nextcloud config files"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,7 @@ pub enum Database {
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
connect: DbConnect,
|
connect: DbConnect,
|
||||||
|
disable_ssl: bool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,7 +157,7 @@ impl From<Database> for sqlx::any::AnyConnectOptions {
|
||||||
fn from(cfg: Database) -> Self {
|
fn from(cfg: Database) -> Self {
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
mysql::{MySqlConnectOptions, MySqlSslMode},
|
mysql::{MySqlConnectOptions, MySqlSslMode},
|
||||||
postgres::PgConnectOptions,
|
postgres::{PgConnectOptions, PgSslMode},
|
||||||
sqlite::SqliteConnectOptions,
|
sqlite::SqliteConnectOptions,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -193,11 +194,15 @@ impl From<Database> for sqlx::any::AnyConnectOptions {
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
connect,
|
connect,
|
||||||
|
disable_ssl,
|
||||||
} => {
|
} => {
|
||||||
let mut options = PgConnectOptions::default()
|
let mut options = PgConnectOptions::default()
|
||||||
.database(&database)
|
.database(&database)
|
||||||
.username(&username)
|
.username(&username)
|
||||||
.password(&password);
|
.password(&password);
|
||||||
|
if disable_ssl {
|
||||||
|
options = options.ssl_mode(PgSslMode::Disable);
|
||||||
|
}
|
||||||
match connect {
|
match connect {
|
||||||
DbConnect::Socket(socket) => {
|
DbConnect::Socket(socket) => {
|
||||||
options = options.socket(socket);
|
options = options.socket(socket);
|
||||||
|
|
|
||||||
117
src/nc.rs
117
src/nc.rs
|
|
@ -173,32 +173,39 @@ fn parse_db_options(parsed: &Value) -> Result<Database> {
|
||||||
Some("pgsql") => {
|
Some("pgsql") => {
|
||||||
let username = parsed["dbuser"].as_str().ok_or(DbError::NoUsername)?;
|
let username = parsed["dbuser"].as_str().ok_or(DbError::NoUsername)?;
|
||||||
let password = parsed["dbpassword"].as_str().ok_or(DbError::NoPassword)?;
|
let password = parsed["dbpassword"].as_str().ok_or(DbError::NoPassword)?;
|
||||||
let mut connect = match split_host(parsed["dbhost"].as_str().unwrap_or_default()) {
|
let (mut connect, disable_ssl) =
|
||||||
(addr, None, None) => DbConnect::Tcp {
|
match split_host(parsed["dbhost"].as_str().unwrap_or_default()) {
|
||||||
host: addr.into(),
|
(addr, None, None) => (
|
||||||
port: 5432,
|
DbConnect::Tcp {
|
||||||
},
|
host: addr.into(),
|
||||||
(addr, Some(port), None) => DbConnect::Tcp {
|
port: 5432,
|
||||||
host: addr.into(),
|
},
|
||||||
port,
|
IpAddr::from_str(addr).is_ok(),
|
||||||
},
|
),
|
||||||
(_, None, Some(socket)) => {
|
(addr, Some(port), None) => (
|
||||||
let mut socket_path = Path::new(socket);
|
DbConnect::Tcp {
|
||||||
|
host: addr.into(),
|
||||||
|
port,
|
||||||
|
},
|
||||||
|
IpAddr::from_str(addr).is_ok(),
|
||||||
|
),
|
||||||
|
(_, None, Some(socket)) => {
|
||||||
|
let mut socket_path = Path::new(socket);
|
||||||
|
|
||||||
// sqlx wants the folder the socket is in, not the socket itself
|
// sqlx wants the folder the socket is in, not the socket itself
|
||||||
if socket_path
|
if socket_path
|
||||||
.file_name()
|
.file_name()
|
||||||
.map(|name| name.to_str().unwrap().starts_with(".s"))
|
.map(|name| name.to_str().unwrap().starts_with(".s"))
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
socket_path = socket_path.parent().unwrap();
|
socket_path = socket_path.parent().unwrap();
|
||||||
|
}
|
||||||
|
(DbConnect::Socket(socket_path.into()), false)
|
||||||
}
|
}
|
||||||
DbConnect::Socket(socket_path.into())
|
(_, Some(_), Some(_)) => {
|
||||||
}
|
unreachable!()
|
||||||
(_, Some(_), Some(_)) => {
|
}
|
||||||
unreachable!()
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(port) = parsed["dbport"].clone().into_int() {
|
if let Some(port) = parsed["dbport"].clone().into_int() {
|
||||||
if let DbConnect::Tcp {
|
if let DbConnect::Tcp {
|
||||||
port: connect_port, ..
|
port: connect_port, ..
|
||||||
|
|
@ -214,6 +221,7 @@ fn parse_db_options(parsed: &Value) -> Result<Database> {
|
||||||
username: username.into(),
|
username: username.into(),
|
||||||
password: password.into(),
|
password: password.into(),
|
||||||
connect,
|
connect,
|
||||||
|
disable_ssl,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Some("sqlite3") => {
|
Some("sqlite3") => {
|
||||||
|
|
@ -474,6 +482,7 @@ fn test_parse_postgres_socket() {
|
||||||
username: "redacted".to_string(),
|
username: "redacted".to_string(),
|
||||||
password: "redacted".to_string(),
|
password: "redacted".to_string(),
|
||||||
connect: DbConnect::Socket("/var/run/postgresql".into()),
|
connect: DbConnect::Socket("/var/run/postgresql".into()),
|
||||||
|
disable_ssl: false,
|
||||||
},
|
},
|
||||||
&config.database,
|
&config.database,
|
||||||
);
|
);
|
||||||
|
|
@ -499,6 +508,7 @@ fn test_parse_postgres_socket_folder() {
|
||||||
username: "redacted".to_string(),
|
username: "redacted".to_string(),
|
||||||
password: "redacted".to_string(),
|
password: "redacted".to_string(),
|
||||||
connect: DbConnect::Socket("/var/run/postgresql".into()),
|
connect: DbConnect::Socket("/var/run/postgresql".into()),
|
||||||
|
disable_ssl: false,
|
||||||
},
|
},
|
||||||
&config.database,
|
&config.database,
|
||||||
);
|
);
|
||||||
|
|
@ -592,3 +602,62 @@ fn test_parse_config_mysql_fqdn() {
|
||||||
config.database.into(),
|
config.database.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_postgres_ip() {
|
||||||
|
let config = config_from_file("tests/configs/postgres_ip.php");
|
||||||
|
assert_debug_equal(
|
||||||
|
&Database::Postgres {
|
||||||
|
database: "nextcloud".to_string(),
|
||||||
|
username: "redacted".to_string(),
|
||||||
|
password: "redacted".to_string(),
|
||||||
|
connect: DbConnect::Tcp {
|
||||||
|
host: "1.2.3.4".to_string(),
|
||||||
|
port: 5432,
|
||||||
|
},
|
||||||
|
disable_ssl: true,
|
||||||
|
},
|
||||||
|
&config.database,
|
||||||
|
);
|
||||||
|
#[cfg(feature = "db-sqlx")]
|
||||||
|
assert_debug_equal(
|
||||||
|
AnyConnectOptions::from(
|
||||||
|
PgConnectOptions::new()
|
||||||
|
.host("1.2.3.4")
|
||||||
|
.username("redacted")
|
||||||
|
.password("redacted")
|
||||||
|
.database("nextcloud")
|
||||||
|
.ssl_mode(sqlx::postgres::PgSslMode::Disable),
|
||||||
|
),
|
||||||
|
config.database.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_postgres_fqdn() {
|
||||||
|
let config = config_from_file("tests/configs/postgres_fqdn.php");
|
||||||
|
assert_debug_equal(
|
||||||
|
&Database::Postgres {
|
||||||
|
database: "nextcloud".to_string(),
|
||||||
|
username: "redacted".to_string(),
|
||||||
|
password: "redacted".to_string(),
|
||||||
|
connect: DbConnect::Tcp {
|
||||||
|
host: "pg.example.com".to_string(),
|
||||||
|
port: 5432,
|
||||||
|
},
|
||||||
|
disable_ssl: false,
|
||||||
|
},
|
||||||
|
&config.database,
|
||||||
|
);
|
||||||
|
#[cfg(feature = "db-sqlx")]
|
||||||
|
assert_debug_equal(
|
||||||
|
AnyConnectOptions::from(
|
||||||
|
PgConnectOptions::new()
|
||||||
|
.host("pg.example.com")
|
||||||
|
.username("redacted")
|
||||||
|
.password("redacted")
|
||||||
|
.database("nextcloud"),
|
||||||
|
),
|
||||||
|
config.database.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
15
tests/configs/postgres_fqdn.php
Normal file
15
tests/configs/postgres_fqdn.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$CONFIG = [
|
||||||
|
'overwrite.cli.url' => 'https://cloud.example.com',
|
||||||
|
'dbtype' => 'pgsql',
|
||||||
|
'dbname' => 'nextcloud',
|
||||||
|
'dbhost' => 'pg.example.com',
|
||||||
|
'dbport' => '',
|
||||||
|
'dbtableprefix' => 'oc_',
|
||||||
|
'dbuser' => 'redacted',
|
||||||
|
'dbpassword' => 'redacted',
|
||||||
|
'redis' => [
|
||||||
|
'host' => 'localhost'
|
||||||
|
]
|
||||||
|
];
|
||||||
15
tests/configs/postgres_ip.php
Normal file
15
tests/configs/postgres_ip.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$CONFIG = [
|
||||||
|
'overwrite.cli.url' => 'https://cloud.example.com',
|
||||||
|
'dbtype' => 'pgsql',
|
||||||
|
'dbname' => 'nextcloud',
|
||||||
|
'dbhost' => '1.2.3.4',
|
||||||
|
'dbport' => '',
|
||||||
|
'dbtableprefix' => 'oc_',
|
||||||
|
'dbuser' => 'redacted',
|
||||||
|
'dbpassword' => 'redacted',
|
||||||
|
'redis' => [
|
||||||
|
'host' => 'localhost'
|
||||||
|
]
|
||||||
|
];
|
||||||
Loading…
Add table
Add a link
Reference in a new issue