Skip to content

Commit

Permalink
refactor(rust): swap vault attach-key for identity create --key-id
Browse files Browse the repository at this point in the history
  • Loading branch information
adiSuper94 authored and adrianbenavides committed Oct 6, 2023
1 parent adcb43d commit 656f03e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async fn start_authority_node(
Ok(state) => state.config().identifier(),
Err(_) => {
debug!("creating default identity");
let cmd = identity::CreateCommand::new("authority".into(), None);
let cmd = identity::CreateCommand::new("authority".into(), None, None);
cmd.create_identity(opts.clone()).await?
}
}
Expand Down
45 changes: 38 additions & 7 deletions implementations/rust/ockam/ockam_command/src/identity/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::util::node_rpc;
use crate::{docs, fmt_log, fmt_ok, CommandGlobalOpts};
use clap::Args;
use colorful::Colorful;
use miette::miette;
use ockam::identity::Identifier;
use ockam::Context;
use ockam_api::cli_state::traits::StateDirTrait;
use ockam_api::cli_state::traits::{StateDirTrait, StateItemTrait};
use ockam_vault::{HandleToSecret, SigningSecretKeyHandle};
use rand::prelude::random;
use tokio::sync::Mutex;
use tokio::try_join;
Expand All @@ -26,11 +28,19 @@ pub struct CreateCommand {
/// Vault name to store the identity key
#[arg(long, value_name = "VAULT_NAME", global = true)]
vault: Option<String>,

/// Key ID to use for the identity creation
#[arg(short, long)]
key_id: Option<String>,
}

impl CreateCommand {
pub fn new(name: String, vault: Option<String>) -> CreateCommand {
CreateCommand { name, vault }
pub fn new(name: String, vault: Option<String>, key_id: Option<String>) -> CreateCommand {
CreateCommand {
name,
vault,
key_id,
}
}

pub fn run(self, options: CommandGlobalOpts) {
Expand Down Expand Up @@ -71,13 +81,34 @@ impl CreateCommand {

let vault = vault_state.get().await?;

let identity = opts
let identities_creation = opts
.state
.get_identities(vault)
.await?
.identities_creation()
.create_identity()
.await?;
.identities_creation();

// Create an identity using the KMS key, if provided.
let identity = match &self.key_id {
Some(key_id) => {
if !vault_state.config().is_aws() {
Err(miette!(
"Vault {} is not an AWS KMS vault",
self.vault.clone().unwrap_or("default".to_string()),
))
} else {
let handle = SigningSecretKeyHandle::ECDSASHA256CurveP256(
HandleToSecret::new(key_id.as_bytes().to_vec()),
);

Ok(identities_creation
.identity_builder()
.with_existing_key(handle)
.build()
.await?)
}
}
None => Ok(identities_creation.create_identity().await?),
}?;

opts.state
.create_identity_state(identity.identifier(), Some(&self.name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn get_default_identity_name(cli_state: &CliState) -> String {
/// Create the default identity
pub fn create_default_identity(opts: &CommandGlobalOpts) {
let default = "default";
let create_command = CreateCommand::new(default.into(), None);
let create_command = CreateCommand::new(default.into(), None, None);
create_command.run(opts.clone().set_quiet());

// Retrieve the identifier if available
Expand Down
67 changes: 0 additions & 67 deletions implementations/rust/ockam/ockam_command/src/vault/attach_key.rs

This file was deleted.

4 changes: 0 additions & 4 deletions implementations/rust/ockam/ockam_command/src/vault/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod attach_key;
mod create;
mod default;
mod delete;
mod list;
mod show;

use crate::vault::attach_key::AttachKeyCommand;
use crate::vault::create::CreateCommand;
use crate::vault::default::DefaultCommand;
use crate::vault::delete::DeleteCommand;
Expand Down Expand Up @@ -34,7 +32,6 @@ pub struct VaultCommand {
#[derive(Clone, Debug, Subcommand)]
pub enum VaultSubcommand {
Create(CreateCommand),
AttachKey(AttachKeyCommand),
Show(ShowCommand),
Delete(DeleteCommand),
List(ListCommand),
Expand All @@ -45,7 +42,6 @@ impl VaultCommand {
pub fn run(self, opts: CommandGlobalOpts) {
match self.subcommand {
VaultSubcommand::Create(cmd) => cmd.run(opts),
VaultSubcommand::AttachKey(cmd) => cmd.run(opts),
VaultSubcommand::Show(cmd) => cmd.run(opts),
VaultSubcommand::List(cmd) => cmd.run(opts),
VaultSubcommand::Delete(cmd) => cmd.run(opts),
Expand Down

0 comments on commit 656f03e

Please sign in to comment.