Skip to content

Commit efb579b

Browse files
committed
feat: Add support to parse a SecretStore to a struct using the config crate
1 parent f41afde commit efb579b

File tree

5 files changed

+221
-2
lines changed

5 files changed

+221
-2
lines changed

Cargo.lock

Lines changed: 190 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ url = { workspace = true, features = ["serde"] }
5050
uuid = { workspace = true, features = ["v4", "serde"], optional = true }
5151
zeroize = { workspace = true }
5252
wiremock = { workspace = true, optional = true }
53+
config = { version = "0.14.0", optional = true }
5354

5455
[features]
5556
backend = [
@@ -101,6 +102,7 @@ sqlx = ["dep:sqlx", "sqlx/sqlite"]
101102
service = ["chrono/serde", "display", "tracing", "tracing-subscriber", "uuid"]
102103
test-utils = ["wiremock"]
103104
tracing = ["dep:tracing"]
105+
config = ["dep:config"]
104106

105107
[dev-dependencies]
106108
axum = { workspace = true }

common/src/secrets.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use serde::{Deserialize, Serialize};
1+
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2+
#[cfg(config)]
3+
use std::collections::HashMap;
24
use std::{collections::BTreeMap, fmt::Debug};
35
use zeroize::Zeroize;
46

@@ -78,6 +80,28 @@ impl IntoIterator for SecretStore {
7880
}
7981
}
8082

83+
#[cfg(config)]
84+
impl SecretStore {
85+
pub fn deserialize(self) -> T
86+
where
87+
T: DeserializeOwned,
88+
{
89+
let secrets = self.into_iter().collect::<HashMap<_, _>>();
90+
91+
config::Config::builder()
92+
.add_source(
93+
config::Environment::default()
94+
.source(Some(secrets))
95+
.try_parsing(true)
96+
.separator("__"),
97+
)
98+
.build()
99+
.expect("Failed to load app configuration")
100+
.try_deserialize()
101+
.expect("Cannot deserialize configuration")
102+
}
103+
}
104+
81105
#[cfg(test)]
82106
#[allow(dead_code)]
83107
mod secrets_tests {

resources/secrets/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ license = "Apache-2.0"
66
description = "Plugin to for managing secrets on shuttle"
77
keywords = ["shuttle-service", "secrets"]
88

9+
[features]
10+
config = ["shuttle-service/config"]
11+
912
[dependencies]
1013
async-trait = "0.1.56"
1114
serde_json = "1"

service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ builder = [
4444
"tracing",
4545
]
4646
runner = ["shuttle-proto/runtime-client", "tokio/process", "dunce"]
47+
config = ["shuttle-common/config"]

0 commit comments

Comments
 (0)