Skip to content

Commit

Permalink
Remove credentials.toml support, using environment instead
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Dec 8, 2024
1 parent fee539e commit adc74c2
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 146 deletions.
95 changes: 1 addition & 94 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions notifico-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ anyhow = "1.0.94"
utoipa = { version = "5", features = ["uuid"] }
flume = "0.11.1"
thiserror = "2.0.5"
regex = "1.11.1"
1 change: 0 additions & 1 deletion notifico-core/src/config/mod.rs

This file was deleted.

72 changes: 72 additions & 0 deletions notifico-core/src/credentials/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::credentials::{Credential, CredentialSelector, CredentialStorage};
use crate::error::EngineError;
use async_trait::async_trait;
use regex::Regex;
use std::collections::HashMap;
use tracing::info;
use uuid::Uuid;

#[derive(Eq, PartialEq, Hash, Debug)]
struct CredentialKey {
project: Uuid,
name: String,
}

#[derive(Default, Debug)]
pub struct EnvCredentialStorage(HashMap<CredentialKey, Credential>);

impl EnvCredentialStorage {
pub fn new() -> Self {
let mut storage = HashMap::new();

let re = Regex::new("^NOTIFICO_CRED_(?:([[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12})_)?(.+)$").unwrap();
for (name, value) in std::env::vars() {
let Some(captures) = re.captures(&name) else {
continue;
};

let project = captures
.get(1)
.map_or_else(Uuid::nil, |m| Uuid::parse_str(m.as_str()).unwrap());
let name = captures.get(2).unwrap().as_str();
let credential = Credential::Short(value);

storage.insert(
CredentialKey {
project,
name: name.to_lowercase(),
},
credential,
);
}

info!(
"Imported {} credentials from environment variables",
storage.len()
);
Self(storage)
}
}

#[async_trait]
impl CredentialStorage for EnvCredentialStorage {
async fn get_credential(
&self,
project: Uuid,
selector: &CredentialSelector,
) -> Result<Credential, EngineError> {
match selector {
CredentialSelector::ByName(name) => {
let key = CredentialKey {
project,
name: name.to_lowercase(),
};

self.0
.get(&key)
.cloned()
.ok_or(EngineError::CredentialNotFound)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,6 @@ struct CredentialKey<'a> {
pub struct MemoryCredentialStorage(HashMap<CredentialKey<'static>, Credential>);

impl MemoryCredentialStorage {
pub fn from_config(config: serde_json::Value) -> Result<Self, serde_json::Error> {
let mut creds = MemoryCredentialStorage::default();

let obj = config.as_object().unwrap().clone();
for (r#type, v) in obj {
let obj = v.as_object().unwrap().clone();
for (name_or_project_id, value) in obj {
if let Ok(project_id) = Uuid::parse_str(&name_or_project_id) {
for (name, value) in value.as_object().unwrap().iter() {
creds.add_credential(
project_id,
name.clone(),
Credential::Long {
r#type: r#type.clone(),
value: value.clone(),
},
);
}
} else {
creds.add_credential(
Uuid::nil(),
name_or_project_id,
Credential::Long {
r#type: r#type.clone(),
value: value.clone(),
},
);
};
}
}

Ok(creds)
}

pub fn add_credential(&mut self, project: Uuid, name: String, credential: Credential) {
self.0.insert(
CredentialKey {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod env;
pub mod memory;

use crate::error::EngineError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
Expand Down
1 change: 0 additions & 1 deletion notifico-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod config;
pub mod contact;
pub mod credentials;
pub mod db;
Expand Down
5 changes: 5 additions & 0 deletions notifico-core/src/simpletransport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub trait SimpleTransport: Send + Sync {
fn has_contacts(&self) -> bool {
true
}

fn supports_contact(&self, r#type: &str) -> bool;
}

pub struct SimpleTransportWrapper {
Expand Down Expand Up @@ -83,6 +85,9 @@ impl EnginePlugin for SimpleTransportWrapper {
};

for contact in contacts {
if !self.inner.supports_contact(&contact.r#type) {
continue;
}
for message in &context.messages {
let result = self
.inner
Expand Down
3 changes: 1 addition & 2 deletions notifico-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ backoff = { version = "0.4.0", features = ["tokio"] }
clap = { workspace = true }
dotenvy = "0.15.7"
fe2o3-amqp = { version = "0.13.1" }
figment = { version = "0.10.19", features = ["env", "toml"] }
sea-orm = { workspace = true }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
tokio = { version = "1.42", features = ["macros", "rt", "sync", "rt-multi-thread"] }
tokio = { version = "1.42", features = ["macros", "rt", "sync", "rt-multi-thread", "signal"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "fmt"] }
url = "2.5.4"
Expand Down
16 changes: 3 additions & 13 deletions notifico-worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ mod amqp;

use crate::amqp::AmqpClient;
use clap::Parser;
use figment::{providers::Format, providers::Toml, Figment};
use log::debug;
use notifico_core::config::credentials::MemoryCredentialStorage;
use notifico_core::credentials::env::EnvCredentialStorage;
use notifico_core::db::create_sqlite_if_not_exists;
use notifico_core::engine::plugin::core::CorePlugin;
use notifico_core::engine::Engine;
Expand Down Expand Up @@ -55,7 +54,7 @@ async fn main() {
let _ = dotenvy::dotenv();

if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
std::env::set_var("RUST_LOG", "h2=warn,info");
}

let args = Args::parse();
Expand All @@ -74,16 +73,7 @@ async fn main() {

let db_connection = Database::connect(db_conn_options).await.unwrap();

let credentials = {
let credential_config: serde_json::Value = {
let mut config = Figment::new().merge(Toml::file(args.credentials_path));
if let Ok(env_credentials) = std::env::var("NOTIFICO_CREDENTIALS") {
config = config.merge(Toml::string(&env_credentials));
}
config.extract().unwrap()
};
Arc::new(MemoryCredentialStorage::from_config(credential_config).unwrap())
};
let credentials = Arc::new(EnvCredentialStorage::new());
let pipelines = Arc::new(DbPipelineStorage::new(db_connection.clone()));

// Initialize AMQP client
Expand Down
2 changes: 1 addition & 1 deletion notificox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{Parser, Subcommand};
use log::info;
use notifico_core::config::credentials::MemoryCredentialStorage;
use notifico_core::contact::Contact;
use notifico_core::credentials::memory::MemoryCredentialStorage;
use notifico_core::credentials::Credential;
use notifico_core::engine::plugin::core::CorePlugin;
use notifico_core::engine::Engine;
Expand Down
4 changes: 4 additions & 0 deletions transports/notifico-gotify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl SimpleTransport for GotifyTransport {
fn has_contacts(&self) -> bool {
false
}

fn supports_contact(&self, _type: &str) -> bool {
false
}
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
Loading

0 comments on commit adc74c2

Please sign in to comment.