-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #23627: Add configuration file parser to rudder-package
- Loading branch information
Showing
7 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use std::{fs::read_to_string, path::Path}; | ||
|
||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
const PUBLIC_REPO_URL: &str = "https://repository.rudder.io/plugins"; | ||
const PRIVATE_REPO_URL: &str = "https://download.rudder.io/plugins"; | ||
|
||
/// Wrapper as the default config has a "Rudder" section | ||
#[derive(Deserialize, Debug, PartialEq, Eq)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct RawConfiguration { | ||
#[serde(default)] | ||
rudder: RudderSection, | ||
} | ||
|
||
// Note, "key = " lines produce Some("") when using Option | ||
// So let's use String everywhere and clean afterwards. | ||
#[derive(Deserialize, Debug, PartialEq, Eq, Default)] | ||
struct RudderSection { | ||
#[serde(default)] | ||
url: String, | ||
#[serde(default)] | ||
username: String, | ||
#[serde(default)] | ||
password: String, | ||
#[serde(default)] | ||
proxy_url: String, | ||
#[serde(default)] | ||
proxy_user: String, | ||
#[serde(default)] | ||
proxy_password: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct Credentials { | ||
username: String, | ||
password: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct Configuration { | ||
url: String, | ||
credentials: Option<Credentials>, | ||
proxy: Option<ProxyConfiguration>, | ||
} | ||
|
||
impl Configuration { | ||
fn parse(src: &str) -> Result<Self> { | ||
let parsed: RawConfiguration = serde_ini::from_str(src)?; | ||
Ok(Configuration::from(parsed)) | ||
} | ||
|
||
pub fn read(path: &Path) -> Result<Self> { | ||
let c = read_to_string(path)?; | ||
Self::parse(&c) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct ProxyConfiguration { | ||
url: String, | ||
credentials: Option<Credentials>, | ||
} | ||
|
||
impl From<RawConfiguration> for Configuration { | ||
fn from(raw: RawConfiguration) -> Self { | ||
let r = raw.rudder; | ||
let credentials = match (r.username.is_empty(), r.password.is_empty()) { | ||
(false, false) => Some(Credentials { | ||
username: r.username, | ||
password: r.password, | ||
}), | ||
_ => None, | ||
}; | ||
let proxy_credentials = match (r.proxy_user.is_empty(), r.proxy_password.is_empty()) { | ||
(false, false) => Some(Credentials { | ||
username: r.proxy_user, | ||
password: r.proxy_password, | ||
}), | ||
_ => None, | ||
}; | ||
let proxy = match (r.proxy_url.is_empty(), proxy_credentials) { | ||
(false, credentials) => Some(ProxyConfiguration { | ||
url: r.proxy_url, | ||
credentials, | ||
}), | ||
_ => None, | ||
}; | ||
let url = if r.url.is_empty() { | ||
if credentials.is_some() { | ||
PRIVATE_REPO_URL.to_owned() | ||
} else { | ||
PUBLIC_REPO_URL.to_owned() | ||
} | ||
} else { | ||
r.url | ||
}; | ||
Self { | ||
url, | ||
credentials, | ||
proxy, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::rpkg::config::{Configuration, Credentials, ProxyConfiguration}; | ||
use pretty_assertions::assert_eq; | ||
use std::path::Path; | ||
|
||
#[test] | ||
fn it_parses_default_config_file() { | ||
let reference = Configuration { | ||
url: "https://download.rudder.io/plugins".to_string(), | ||
credentials: Some(Credentials { | ||
username: "user".to_string(), | ||
password: "password".to_string(), | ||
}), | ||
proxy: None, | ||
}; | ||
let conf = Configuration::read(Path::new("./tests/rudder-pkg.conf")).unwrap(); | ||
assert_eq!(reference, conf); | ||
} | ||
#[test] | ||
fn it_parses_empty_config_file() { | ||
let reference = Configuration { | ||
url: "https://repository.rudder.io/plugins".to_string(), | ||
credentials: None, | ||
proxy: None, | ||
}; | ||
let conf = Configuration::parse("").unwrap(); | ||
assert_eq!(reference, conf); | ||
} | ||
#[test] | ||
fn it_parses_full_config_file() { | ||
let reference = Configuration { | ||
url: "https://download2.rudder.io/plugins".to_string(), | ||
credentials: Some(Credentials { | ||
username: "user".to_string(), | ||
password: "password".to_string(), | ||
}), | ||
proxy: Some(ProxyConfiguration { | ||
url: "http://22.29.35.56".to_string(), | ||
credentials: Some(Credentials { | ||
username: "mario".to_string(), | ||
password: "daisy".to_string(), | ||
}), | ||
}), | ||
}; | ||
let conf = Configuration::read(Path::new("./tests/rudder-pkg.proxy.conf")).unwrap(); | ||
assert_eq!(reference, conf); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
pub mod database; | ||
pub use database::Database; | ||
pub mod archive; | ||
pub mod config; | ||
pub mod plugin; | ||
pub mod repo_index; | ||
pub mod webapp_xml; | ||
|
||
const PACKAGES_FOLDER: &str = "/var/rudder/packagesA"; | ||
const PACKAGES_FOLDER: &str = "/var/rudder/packages"; | ||
const WEBAPP_XML_PATH: &str = "/opt/rudder/share/webapps/rudder.xml"; | ||
const PACKAGES_DATABASE_PATH: &str = "/var/rudder/packages/index.json"; | ||
const CONFIG_PATH: &str = "/opt/rudder/etc/rudder-pkg/rudder-pkg.conf"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Rudder] | ||
url = https://download.rudder.io/plugins | ||
username = user | ||
password = password | ||
proxy_url = | ||
proxy_user = | ||
proxy_password = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Rudder] | ||
url = https://download2.rudder.io/plugins | ||
username = user | ||
password = password | ||
proxy_url = http://22.29.35.56 | ||
proxy_user = mario | ||
proxy_password = daisy |