diff --git a/implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs b/implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs index c8105728054..37b1ef4a63c 100644 --- a/implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs +++ b/implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs @@ -266,7 +266,7 @@ impl LegacyEnrollmentTicket { pub fn hex_encoded(&self) -> Result { let serialized = serde_json::to_vec(&self) - .map_err(|_err| ApiError::core("Failed to authenticate with Okta"))?; + .map_err(|_err| ApiError::core("Failed to hex-encode enrollment ticket"))?; Ok(hex::encode(serialized)) } } @@ -344,6 +344,10 @@ impl ExportedEnrollmentTicket { ) .await } + + pub fn hex_encoded(&self) -> Result { + Ok(hex::encode(self.to_string())) + } } impl FromStr for ExportedEnrollmentTicket { @@ -398,8 +402,8 @@ impl FromStr for ExportedEnrollmentTicket { impl Display for ExportedEnrollmentTicket { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let mut output = String::new(); - output.push_str(&format!( + write!( + f, "{},{},{},{},{},{}", self.project_route.route, self.project_identifier, @@ -407,11 +411,10 @@ impl Display for ExportedEnrollmentTicket { String::from(&self.one_time_code), self.project_change_history, self.authority_change_history, - )); + )?; if let Some(authority_route) = &self.authority_route { - output.push_str(&format!(",{}", authority_route)); + write!(f, ",{}", authority_route)?; } - write!(f, "{}", hex::encode(output))?; Ok(()) } } @@ -646,10 +649,9 @@ mod tests { } #[test] - fn test_exported_enrollment_ticket() { + fn test_enrollment_ticket_encoding_decoding() { let exported = ExportedEnrollmentTicket::new_test(); - let encoded = exported.to_string(); - let plain = String::from_utf8(hex::decode(&encoded).unwrap()).unwrap(); + let plain = exported.to_string(); assert!(plain.contains(&String::from(&exported.one_time_code))); assert!(plain.contains(&exported.project_route.id)); assert!(plain.contains(&exported.project_route.route.to_string())); @@ -657,21 +659,14 @@ mod tests { assert!(plain.contains(&exported.project_change_history)); assert!(plain.contains(&exported.authority_change_history)); - let decoded = ExportedEnrollmentTicket::from_str(&encoded).unwrap(); - assert_eq!(decoded, exported); - let decoded = ExportedEnrollmentTicket::from_str(&plain).unwrap(); assert_eq!(decoded, exported); let json_encoded = serde_json::to_string(&exported).unwrap(); let decoded = ExportedEnrollmentTicket::from_str(&json_encoded).unwrap(); assert_eq!(decoded, exported); - } - #[test] - fn exported_enrollment_ticket_from_hex() { - let exported = ExportedEnrollmentTicket::new_test(); - let encoded = exported.to_string(); + let encoded = exported.hex_encoded().unwrap(); let decoded = ExportedEnrollmentTicket::from_str(&encoded).unwrap(); assert_eq!(decoded, exported); } diff --git a/implementations/rust/ockam/ockam_command/src/project/ticket.rs b/implementations/rust/ockam/ockam_command/src/project/ticket.rs index 03fbe4d1a31..2ca4785202b 100644 --- a/implementations/rust/ockam/ockam_command/src/project/ticket.rs +++ b/implementations/rust/ockam/ockam_command/src/project/ticket.rs @@ -5,7 +5,7 @@ use std::time::Duration; use async_trait::async_trait; use clap::Args; use colorful::Colorful; -use miette::miette; +use miette::{miette, IntoDiagnostic}; use tracing::debug; use crate::shared_args::{IdentityOpts, RetryOpts, TrustOpts}; @@ -73,6 +73,14 @@ pub struct TicketCommand { #[command(flatten)] retry_opts: RetryOpts, + + /// Return the ticket in hex encoded format + #[arg(long, hide = true)] + hex_encoded: bool, + + /// Return the ticket using the legacy encoding format + #[arg(long, hide = true)] + legacy: bool, } #[async_trait] @@ -142,9 +150,22 @@ impl Command for TicketCommand { project.authority_access_route.as_ref(), ) .import() - .await? - .export_legacy()?; - let encoded_ticket = ticket.hex_encoded()?.to_string(); + .await?; + let (as_json, encoded_ticket) = if cmd.legacy { + let exported = ticket.export_legacy()?; + ( + serde_json::to_string(&exported).into_diagnostic()?, + exported.hex_encoded()?, + ) + } else { + let exported = ticket.export()?; + let encoded = if cmd.hex_encoded { + exported.hex_encoded()? + } else { + exported.to_string() + }; + (serde_json::to_string(&exported).into_diagnostic()?, encoded) + }; let usage_count = cmd.usage_count.unwrap_or(DEFAULT_TOKEN_USAGE_COUNT); let attributes_msg = if attributes.is_empty() { @@ -187,7 +208,7 @@ impl Command for TicketCommand { .stdout() .plain(format!("\n{encoded_ticket}")) .machine(encoded_ticket) - .json_obj(ticket)? + .json(as_json) .write_line()?; Ok(()) diff --git a/implementations/rust/ockam/ockam_command/src/value_parsers.rs b/implementations/rust/ockam/ockam_command/src/value_parsers.rs index 2a21ce62e2a..17e89044374 100644 --- a/implementations/rust/ockam/ockam_command/src/value_parsers.rs +++ b/implementations/rust/ockam/ockam_command/src/value_parsers.rs @@ -34,12 +34,6 @@ pub async fn parse_enrollment_ticket( // Try to parse it using the old format if let Ok(ticket) = LegacyEnrollmentTicket::from_str(&contents) { - // TODO: disabled until release 0.138.0 - // opts.terminal.write_line(fmt_warn!( - // "The enrollment ticket was generated from an old Ockam version" - // ))?; - // opts.terminal - // .write_line(fmt_warn!("Please make sure the machine that generated the ticket is using the latest Ockam version"))?; return Ok(EnrollmentTicket::new_from_legacy(ticket).await?); }