Skip to content

Add functions for interval based secret loading #261

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 12 additions & 5 deletions bitwarden_license/bitwarden-sm/src/client_secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use crate::{
error::SecretsManagerError,
secrets::{
create_secret, delete_secrets, get_secret, get_secrets_by_ids, list_secrets,
list_secrets_by_project, sync_secrets, update_secret, SecretCreateRequest,
create_secret, delete_secrets, get_secret, get_secrets_by_ids, get_secrets_by_project,
list_secrets, list_secrets_by_project, sync_secrets, update_secret, SecretCreateRequest,
SecretGetRequest, SecretIdentifiersByProjectRequest, SecretIdentifiersRequest,
SecretIdentifiersResponse, SecretPutRequest, SecretResponse, SecretsDeleteRequest,
SecretsDeleteResponse, SecretsGetRequest, SecretsResponse, SecretsSyncRequest,
SecretsSyncResponse,
SecretIdentifiersResponse, SecretPutRequest, SecretResponse, SecretsByProjectGetRequest,
SecretsDeleteRequest, SecretsDeleteResponse, SecretsGetRequest, SecretsResponse,
SecretsSyncRequest, SecretsSyncResponse,
},
};

Expand Down Expand Up @@ -79,6 +79,13 @@
) -> Result<SecretsSyncResponse, SecretsManagerError> {
sync_secrets(&self.client, input).await
}

pub async fn get_by_project(
&self,
input: &SecretsByProjectGetRequest,
) -> Result<SecretsResponse, SecretsManagerError> {
get_secrets_by_project(&self.client, input).await
}

Check warning on line 88 in bitwarden_license/bitwarden-sm/src/client_secrets.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/client_secrets.rs#L83-L88

Added lines #L83 - L88 were not covered by tests
}

/// This trait is for backward compatibility
Expand Down
85 changes: 85 additions & 0 deletions bitwarden_license/bitwarden-sm/src/secrets/get.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use bitwarden_api_api::models::GetSecretsRequestModel;
use bitwarden_core::Client;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{error::SecretsManagerError, secrets::SecretResponse};

use super::SecretsResponse;

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SecretGetRequest {
Expand All @@ -23,3 +26,85 @@

SecretResponse::process_response(res, &mut key_store.context())
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Request to sync secrets for a specific project
pub struct SecretsByProjectGetRequest {
/// Project ID to sync secrets from
pub project_id: Uuid,
}

// get_secrets_full(id: ProjectId);
// return: Vec<{ secrets_name: }>

// get_secrets_by_project_view(id: ProjectId)
// return: Vec<{ secret_id: SecretId, secret_name: &str, revision_date: DateTime<Utc> }>

// get_secret(id: SecretId)

// Will call on initial load to get all the data
pub(crate) async fn get_secrets_by_project(
client: &Client,
input: &SecretsByProjectGetRequest,
) -> Result<SecretsResponse, SecretsManagerError> {
let config = client.internal.get_api_configurations().await;

Check warning on line 51 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L47-L51

Added lines #L47 - L51 were not covered by tests
// let last_synced_date = input.last_synced_date.map(|date| date.to_rfc3339());

let secrets_with_project_list =
bitwarden_api_api::apis::secrets_api::projects_project_id_secrets_get(
&config.api,
input.project_id,
// last_synced_date,
)
.await?;

Check warning on line 60 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L54-L60

Added lines #L54 - L60 were not covered by tests

let secret_ids: Vec<Uuid> = secrets_with_project_list
.secrets
.unwrap_or_default()
.into_iter()
.map(|s| s.id.unwrap_or_default())
.collect();

let request = Some(GetSecretsRequestModel { ids: secret_ids });

Check warning on line 69 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L62-L69

Added lines #L62 - L69 were not covered by tests

let res =
bitwarden_api_api::apis::secrets_api::secrets_get_by_ids_post(&config.api, request).await?;

Check warning on line 72 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L71-L72

Added lines #L71 - L72 were not covered by tests

let key_store = client.internal.get_key_store();

SecretsResponse::process_response(res, &mut key_store.context())
}

Check warning on line 77 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L74-L77

Added lines #L74 - L77 were not covered by tests

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SecretSlimResponse {
pub id: Uuid,
pub revision_date: chrono::DateTime<chrono::Utc>,
}

pub(crate) async fn get_secrets_view_by_project(
client: &Client,
input: &SecretsByProjectGetRequest,
) -> Result<Vec<SecretSlimResponse>, SecretsManagerError> {
let config = client.internal.get_api_configurations().await;

Check warning on line 90 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L86-L90

Added lines #L86 - L90 were not covered by tests

let secrets_with_project_list =
bitwarden_api_api::apis::secrets_api::projects_project_id_secrets_get(
&config.api,
input.project_id,
)
.await?;

Check warning on line 97 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L92-L97

Added lines #L92 - L97 were not covered by tests

secrets_with_project_list
.secrets
.unwrap_or_default()
.into_iter()
.map(|s| {

Check warning on line 103 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L99-L103

Added lines #L99 - L103 were not covered by tests
Ok(SecretSlimResponse {
id: bitwarden_core::require!(s.id),
revision_date: bitwarden_core::require!(s.revision_date).parse()?,

Check warning on line 106 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L105-L106

Added lines #L105 - L106 were not covered by tests
})
})
.collect()
}

Check warning on line 110 in bitwarden_license/bitwarden-sm/src/secrets/get.rs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/bitwarden-sm/src/secrets/get.rs#L108-L110

Added lines #L108 - L110 were not covered by tests
4 changes: 2 additions & 2 deletions bitwarden_license/bitwarden-sm/src/secrets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub(crate) use create::create_secret;
pub use create::SecretCreateRequest;
pub(crate) use delete::delete_secrets;
pub use delete::{SecretsDeleteRequest, SecretsDeleteResponse};
pub(crate) use get::get_secret;
pub use get::SecretGetRequest;
pub(crate) use get::{get_secret, get_secrets_by_project, get_secrets_view_by_project};
pub use get::{SecretGetRequest, SecretsByProjectGetRequest};
pub(crate) use get_by_ids::get_secrets_by_ids;
pub use get_by_ids::SecretsGetRequest;
pub(crate) use list::{list_secrets, list_secrets_by_project};
Expand Down
Loading