Skip to content

Commit

Permalink
chore(rust): add more logs around enrollment tickets parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Feb 11, 2025
1 parent a5c7586 commit aa41bfd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
17 changes: 13 additions & 4 deletions implementations/rust/ockam/ockam_api/src/cli_state/enrollments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,17 @@ impl FromStr for LegacyEnrollmentTicket {

fn from_str(contents: &str) -> std::result::Result<Self, Self::Err> {
if let Ok(data) = hex::decode(contents) {
Ok(serde_json::from_slice(&data)
.map_err(|_err| ApiError::core("Failed to decode EnrollmentTicket json"))?)
debug!(%contents, "decoding hex-encoded LegacyEnrollmentTicket");
Ok(serde_json::from_slice(&data).map_err(|_err| {
ApiError::core(
"Failed to decode LegacyEnrollmentTicket json from hex-encoded string",
)
})?)
} else {
Ok(serde_json::from_str(contents)
.map_err(|_err| ApiError::core("Failed to decode EnrollmentTicket json"))?)
debug!(%contents, "decoding LegacyEnrollmentTicket from raw contents");
Ok(serde_json::from_str(contents).map_err(|_err| {
ApiError::core("Failed to decode LegacyEnrollmentTicket json from raw contents")
})?)
}
}
}
Expand Down Expand Up @@ -370,6 +376,7 @@ impl FromStr for ExportedEnrollmentTicket {
// Decode as comma-separated text
let values: Vec<&str> = contents.split(',').collect();
if values.len() < Self::MANDATORY_FIELDS_NUM {
error!(%contents, ?values, "missing fields in enrollment ticket: expected at least {}, got {}", Self::MANDATORY_FIELDS_NUM, values.len());
return Err(ApiError::core("Missing fields in enrollment ticket").into());
}
let (
Expand Down Expand Up @@ -463,6 +470,7 @@ impl EnrollmentTicket {
authority_change_history: impl Into<String>,
authority_route: MultiAddr,
) -> Result<Self> {
debug!("Creating enrollment ticket");
let project_id = project_id.into();
let project_change_history = project_change_history.into();
let project_identity = Identity::import_from_string(
Expand Down Expand Up @@ -493,6 +501,7 @@ impl EnrollmentTicket {
one_time_code: OneTimeCode,
project: &ProjectModel,
) -> Result<Self> {
debug!(?project, "Creating enrollment ticket from project");
let project_change_history = project
.project_change_history
.as_ref()
Expand Down
14 changes: 8 additions & 6 deletions implementations/rust/ockam/ockam_command/src/value_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use miette::{miette, Context, IntoDiagnostic};
use ockam_api::cli_state::{EnrollmentTicket, ExportedEnrollmentTicket, LegacyEnrollmentTicket};
use serde::Deserialize;
use std::str::FromStr;
use tracing::trace;
use tracing::{trace, warn};
use url::Url;

/// Parse a single key-value pair
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) async fn parse_config_or_path_or_url<'de, T: Deserialize<'de>>(
Ok(value.to_string())
} else {
Err(miette!(
"Failed to parse value {} as a path, URL or configuration",
"Failed to parse value {} as a path, URL or inline configuration",
value
))
}
Expand All @@ -61,9 +61,10 @@ pub(crate) async fn parse_config_or_path_or_url<'de, T: Deserialize<'de>>(
}

pub(crate) async fn parse_string_or_path_or_url(value: &str) -> miette::Result<String> {
parse_path_or_url(value)
.await
.or_else(|_| Ok(value.to_string()))
parse_path_or_url(value).await.or_else(|err| {
warn!(%value, %err, "Couldn't parse value as a path or URL. Returning plain value to be processed as inline contents");
Ok(value.to_string())
})
}

pub(crate) async fn parse_path_or_url(value: &str) -> miette::Result<String> {
Expand All @@ -85,7 +86,8 @@ pub(crate) async fn parse_path_or_url(value: &str) -> miette::Result<String> {
.into_diagnostic()
.context("Failed to read contents from file")
} else {
Err(miette!("Failed to parse value {} as a path or URL", value))
warn!(%value, "Couldn't parse value as a path or URL");
Err(miette!("Couldn't parse value '{}' as a path or URL", value))
}
}

Expand Down

0 comments on commit aa41bfd

Please sign in to comment.