Skip to content

Commit

Permalink
Return DeviceRemoved error on timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jan 3, 2024
1 parent 6fdead9 commit 4660f8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 15 additions & 5 deletions pkcs11/src/backend/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use log::{debug, error, trace};
use nethsm_sdk_rs::{
apis::{self, configuration::Configuration, default_api, ResponseContent},
models::UserRole,
ureq,
};
use std::fmt::Debug;

use crate::config::config_file::UserConfig;

Expand Down Expand Up @@ -190,7 +190,7 @@ impl LoginCtx {
// Try to run the api call on each instance until one succeeds
pub fn try_<F, T, R>(&mut self, api_call: F, user_mode: UserMode) -> Result<R, ApiError>
where
F: FnOnce(Configuration) -> Result<R, apis::Error<T>> + Clone,
F: FnOnce(&Configuration) -> Result<R, apis::Error<T>> + Clone,
{
// we loop for a maximum of instances.len() times
for _ in 0..self.instances.len() {
Expand All @@ -200,16 +200,26 @@ impl LoginCtx {
};

let api_call_clone = api_call.clone();
match api_call_clone(conf) {
match api_call_clone(&conf) {
Ok(result) => return Ok(result),

// If the server is in an unusable state, try the next one
Err(apis::Error::ResponseError(ResponseContent { status: 500, .. }))
| Err(apis::Error::ResponseError(ResponseContent { status: 501, .. }))
| Err(apis::Error::ResponseError(ResponseContent { status: 502, .. }))
| Err(apis::Error::ResponseError(ResponseContent { status: 503, .. }))
| Err(apis::Error::ResponseError(ResponseContent { status: 412, .. })) => continue,

| Err(apis::Error::ResponseError(ResponseContent { status: 412, .. })) => {
continue;
}

Err(apis::Error::Ureq(ureq::Error::Transport(err))) => {
if matches!(
err.kind(),
ureq::ErrorKind::Io | ureq::ErrorKind::ConnectionFailed
) {
return Err(ApiError::InstanceRemoved);
}
}
// Otherwise, return the error
Err(err) => return Err(err.into()),
}
Expand Down
10 changes: 7 additions & 3 deletions pkcs11/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use self::{
};
use cryptoki_sys::{
CKR_ARGUMENTS_BAD, CKR_ATTRIBUTE_VALUE_INVALID, CKR_DATA_INVALID, CKR_DATA_LEN_RANGE,
CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_ENCRYPTED_DATA_LEN_RANGE, CKR_KEY_HANDLE_INVALID,
CKR_MECHANISM_INVALID, CKR_OPERATION_ACTIVE, CKR_OPERATION_NOT_INITIALIZED,
CKR_TOKEN_NOT_PRESENT, CKR_USER_NOT_LOGGED_IN, CK_ATTRIBUTE_TYPE, CK_OBJECT_HANDLE, CK_RV,
CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_DEVICE_REMOVED, CKR_ENCRYPTED_DATA_LEN_RANGE,
CKR_KEY_HANDLE_INVALID, CKR_MECHANISM_INVALID, CKR_OPERATION_ACTIVE,
CKR_OPERATION_NOT_INITIALIZED, CKR_TOKEN_NOT_PRESENT, CKR_USER_NOT_LOGGED_IN,
CK_ATTRIBUTE_TYPE, CK_OBJECT_HANDLE, CK_RV,
};
use log::error;
use nethsm_sdk_rs::apis;
Expand Down Expand Up @@ -38,6 +39,7 @@ pub enum ApiError {
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent),
InstanceRemoved,
NoInstance,
StringParse(std::string::FromUtf8Error),
}
Expand Down Expand Up @@ -153,6 +155,7 @@ impl From<Error> for CK_RV {
_ => CKR_DEVICE_ERROR,
},
ApiError::StringParse(_) => CKR_DEVICE_ERROR,
ApiError::InstanceRemoved => CKR_DEVICE_REMOVED,
},
}
}
Expand Down Expand Up @@ -205,6 +208,7 @@ impl std::fmt::Display for Error {
_ => format!("Api error: {:?}", resp),
},
ApiError::StringParse(err) => format!("String parse error: {:?}", err),
ApiError::InstanceRemoved => format!("Failed to connect to instance"),
},
Error::Base64(err) => format!("Base64 Decode error: {:?}", err),
Error::StringParse(err) => format!("String parse error: {:?}", err),
Expand Down

0 comments on commit 4660f8a

Please sign in to comment.