-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Type of change <!-- (mark with an `X`) --> ``` - [ ] Bug fix - [X] New feature development - [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [ ] Build/deploy pipeline (DevOps) - [ ] Other ``` ## Objective <!--Describe what the purpose of this PR is. For example: what bug you're fixing or what new feature you're adding--> The purpose of this PR is to expose the ability to preform secrets syncs via the Secrets Manager SDK. Requires API bindings in #674 This should not be merged into main prior to: - The server PR bitwarden/server#3906 being merged and released - The API bindings PR #674 being merged into main ## Code changes <!--Explain the changes you've made to each file or major component. This should help the reviewer understand your changes--> <!--Also refer to any related changes or PRs in other repositories--> - **crates/bitwarden/src/secrets_manager/client_secrets.rs:** Add the `sync` method to the client secrets. - **crates/bitwarden/src/secrets_manager/secrets/mod.rs:** Expose `sync` `SecretsSyncRequest`, and `SecretsSyncResponse` - **crates/bitwarden/src/secrets_manager/secrets/sync.rs:** Implement `sync` `SecretsSyncRequest`, and `SecretsSyncResponse` ## Before you submit - Please add **unit tests** where it makes sense to do so
- Loading branch information
1 parent
f07d9a7
commit 295d3c5
Showing
4 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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::{require, Result}, | ||
Client, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, JsonSchema)] | ||
#[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()); | ||
|
||
let res = bitwarden_api_api::apis::secrets_api::organizations_organization_id_secrets_sync_get( | ||
&config.api, | ||
input.organization_id, | ||
last_synced_date, | ||
) | ||
.await?; | ||
|
||
let enc = client.get_encryption_settings()?; | ||
|
||
SecretsSyncResponse::process_response(res, enc) | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, JsonSchema)] | ||
#[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 = require!(response.has_changes); | ||
|
||
if has_changes { | ||
let secrets = require!(response.secrets) | ||
.data | ||
.unwrap_or_default() | ||
.into_iter() | ||
.map(|r| SecretResponse::process_base_response(r, enc)) | ||
.collect::<Result<_, _>>()?; | ||
return Ok(SecretsSyncResponse { | ||
has_changes, | ||
secrets: Some(secrets), | ||
}); | ||
} | ||
|
||
Ok(SecretsSyncResponse { | ||
has_changes: false, | ||
secrets: None, | ||
}) | ||
} | ||
} |