Skip to content

Commit

Permalink
Merge pull request #37 from Infisical/daniel/fix-cache-path-bug
Browse files Browse the repository at this point in the history
Fix: Caching path bug #29
  • Loading branch information
DanielHougaard authored May 27, 2024
2 parents 0d687a0 + 78050fa commit 987f62f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/infisical/src/api/secrets/create_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub async fn create_secret_request(
&response.secret.secret_key,
&response.secret.r#type,
&response.secret.environment,
input.path.as_ref().unwrap_or(&"/".to_string()),
);

Ok(response)
Expand Down
1 change: 1 addition & 0 deletions crates/infisical/src/api/secrets/delete_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub async fn delete_secret_request(
&response.secret.secret_key,
&response.secret.r#type,
&response.secret.environment,
input.path.as_ref().unwrap_or(&"/".to_string()),
);

Ok(response)
Expand Down
19 changes: 17 additions & 2 deletions crates/infisical/src/api/secrets/get_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,20 @@ pub async fn get_secret_request(
Some(r#type) => r#type,
None => "shared",
};

let secret_path = match input.path.as_ref() {
Some(path) => path,
None => "/",
};

let cached_secret = get_secret_from_cache(
client,
&create_cache_key(&input.secret_name, secret_type, &input.environment),
&create_cache_key(
&input.secret_name,
secret_type,
&input.environment,
secret_path,
),
);

if cached_secret.is_some() {
Expand Down Expand Up @@ -65,7 +76,11 @@ pub async fn get_secret_request(
if status == StatusCode::OK {
let response = response.json::<GetSecretResponse>().await?;

add_to_cache(client, &response.secret);
add_to_cache(
client,
&response.secret,
input.path.as_ref().unwrap_or(&"/".to_string()),
);

Ok(response)
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/infisical/src/api/secrets/update_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub async fn update_secret_request(
&response.secret.secret_key,
&response.secret.r#type,
&response.secret.environment,
input.path.as_ref().unwrap_or(&"/".to_string()),
);

Ok(response)
Expand Down
24 changes: 19 additions & 5 deletions crates/infisical/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,30 @@ fn get_sys_time_in_ms() -> Result<u64, SystemTimeError> {
return Ok(sec * 1000);
}

pub fn create_cache_key(secret_key: &str, secret_type: &str, environment: &str) -> String {
return format!("{}-{}-{}", secret_key, environment, secret_type);
pub fn create_cache_key(
secret_key: &str,
secret_type: &str,
environment: &str,
secret_path: &str,
) -> String {
return format!(
"{}-{}-{}-{}",
secret_key, environment, secret_type, secret_path
);
}

pub fn add_to_cache(client: &mut Client, secret: &Secret) {
pub fn add_to_cache(client: &mut Client, secret: &Secret, secret_path: &str) {
if client.cache_ttl == 0 {
debug!("[CACHE]: Cache TTL is set to 0, not adding secret to cache.");
return;
}

let key = create_cache_key(&secret.secret_key, &secret.r#type, &secret.environment);
let key = create_cache_key(
&secret.secret_key,
&secret.r#type,
&secret.environment,
secret_path,
);

let existing_secret = get_secret_from_cache(client, &key);

Expand Down Expand Up @@ -72,13 +85,14 @@ pub fn remove_from_cache(
secret_key: &str,
secret_type: &str,
environment: &str,
secret_path: &str,
) {
if client.cache_ttl == 0 {
debug!("[CACHE]: Cache TTL is set to 0, not removing secret from cache.");
return;
}

let key = create_cache_key(&secret_key, &secret_type, &environment);
let key = create_cache_key(secret_key, secret_type, environment, secret_path);

let mut cache = client.cache.lock().unwrap();

Expand Down
1 change: 1 addition & 0 deletions crates/infisical/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub fn get_fallback_env_secret(key: &str) -> Option<Secret> {
secret_comment: "".to_string(),
r#type: "".to_string(),
environment: "".to_string(),
secret_path: None,

secret_key: key.to_string(),
secret_value: "".to_string(),
Expand Down
5 changes: 5 additions & 0 deletions crates/infisical/src/manager/secrets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub struct Secret {
pub secret_value: String,
pub secret_comment: String,

#[schemars(
description = "The path of the secret.\n\nNote that this will only be present when using the `list secrets` method."
)]
pub secret_path: Option<String>,

#[serde(default = "default_as_false")]
pub is_fallback: bool,
}

0 comments on commit 987f62f

Please sign in to comment.