-
Notifications
You must be signed in to change notification settings - Fork 260
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: Frank Yang <[email protected]>
- Loading branch information
1 parent
13f7916
commit becd76c
Showing
4 changed files
with
185 additions
and
4 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,50 @@ | ||
use anyhow::{anyhow, Result}; | ||
use async_trait::async_trait; | ||
use serde::{Deserialize, Serialize}; | ||
use vaultrs::{ | ||
client::{VaultClient, VaultClientSettingsBuilder}, | ||
kv2, | ||
}; | ||
|
||
use crate::{Key, Provider}; | ||
|
||
/// A config Provider that uses HashiCorp Vault. | ||
#[derive(Debug)] | ||
pub struct VaultProvider { | ||
url: String, | ||
token: String, | ||
} | ||
|
||
impl VaultProvider { | ||
pub fn new(url: impl AsRef<String>, token: impl AsRef<String>) -> Result<Self> { | ||
Ok(Self { | ||
url: url.as_ref().to_string(), | ||
token: token.as_ref().to_string(), | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Secret { | ||
value: String, | ||
} | ||
|
||
#[async_trait] | ||
impl Provider for VaultProvider { | ||
async fn get(&self, key: &Key) -> Result<Option<String>> { | ||
let client = VaultClient::new( | ||
VaultClientSettingsBuilder::default() | ||
.address(&self.url) | ||
.token(&self.token) | ||
.build()?, | ||
)?; | ||
let keys = key.0.split("/").collect::<Vec<_>>(); | ||
if keys.len() == 1 { | ||
return Err(anyhow!("vault key must contain the mount path")); | ||
} | ||
let mount = keys[0]; | ||
let path = keys[1..].join("/"); | ||
let secret: Secret = kv2::read(&client, mount, &path).await?; | ||
Ok(Some(secret.value)) | ||
} | ||
} |