Skip to content

Commit

Permalink
feat(node_manager): pass beta encryption sk to the auditor
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed May 22, 2024
1 parent 0fba624 commit 5b99e0a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
12 changes: 9 additions & 3 deletions sn_node_manager/src/add_services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ pub struct AddNodeServiceOptions {

#[derive(Debug, PartialEq)]
pub struct InstallAuditorServiceCtxBuilder {
pub auditor_path: PathBuf,
pub beta_encryption_key: Option<String>,
pub bootstrap_peers: Vec<Multiaddr>,
pub env_variables: Option<Vec<(String, String)>>,
pub auditor_path: PathBuf,
pub log_dir_path: PathBuf,
pub name: String,
pub service_user: String,
Expand All @@ -175,6 +176,10 @@ impl InstallAuditorServiceCtxBuilder {
args.push(OsString::from("--peer"));
args.push(OsString::from(peers_str));
}
if let Some(beta_encryption_key) = self.beta_encryption_key {
args.push(OsString::from("--beta-encryption-key"));
args.push(OsString::from(beta_encryption_key));
}

Ok(ServiceInstallCtx {
label: self.name.parse()?,
Expand Down Expand Up @@ -232,10 +237,11 @@ impl InstallFaucetServiceCtxBuilder {
}

pub struct AddAuditorServiceOptions {
pub bootstrap_peers: Vec<Multiaddr>,
pub env_variables: Option<Vec<(String, String)>>,
pub auditor_install_bin_path: PathBuf,
pub auditor_src_bin_path: PathBuf,
pub beta_encryption_key: Option<String>,
pub bootstrap_peers: Vec<Multiaddr>,
pub env_variables: Option<Vec<(String, String)>>,
pub service_log_dir_path: PathBuf,
pub user: String,
pub version: String,
Expand Down
3 changes: 2 additions & 1 deletion sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ pub fn add_auditor(
)?;

let install_ctx = InstallAuditorServiceCtxBuilder {
auditor_path: install_options.auditor_install_bin_path.clone(),
beta_encryption_key: install_options.beta_encryption_key.clone(),
bootstrap_peers: install_options.bootstrap_peers.clone(),
env_variables: install_options.env_variables.clone(),
auditor_path: install_options.auditor_install_bin_path.clone(),
log_dir_path: install_options.service_log_dir_path.clone(),
name: "auditor".to_string(),
service_user: install_options.user.clone(),
Expand Down
88 changes: 88 additions & 0 deletions sn_node_manager/src/add_services/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,7 @@ async fn add_auditor_should_add_a_auditor_service() -> Result<()> {
add_auditor(
AddAuditorServiceOptions {
bootstrap_peers: vec![],
beta_encryption_key: None,
env_variables: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
auditor_src_bin_path: auditor_download_path.to_path_buf(),
auditor_install_bin_path: auditor_install_path.to_path_buf(),
Expand Down Expand Up @@ -2455,6 +2456,7 @@ async fn add_auditor_should_return_an_error_if_a_auditor_service_was_already_cre
let result = add_auditor(
AddAuditorServiceOptions {
bootstrap_peers: vec![],
beta_encryption_key: None,
env_variables: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
auditor_src_bin_path: auditor_download_path.to_path_buf(),
auditor_install_bin_path: auditor_install_path.to_path_buf(),
Expand All @@ -2480,6 +2482,92 @@ async fn add_auditor_should_return_an_error_if_a_auditor_service_was_already_cre
Ok(())
}

#[tokio::test]
async fn add_auditor_should_include_beta_encryption_key_if_specified() -> Result<()> {
let tmp_data_dir = assert_fs::TempDir::new()?;
let node_reg_path = tmp_data_dir.child("node_reg.json");

let latest_version = "0.96.4";
let temp_dir = assert_fs::TempDir::new()?;
let auditor_logs_dir = temp_dir.child("logs");
auditor_logs_dir.create_dir_all()?;
let auditor_install_dir = temp_dir.child("install");
auditor_install_dir.create_dir_all()?;
let auditor_install_path = auditor_install_dir.child(AUDITOR_FILE_NAME);
let auditor_download_path = temp_dir.child(AUDITOR_FILE_NAME);
auditor_download_path.write_binary(b"fake auditor bin")?;

let mut node_registry = NodeRegistry {
bootstrap_peers: vec![],
daemon: None,
auditor: None,
faucet: None,
environment_variables: None,
nodes: vec![],
save_path: node_reg_path.to_path_buf(),
};

let mut mock_service_control = MockServiceControl::new();

mock_service_control
.expect_install()
.times(1)
.with(
eq(ServiceInstallCtx {
args: vec![
OsString::from("--log-output-dest"),
OsString::from(auditor_logs_dir.to_path_buf().as_os_str()),
OsString::from("--beta-encryption-key"),
OsString::from("test"),
],
contents: None,
environment: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
label: "auditor".parse()?,
program: auditor_install_path.to_path_buf(),
username: Some(get_username()),
working_directory: None,
}),
eq(false),
)
.returning(|_, _| Ok(()));

add_auditor(
AddAuditorServiceOptions {
bootstrap_peers: vec![],
beta_encryption_key: Some("test".to_string()),
env_variables: Some(vec![("SN_LOG".to_string(), "all".to_string())]),
auditor_src_bin_path: auditor_download_path.to_path_buf(),
auditor_install_bin_path: auditor_install_path.to_path_buf(),
service_log_dir_path: auditor_logs_dir.to_path_buf(),
user: get_username(),
version: latest_version.to_string(),
},
&mut node_registry,
&mock_service_control,
VerbosityLevel::Normal,
)?;

auditor_download_path.assert(predicate::path::missing());
auditor_install_path.assert(predicate::path::is_file());
auditor_logs_dir.assert(predicate::path::is_dir());

node_reg_path.assert(predicates::path::is_file());

let saved_auditor = node_registry.auditor.unwrap();
assert_eq!(
saved_auditor.auditor_path,
auditor_install_path.to_path_buf()
);
assert_eq!(saved_auditor.log_dir_path, auditor_logs_dir.to_path_buf());
assert!(saved_auditor.pid.is_none());
assert_eq!(saved_auditor.service_name, "auditor");
assert_eq!(saved_auditor.status, ServiceStatus::Added);
assert_eq!(saved_auditor.user, get_username());
assert_eq!(saved_auditor.version, latest_version);

Ok(())
}

#[tokio::test]
async fn add_faucet_should_add_a_faucet_service() -> Result<()> {
let tmp_data_dir = assert_fs::TempDir::new()?;
Expand Down
6 changes: 6 additions & 0 deletions sn_node_manager/src/bin/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ pub enum AuditorSubCmd {
/// This command must run as the root/administrative user.
#[clap(name = "add")]
Add {
/// Secret encryption key of the beta rewards to decypher
/// discord usernames of the beta participants
#[clap(short = 'k', long, value_name = "hex_secret_key")]
beta_encryption_key: Option<String>,
/// Provide environment variables for the auditor service.
///
/// Useful to set log levels. Variables should be comma separated without spaces.
Expand Down Expand Up @@ -847,6 +851,7 @@ async fn main() -> Result<()> {
Ok(())
}
SubCmd::Auditor(AuditorSubCmd::Add {
beta_encryption_key,
env_variables,
log_dir_path,
path,
Expand All @@ -855,6 +860,7 @@ async fn main() -> Result<()> {
version,
}) => {
cmd::auditor::add(
beta_encryption_key,
env_variables,
log_dir_path,
peers,
Expand Down
7 changes: 5 additions & 2 deletions sn_node_manager/src/cmd/auditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use sn_service_management::{
};
use std::path::PathBuf;

#[allow(clippy::too_many_arguments)]
pub async fn add(
beta_encryption_key: Option<String>,
env_variables: Option<Vec<(String, String)>>,
log_dir_path: Option<PathBuf>,
peers: PeersArgs,
Expand Down Expand Up @@ -72,10 +74,11 @@ pub async fn add(

add_auditor(
AddAuditorServiceOptions {
bootstrap_peers: get_peers_from_args(peers).await?,
env_variables,
auditor_src_bin_path,
auditor_install_bin_path: PathBuf::from("/usr/local/bin/auditor"),
beta_encryption_key,
bootstrap_peers: get_peers_from_args(peers).await?,
env_variables,
service_log_dir_path,
user: service_user.to_string(),
version,
Expand Down

0 comments on commit 5b99e0a

Please sign in to comment.