From 59ebf90d5b3f368d84086a813c8714e046ed4fb5 Mon Sep 17 00:00:00 2001 From: Victor Martinez <49537445+JasterV@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:48:54 +0100 Subject: [PATCH] feat: Add support to parse a SecretStore to a struct using the config crate --- Cargo.lock | 191 ++++++++++++++++++++++++++++++++++- common/Cargo.toml | 2 + common/src/secrets.rs | 26 +++++ resources/secrets/Cargo.toml | 3 + service/Cargo.toml | 1 + 5 files changed, 222 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index f5e472abc..124f0a8e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1195,6 +1195,26 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "config" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7328b20597b53c2454f0b1919720c25c7339051c02b72b7e05409e00b14132be" +dependencies = [ + "async-trait", + "convert_case 0.6.0", + "json5", + "lazy_static", + "nom", + "pathdiff", + "ron", + "rust-ini", + "serde", + "serde_json", + "toml", + "yaml-rust", +] + [[package]] name = "console" version = "0.15.8" @@ -1214,12 +1234,41 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "const-random" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom 0.2.12", + "once_cell", + "tiny-keccak", +] + [[package]] name = "convert_case" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -1353,6 +1402,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-common" version = "0.1.6" @@ -1484,7 +1539,7 @@ version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ - "convert_case", + "convert_case 0.4.0", "proc-macro2", "quote", "rustc_version 0.4.0", @@ -1561,6 +1616,15 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "dlv-list" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -2737,6 +2801,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" + [[package]] name = "hashbrown" version = "0.14.3" @@ -3298,6 +3368,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "jsonwebtoken" version = "9.2.0" @@ -3950,6 +4031,16 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ordered-multimap" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e" +dependencies = [ + "dlv-list", + "hashbrown 0.13.2", +] + [[package]] name = "outref" version = "0.5.1" @@ -4022,6 +4113,12 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "pbkdf2" version = "0.11.0" @@ -4075,6 +4172,51 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "pest" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 2.0.52", +] + +[[package]] +name = "pest_meta" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "pin-project" version = "1.1.4" @@ -4694,6 +4836,18 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" +[[package]] +name = "ron" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" +dependencies = [ + "base64 0.21.7", + "bitflags 2.4.2", + "serde", + "serde_derive", +] + [[package]] name = "rsa" version = "0.9.6" @@ -4714,6 +4868,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rust-ini" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -5220,6 +5384,7 @@ dependencies = [ "bytes", "chrono", "comfy-table", + "config", "crossterm 0.27.0", "headers", "http 0.2.12", @@ -6176,6 +6341,15 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -6755,6 +6929,12 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + [[package]] name = "ulid" version = "1.1.2" @@ -7453,6 +7633,15 @@ version = "0.13.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "yansi" version = "0.5.1" diff --git a/common/Cargo.toml b/common/Cargo.toml index 440bc244b..50c192387 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -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 = [ @@ -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 } diff --git a/common/src/secrets.rs b/common/src/secrets.rs index 7ce4684b9..ec46202cc 100644 --- a/common/src/secrets.rs +++ b/common/src/secrets.rs @@ -1,4 +1,8 @@ +#[cfg(config)] +use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; +#[cfg(config)] +use std::collections::HashMap; use std::{collections::BTreeMap, fmt::Debug}; use zeroize::Zeroize; @@ -78,6 +82,28 @@ impl IntoIterator for SecretStore { } } +#[cfg(config)] +impl SecretStore { + pub fn deserialize(self) -> T + where + T: DeserializeOwned, + { + let secrets = self.into_iter().collect::>(); + + 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 { diff --git a/resources/secrets/Cargo.toml b/resources/secrets/Cargo.toml index af60b9520..0550e7123 100644 --- a/resources/secrets/Cargo.toml +++ b/resources/secrets/Cargo.toml @@ -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" diff --git a/service/Cargo.toml b/service/Cargo.toml index dc39d4aa2..ae4758c15 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -44,3 +44,4 @@ builder = [ "tracing", ] runner = ["shuttle-proto/runtime-client", "tokio/process", "dunce"] +config = ["shuttle-common/config"]