parse toml secrets

This commit is contained in:
mbecker20
2022-11-11 15:08:29 -05:00
parent fdf5626a93
commit e740128baf
5 changed files with 30 additions and 8 deletions

3
.gitignore vendored
View File

@@ -4,4 +4,5 @@ dist
.env
config.json
secrets.json
secrets.json
secrets.toml

10
Cargo.lock generated
View File

@@ -1275,6 +1275,7 @@ dependencies = [
"serde_json",
"sysinfo",
"tokio",
"toml",
"tower",
"types",
]
@@ -2271,6 +2272,15 @@ dependencies = [
"tracing",
]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]]
name = "tower"
version = "0.4.13"

View File

@@ -265,7 +265,6 @@ fn default_core_mongo_db_name() -> String {
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub struct PeripherySecrets {
pub passkey: String,
#[serde(default)]

View File

@@ -20,4 +20,5 @@ serde_json = "1.0"
bollard = "0.13"
anyhow = "1.0"
envy = "0.4"
sysinfo = "0.26.7"
sysinfo = "0.26.7"
toml = "0.5"

View File

@@ -1,4 +1,4 @@
use std::fs::File;
use std::{fs::File, io::Read};
use dotenv::dotenv;
use serde::Deserialize;
@@ -15,9 +15,7 @@ struct Env {
pub fn load() -> (u16, PeripherySecrets) {
dotenv().ok();
let env: Env = envy::from_env().expect("failed to parse env");
let secrets_file = File::open(&env.secrets_path).expect("failed to find secrets");
let secrets: PeripherySecrets =
serde_json::from_reader(secrets_file).expect("failed to parse secrets file");
let secrets = read_secrets(&env.secrets_path);
(env.port, secrets)
}
@@ -26,5 +24,18 @@ fn default_port() -> u16 {
}
fn default_secrets_path() -> String {
"/secrets/secrets.json".to_string()
"/secrets/secrets.toml".to_string()
}
fn read_secrets(secrets_path: &str) -> PeripherySecrets {
let mut secrets_file = File::open(&secrets_path).expect("failed to find secrets");
if secrets_path.ends_with("toml") {
let mut contents = String::new();
secrets_file.read_to_string(&mut contents);
toml::from_str(&contents).expect("failed to parse secrets toml")
} else if secrets_path.ends_with("json") {
serde_json::from_reader(secrets_file).expect("failed to parse secrets json")
} else {
panic!("unsupported secrets file type: {}", secrets_path)
}
}