Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Dec 31, 2020
1 parent 652c78b commit 74db555
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
4 changes: 2 additions & 2 deletions relay/sources/relayd/src/api/shared_files.rs
Expand Up @@ -147,7 +147,7 @@ pub async fn put(

if job_config.nodes.read().await.is_subnode(&file.target_id) {
put_local(file, params, job_config, body).await
} else if job_config.cfg.general.node_id == "root" {
} else if job_config.nodes.read().await.my_id() == "root" {
Err(RudderError::UnknownNode(file.target_id).into())
} else {
put_forward(file, params, job_config, body).await
Expand Down Expand Up @@ -292,7 +292,7 @@ pub async fn head(

if job_config.nodes.read().await.is_subnode(&file.target_id) {
head_local(file, params, job_config).await
} else if job_config.cfg.general.node_id == "root" {
} else if job_config.nodes.read().await.my_id() == "root" {
Err(RudderError::UnknownNode(file.target_id).into())
} else {
head_forward(file, params, job_config).await
Expand Down
58 changes: 40 additions & 18 deletions relay/sources/relayd/src/configuration/main.rs
Expand Up @@ -17,6 +17,7 @@ use std::{
time::Duration,
};
use tracing::debug;
use tracing::warn;

pub type BaseDirectory = PathBuf;
pub type WatchedDirectory = PathBuf;
Expand Down Expand Up @@ -60,7 +61,7 @@ where
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
// Default can be implemented in serde using the Default trait
pub struct Configuration {
// general section is mandatory
#[serde(default)]
pub general: GeneralConfig,
#[serde(default)]
pub processing: ProcessingConfig,
Expand All @@ -82,6 +83,18 @@ impl Configuration {
}
res
}

/// Read current node_id, and handle override by node_id
/// Can be removed once node_id is removed
pub fn read_node_id(&self) -> Result<NodeId, Error> {
Ok(match &self.general.node_id {
Some(id) => {
warn!("node_id setting is deprecated, use node_id_file instead");
id.clone()
}
None => read_to_string(&self.general.node_id_file)?,
})
}
}

impl FromStr for Configuration {
Expand All @@ -103,8 +116,11 @@ pub struct GeneralConfig {
pub nodes_list_file: NodesListFile,
#[serde(default = "GeneralConfig::default_nodes_certs_file")]
pub nodes_certs_file: NodesCertsFile,
/// No possible sane default value
pub node_id: NodeId,
#[serde(default = "GeneralConfig::default_node_id_file")]
node_id_file: PathBuf,
/// DEPRECATED: Has priority over node_id_file, use the
/// `Configuration::node_id()` method to get correct value
node_id: Option<NodeId>,
#[serde(default = "GeneralConfig::default_listen")]
pub listen: String,
/// None means using the number of available CPUs
Expand All @@ -129,6 +145,10 @@ impl GeneralConfig {
fn default_listen() -> String {
"127.0.0.1:3030".to_string()
}

fn default_node_id_file() -> PathBuf {
PathBuf::from("/opt/rudder/etc/uuid.hive")
}
}

#[derive(Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
Expand Down Expand Up @@ -456,32 +476,32 @@ mod tests {
use super::*;

#[test]
fn it_fails_with_empty_config() {
let empty = "";
let config = empty.parse::<Configuration>();
assert!(config.is_err());
fn it_parses_deprecated_node_id() {
let default = "[general]\n\
node_id = \"test\"\n";
let config = default.parse::<Configuration>().unwrap();
assert_eq!(config.read_node_id().unwrap(), "test".to_string());
}

#[test]
fn it_parses_listen_with_hostname() {
let default = "[general]\n\
node_id = \"root\"\n\
listen = \"relayd:3030\"";
let config = default.parse::<Configuration>().unwrap();
dbg!(&config);
let _ = default.parse::<Configuration>().unwrap();
}

#[test]
fn it_parses_main_configuration_with_defaults() {
let default = "[general]\n\
node_id = \"root\"";
let config = default.parse::<Configuration>();
fn it_parses_empty_main_configuration() {
let empty = "";
let config = empty.parse::<Configuration>().unwrap();

let reference = Configuration {
general: GeneralConfig {
nodes_list_file: PathBuf::from("/var/rudder/lib/relay/nodeslist.json"),
nodes_certs_file: PathBuf::from("/var/rudder/lib/ssl/allnodescerts.pem"),
node_id: "root".to_string(),
node_id: None,
node_id_file: PathBuf::from("/opt/rudder/etc/uuid.hive"),
listen: "127.0.0.1:3030".parse().unwrap(),
core_threads: None,
max_threads: None,
Expand Down Expand Up @@ -540,7 +560,7 @@ mod tests {
},
};

assert_eq!(config.unwrap(), reference);
assert_eq!(config, reference);
}

#[test]
Expand All @@ -563,13 +583,14 @@ mod tests {

#[test]
fn it_parses_main_configuration() {
let config = Configuration::new("tests/files/config/");
let config = Configuration::new("tests/files/config/").unwrap();

let reference = Configuration {
general: GeneralConfig {
nodes_list_file: PathBuf::from("tests/files/nodeslist.json"),
nodes_certs_file: PathBuf::from("tests/files/keys/nodescerts.pem"),
node_id: "root".to_string(),
node_id: None,
node_id_file: PathBuf::from("tests/files/config/uuid.hive"),
listen: "127.0.0.1:3030".parse().unwrap(),
core_threads: None,
max_threads: Some(512),
Expand Down Expand Up @@ -627,6 +648,7 @@ mod tests {
path: PathBuf::from("tests/api_shared_folder"),
},
};
assert_eq!(config.unwrap(), reference);
assert_eq!(config, reference);
assert_eq!(config.read_node_id().unwrap(), "root".to_string());
}
}
4 changes: 4 additions & 0 deletions relay/sources/relayd/src/data/node.rs
Expand Up @@ -172,6 +172,10 @@ impl NodesList {
.and_then(|node| node.certificates.as_ref())
}

pub fn my_id(&self) -> &str {
&self.my_id
}

fn id_from_cert(cert: &X509) -> Result<NodeId, Error> {
Ok(cert
.subject_name()
Expand Down
7 changes: 5 additions & 2 deletions relay/sources/relayd/src/lib.rs
Expand Up @@ -22,13 +22,15 @@ use crate::{
logging::LogConfig,
main::{Configuration, InventoryOutputSelect, OutputSelect, ReportingOutputSelect},
},
data::node::NodeId,
data::node::NodesList,
metrics::{MANAGED_NODES, SUB_NODES},
output::database::{pg_pool, PgPool},
processing::{inventory, reporting},
};
use anyhow::Error;
use reqwest::Client;
use std::fs::read_to_string;
use std::{fs::create_dir_all, path::Path, process::exit, string::ToString, sync::Arc};
use structopt::clap::crate_version;
use tokio::{
Expand Down Expand Up @@ -255,7 +257,7 @@ impl JobConfig {
.build()?;

let nodes = RwLock::new(NodesList::new(
cfg.general.node_id.to_string(),
cfg.read_node_id()?,
&cfg.general.nodes_list_file,
Some(&cfg.general.nodes_certs_file),
)?);
Expand All @@ -273,7 +275,8 @@ impl JobConfig {
async fn reload_nodeslist(&self) -> Result<(), Error> {
let mut nodes = self.nodes.write().await;
*nodes = NodesList::new(
self.cfg.general.node_id.to_string(),
// Reload node_id too
self.cfg.read_node_id()?,
&self.cfg.general.nodes_list_file,
Some(&self.cfg.general.nodes_certs_file),
)?;
Expand Down
3 changes: 2 additions & 1 deletion relay/sources/relayd/tests/files/config/main.conf
@@ -1,7 +1,8 @@
[general]
nodes_list_file = "tests/files/nodeslist.json"
nodes_certs_file = "tests/files/keys/nodescerts.pem"
node_id = "root"
node_id_file = "tests/files/config/uuid.hive"

listen = "127.0.0.1:3030"

# By default, the number of CPUs
Expand Down
3 changes: 2 additions & 1 deletion relay/sources/relayd/tools/config/main.conf
Expand Up @@ -5,7 +5,8 @@

nodes_list_file = "/var/rudder/lib/relay/nodeslist.json"
nodes_certs_file = "/var/rudder/lib/ssl/nodescerts.pem"
node_id = "UNKNOWN"
node_id_file = "/opt/rudder/etc/uuid.hive"

listen = "127.0.0.1:3030"

# By default, the number of CPUs
Expand Down

0 comments on commit 74db555

Please sign in to comment.