Skip to content

Commit

Permalink
chore(rust): Visualize identity change history in `ockam identity sho…
Browse files Browse the repository at this point in the history
…w --full` command

This commit adds a more pleasing and human readable output for `ockam identity show --full`
command. This was mostly implmented by adding the `fmt::Display` trait to
the underlying types in the identity change history.
Before:
```
$ ockam identity show --full --node n1
01cdb5565163e5b1278eb31e6dbd213066e335da0c3e5d8ffed3789ce1305...6778a9ae9204bf7fbc8adbc00138b2756a09
```
After:
```
$ ockam identity show --full --node n1
Change History:
  Change[0]:
    identifier: cdb5565163e5b1278eb31e6dbd213066e335da0c3e5d8ffed3789ce130523391
    change:
      prev_change_identifier: 0547c93239ba3d818ec26c9cdadd2a35cbdf1fa3b6d1a731e06164b1079fb7b8
      label:        OCKAM_RK
      public_key:   Ed25519 e7417e5ea17b05684cb56171f6f37ac92d37a03587fa43f66663bfda878a1322
    signatures:
      [0]: SelfSign 5e48f7a133ac1d9218f9fb7185cf890af6bbe9ca5ca58a741...9204bf7fbc8adbc00138b2756a09
```

issue ref: build-trust#3258
  • Loading branch information
noyez committed Oct 20, 2022
1 parent a099804 commit cf3df9d
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions implementations/rust/ockam/ockam_command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rand = "0.8"
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls-native-roots"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_bare = { version = "0.5.0", default-features = false, features = ["alloc"] }
slug = "0.1"
sysinfo = { version = "0.26", default-features = false }
syntect = "5"
Expand All @@ -85,6 +86,7 @@ ockam_api = { path = "../ockam_api", version = "0.19.0", features = ["std", "aut
ockam_multiaddr = { path = "../ockam_multiaddr", version = "0.10.0", features = ["std"] }
ockam_vault = { path = "../ockam_vault", version = "^0.66.0", features = ["storage"] }
ockam_core = { path = "../ockam_core", version = "^0.70.0" }
ockam_identity = { path = "../ockam_identity", version = "^0.64.0" }

[dev-dependencies]
assert_cmd = "2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::fmt::Write;
use ockam::Context;
use ockam_api::nodes::models::identity::{LongIdentityResponse, ShortIdentityResponse};
use ockam_core::api::Request;
use ockam_identity::change_history::IdentityChangeHistory;

#[derive(Clone, Debug, Args)]
pub struct ShowCommand {
Expand Down Expand Up @@ -45,7 +46,8 @@ async fn run_impl(
impl Output for LongIdentityResponse<'_> {
fn output(&self) -> anyhow::Result<String> {
let mut w = String::new();
write!(w, "{}", hex::encode(self.identity.0.as_ref()))?;
let id: IdentityChangeHistory = serde_bare::from_slice(self.identity.0.as_ref())?;
write!(w, "{}", id)?;
Ok(w)
}
}
Expand Down
19 changes: 19 additions & 0 deletions implementations/rust/ockam/ockam_core/src/vault/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use cfg_if::cfg_if;
use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -140,6 +141,12 @@ impl PublicKey {
}
}

impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} {}", self.stype(), hex::encode(self.data()))
}
}

/// Binary representation of Signature.
#[derive(Serialize, Deserialize, Clone, Debug, Zeroize)]
pub struct Signature(SignatureVec);
Expand Down Expand Up @@ -235,6 +242,18 @@ impl SecretAttributes {
}
}

impl fmt::Display for SecretAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?}({:?}) len:{}",
self.stype(),
self.persistence(),
self.length()
)
}
}

/// A public key
#[derive(Clone, Debug, Zeroize)]
#[zeroize(drop)]
Expand Down
21 changes: 21 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/change.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use crate::ChangeIdentifier;
use ockam_core::compat::vec::Vec;
use ockam_core::vault::PublicKey;
Expand All @@ -21,6 +22,15 @@ pub enum IdentityChange {
RotateKey(RotateKeyChangeData),
}

impl fmt::Display for IdentityChange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IdentityChange::CreateKey(data) => write!(f, " CreateKey:{}", data),
IdentityChange::RotateKey(data) => write!(f, " RotateKey:{}", data),
}
}
}

impl IdentityChange {
pub(crate) fn has_label(&self, label: &str) -> bool {
self.label() == label
Expand Down Expand Up @@ -87,3 +97,14 @@ impl IdentitySignedChange {
}
}
}

