Skip to content

Commit

Permalink
release 0.4: use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Perreau committed May 14, 2021
1 parent 9df6a78 commit aaf8b3f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "vault-credentials"
description = "Rust Library that fetch secrets from Vault and load them as environment variables."
version = "0.3.0"
version = "0.4.0"
authors = ["Lucas Perreau <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -21,8 +21,8 @@ name = "vault_credentials_test"
path = "src/examples/bin.rs"

[dependencies]
reqwest = { version = "0.11", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11.3", features = ["json"] }
tokio = { version = "1.5.0", features = ["full"] }
serde_json = "1.0.63"

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/examples/bin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use dotenv::dotenv;

fn main() {
#[tokio::main]
async fn main() {
dotenv().ok();
vault_credentials::initialize();
vault_credentials::initialize().await;

println!("{}", std::env::var("app.mongo.uri").unwrap());
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::secret_retriever::SecretRetriever;
/// # Vault Credentials
/// Rust Library that fetch secrets from Vault and load them as environment variables.
/// Inspired by [Spring Cloud Vault](https://cloud.spring.io/spring-cloud-vault/reference/html/#vault.config.authentication).
pub fn initialize() {
pub async fn initialize() {
let vault_credentials = Credentials::new();
let auth_token = TokenRetriever::retrieve_token(vault_credentials.clone());
SecretRetriever::env_setter(vault_credentials, auth_token);
let auth_token = TokenRetriever::retrieve_token(vault_credentials.clone()).await;
SecretRetriever::env_setter(vault_credentials, auth_token).await;
}
6 changes: 4 additions & 2 deletions src/secret_retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::env;
pub struct SecretRetriever {}

impl SecretRetriever {
pub fn env_setter(vault_credentials: Credentials, auth_token: String) {
pub async fn env_setter(vault_credentials: Credentials, auth_token: String) {
let vault_path = std::env::var("VAULT_PATH").unwrap();

let request_uri = {
format!("{}/v1/secret/data/{}", String::from(vault_credentials.vault_addr), vault_path)
};

let mut request_builder = reqwest::blocking::Client::new()
let mut request_builder = reqwest::Client::new()
.get(&request_uri)
.header("X-Vault-Token", auth_token);

Expand All @@ -22,8 +22,10 @@ impl SecretRetriever {
}

let response: serde_json::Value = request_builder.send()
.await
.unwrap()
.json()
.await
.unwrap();

if let Some(errors) = response.get("errors") {
Expand Down
10 changes: 6 additions & 4 deletions src/token_retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashMap;
pub struct TokenRetriever {}

impl TokenRetriever {
pub fn retrieve_token(vault_credentials: Credentials) -> String {
pub async fn retrieve_token(vault_credentials: Credentials) -> String {
let authentication_type = match env::var("VAULT_TYPE") {
Ok(value) => value,
_ => String::from("token")
Expand All @@ -26,7 +26,7 @@ impl TokenRetriever {
.expect("Cannot get environment variable VAULT_TOKEN").as_str())
} else {
let payload = TokenRetriever::generate_payload(authentication_type.as_str());
TokenRetriever::call_vault_login(&request_url, Some(headers), &payload)
TokenRetriever::call_vault_login(&request_url, Some(headers), &payload).await
}
}

Expand Down Expand Up @@ -72,8 +72,8 @@ impl TokenRetriever {
}
}

fn call_vault_login(request_url: &str, headers_option: Option<HashMap<String, String>>, payload: &serde_json::Value) -> String {
let mut request_builder = reqwest::blocking::Client::new()
async fn call_vault_login(request_url: &str, headers_option: Option<HashMap<String, String>>, payload: &serde_json::Value) -> String {
let mut request_builder = reqwest::Client::new()
.post(request_url);

if let Some(headers) = headers_option {
Expand All @@ -87,8 +87,10 @@ impl TokenRetriever {
let response: serde_json::Value = request_builder
.json(payload)
.send()
.await
.unwrap()
.json()
.await
.unwrap();

if let Some(errors) = response.get("errors") {
Expand Down

0 comments on commit aaf8b3f

Please sign in to comment.