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

Noyez vis change hist identity show #3699

Merged
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
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 @@ -86,6 +87,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
11 changes: 11 additions & 0 deletions implementations/rust/ockam/ockam_command/tests/commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ teardown() {
assert_output --regexp '^P'
}

@test "create a node and show identity change history" {
run $OCKAM node create n1
assert_success

run $OCKAM identity show --full --node n1
assert_success
assert_output --partial "Change History"
assert_output --partial "identifier"
assert_output --partial "signatures"
}

@test "create a node with a name and do show on it" {
run $OCKAM node create n1
assert_success
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,4 +1,5 @@
use cfg_if::cfg_if;
use core::fmt;
use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
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 @@ -232,6 +239,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,4 +1,5 @@
use crate::ChangeIdentifier;
use core::fmt;
use ockam_core::compat::vec::Vec;
use ockam_core::vault::PublicKey;
use ockam_core::Result;
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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::change::{IdentityChange, IdentitySignedChange, Signature, SignatureTy
use crate::change_history::IdentityChangeHistory;
use crate::IdentityError::InvalidInternalState;
use crate::{ChangeIdentifier, Identity, IdentityError, IdentityVault, KeyAttributes};
use core::fmt;
use ockam_core::vault::{KeyId, PublicKey};
use ockam_core::{Encodable, Result};
use serde::{Deserialize, Serialize};
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::change::{IdentityChange, IdentitySignedChange, Signature, SignatureType};
use crate::change_history::IdentityChangeHistory;
use crate::{ChangeIdentifier, Identity, IdentityError, IdentityVault, KeyAttributes};
use core::fmt;
use ockam_core::vault::PublicKey;
use ockam_core::{Encodable, Result};
use serde::{Deserialize, Serialize};
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
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,7 +30,31 @@ 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>> {
Expand Down
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()))
}
}