impl fmt::Display for IdentitySignedChange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, " identifier: {}", self.identifier())?;
writeln!(f, " identity change: {}", self.change())?;
for s in self.signatures() {
writeln!(f, "signatures: {}", s)?;
}
Ok(())
}
}
13 changes: 13 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/change/create_key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use crate::change::{IdentityChange, IdentitySignedChange, Signature, SignatureType};
use crate::change_history::IdentityChangeHistory;
use crate::IdentityError::InvalidInternalState;
Expand Down Expand Up @@ -44,6 +45,18 @@ impl CreateKeyChangeData {
}
}

impl fmt::Display for CreateKeyChangeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"prev_change_id:{} key attibutes:{} public key:{}",
self.prev_change_id(),
self.key_attributes(),
self.public_key()
)
}
}

impl<V: IdentityVault> Identity<V> {
async fn generate_key_if_needed(
secret: Option<&KeyId>,
Expand Down
13 changes: 13 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/change/rotate_key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use crate::change::{IdentityChange, IdentitySignedChange, Signature, SignatureType};
use crate::change_history::IdentityChangeHistory;
use crate::{ChangeIdentifier, Identity, IdentityError, IdentityVault, KeyAttributes};
Expand Down Expand Up @@ -43,6 +44,18 @@ impl RotateKeyChangeData {
}
}

impl fmt::Display for RotateKeyChangeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"prev_change_id:{} key attibutes:{} public key:{}",
self.prev_change_id(),
self.key_attributes(),
self.public_key()
)
}
}

impl<V: IdentityVault> Identity<V> {
/// Rotate key change
pub(crate) async fn make_rotate_key_change(
Expand Down
28 changes: 27 additions & 1 deletion implementations/rust/ockam/ockam_identity/src/change_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
ChangeIdentifier, IdentityError, IdentityIdentifier, IdentityStateConst, IdentityVault,
};
use core::cmp::Ordering;
use core::fmt;
use minicbor::{Decode, Encode};
use ockam_core::compat::vec::Vec;
use ockam_core::{allow, deny, Encodable, Result};
Expand All @@ -29,10 +30,35 @@ pub enum IdentityHistoryComparison {

/// Full history of [`Identity`] changes. History and corresponding secret keys are enough to recreate [`Identity`]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct IdentityChangeHistory(Vec<IdentitySignedChange>);
pub struct IdentityChangeHistory(Vec<IdentitySignedChange>);

impl fmt::Display for IdentityChangeHistory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Change History:")?;
for (i_num, ident) in self.0.iter().enumerate() {
let public_key = ident.change().public_key().unwrap();
writeln!(f, " Change[{}]:", i_num)?;
writeln!(f, " identifier: {}", ident.identifier())?;
writeln!(f, " change:")?;
writeln!(
f,
" prev_change_identifier: {}",
ident.change().previous_change_identifier()
)?;
writeln!(f, " label: {}", ident.change().label())?;
writeln!(f, " public_key: {}", public_key)?;
writeln!(f, " signatures:")?;
for (sig_num, sig) in ident.signatures().iter().enumerate() {
writeln!(f, " [{}]: {}", sig_num, sig)?;
}
}
Ok(())
}
}

impl IdentityChangeHistory {
pub fn export(&self) -> Result<Vec<u8>> {
//panic!("asdf");
serde_bare::to_vec(self).map_err(|_| IdentityError::ConsistencyError.into())
}

Expand Down
5 changes: 5 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ impl FromStr for IdentityIdentifier {
/// Unique [`crate::IdentityChangeChange`] identifier, computed as SHA256 of the change data
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
pub struct ChangeIdentifier([u8; 32]);
impl Display for ChangeIdentifier {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

impl AsRef<[u8]> for ChangeIdentifier {
fn as_ref(&self) -> &[u8] {
Expand Down
11 changes: 11 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/key_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use ockam_core::compat::string::String;
use ockam_core::vault::{SecretPersistence, SecretType, CURVE25519_SECRET_LENGTH_U32};
use ockam_vault::SecretAttributes;
Expand Down Expand Up @@ -39,3 +40,13 @@ impl KeyAttributes {
}
}
}
impl fmt::Display for KeyAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
" label:{}, secrets:{}",
self.label(),
self.secret_attributes()
)
}
}
7 changes: 7 additions & 0 deletions implementations/rust/ockam/ockam_identity/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use ockam_core::vault::Signature as OckamVaultSignature;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -36,3 +37,9 @@ impl Signature {
Signature { stype, data }
}
}

impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} {}", self.stype(), hex::encode(self.data()))
}
}

0 comments on commit cf3df9d

Please sign in to comment.