Skip to content

Commit

Permalink
[SM-1153] Part 2 Secrets sync | Expose in SM SDK (#678)
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
Thomas-Avery authored May 15, 2024
1 parent f07d9a7 commit 295d3c5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
4 changes: 4 additions & 0 deletions crates/bitwarden/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Support for secrets sync (#678)

## [0.5.0] - 2024-04-26

### Changed
Expand Down
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
}
}

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;
74 changes: 74 additions & 0 deletions crates/bitwarden/src/secrets_manager/secrets/sync.rs
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,
})
}
}

0 comments on commit 295d3c5

Please sign in to comment.