Skip to content

Commit

Permalink
Fixes #23627: Add configuration file parser to rudder-package
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Oct 23, 2023
1 parent a4912ac commit 8bf4956
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 4 deletions.
15 changes: 12 additions & 3 deletions relay/sources/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ target/man/rudder-relayd.1.gz:

CACHE=cache() { [ -x ../../../../../build-caching ] && ../../../../../build-caching "$$@"; [ -x ../../../../../../build-caching ] && ../../../../../../build-caching "$$@" ; } ; cache
build: man rudder-pkg/rudder_plugins_key.pub autocomplete/rudder-pkg.sh
# Build relayd
mkdir -p ../../../../../.cargo/ relayd/target/
# Build rust binaries
mkdir -p ../../../../../.cargo/ relayd/target/ rudder-package/target/
$(CACHE) get ../../../../../.cargo/ name=cargo major=$(RUDDER_MAJOR_VERSION) || true
### relayd
$(CACHE) get relayd/target/ --with-env name=relayd major=$(RUDDER_MAJOR_VERSION) || true
cd relayd && make build
# the cache is always updated and cargo will rebuild only what is needed
$(CACHE) put ../../../../../.cargo/ name=cargo major=$(RUDDER_MAJOR_VERSION) || true
$(CACHE) put relayd/target/ --with-env name=relayd major=$(RUDDER_MAJOR_VERSION) || true
### rudder-package
$(CACHE) get rudder-package/target/ --with-env name=rudder-package major=$(RUDDER_MAJOR_VERSION) || true
cd rudder-package && make build
# the cache is always updated and cargo will rebuild only what is needed
$(CACHE) put rudder-package/target/ --with-env name=rudder-package major=$(RUDDER_MAJOR_VERSION) || true
### common
$(CACHE) put ../../../../../.cargo/ name=cargo major=$(RUDDER_MAJOR_VERSION) || true

ifeq ($(SELINUX),true)
# Build SELinux policy package
Expand Down Expand Up @@ -108,6 +115,8 @@ install: build
# Install man pages
install -m 644 target/man/rudder-relayd.1.gz $(DESTDIR)/opt/rudder/share/man/man1/

# rudder-package binary
install -m 755 rudder-package/target/release/rudder-package $(DESTDIR)/opt/rudder/bin/rudder-package
# rudder packaging
install -m 755 rudder-pkg/rudder-pkg $(DESTDIR)/opt/rudder/share/commands/package
ln -ns ../share/commands/package $(DESTDIR)/opt/rudder/bin/rudder-pkg
Expand Down
24 changes: 24 additions & 0 deletions relay/sources/rudder-package/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relay/sources/rudder-package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lzma-rs = "0.3.0"
pretty_assertions = "1.4.0"
rstest = "0.18.2"
serde = { version = "1.0.189", features = ["derive"] }
serde_ini = "0.2.0"
serde_json = "1.0.107"
serde_toml = "0.0.1"
tar = "0.4.40"
Expand Down
155 changes: 155 additions & 0 deletions relay/sources/rudder-package/src/rpkg/config.rs
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);
}
}
4 changes: 3 additions & 1 deletion relay/sources/rudder-package/src/rpkg/mod.rs
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";
7 changes: 7 additions & 0 deletions relay/sources/rudder-package/tests/rudder-pkg.conf
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 =
7 changes: 7 additions & 0 deletions relay/sources/rudder-package/tests/rudder-pkg.proxy.conf
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

0 comments on commit 8bf4956

Please sign in to comment.