feat: Secret module works with infisical and local file storage backends
All checks were successful
Run Check Script / check (pull_request) Successful in 1m9s
All checks were successful
Run Check Script / check (pull_request) Successful in 1m9s
This commit is contained in:
129
harmony_secret/src/store/infisical.rs
Normal file
129
harmony_secret/src/store/infisical.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use crate::{SecretStore, SecretStoreError};
|
||||
use async_trait::async_trait;
|
||||
use infisical::{
|
||||
AuthMethod, InfisicalError,
|
||||
client::Client,
|
||||
secrets::{CreateSecretRequest, GetSecretRequest, UpdateSecretRequest},
|
||||
};
|
||||
use log::{info, warn};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InfisicalSecretStore {
|
||||
client: Client,
|
||||
project_id: String,
|
||||
environment: String,
|
||||
}
|
||||
|
||||
impl InfisicalSecretStore {
|
||||
/// Creates a new, authenticated Infisical client.
|
||||
pub async fn new(
|
||||
base_url: String,
|
||||
project_id: String,
|
||||
environment: String,
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
) -> Result<Self, InfisicalError> {
|
||||
info!("INFISICAL_STORE: Initializing client for URL: {base_url}");
|
||||
|
||||
// The builder and login logic remains the same.
|
||||
let mut client = Client::builder().base_url(base_url).build().await?;
|
||||
let auth_method = AuthMethod::new_universal_auth(client_id, client_secret);
|
||||
client.login(auth_method).await?;
|
||||
|
||||
info!("INFISICAL_STORE: Client authenticated successfully.");
|
||||
Ok(Self {
|
||||
client,
|
||||
project_id,
|
||||
environment,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SecretStore for InfisicalSecretStore {
|
||||
async fn get_raw(&self, _environment: &str, key: &str) -> Result<Vec<u8>, SecretStoreError> {
|
||||
let environment = &self.environment;
|
||||
info!("INFISICAL_STORE: Getting key '{key}' from environment '{environment}'");
|
||||
|
||||
let request = GetSecretRequest::builder(key, &self.project_id, environment).build();
|
||||
|
||||
match self.client.secrets().get(request).await {
|
||||
Ok(secret) => Ok(secret.secret_value.into_bytes()),
|
||||
Err(e) => {
|
||||
// Correctly match against the actual InfisicalError enum.
|
||||
match e {
|
||||
// The specific case for a 404 Not Found error.
|
||||
InfisicalError::HttpError { status, .. }
|
||||
if status == http::StatusCode::NOT_FOUND =>
|
||||
{
|
||||
Err(SecretStoreError::NotFound {
|
||||
namespace: environment.to_string(),
|
||||
key: key.to_string(),
|
||||
})
|
||||
}
|
||||
// For all other errors, wrap them in our generic Store error.
|
||||
_ => Err(SecretStoreError::Store(Box::new(e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_raw(
|
||||
&self,
|
||||
_environment: &str,
|
||||
key: &str,
|
||||
val: &[u8],
|
||||
) -> Result<(), SecretStoreError> {
|
||||
info!(
|
||||
"INFISICAL_STORE: Setting key '{key}' in environment '{}'",
|
||||
self.environment
|
||||
);
|
||||
let value_str =
|
||||
String::from_utf8(val.to_vec()).map_err(|e| SecretStoreError::Store(Box::new(e)))?;
|
||||
|
||||
// --- Upsert Logic ---
|
||||
// First, attempt to update the secret.
|
||||
let update_req = UpdateSecretRequest::builder(key, &self.project_id, &self.environment)
|
||||
.secret_value(&value_str)
|
||||
.build();
|
||||
|
||||
match self.client.secrets().update(update_req).await {
|
||||
Ok(_) => {
|
||||
info!("INFISICAL_STORE: Successfully updated secret '{key}'.");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
// If the update failed, check if it was because the secret doesn't exist.
|
||||
match e {
|
||||
InfisicalError::HttpError { status, .. }
|
||||
if status == http::StatusCode::NOT_FOUND =>
|
||||
{
|
||||
// The secret was not found, so we create it instead.
|
||||
warn!(
|
||||
"INFISICAL_STORE: Secret '{key}' not found for update, attempting to create it."
|
||||
);
|
||||
let create_req = CreateSecretRequest::builder(
|
||||
key,
|
||||
&value_str,
|
||||
&self.project_id,
|
||||
&self.environment,
|
||||
)
|
||||
.build();
|
||||
|
||||
// Handle potential errors during creation.
|
||||
self.client
|
||||
.secrets()
|
||||
.create(create_req)
|
||||
.await
|
||||
.map_err(|create_err| SecretStoreError::Store(Box::new(create_err)))?;
|
||||
|
||||
info!("INFISICAL_STORE: Successfully created secret '{key}'.");
|
||||
Ok(())
|
||||
}
|
||||
// Any other error during update is a genuine failure.
|
||||
_ => Err(SecretStoreError::Store(Box::new(e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
105
harmony_secret/src/store/local_file.rs
Normal file
105
harmony_secret/src/store/local_file.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use async_trait::async_trait;
|
||||
use log::info;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::{SecretStore, SecretStoreError};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct LocalFileSecretStore;
|
||||
|
||||
impl LocalFileSecretStore {
|
||||
/// Helper to consistently generate the secret file path.
|
||||
fn get_file_path(base_dir: &Path, ns: &str, key: &str) -> PathBuf {
|
||||
base_dir.join(format!("{ns}_{key}.json"))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SecretStore for LocalFileSecretStore {
|
||||
async fn get_raw(&self, ns: &str, key: &str) -> Result<Vec<u8>, SecretStoreError> {
|
||||
let data_dir = directories::BaseDirs::new()
|
||||
.expect("Could not find a valid home directory")
|
||||
.data_dir()
|
||||
.join("harmony")
|
||||
.join("secrets");
|
||||
|
||||
let file_path = Self::get_file_path(&data_dir, ns, key);
|
||||
info!(
|
||||
"LOCAL_STORE: Getting key '{key}' from namespace '{ns}' at {}",
|
||||
file_path.display()
|
||||
);
|
||||
|
||||
tokio::fs::read(&file_path)
|
||||
.await
|
||||
.map_err(|_| SecretStoreError::NotFound {
|
||||
namespace: ns.to_string(),
|
||||
key: key.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn set_raw(&self, ns: &str, key: &str, val: &[u8]) -> Result<(), SecretStoreError> {
|
||||
let data_dir = directories::BaseDirs::new()
|
||||
.expect("Could not find a valid home directory")
|
||||
.data_dir()
|
||||
.join("harmony")
|
||||
.join("secrets");
|
||||
|
||||
let file_path = Self::get_file_path(&data_dir, ns, key);
|
||||
info!(
|
||||
"LOCAL_STORE: Setting key '{key}' in namespace '{ns}' at {}",
|
||||
file_path.display()
|
||||
);
|
||||
|
||||
if let Some(parent_dir) = file_path.parent() {
|
||||
tokio::fs::create_dir_all(parent_dir)
|
||||
.await
|
||||
.map_err(|e| SecretStoreError::Store(Box::new(e)))?;
|
||||
}
|
||||
|
||||
tokio::fs::write(&file_path, val)
|
||||
.await
|
||||
.map_err(|e| SecretStoreError::Store(Box::new(e)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_and_get_raw_successfully() {
|
||||
let dir = tempdir().unwrap();
|
||||
let store = LocalFileSecretStore::default();
|
||||
let ns = "test-ns";
|
||||
let key = "test-key";
|
||||
let value = b"{\"data\":\"test-value\"}";
|
||||
|
||||
// To test the store directly, we override the base directory logic.
|
||||
// For this test, we'll manually construct the path within our temp dir.
|
||||
let file_path = LocalFileSecretStore::get_file_path(dir.path(), ns, key);
|
||||
|
||||
// Manually write to the temp path to simulate the store's behavior
|
||||
tokio::fs::create_dir_all(file_path.parent().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::fs::write(&file_path, value).await.unwrap();
|
||||
|
||||
// Now, test get_raw by reading from that same temp path (by mocking the path logic)
|
||||
let retrieved_value = tokio::fs::read(&file_path).await.unwrap();
|
||||
assert_eq!(retrieved_value, value);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_raw_not_found() {
|
||||
let dir = tempdir().unwrap();
|
||||
let ns = "test-ns";
|
||||
let key = "non-existent-key";
|
||||
|
||||
// We need to check if reading a non-existent file gives the correct error
|
||||
let file_path = LocalFileSecretStore::get_file_path(dir.path(), ns, key);
|
||||
let result = tokio::fs::read(&file_path).await;
|
||||
|
||||
assert!(matches!(result, Err(_)));
|
||||
}
|
||||
}
|
||||
4
harmony_secret/src/store/mod.rs
Normal file
4
harmony_secret/src/store/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod infisical;
|
||||
mod local_file;
|
||||
pub use infisical::*;
|
||||
pub use local_file::*;
|
||||
Reference in New Issue
Block a user