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 list-authenticators subcommand #17

Merged
merged 1 commit into from
Oct 21, 2020
Merged
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
64 changes: 64 additions & 0 deletions src/subcommands/list_authenticators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

//! List the authenticators supported by the Parsec service.

pub use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::auth::Authentication;
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 std::convert::TryFrom;
use structopt::StructOpt;

/// List the authenticators supported by the Parsec service.
#[derive(Debug, StructOpt)]
#[structopt(name = "list_authenticators")]
pub struct ListAuthenticatorsSubcommand {}

impl TryFrom<&ListAuthenticatorsSubcommand> for NativeOperation {
type Error = ParsecToolError;

fn try_from(
_list_authenticators_subcommand: &ListAuthenticatorsSubcommand,
) -> Result<Self, Self::Error> {
// Trivially converted to a `NativeOperation`.
Ok(NativeOperation::ListAuthenticators(
list_authenticators::Operation {},
))
}
}

impl ParsecToolSubcommand<'_> for ListAuthenticatorsSubcommand {
/// Lists the available authenticators supported by the Parsec service.
fn run(&self, _matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&Authentication::None,
)?;

if let NativeResult::ListAuthenticators(result) = native_result {
info!("Available authenticators:");
for authenticator in result.authenticators {
title!("0x{:02x} ({:?})", authenticator.id as u32, authenticator.id);
field!("Description", "{}", authenticator.description);
field!(
"Version",
"{}.{}.{}",
authenticator.version_maj,
authenticator.version_min,
authenticator.version_rev
);
println!();
}
Ok(())
} else {
Err(ParsecToolError::UnexpectedNativeResult(native_result))
}
}
}
5 changes: 3 additions & 2 deletions src/subcommands/list_opcodes.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::common::ProviderOpts;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::auth::Authentication;
use parsec_client::core::interface::operations::list_opcodes;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::interface::requests::ProviderID;
@@ -35,14 +36,14 @@ impl TryFrom<&ListOpcodesSubcommand> for NativeOperation {

impl ParsecToolSubcommand<'_> for ListOpcodesSubcommand {
/// Lists the supported opcodes for a given provider.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(&self, _matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
// We still use the core provider beacuse listing opcodes is a core operation. Note the
// distinction between the provider we're _using_ and the provider we're querying.
ProviderID::Core,
&matches.authentication_data()?,
&Authentication::None,
)?;

if let NativeResult::ListOpcodes(result) = native_result {
5 changes: 3 additions & 2 deletions src/subcommands/list_providers.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
pub use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::auth::Authentication;
use parsec_client::core::interface::operations::list_providers;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::interface::requests::ProviderID;
@@ -29,12 +30,12 @@ impl TryFrom<&ListProvidersSubcommand> for NativeOperation {

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

if let NativeResult::ListProviders(result) = native_result {
13 changes: 9 additions & 4 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
//! Subcommand implementations. Interacts with parsec-client-rust.

pub mod common;
pub mod list_authenticators;
pub mod list_keys;
pub mod list_opcodes;
pub mod list_providers;
@@ -17,10 +18,10 @@ pub mod psa_generate_random;
use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::{
list_keys::ListKeysSubcommand, list_opcodes::ListOpcodesSubcommand,
list_providers::ListProvidersSubcommand, ping::PingSubcommand,
psa_destroy_key::PsaDestroyKeySubcommand, psa_export_key::PsaExportKeySubcommand,
psa_export_public_key::PsaExportPublicKeySubcommand,
list_authenticators::ListAuthenticatorsSubcommand, list_keys::ListKeysSubcommand,
list_opcodes::ListOpcodesSubcommand, list_providers::ListProvidersSubcommand,
ping::PingSubcommand, psa_destroy_key::PsaDestroyKeySubcommand,
psa_export_key::PsaExportKeySubcommand, psa_export_public_key::PsaExportPublicKeySubcommand,
psa_generate_key::PsaGenerateKeySubcommand, psa_generate_random::PsaGenerateRandomSubcommand,
};
use anyhow::Result;
@@ -52,6 +53,9 @@ pub enum Subcommand {
/// Lists the available providers supported by the Parsec service.
ListProviders(ListProvidersSubcommand),

/// Lists the available authenticators supported by the Parsec service.
ListAuthenticators(ListAuthenticatorsSubcommand),

/// Lists the supported opcodes for a given provider.
ListOpcodes(ListOpcodesSubcommand),

@@ -80,6 +84,7 @@ impl Subcommand {
match &self {
Subcommand::Ping(cmd) => cmd.run(matches),
Subcommand::ListProviders(cmd) => cmd.run(matches),
Subcommand::ListAuthenticators(cmd) => cmd.run(matches),
Subcommand::ListKeys(cmd) => cmd.run(matches),
Subcommand::ListOpcodes(cmd) => cmd.run(matches),
Subcommand::PsaGenerateRandom(cmd) => cmd.run(matches),
5 changes: 3 additions & 2 deletions src/subcommands/ping.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
pub use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::auth::Authentication;
use parsec_client::core::interface::operations::ping;
use parsec_client::core::interface::operations::{NativeOperation, NativeResult};
use parsec_client::core::interface::requests::ProviderID;
@@ -29,14 +30,14 @@ impl TryFrom<&PingSubcommand> for NativeOperation {

impl ParsecToolSubcommand<'_> for PingSubcommand {
/// Pings the Parsec service and prints the wire protocol version.
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
fn run(&self, _matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
info!("Pinging Parsec service...");

let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
&Authentication::None,
)?;

if let NativeResult::Ping(result) = native_result {