Skip to content

Commit

Permalink
Add require! macro to include field information in MissingFields error
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Mar 28, 2024
1 parent 27cf054 commit 84507c5
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 119 deletions.
10 changes: 5 additions & 5 deletions crates/bitwarden/src/admin_console/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use uuid::Uuid;

use crate::error::{Error, Result};
use crate::error::{require, Error, Result};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
pub struct Policy {
Expand Down Expand Up @@ -41,11 +41,11 @@ impl TryFrom<PolicyResponseModel> for Policy {

fn try_from(policy: PolicyResponseModel) -> Result<Self> {
Ok(Self {
id: policy.id.ok_or(Error::MissingFields)?,
organization_id: policy.organization_id.ok_or(Error::MissingFields)?,
r#type: policy.r#type.ok_or(Error::MissingFields)?.into(),
id: require!(policy.id),
organization_id: require!(policy.organization_id),
r#type: require!(policy.r#type).into(),

Check warning on line 46 in crates/bitwarden/src/admin_console/policy.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/admin_console/policy.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
data: policy.data,
enabled: policy.enabled.ok_or(Error::MissingFields)?,
enabled: require!(policy.enabled),

Check warning on line 48 in crates/bitwarden/src/admin_console/policy.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/admin_console/policy.rs#L48

Added line #L48 was not covered by tests
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/bitwarden/src/auth/login/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
AccessToken, JWTToken,
},
client::{LoginMethod, ServiceAccountLoginMethod},
error::{Error, Result},
error::{require, Error, Result},
secrets_manager::state::{self, ClientState},
Client,
};
Expand Down Expand Up @@ -69,9 +69,7 @@ pub(crate) async fn login_access_token(
let access_token_obj: JWTToken = r.access_token.parse()?;

// This should always be Some() when logging in with an access token
let organization_id = access_token_obj
.organization
.ok_or(Error::MissingFields)?
let organization_id = require!(access_token_obj.organization)
.parse()
.map_err(|_| Error::InvalidResponse)?;

Expand Down
14 changes: 12 additions & 2 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub enum Error {

#[error("The response received was invalid and could not be processed")]
InvalidResponse,
#[error("The response received was missing some of the required fields")]
MissingFields,
#[error("The response received was missing some of the required fields: {0}")]
MissingFields(&'static str),

#[error("Cryptography error, {0}")]
Crypto(#[from] bitwarden_crypto::CryptoError),
Expand Down Expand Up @@ -133,4 +133,14 @@ macro_rules! impl_bitwarden_error {
impl_bitwarden_error!(ApiError);
impl_bitwarden_error!(IdentityError);

macro_rules! require {
($val:expr) => {
match $val {
Some(val) => val,
None => return Err($crate::error::Error::MissingFields(stringify!($val))),
}
};
}
pub(crate) use require;

pub type Result<T, E = Error> = std::result::Result<T, E>;
8 changes: 4 additions & 4 deletions crates/bitwarden/src/platform/domain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::error::{require, Error, Result};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
pub struct GlobalDomains {
Expand All @@ -15,9 +15,9 @@ impl TryFrom<bitwarden_api_api::models::GlobalDomains> for GlobalDomains {

fn try_from(global_domains: bitwarden_api_api::models::GlobalDomains) -> Result<Self> {
Ok(Self {
r#type: global_domains.r#type.ok_or(Error::MissingFields)?,
domains: global_domains.domains.ok_or(Error::MissingFields)?,
excluded: global_domains.excluded.ok_or(Error::MissingFields)?,
r#type: require!(global_domains.r#type),
domains: require!(global_domains.domains),
excluded: require!(global_domains.excluded),

Check warning on line 20 in crates/bitwarden/src/platform/domain.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/domain.rs#L18-L20

Added lines #L18 - L20 were not covered by tests
})
}
}
8 changes: 3 additions & 5 deletions crates/bitwarden/src/platform/get_user_api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use super::SecretVerificationRequest;
use crate::{
client::{LoginMethod, UserLoginMethod},
error::{Error, Result},
error::{require, Error, Result},
Client,
};

Expand Down Expand Up @@ -75,9 +75,7 @@ pub struct UserApiKeyResponse {

impl UserApiKeyResponse {
pub(crate) fn process_response(response: ApiKeyResponseModel) -> Result<UserApiKeyResponse> {
match response.api_key {
Some(api_key) => Ok(UserApiKeyResponse { api_key }),
None => Err(Error::MissingFields),
}
let api_key = require!(response.api_key);
Ok(UserApiKeyResponse { api_key })

Check warning on line 79 in crates/bitwarden/src/platform/get_user_api_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/get_user_api_key.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
}
}
29 changes: 13 additions & 16 deletions crates/bitwarden/src/platform/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::domain::GlobalDomains;
use crate::{
admin_console::Policy,
client::{encryption_settings::EncryptionSettings, Client},
error::{Error, Result},
error::{require, Error, Result},
vault::{Cipher, Collection, Folder},
};

Expand All @@ -25,10 +25,7 @@ pub(crate) async fn sync(client: &mut Client, input: &SyncRequest) -> Result<Syn
let sync =
bitwarden_api_api::apis::sync_api::sync_get(&config.api, input.exclude_subdomains).await?;

let org_keys: Vec<_> = sync
.profile
.as_ref()
.ok_or(Error::MissingFields)?
let org_keys: Vec<_> = require!(sync.profile.as_ref())

Check warning on line 28 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L28

Added line #L28 was not covered by tests
.organizations
.as_deref()
.unwrap_or_default()
Expand Down Expand Up @@ -86,8 +83,8 @@ impl SyncResponse {
response: SyncResponseModel,
enc: &EncryptionSettings,
) -> Result<SyncResponse> {
let profile = *response.profile.ok_or(Error::MissingFields)?;
let ciphers = response.ciphers.ok_or(Error::MissingFields)?;
let profile = require!(response.profile);
let ciphers = require!(response.ciphers);

Check warning on line 87 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L86-L87

Added lines #L86 - L87 were not covered by tests

fn try_into_iter<In, InItem, Out, OutItem>(iter: In) -> Result<Out, InItem::Error>
where
Expand All @@ -99,13 +96,13 @@ impl SyncResponse {
}

Ok(SyncResponse {
profile: ProfileResponse::process_response(profile, enc)?,
folders: try_into_iter(response.folders.ok_or(Error::MissingFields)?)?,
collections: try_into_iter(response.collections.ok_or(Error::MissingFields)?)?,
profile: ProfileResponse::process_response(*profile, enc)?,
folders: try_into_iter(require!(response.folders))?,
collections: try_into_iter(require!(response.collections))?,

Check warning on line 101 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L99-L101

Added lines #L99 - L101 were not covered by tests
ciphers: try_into_iter(ciphers)?,
domains: response.domains.map(|d| (*d).try_into()).transpose()?,
policies: try_into_iter(response.policies.ok_or(Error::MissingFields)?)?,
sends: try_into_iter(response.sends.ok_or(Error::MissingFields)?)?,
policies: try_into_iter(require!(response.policies))?,
sends: try_into_iter(require!(response.sends))?,

Check warning on line 105 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L104-L105

Added lines #L104 - L105 were not covered by tests
})
}
}
Expand All @@ -115,7 +112,7 @@ impl ProfileOrganizationResponse {
response: ProfileOrganizationResponseModel,
) -> Result<ProfileOrganizationResponse> {
Ok(ProfileOrganizationResponse {
id: response.id.ok_or(Error::MissingFields)?,
id: require!(response.id),

Check warning on line 115 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L115

Added line #L115 was not covered by tests
})
}
}
Expand All @@ -126,9 +123,9 @@ impl ProfileResponse {
_enc: &EncryptionSettings,
) -> Result<ProfileResponse> {
Ok(ProfileResponse {
id: response.id.ok_or(Error::MissingFields)?,
name: response.name.ok_or(Error::MissingFields)?,
email: response.email.ok_or(Error::MissingFields)?,
id: require!(response.id),
name: require!(response.name),
email: require!(response.email),

Check warning on line 128 in crates/bitwarden/src/platform/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/sync.rs#L126-L128

Added lines #L126 - L128 were not covered by tests
//key: response.key,
//private_key: response.private_key,
organizations: response
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/secrets_manager/projects/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use uuid::Uuid;

use crate::{
client::Client,
error::{Error, Result},
error::{require, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ProjectDeleteResponse {
response: BulkDeleteResponseModel,
) -> Result<ProjectDeleteResponse> {
Ok(ProjectDeleteResponse {
id: response.id.ok_or(Error::MissingFields)?,
id: require!(response.id),

Check warning on line 65 in crates/bitwarden/src/secrets_manager/projects/delete.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/projects/delete.rs#L65

Added line #L65 was not covered by tests
error: response.error,
})
}
Expand Down
20 changes: 6 additions & 14 deletions crates/bitwarden/src/secrets_manager/projects/project_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use uuid::Uuid;

use crate::{
client::encryption_settings::EncryptionSettings,
error::{Error, Result},
error::{require, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand All @@ -25,27 +25,19 @@ impl ProjectResponse {
response: ProjectResponseModel,
enc: &EncryptionSettings,
) -> Result<Self> {
let organization_id = response.organization_id.ok_or(Error::MissingFields)?;
let organization_id = require!(response.organization_id);

Check warning on line 28 in crates/bitwarden/src/secrets_manager/projects/project_response.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/projects/project_response.rs#L28

Added line #L28 was not covered by tests

let name = response
.name
.ok_or(Error::MissingFields)?
let name = require!(response.name)

Check warning on line 30 in crates/bitwarden/src/secrets_manager/projects/project_response.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/projects/project_response.rs#L30

Added line #L30 was not covered by tests
.parse::<EncString>()?
.decrypt(enc, &Some(organization_id))?;

Ok(ProjectResponse {
id: response.id.ok_or(Error::MissingFields)?,
id: require!(response.id),

Check warning on line 35 in crates/bitwarden/src/secrets_manager/projects/project_response.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/projects/project_response.rs#L35

Added line #L35 was not covered by tests
organization_id,
name,

creation_date: response
.creation_date
.ok_or(Error::MissingFields)?
.parse()?,
revision_date: response
.revision_date
.ok_or(Error::MissingFields)?
.parse()?,
creation_date: require!(response.creation_date).parse()?,
revision_date: require!(response.revision_date).parse()?,

Check warning on line 40 in crates/bitwarden/src/secrets_manager/projects/project_response.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/projects/project_response.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
})
}
}
4 changes: 2 additions & 2 deletions crates/bitwarden/src/secrets_manager/secrets/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use uuid::Uuid;

use crate::{
client::Client,
error::{Error, Result},
error::{require, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -62,7 +62,7 @@ impl SecretDeleteResponse {
response: BulkDeleteResponseModel,
) -> Result<SecretDeleteResponse> {
Ok(SecretDeleteResponse {
id: response.id.ok_or(Error::MissingFields)?,
id: require!(response.id),

Check warning on line 65 in crates/bitwarden/src/secrets_manager/secrets/delete.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/delete.rs#L65

Added line #L65 was not covered by tests
error: response.error,
})
}
Expand Down
10 changes: 4 additions & 6 deletions crates/bitwarden/src/secrets_manager/secrets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use uuid::Uuid;

use crate::{
client::{encryption_settings::EncryptionSettings, Client},
error::{Error, Result},
error::{require, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -93,16 +93,14 @@ impl SecretIdentifierResponse {
response: SecretsWithProjectsInnerSecret,
enc: &EncryptionSettings,
) -> Result<SecretIdentifierResponse> {
let organization_id = response.organization_id.ok_or(Error::MissingFields)?;
let organization_id = require!(response.organization_id);

let key = response
.key
.ok_or(Error::MissingFields)?
let key = require!(response.key)
.parse::<EncString>()?
.decrypt(enc, &Some(organization_id))?;

Ok(SecretIdentifierResponse {
id: response.id.ok_or(Error::MissingFields)?,
id: require!(response.id),
organization_id,
key,
})
Expand Down
28 changes: 8 additions & 20 deletions crates/bitwarden/src/secrets_manager/secrets/secret_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uuid::Uuid;

use crate::{
client::encryption_settings::EncryptionSettings,
error::{Error, Result},
error::{require, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -51,19 +51,13 @@ impl SecretResponse {
) -> Result<SecretResponse> {
let org_id = response.organization_id;

let key = response
.key
.ok_or(Error::MissingFields)?
let key = require!(response.key)
.parse::<EncString>()?
.decrypt(enc, &org_id)?;
let value = response
.value
.ok_or(Error::MissingFields)?
let value = require!(response.value)
.parse::<EncString>()?
.decrypt(enc, &org_id)?;
let note = response
.note
.ok_or(Error::MissingFields)?
let note = require!(response.note)
.parse::<EncString>()?
.decrypt(enc, &org_id)?;

Expand All @@ -73,21 +67,15 @@ impl SecretResponse {
.and_then(|p| p.id);

Ok(SecretResponse {
id: response.id.ok_or(Error::MissingFields)?,
organization_id: org_id.ok_or(Error::MissingFields)?,
id: require!(response.id),
organization_id: require!(org_id),
project_id: project,
key,
value,
note,

creation_date: response
.creation_date
.ok_or(Error::MissingFields)?
.parse()?,
revision_date: response
.revision_date
.ok_or(Error::MissingFields)?
.parse()?,
creation_date: require!(response.creation_date).parse()?,
revision_date: require!(response.revision_date).parse()?,
})
}
}
Expand Down
Loading

0 comments on commit 84507c5

Please sign in to comment.