Skip to content

Commit

Permalink
support specifying vault parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Esgrove committed Nov 12, 2024
1 parent 5714c02 commit 41d3a7c
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 47 deletions.
4 changes: 4 additions & 0 deletions python-pyo3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 76 additions & 18 deletions python-pyo3/python/n_vault/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
110 changes: 81 additions & 29 deletions python-pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> 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<String>) -> PyResult<()> {
fn delete_many(
names: Vec<String>,
vault_stack: Option<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> 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<bool> {
#[pyfunction(signature = (name, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))]
fn exists(
name: &str,
vault_stack: Option<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> PyResult<bool> {
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)
Expand All @@ -47,10 +75,17 @@ fn exists(name: &str) -> PyResult<bool> {
})
}

#[pyfunction]
fn list_all() -> PyResult<Vec<String>> {
#[pyfunction(signature = (vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))]
fn list_all(
vault_stack: Option<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> PyResult<Vec<String>> {
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()
Expand All @@ -61,11 +96,19 @@ fn list_all() -> PyResult<Vec<String>> {
})
}

#[pyfunction]
fn lookup(name: &str) -> PyResult<String> {
#[pyfunction(signature = (name, vault_stack=None, region=None, bucket=None, key=None, prefix=None, profile=None))]
fn lookup(
name: &str,
vault_stack: Option<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> PyResult<String> {
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),
Expand All @@ -86,14 +129,23 @@ fn run(args: Vec<String>) -> 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<String>,
region: Option<String>,
bucket: Option<String>,
key: Option<String>,
prefix: Option<String>,
profile: Option<String>,
) -> 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)?)
Expand Down

0 comments on commit 41d3a7c

Please sign in to comment.