Skip to content

Commit

Permalink
Do not panic if protobuf UUID can not be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Sep 26, 2023
1 parent e87e044 commit d62b60e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ impl ManagementService for ManagementServiceImpl {
async fn remove_api_access_method(&self, request: Request<types::Uuid>) -> ServiceResult<()> {
log::debug!("remove_api_access_method");
let api_access_method =
mullvad_types::api_access::ApiAccessMethodId::from(request.into_inner());
mullvad_types::api_access::ApiAccessMethodId::try_from(request.into_inner())?;
let (tx, rx) = oneshot::channel();
self.send_command_to_daemon(DaemonCommand::RemoveApiAccessMethod(tx, api_access_method))?;
self.wait_for_result(rx)
Expand Down Expand Up @@ -676,7 +676,7 @@ impl ManagementService for ManagementServiceImpl {
async fn set_api_access_method(&self, request: Request<types::Uuid>) -> ServiceResult<()> {
log::debug!("set_api_access_method");
let api_access_method =
mullvad_types::api_access::ApiAccessMethodId::from(request.into_inner());
mullvad_types::api_access::ApiAccessMethodId::try_from(request.into_inner())?;
let (tx, rx) = oneshot::channel();
self.send_command_to_daemon(DaemonCommand::SetApiAccessMethod(tx, api_access_method))?;
self.wait_for_result(rx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod data {
.ok_or(FromProtobufTypeError::InvalidArgument(
"Could not deserialize Access Method from protobuf",
))
.map(ApiAccessMethodId::from)?;
.and_then(ApiAccessMethodId::try_from)?;
let name = value.name;
let enabled = value.enabled;
let access_method = value
Expand Down Expand Up @@ -238,9 +238,13 @@ mod data {
}
}

impl From<proto::Uuid> for ApiAccessMethodId {
fn from(value: proto::Uuid) -> Self {
Self::from_string(value.value)
impl TryFrom<proto::Uuid> for ApiAccessMethodId {
type Error = FromProtobufTypeError;

fn try_from(value: proto::Uuid) -> Result<Self, Self::Error> {
Self::from_string(value.value).ok_or(FromProtobufTypeError::InvalidArgument(
"Could not parse UUID message from protobuf",
))
}
}

Expand Down
9 changes: 4 additions & 5 deletions mullvad-types/src/api_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ impl ApiAccessMethodId {
pub fn new() -> Self {
Self(uuid::Uuid::new_v4())
}
/// It is up to the caller to make sure that the supplied String is actually
/// a valid UUID in the context of [`ApiAccessMethod`]s.
pub fn from_string(id: String) -> Self {
// TODO: Remove unwrap
Self(uuid::Uuid::from_str(&id).unwrap())
/// Tries to parse a UUID from a raw String. If it is successful, an
/// [`ApiAccessMethodId`] is instantiated.
pub fn from_string(id: String) -> Option<Self> {
uuid::Uuid::from_str(&id).ok().map(Self)
}
}

Expand Down

0 comments on commit d62b60e

Please sign in to comment.