From 2d7af6d1494dca7c0ae1055db7689d6f1f827aa0 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 5 Aug 2024 13:28:16 -0500 Subject: [PATCH] wip rebrand --- Cargo.lock | 142 ++++++++--------------------------------------- Cargo.toml | 3 +- src/chatbot.rs | 9 +-- src/config.rs | 55 ++++++++++++++---- src/observe.rs | 2 +- src/prelude.rs | 2 +- src/webserver.rs | 1 + 7 files changed, 75 insertions(+), 139 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index afda21a..b7b374a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,54 +41,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anstream" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" - -[[package]] -name = "anstyle-parse" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" -dependencies = [ - "windows-sys 0.52.0", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" -dependencies = [ - "anstyle", - "windows-sys 0.52.0", -] - [[package]] name = "antidote" version = "1.0.0" @@ -288,30 +240,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chatbot" -version = "0.1.0" -dependencies = [ - "anyhow", - "axum", - "clap", - "color-eyre", - "config", - "discord", - "redact", - "reqwest", - "reqwest-middleware", - "reqwest-retry", - "reqwest-tracing", - "serde", - "serde_json", - "serde_with", - "tokio", - "tracing", - "tracing-subscriber", - "urlencoding", -] - [[package]] name = "chrono" version = "0.4.37" @@ -327,33 +255,6 @@ dependencies = [ "windows-targets 0.52.4", ] -[[package]] -name = "clap" -version = "4.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim 0.11.1", -] - -[[package]] -name = "clap_lex" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" - [[package]] name = "color-eyre" version = "0.6.3" @@ -381,12 +282,6 @@ dependencies = [ "tracing-error", ] -[[package]] -name = "colorchoice" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" - [[package]] name = "config" version = "0.14.0" @@ -506,7 +401,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim 0.10.0", + "strsim", "syn", ] @@ -852,6 +747,29 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "homecloud" +version = "0.1.0" +dependencies = [ + "anyhow", + "axum", + "color-eyre", + "config", + "discord", + "redact", + "reqwest", + "reqwest-middleware", + "reqwest-retry", + "reqwest-tracing", + "serde", + "serde_json", + "serde_with", + "tokio", + "tracing", + "tracing-subscriber", + "urlencoding", +] + [[package]] name = "http" version = "1.1.0" @@ -2177,12 +2095,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - [[package]] name = "syn" version = "2.0.58" @@ -2636,12 +2548,6 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" -[[package]] -name = "utf8parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" - [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 2531355..5c952c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,11 @@ [package] -name = "chatbot" +name = "homecloud" version = "0.1.0" edition = "2021" [dependencies] anyhow = "1.0.82" axum = "0.7.5" -clap = "4.5.4" color-eyre = "0.6.3" config = "0.14.0" discord = { git = "https://github.com/SpaceManiac/discord-rs" } diff --git a/src/chatbot.rs b/src/chatbot.rs index bf2bbe7..c000fdf 100644 --- a/src/chatbot.rs +++ b/src/chatbot.rs @@ -5,22 +5,19 @@ use std::{any::Any, future}; -use discord::{ - model::{ChannelId, Event, Message, ReadyEvent}, - Discord as DDiscord, -}; +use discord::model::{ChannelId, Event, Message, ReadyEvent}; use tokio::sync::Mutex; const COMMAND_PREFIX: &str = "!"; struct Discord { - discord: DDiscord, + discord: discord::Discord, connection: Option>>, } impl Discord { pub fn try_new(conf: Arc) -> Result { - let discord = DDiscord::from_bot_token( + let discord = discord::Discord::from_bot_token( conf.discord .as_ref() .unwrap() diff --git a/src/config.rs b/src/config.rs index 93a1818..17d2737 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,12 @@ -use std::{collections::HashMap, fmt::Debug}; - +use crate::prelude::*; use config::{builder::DefaultState, Config as CConfig, ConfigBuilder}; use redact::serde::redact_secret; - -use crate::prelude::*; +use std::ffi::OsStr; +use std::fs::OpenOptions; +use std::os::unix::fs::OpenOptionsExt; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::{collections::HashMap, fmt::Debug}; const CURRENT_VERSION: u64 = 1; @@ -32,6 +35,8 @@ pub struct ConfigLoadResult { } const CONFIG_FILE_PATH: &str = "./conf.toml"; +const ENCRYPTED_CONFIG_FILE_PATH: &str = "./conf.toml.sops"; +const TMP_CONFIG_FILE_NAME: &str = "homecloud-conf.toml"; impl Config { pub fn load() -> Result { @@ -39,24 +44,52 @@ impl Config { Ok(c.try_deserialize()?) } + #[instrument] pub fn builder() -> Result> { - let p = std::path::PathBuf::from(CONFIG_FILE_PATH); + info!("builder"); + let mut p: PathBuf = CONFIG_FILE_PATH.into(); + let mut tmp_file_to_cleanup: Option = None; if !p.exists() { // TODO: this won't output since tracing is not setup before we load // config, so we need something like // https://docs.rs/tracing-capture/latest/tracing_capture/ // to capture trace events to be output _after_ configuration has // loaded and the "actual" subscriber is setup - warn!( - "Config file '{}' doesn't exist, so defaults will likely be loaded!", - p.display(), - ); + if PathBuf::from(ENCRYPTED_CONFIG_FILE_PATH).exists() { + let tmp_file = Path::join(&std::env::temp_dir(), TMP_CONFIG_FILE_NAME); + let mut options = OpenOptions::new(); + options.mode(0o600); // Give read/write for owner and read for others. + info!("Detected encrypted config file '{}' - attempting to use `sops` to decrypt it to '{}' ", ENCRYPTED_CONFIG_FILE_PATH, tmp_file.display()); + let sops_result = Command::new("sops") + .args([ + OsStr::new("--decrypt"), + &p.into_os_string(), + OsStr::new("--output"), + &tmp_file.clone().into_os_string(), + ]) + .output(); + p = tmp_file.clone(); + tmp_file_to_cleanup = Some(tmp_file); + info!("p: {}, sops_result: {:?}", p.display(), sops_result); + } else { + warn!( + "Config file '{}' doesn't exist, so defaults will likely be loaded!", + p.display(), + ); + } } // TODO: log whether or not we were able to load conf.toml? - Ok(Self::default_builder() + let result = Ok(Self::default_builder() .map_err(Error::from)? .add_source(config::File::from(p).required(false)) - .add_source(config::Environment::with_prefix("chatbot").separator("__"))) + .add_source(config::Environment::with_prefix("homecloud").separator("__"))); + + // cleanup + if let Some(path) = tmp_file_to_cleanup { + let _ = std::fs::remove_file(path); + } + + result } pub fn default_builder() -> Result> { diff --git a/src/observe.rs b/src/observe.rs index 9576716..2c67d00 100644 --- a/src/observe.rs +++ b/src/observe.rs @@ -8,7 +8,7 @@ pub fn setup_logging(_conf: Arc) { let filter = EnvFilter::builder() .with_default_directive(LevelFilter::TRACE.into()) - .parse_lossy("info,chatbot=trace"); + .parse_lossy("info,homecloud=trace"); tracing_subscriber::fmt().with_env_filter(filter).init(); } diff --git a/src/prelude.rs b/src/prelude.rs index b4d223a..20b7685 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -7,4 +7,4 @@ pub use anyhow::Result; pub use redact::Secret; pub use serde::{Deserialize, Serialize}; pub use std::sync::Arc; -pub use tracing::{debug, error, info, trace, warn}; +pub use tracing::{debug, error, info, instrument, trace, warn}; diff --git a/src/webserver.rs b/src/webserver.rs index 0f54430..ddd2cdc 100644 --- a/src/webserver.rs +++ b/src/webserver.rs @@ -41,6 +41,7 @@ async fn health() -> impl IntoResponse { }) } +/// Fetches server statuses for the configured minecraft servers simultaneously. async fn minecraft_server_status() -> WebserverResult { let c = Arc::new(MinecraftServerStatus::try_new()?); let reqs: Vec<&str> = vec!["h.lyte.dev:26965", "ourcraft.lyte.dev"];