Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SM-1153] Part 2 Secrets sync | Expose in SM SDK #678

Merged
merged 16 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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;
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)]

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 = require!(response.has_changes);

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 {
let secrets = require!(response.secrets)

Check warning on line 57 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-L57

Added lines #L56 - L57 were not covered by tests
.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,
})
}

Check warning on line 73 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#L59-L73

Added lines #L59 - L73 were not covered by tests
}
Loading