diff --git a/python-pyo3/README.md b/python-pyo3/README.md index 31782dda..378d8d02 100644 --- a/python-pyo3/README.md +++ b/python-pyo3/README.md @@ -99,6 +99,10 @@ value = Vault().lookup("key") if Vault().exists("key"): Vault().delete("key") + +# specify vault parameters +vault = Vault(vault_stack="stack-name", profile="aws-credentials-name") +value = vault.lookup("key") ``` ## Development diff --git a/python-pyo3/python/n_vault/vault.py b/python-pyo3/python/n_vault/vault.py index 0d8a2893..294e7f46 100644 --- a/python-pyo3/python/n_vault/vault.py +++ b/python-pyo3/python/n_vault/vault.py @@ -21,29 +21,87 @@ class Vault: """Nitor Vault wrapper around the Rust vault library.""" - @staticmethod - def delete(name: str) -> None: - return nitor_vault_rs.delete(name) + def __init__( + self, + vault_stack: str = None, + region: str = None, + bucket: str = None, + key: str = None, + prefix: str = None, + profile: str = None, + ): + self.vault_stack = vault_stack + self.region = region + self.bucket = bucket + self.key = key + self.prefix = prefix + self.profile = profile - @staticmethod - def delete_many(names: Collection[str]) -> None: - return nitor_vault_rs.delete_many(sorted(names)) + def delete(self, name: str) -> None: + return nitor_vault_rs.delete( + name, + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) - @staticmethod - def exists(name: str) -> bool: - return nitor_vault_rs.exists(name) + def delete_many(self, names: Collection[str]) -> None: + return nitor_vault_rs.delete_many( + sorted(names), + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) - @staticmethod - def list_all() -> list[str]: - return nitor_vault_rs.list_all() + def exists(self, name: str) -> bool: + return nitor_vault_rs.exists( + name, + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) - @staticmethod - def lookup(name: str) -> str: - return nitor_vault_rs.lookup(name) + def list_all(self) -> list[str]: + return nitor_vault_rs.list_all( + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) - @staticmethod - def store(key: str, value: bytes | str) -> None: + def lookup(self, name: str) -> str: + return nitor_vault_rs.lookup( + name, + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) + + def store(self, key: str, value: bytes | str) -> None: if isinstance(value, str): value = value.encode("utf-8") - return nitor_vault_rs.store(key, value) + return nitor_vault_rs.store( + key, + value, + vault_stack=self.vault_stack, + region=self.region, + bucket=self.bucket, + key=self.key, + prefix=self.prefix, + profile=self.profile, + ) diff --git a/python-pyo3/src/lib.rs b/python-pyo3/src/lib.rs index 9013ad78..2fe28fd2 100644 --- a/python-pyo3/src/lib.rs +++ b/python-pyo3/src/lib.rs @@ -8,35 +8,63 @@ fn vault_error_to_anyhow(err: VaultError) -> anyhow::Error { err.into() } -#[pyfunction] -fn delete(name: &str) -> PyResult<()> { +#[pyfunction(signature = (name, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] +fn delete( + name: &str, + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult<()> { Runtime::new()?.block_on(async { - Ok(Vault::default() - .await - .map_err(vault_error_to_anyhow)? - .delete(name) - .await - .map_err(vault_error_to_anyhow)?) + Ok( + Vault::new(vault_stack, region, bucket, key, prefix, profile) + .await + .map_err(vault_error_to_anyhow)? + .delete(name) + .await + .map_err(vault_error_to_anyhow)?, + ) }) } -#[pyfunction] +#[pyfunction(signature = (names, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] #[allow(clippy::needless_pass_by_value)] -fn delete_many(names: Vec) -> PyResult<()> { +fn delete_many( + names: Vec, + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult<()> { Runtime::new()?.block_on(async { - Ok(Vault::default() - .await - .map_err(vault_error_to_anyhow)? - .delete_many(&names) - .await - .map_err(vault_error_to_anyhow)?) + Ok( + Vault::new(vault_stack, region, bucket, key, prefix, profile) + .await + .map_err(vault_error_to_anyhow)? + .delete_many(&names) + .await + .map_err(vault_error_to_anyhow)?, + ) }) } -#[pyfunction] -fn exists(name: &str) -> PyResult { +#[pyfunction(signature = (name, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] +fn exists( + name: &str, + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult { Runtime::new()?.block_on(async { - let result: bool = Vault::default() + let result: bool = Vault::new(vault_stack, region, bucket, key, prefix, profile) .await .map_err(vault_error_to_anyhow)? .exists(name) @@ -47,10 +75,17 @@ fn exists(name: &str) -> PyResult { }) } -#[pyfunction] -fn list_all() -> PyResult> { +#[pyfunction(signature = (vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] +fn list_all( + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult> { Runtime::new()?.block_on(async { - let result = Vault::default() + let result = Vault::new(vault_stack, region, bucket, key, prefix, profile) .await .map_err(vault_error_to_anyhow)? .all() @@ -61,11 +96,19 @@ fn list_all() -> PyResult> { }) } -#[pyfunction] -fn lookup(name: &str) -> PyResult { +#[pyfunction(signature = (name, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] +fn lookup( + name: &str, + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult { Runtime::new()?.block_on(async { let result: Value = Box::pin( - Vault::default() + Vault::new(vault_stack, region, bucket, key, prefix, profile) .await .map_err(vault_error_to_anyhow)? .lookup(name), @@ -86,14 +129,23 @@ fn run(args: Vec) -> PyResult<()> { }) } -#[pyfunction] -fn store(key: &str, value: &[u8]) -> PyResult<()> { +#[pyfunction(signature = (name, value, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))] +fn store( + name: &str, + value: &[u8], + vault_stack: Option, + region: Option, + bucket: Option, + key: Option, + prefix: Option, + profile: Option, +) -> PyResult<()> { Runtime::new()?.block_on(async { Ok(Box::pin( - Vault::default() + Vault::new(vault_stack, region, bucket, key, prefix, profile) .await .map_err(vault_error_to_anyhow)? - .store(key, value), + .store(name, value), ) .await .map_err(vault_error_to_anyhow)?)