-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan Levick <[email protected]>
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use spin_expressions::async_trait::async_trait; | ||
use spin_factors::anyhow::{self, Context as _}; | ||
use tracing::{instrument, Level}; | ||
use vaultrs::{ | ||
client::{VaultClient, VaultClientSettingsBuilder}, | ||
error::ClientError, | ||
kv2, | ||
}; | ||
|
||
use spin_expressions::{Key, Provider}; | ||
|
||
#[derive(Debug, Default, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
/// A config Provider that uses HashiCorp Vault. | ||
pub struct VaultVariablesProvider { | ||
/// The URL of the Vault server. | ||
url: String, | ||
/// The token to authenticate with. | ||
token: String, | ||
/// The mount point of the KV engine. | ||
mount: String, | ||
/// The optional prefix to use for all keys. | ||
#[serde(default)] | ||
prefix: Option<String>, | ||
} | ||
|
||
#[async_trait] | ||
impl Provider for VaultVariablesProvider { | ||
#[instrument(name = "spin_variables.get_from_vault", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))] | ||
async fn get(&self, key: &Key) -> anyhow::Result<Option<String>> { | ||
let client = VaultClient::new( | ||
VaultClientSettingsBuilder::default() | ||
.address(&self.url) | ||
.token(&self.token) | ||
.build()?, | ||
)?; | ||
let path = match &self.prefix { | ||
Some(prefix) => format!("{}/{}", prefix, key.as_str()), | ||
None => key.as_str().to_string(), | ||
}; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
struct Secret { | ||
value: String, | ||
} | ||
match kv2::read::<Secret>(&client, &self.mount, &path).await { | ||
Ok(secret) => Ok(Some(secret.value)), | ||
// Vault doesn't have this entry so pass along the chain | ||
Err(ClientError::APIError { code: 404, .. }) => Ok(None), | ||
// Other Vault error so bail rather than looking elsewhere | ||
Err(e) => Err(e).context("Failed to check Vault for config"), | ||
} | ||
} | ||
} |