feat(config)!: Reworked how configuration is loaded
The environment variable `AUTOMATION_CONFIG` has been renamed to `AUTOMATION__ENTRYPOINT` and can now also be set in `automation.toml` by specifying: ``` automation = "<path>" ``` Directly accessing the environment variables in lua in no longer possible. To pass in configuration or secrets you can now instead make use of the `variables` and `secrets` modules. To set values in these modules you can either specify them in `automation.toml`: ``` [variables] <name> = <value> [secrets] <name> = <value> ``` Note that these values will get converted to a string. You can also specify the environment variables `AUTOMATION__VARIABLES__<name>` and `AUTOMATION__SECRETS__<name>` to set variables and secrets respectively. By adding the suffix `__FILE` to the environment variable name the contents of a file can be loaded into the variable or secret. Note that variables and secrets are identical in functionality and the name difference exists purely to make it clear that secret values are meant to be kept secret.
This commit is contained in:
17
src/config.rs
Normal file
17
src/config.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default = "default_entrypoint")]
|
||||
pub entrypoint: String,
|
||||
#[serde(default)]
|
||||
pub variables: HashMap<String, String>,
|
||||
#[serde(default)]
|
||||
pub secrets: HashMap<String, String>,
|
||||
}
|
||||
|
||||
fn default_entrypoint() -> String {
|
||||
"./config.lua".into()
|
||||
}
|
||||
37
src/main.rs
37
src/main.rs
@@ -1,4 +1,6 @@
|
||||
#![feature(iter_intersperse)]
|
||||
mod config;
|
||||
mod secret;
|
||||
mod web;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
@@ -6,6 +8,7 @@ use std::path::Path;
|
||||
use std::process;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use ::config::{Environment, File};
|
||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||
use automation_lib::device_manager::DeviceManager;
|
||||
use automation_lib::helpers;
|
||||
@@ -14,6 +17,7 @@ use axum::extract::{FromRef, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::post;
|
||||
use axum::{Json, Router};
|
||||
use config::Config;
|
||||
use dotenvy::dotenv;
|
||||
use google_home::{GoogleHome, Request, Response};
|
||||
use mlua::LuaSerdeExt;
|
||||
@@ -22,6 +26,8 @@ use tokio::net::TcpListener;
|
||||
use tracing::{debug, error, info, warn};
|
||||
use web::{ApiError, User};
|
||||
|
||||
use crate::secret::EnvironmentSecretFile;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
pub openid_url: String,
|
||||
@@ -69,7 +75,21 @@ async fn app() -> anyhow::Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
tracing_subscriber::fmt::init();
|
||||
// console_subscriber::init();
|
||||
|
||||
let config: Config = ::config::Config::builder()
|
||||
.add_source(
|
||||
File::with_name(&format!("{}.toml", std::env!("CARGO_PKG_NAME"))).required(false),
|
||||
)
|
||||
.add_source(
|
||||
Environment::default()
|
||||
.prefix(std::env!("CARGO_PKG_NAME"))
|
||||
.separator("__"),
|
||||
)
|
||||
.add_source(EnvironmentSecretFile::default())
|
||||
.build()
|
||||
.unwrap()
|
||||
.try_deserialize()
|
||||
.unwrap();
|
||||
|
||||
info!("Starting automation_rs...");
|
||||
|
||||
@@ -133,11 +153,10 @@ async fn app() -> anyhow::Result<()> {
|
||||
|
||||
lua.register_module("device_manager", device_manager.clone())?;
|
||||
|
||||
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
||||
lua.register_module("secrets", lua.to_value(&config.secrets)?)?;
|
||||
|
||||
let utils = lua.create_table()?;
|
||||
let get_env = lua.create_function(|_lua, name: String| {
|
||||
std::env::var(name).map_err(mlua::ExternalError::into_lua_err)
|
||||
})?;
|
||||
utils.set("get_env", get_env)?;
|
||||
let get_hostname = lua.create_function(|_lua, ()| {
|
||||
hostname::get()
|
||||
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
|
||||
@@ -151,17 +170,13 @@ async fn app() -> anyhow::Result<()> {
|
||||
.as_millis())
|
||||
})?;
|
||||
utils.set("get_epoch", get_epoch)?;
|
||||
|
||||
lua.register_module("utils", utils)?;
|
||||
|
||||
automation_devices::register_with_lua(&lua)?;
|
||||
helpers::register_with_lua(&lua)?;
|
||||
|
||||
// TODO: Make this not hardcoded
|
||||
let config_filename = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config.lua".into());
|
||||
let config_path = Path::new(&config_filename);
|
||||
|
||||
let fulfillment_config: mlua::Value = lua.load(config_path).eval_async().await?;
|
||||
let entrypoint = Path::new(&config.entrypoint);
|
||||
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
||||
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
||||
|
||||
// Create google home fulfillment route
|
||||
|
||||
43
src/secret.rs
Normal file
43
src/secret.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::str::from_utf8;
|
||||
|
||||
use config::{ConfigError, Source, Value, ValueKind};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EnvironmentSecretFile {}
|
||||
|
||||
const SUFFIX: &str = "__file";
|
||||
const PREFIX: &str = concat!(std::env!("CARGO_PKG_NAME"), "__");
|
||||
|
||||
impl Source for EnvironmentSecretFile {
|
||||
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn collect(&self) -> Result<config::Map<String, config::Value>, ConfigError> {
|
||||
Ok(std::env::vars()
|
||||
.flat_map(|(key, value): (String, String)| {
|
||||
let key = key.to_lowercase();
|
||||
if !key.starts_with(PREFIX) {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !key.ends_with(SUFFIX) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let suffix_length = key.len() - SUFFIX.len();
|
||||
let key = key[PREFIX.len()..suffix_length].replace("__", ".");
|
||||
|
||||
if key.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let content = from_utf8(&std::fs::read(&value).unwrap())
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
|
||||
Some((key, Value::new(Some(&value), ValueKind::String(content))))
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user