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

Add BasicClient to ParsecToolApp #35

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 0 additions & 14 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
//! Base CLI implementation.

use crate::common::{PROJECT_AUTHOR, PROJECT_DESC, PROJECT_NAME, PROJECT_VERSION};
use crate::error::ParsecToolError;
use crate::subcommands::Subcommand;
use parsec_client::auth::Authentication;
use parsec_client::BasicClient;
use structopt::StructOpt;

/// Struct representing the command-line interface of parsec-tool.
Expand All @@ -27,14 +24,3 @@ pub struct ParsecToolApp {
#[structopt(subcommand)]
pub subcommand: Subcommand,
}

impl ParsecToolApp {
/// Given an optional app name, generate the corresponding Authentication instance. This method
/// makes use of the authentication bootstrapping mechanism in `BasicClient` to obtain the
/// appropriate data for the tool.
pub fn authentication_data(&self) -> Result<Authentication, ParsecToolError> {
let mut client = BasicClient::new_naked();
client.set_default_auth(self.app_name.clone())?;
Ok(client.auth_data())
}
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

//! parsec-tool: a tool for interfacing with the Parsec service from the command-line.

use parsec_tool::err;

use parsec_client::BasicClient;
use parsec_tool::cli;
use parsec_tool::err;
use structopt::StructOpt;

fn main() -> std::io::Result<()> {
env_logger::init();

let matches = cli::ParsecToolApp::from_args();
matches.subcommand.run(&matches).map_err(|e| {
let client = BasicClient::new(matches.app_name.clone()).map_err(|e| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 looks nice!

err!("{:?}", e);
std::io::Error::new(std::io::ErrorKind::Other, "Failed to spin up basic client.")
})?;
matches.subcommand.run(&matches, client).map_err(|e| {
err!("{:?}", e);
std::io::Error::new(std::io::ErrorKind::Other, "Executing subcommand failed.")
})
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/create_ecc_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use parsec_client::core::interface::operations::psa_key_attributes::{
};
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand Down Expand Up @@ -67,14 +68,18 @@ impl TryFrom<&CreateEccKey> for NativeOperation {

impl ParsecToolSubcommand<'_> for CreateEccKey {
/// Exports a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Generating key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/create_rsa_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use parsec_client::core::interface::operations::psa_key_attributes::{
};
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand Down Expand Up @@ -67,14 +68,18 @@ impl TryFrom<&CreateRsaKey> for NativeOperation {

impl ParsecToolSubcommand<'_> for CreateRsaKey {
/// Exports a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Generating key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use parsec_client::core::interface::operations::psa_key_attributes::{
};
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand Down Expand Up @@ -65,14 +66,18 @@ impl TryFrom<&Decrypt> for NativeOperation {

impl ParsecToolSubcommand<'_> for Decrypt {
/// Exports a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Generating key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/delete_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use parsec_client::core::interface::operations::{delete_client, NativeOperation,

use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand All @@ -32,12 +33,16 @@ impl TryFrom<&DeleteClient> for NativeOperation {
}

impl ParsecToolSubcommand<'_> for DeleteClient {
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/delete_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::psa_destroy_key;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand All @@ -35,14 +36,18 @@ impl TryFrom<&DeleteKey> for NativeOperation {

impl ParsecToolSubcommand<'_> for DeleteKey {
/// Destroys a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Destroying a key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use parsec_client::core::interface::operations::psa_key_attributes::{
};
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand Down Expand Up @@ -65,14 +66,18 @@ impl TryFrom<&Encrypt> for NativeOperation {

impl ParsecToolSubcommand<'_> for Encrypt {
/// Exports a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Generating key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/export_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use parsec_client::core::interface::operations::psa_export_key;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::interface::secrecy::ExposeSecret;
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -42,14 +43,18 @@ impl TryFrom<&ExportKey> for NativeOperation {

impl ParsecToolSubcommand<'_> for ExportKey {
/// Exports a key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Exporting key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

let result = match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/export_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::psa_export_public_key;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -45,14 +46,18 @@ impl TryFrom<&ExportPublicKey> for NativeOperation {

impl ParsecToolSubcommand<'_> for ExportPublicKey {
/// Exports a public key.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Exporting public key...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

let result = match native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/generate_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::psa_generate_random;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -44,14 +45,18 @@ impl TryFrom<&GenerateRandom> for NativeOperation {

impl ParsecToolSubcommand<'_> for GenerateRandom {
/// Generates a sequence of random bytes.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
info!("Generating {} random bytes...", self.nbytes);

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
self.provider_opts.provider()?,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

let result = match native_result {
Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/list_authenticators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use parsec_client::core::interface::operations::list_authenticators;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand All @@ -31,7 +32,11 @@ impl TryFrom<&ListAuthenticators> for NativeOperation {

impl ParsecToolSubcommand<'_> for ListAuthenticators {
/// Lists the available authenticators supported by the Parsec service.
fn run(&self, _matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
_basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/list_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use parsec_client::core::interface::operations::{list_clients, NativeOperation,

use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand All @@ -27,12 +28,16 @@ impl TryFrom<&ListClients> for NativeOperation {
}

impl ParsecToolSubcommand<'_> for ListClients {
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

if let NativeResult::ListClients(result) = native_result {
Expand Down
9 changes: 7 additions & 2 deletions src/subcommands/list_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::{list_keys, NativeOperation, NativeResult};
use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use parsec_client::BasicClient;
use std::convert::TryFrom;
use structopt::StructOpt;

Expand All @@ -27,12 +28,16 @@ impl TryFrom<&ListKeys> for NativeOperation {

impl ParsecToolSubcommand<'_> for ListKeys {
/// Lists the available providers supported by the Parsec service.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(
&self,
_matches: &ParsecToolApp,
basic_client: BasicClient,
) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
&basic_client.auth_data(),
)?;

if let NativeResult::ListKeys(result) = native_result {
Expand Down
Loading