Skip to content

Commit

Permalink
feat: Add support to parse a SecretStore to a struct using the config…
Browse files Browse the repository at this point in the history
… crate
  • Loading branch information
JasterV committed Mar 5, 2024
1 parent f41afde commit efb579b
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 2 deletions.
191 changes: 190 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ url = { workspace = true, features = ["serde"] }
uuid = { workspace = true, features = ["v4", "serde"], optional = true }
zeroize = { workspace = true }
wiremock = { workspace = true, optional = true }
config = { version = "0.14.0", optional = true }

[features]
backend = [
Expand Down Expand Up @@ -101,6 +102,7 @@ sqlx = ["dep:sqlx", "sqlx/sqlite"]
service = ["chrono/serde", "display", "tracing", "tracing-subscriber", "uuid"]
test-utils = ["wiremock"]
tracing = ["dep:tracing"]
config = ["dep:config"]

[dev-dependencies]
axum = { workspace = true }
Expand Down
26 changes: 25 additions & 1 deletion common/src/secrets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use serde::{Deserialize, Serialize};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(config)]
use std::collections::HashMap;
use std::{collections::BTreeMap, fmt::Debug};
use zeroize::Zeroize;

Expand Down Expand Up @@ -78,6 +80,28 @@ impl IntoIterator for SecretStore {
}
}

#[cfg(config)]
impl SecretStore {
pub fn deserialize(self) -> T
where
T: DeserializeOwned,
{
let secrets = self.into_iter().collect::<HashMap<_, _>>();

config::Config::builder()
.add_source(
config::Environment::default()
.source(Some(secrets))
.try_parsing(true)
.separator("__"),
)
.build()
.expect("Failed to load app configuration")
.try_deserialize()
.expect("Cannot deserialize configuration")
}
}

#[cfg(test)]
#[allow(dead_code)]
mod secrets_tests {
Expand Down
3 changes: 3 additions & 0 deletions resources/secrets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ license = "Apache-2.0"
description = "Plugin to for managing secrets on shuttle"
keywords = ["shuttle-service", "secrets"]

[features]
config = ["shuttle-service/config"]

[dependencies]
async-trait = "0.1.56"
serde_json = "1"
Expand Down
1 change: 1 addition & 0 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ builder = [
"tracing",
]
runner = ["shuttle-proto/runtime-client", "tokio/process", "dunce"]
config = ["shuttle-common/config"]

0 comments on commit efb579b

Please sign in to comment.