Skip to content

Commit

Permalink
Add secrets sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Avery committed Mar 22, 2024
1 parent 89ec8c0 commit 2325628
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/bitwarden/src/secrets_manager/client_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::{
error::Result,
secrets_manager::secrets::{
create_secret, delete_secrets, get_secret, get_secrets_by_ids, list_secrets,
list_secrets_by_project, update_secret, SecretCreateRequest, SecretGetRequest,
SecretIdentifiersByProjectRequest, SecretIdentifiersRequest, SecretIdentifiersResponse,
SecretPutRequest, SecretResponse, SecretsDeleteRequest, SecretsDeleteResponse,
SecretsGetRequest, SecretsResponse,
list_secrets_by_project, sync_secrets, update_secret, SecretCreateRequest,
SecretGetRequest, SecretIdentifiersByProjectRequest, SecretIdentifiersRequest,
SecretIdentifiersResponse, SecretPutRequest, SecretResponse, SecretsDeleteRequest,
SecretsDeleteResponse, SecretsGetRequest, SecretsResponse, SecretsSyncRequest,
SecretsSyncResponse,
},
Client,
};
Expand Down Expand Up @@ -48,6 +49,10 @@ impl<'a> ClientSecrets<'a> {
pub async fn delete(&mut self, input: SecretsDeleteRequest) -> Result<SecretsDeleteResponse> {
delete_secrets(self.client, input).await
}

pub async fn sync(&mut self, input: &SecretsSyncRequest) -> Result<SecretsSyncResponse> {
sync_secrets(self.client, input).await
}

Check warning on line 55 in crates/bitwarden/src/secrets_manager/client_secrets.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/client_secrets.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
}

impl<'a> Client {
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden/src/secrets_manager/secrets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod get;
mod get_by_ids;
mod list;
mod secret_response;
mod sync;
mod update;

pub(crate) use create::create_secret;
Expand All @@ -19,5 +20,7 @@ pub use list::{
SecretIdentifiersByProjectRequest, SecretIdentifiersRequest, SecretIdentifiersResponse,
};
pub use secret_response::{SecretResponse, SecretsResponse};
pub(crate) use sync::sync_secrets;
pub use sync::{SecretsSyncRequest, SecretsSyncResponse};
pub(crate) use update::update_secret;
pub use update::SecretPutRequest;
79 changes: 79 additions & 0 deletions crates/bitwarden/src/secrets_manager/secrets/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use bitwarden_api_api::models::SecretsSyncResponseModel;
use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

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

#[derive(Serialize, Deserialize, Debug, JsonSchema)]

Check warning on line 14 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L14

Added line #L14 was not covered by tests
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SecretsSyncRequest {
/// Organization to sync secrets from
pub organization_id: Uuid,
/// Optional date time a sync last occurred
pub last_synced_date: Option<DateTime<Utc>>,
}

pub(crate) async fn sync_secrets(
client: &mut Client,
input: &SecretsSyncRequest,
) -> Result<SecretsSyncResponse> {
let config = client.get_api_configurations().await;
let last_synced_date = input.last_synced_date.map(|date| date.to_rfc3339());

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

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L23-L28

Added lines #L23 - L28 were not covered by tests

let res = bitwarden_api_api::apis::secrets_api::organizations_organization_id_secrets_sync_get(
&config.api,
input.organization_id,
last_synced_date,
)
.await?;

Check warning on line 35 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L30-L35

Added lines #L30 - L35 were not covered by tests

let enc = client.get_encryption_settings()?;

Check warning on line 37 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L37

Added line #L37 was not covered by tests

SecretsSyncResponse::process_response(res, enc)
}

Check warning on line 40 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

#[derive(Serialize, Deserialize, Debug, JsonSchema)]

Check warning on line 42 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L42

Added line #L42 was not covered by tests
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SecretsSyncResponse {
pub has_changes: bool,
pub secrets: Option<Vec<SecretResponse>>,
}

impl SecretsSyncResponse {
pub(crate) fn process_response(
response: SecretsSyncResponseModel,
enc: &EncryptionSettings,
) -> Result<SecretsSyncResponse> {
let has_changes = response.has_changes.ok_or(Error::MissingFields)?;

Check warning on line 54 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L50-L54

Added lines #L50 - L54 were not covered by tests

if has_changes && response.secrets.is_some() {
let secrets = response
.secrets
.unwrap()
.data
.unwrap_or_default()
.into_iter()
.map(|r| SecretResponse::process_base_response(r, enc))
.collect::<Result<_, _>>()?;

Check warning on line 64 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L56-L64

Added lines #L56 - L64 were not covered by tests

return Ok(SecretsSyncResponse {
has_changes,
secrets: Some(secrets),
});
} else if has_changes && response.secrets.is_none() {
return Err(Error::MissingFields);
}

return Ok(SecretsSyncResponse {
has_changes: false,
secrets: None,
});

Check failure

Code scanning / clippy

unneeded return statement Error

unneeded return statement
}

Check warning on line 78 in crates/bitwarden/src/secrets_manager/secrets/sync.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/secrets/sync.rs#L66-L78

Added lines #L66 - L78 were not covered by tests
}

0 comments on commit 2325628

Please sign in to comment.