Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dre): Do not require an HSM for dry runs #480

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions rs/cli/src/detect_neuron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,29 @@ impl Neuron {
Ok(neuron_id)
}

pub async fn as_arg_vec(&self, with_auth: bool) -> anyhow::Result<Vec<String>> {
if !with_auth {
return Ok(vec![]);
};

pub async fn as_arg_vec(&self, require_auth: bool) -> anyhow::Result<Vec<String>> {
// Auth required, try to find valid neuron id using HSM or with the private key
// If private key is provided, use it without checking
let auth = self.get_auth().await?;
let neuron_id = auto_detect_neuron_id(self.network.get_nns_urls(), auth).await?;
let auth = match self.get_auth().await {
Ok(auth) => auth,
Err(e) => {
if require_auth {
return Err(anyhow::anyhow!(e));
} else {
return Ok(vec![]);
}
}
};
let neuron_id = match auto_detect_neuron_id(self.network.get_nns_urls(), auth).await {
Ok(neuron_id) => neuron_id,
Err(e) => {
if require_auth {
return Err(anyhow::anyhow!(e));
} else {
return Ok(vec![]);
}
sasa-tomic marked this conversation as resolved.
Show resolved Hide resolved
}
};
Ok(vec!["--proposer".to_string(), neuron_id.to_string()])
}

Expand All @@ -126,6 +140,7 @@ impl Neuron {
pub enum Auth {
Hsm { pin: String, slot: u64, key_id: String },
Keyfile { path: PathBuf },
None,
}

fn pkcs11_lib_path() -> anyhow::Result<PathBuf> {
Expand Down Expand Up @@ -159,6 +174,7 @@ impl Auth {
key_id.clone(),
],
Auth::Keyfile { path } => vec!["--secret-key-pem".to_string(), path.to_string_lossy().to_string()],
Auth::None => vec![],
}
}

Expand Down Expand Up @@ -220,6 +236,7 @@ pub async fn auto_detect_neuron_id(nns_urls: &[url::Url], auth: Auth) -> anyhow:
let sig_keys = SigKeys::from_pem(&contents).expect("Failed to parse pem file");
Sender::SigKeys(sig_keys)
}
Auth::None => return Err(anyhow::anyhow!("No auth provided")),
};
let agent = Agent::new(nns_urls[0].clone(), sender);
if let Some(response) = agent
Expand Down
2 changes: 2 additions & 0 deletions rs/cli/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub async fn vote_on_proposals(
let client: GovernanceCanisterWrapper = match &neuron.get_auth().await? {
Auth::Hsm { pin, slot, key_id } => CanisterClient::from_hsm(pin.to_string(), *slot, key_id.to_string(), &nns_urls[0])?.into(),
Auth::Keyfile { path } => CanisterClient::from_key_file(path.into(), &nns_urls[0])?.into(),
Auth::None => CanisterClient::from_anonymous(&nns_urls[0])?.into(),
};

// In case of incorrectly set voting following, or in case of some other errors,
Expand Down Expand Up @@ -147,6 +148,7 @@ pub async fn get_node_metrics_history(
IcAgentCanisterClient::from_hsm(pin.to_string(), *slot, key_id.to_string(), nns_urls[0].clone(), Some(lock))?
}
Auth::Keyfile { path } => IcAgentCanisterClient::from_key_file(path.into(), nns_urls[0].clone())?,
Auth::None => IcAgentCanisterClient::from_anonymous(nns_urls[0].clone())?,
};
info!("Started action...");
let wallet_client = WalletCanisterWrapper::new(canister_agent.agent.clone());
Expand Down
13 changes: 7 additions & 6 deletions rs/cli/src/ic_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ impl IcAdminWrapper {
}
}

async fn print_ic_admin_command_line(&self, cmd: &Command) {
let auth = self.neuron.get_auth().await.unwrap();
async fn print_ic_admin_command_line(&self, cmd: &Command, with_auth: bool) {
let auth = if with_auth { self.neuron.get_auth().await.unwrap() } else { Auth::None };
info!(
"running ic-admin: \n$ {}{}",
cmd.get_program().to_str().unwrap().yellow(),
Expand Down Expand Up @@ -218,6 +218,7 @@ impl IcAdminWrapper {
}
}

let with_auth = !as_simulation && !cmd.args().contains(&String::from("--dry-run"));
self.run(
&cmd.get_command_name(),
[
Expand All @@ -236,12 +237,12 @@ impl IcAdminWrapper {
]
})
.unwrap_or_default(),
self.neuron.as_arg_vec(true).await?,
self.neuron.as_arg_vec(with_auth).await?,
cmd.args(),
]
.concat()
.as_slice(),
true,
with_auth,
false,
)
.await
Expand All @@ -250,7 +251,7 @@ impl IcAdminWrapper {
pub async fn propose_run(&self, cmd: ProposeCommand, opts: ProposeOptions, simulate: bool) -> anyhow::Result<String> {
// Simulated, or --help executions run immediately and do not proceed.
if simulate || cmd.args().contains(&String::from("--help")) || cmd.args().contains(&String::from("--dry-run")) {
return self._exec(cmd, opts, simulate).await;
return self._exec(cmd, opts, true).await;
}

// If --yes was not specified, ask the user if they want to proceed
Expand All @@ -277,7 +278,7 @@ impl IcAdminWrapper {
if silent {
cmd.stderr(Stdio::piped());
} else {
self.print_ic_admin_command_line(cmd).await;
self.print_ic_admin_command_line(cmd, with_auth).await;
}
cmd.stdout(Stdio::piped());

Expand Down
Loading