Skip to content

Commit

Permalink
feat(rust): return new ticket format in project ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Nov 14, 2024
1 parent 3f01471 commit 2b8021f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
29 changes: 12 additions & 17 deletions implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl LegacyEnrollmentTicket {

pub fn hex_encoded(&self) -> Result<String> {
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))
}
}
Expand Down Expand Up @@ -344,6 +344,10 @@ impl ExportedEnrollmentTicket {
)
.await
}

pub fn hex_encoded(&self) -> Result<String> {
Ok(hex::encode(self.to_string()))
}
}

impl FromStr for ExportedEnrollmentTicket {
Expand Down Expand Up @@ -398,20 +402,19 @@ 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,
self.project_name,
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(())
}
}
Expand Down Expand Up @@ -646,32 +649,24 @@ 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()));
assert!(plain.contains(&exported.project_name));
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);
}
Expand Down
31 changes: 26 additions & 5 deletions implementations/rust/ockam/ockam_command/src/project/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(())
Expand Down
6 changes: 0 additions & 6 deletions implementations/rust/ockam/ockam_command/src/value_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?);
}

Expand Down

0 comments on commit 2b8021f

Please sign in to comment.