-
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
bda5677
commit b657297
Showing
15 changed files
with
425 additions
and
33 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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<str>, token: impl AsRef<str>) -> Self { | ||
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)) | ||
} | ||
} |
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
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,32 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use serde::Deserialize; | ||
use toml; | ||
|
||
// Config for config providers and wasmtime config | ||
#[derive(Debug, Default, Deserialize)] | ||
pub struct TriggerExecutorBuilderConfig { | ||
pub vault_config: Option<VaultConfig>, | ||
} | ||
|
||
// Vault config to initialize vault provider | ||
#[derive(Debug, Default, Deserialize)] | ||
pub struct VaultConfig { | ||
pub url: String, | ||
pub token: String, | ||
} | ||
|
||
impl TriggerExecutorBuilderConfig { | ||
pub fn load_from_file(config_file: Option<PathBuf>) -> Result<Self> { | ||
let config_file = match config_file { | ||
Some(p) => p, | ||
None => { | ||
return Ok(Self::default()); | ||
} | ||
}; | ||
let content = std::fs::read_to_string(config_file)?; | ||
let config: TriggerExecutorBuilderConfig = toml::from_str(&content)?; | ||
Ok(config) | ||
} | ||
} |
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,2 @@ | ||
[build] | ||
target = "wasm32-wasi" |
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 @@ | ||
target/ |
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,21 @@ | ||
[package] | ||
name = "config-test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = [ "cdylib" ] | ||
|
||
[dependencies] | ||
# Useful crate to handle errors. | ||
anyhow = "1" | ||
# Crate to simplify working with bytes. | ||
bytes = "1" | ||
# General-purpose crate with common HTTP types. | ||
http = "0.2" | ||
# The Spin SDK. | ||
spin-sdk = { path = "../../../sdk/rust"} | ||
# Crate that generates Rust Wasm bindings from a WebAssembly interface. | ||
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "e9c7c0a3405845cecd3fe06f3c20ab413302fc73" } | ||
|
||
[workspace] |
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,3 @@ | ||
[vault_config] | ||
url = "http://127.0.0.1:8200" | ||
token = "root" |
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,19 @@ | ||
spin_version = "1" | ||
authors = ["Fermyon Engineering <[email protected]>"] | ||
description = "A simple application that returns query values from config providers" | ||
name = "config-test" | ||
trigger = { type = "http", base = "/" } | ||
version = "0.1.0" | ||
|
||
[variables] | ||
secret_password = { required = true } | ||
|
||
[[component]] | ||
id = "config-test" | ||
source = "target/wasm32-wasi/release/config_test.wasm" | ||
[component.trigger] | ||
route = "/..." | ||
[component.build] | ||
command = "cargo build --target wasm32-wasi --release" | ||
[component.config] | ||
secret_password = "{{ secret_password }}" |
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,15 @@ | ||
use anyhow::Result; | ||
use spin_sdk::{ | ||
config, | ||
http::{Request, Response}, | ||
http_component, | ||
}; | ||
|
||
/// A simple Spin HTTP component. | ||
#[http_component] | ||
fn config_test(_req: Request) -> Result<Response> { | ||
let password = config::get("secret_password").expect("Failed to acquire password from vault"); | ||
Ok(http::Response::builder() | ||
.status(200) | ||
.body(Some(format!("Got password {}", password).into()))?) | ||
} |