feat: nixos side of the config #45

Merged
silver merged 5 commits from #20_wolves-api into main 2023-10-27 01:43:38 +00:00
5 changed files with 67 additions and 77 deletions

22
Cargo.lock generated
View file

@ -661,27 +661,6 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "csv"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086"
dependencies = [
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "ctor" name = "ctor"
version = "0.1.26" version = "0.1.26"
@ -2515,7 +2494,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"async-std", "async-std",
"chrono", "chrono",
"csv",
"dotenvy", "dotenvy",
"ldap3", "ldap3",
"lettre", "lettre",

View file

@ -34,9 +34,6 @@ rand = "0.8.5"
# fancy time stuff # fancy time stuff
chrono = "0.4.26" chrono = "0.4.26"
# handlign teh csv export from wolves
csv = "1.2"
# for email # for email
lettre = "0.10.4" lettre = "0.10.4"
maud = "0.25.0" maud = "0.25.0"

View file

@ -50,7 +50,6 @@
# basic server stuff # basic server stuff
HOME = cfg.home; HOME = cfg.home;
DATABASE = "database.db"; DATABASE = "database.db";
CSV = "wolves.csv";
HOST_PORT = cfg.host_port; HOST_PORT = cfg.host_port;
SSH_ROOT = "skynet_old"; SSH_ROOT = "skynet_old";
@ -80,6 +79,7 @@
"${cfg.env.ldap}" "${cfg.env.ldap}"
"${cfg.env.discord}" "${cfg.env.discord}"
"${cfg.env.mail}" "${cfg.env.mail}"
"${cfg.env.wolves}"
]; ];
}; };
}); });
@ -124,6 +124,10 @@
type = types.str; type = types.str;
description = "Mail details, has EMAIL_SMTP, EMAIL_USER, EMAIL_PASS"; description = "Mail details, has EMAIL_SMTP, EMAIL_USER, EMAIL_PASS";
}; };
wolves = mkOption rec {
type = types.str;
description = "Mail details, has WOLVES_URL, WOLVES_KEY";
};
}; };
users = { users = {
@ -213,12 +217,14 @@
"${cfg.env.ldap}" "${cfg.env.ldap}"
"${cfg.env.discord}" "${cfg.env.discord}"
"${cfg.env.mail}" "${cfg.env.mail}"
"${cfg.env.wolves}"
]; ];
}; };
restartTriggers = [ restartTriggers = [
"${cfg.env.ldap}" "${cfg.env.ldap}"
"${cfg.env.discord}" "${cfg.env.discord}"
"${cfg.env.mail}" "${cfg.env.mail}"
"${cfg.env.wolves}"
]; ];
}; };
} // serviceGenerator scripts; } // serviceGenerator scripts;

View file

@ -1,4 +1,5 @@
use ldap3::{LdapConn, Scope, SearchEntry}; use ldap3::{LdapConn, Scope, SearchEntry};
use serde::{Deserialize, Serialize};
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config}; use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
@ -14,15 +15,7 @@ async fn main() -> tide::Result<()> {
} }
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) { async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
let mut records = vec![]; for account in get_wolves(config).await {
if let Ok(accounts) = get_csv(config) {
for account in accounts {
records.push(AccountWolves::from(account));
}
}
for account in records {
update_account(db, &account).await; update_account(db, &account).await;
} }
} }
@ -103,53 +96,64 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
// done with ldap // done with ldap
ldap.unbind().unwrap(); ldap.unbind().unwrap();
} }
impl From<&WolvesResultUser> for AccountWolves {
#[derive(Debug, serde::Deserialize)] fn from(input: &WolvesResultUser) -> Self {
struct RecordCSV {
#[serde(rename = "MemID")]
mem_id: String,
#[serde(rename = "Student Num")]
id_student: String,
#[serde(rename = "Contact Email")]
email: String,
#[serde(rename = "Expiry")]
expiry: String,
#[serde(rename = "First Name")]
name_first: String,
#[serde(rename = "Last Name")]
name_second: String,
}
impl From<RecordCSV> for AccountWolves {
fn from(input: RecordCSV) -> Self {
AccountWolves { AccountWolves {
id_wolves: input.mem_id, id_wolves: input.wolves_id.to_owned(),
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) }, id_student: input.student_id.to_owned(),
email: input.email, email: input.email.to_owned(),
expiry: input.expiry, expiry: input.expiry.to_owned(),
name_first: if input.name_first.is_empty() { None } else { Some(input.name_first) }, name_first: Some(input.first_name.to_owned()),
name_second: if input.name_second.is_empty() { None } else { Some(input.name_second) }, name_second: Some(input.last_name.to_owned()),
} }
} }
} }
fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>> { #[derive(Deserialize, Serialize, Debug)]
let mut records: Vec<RecordCSV> = vec![]; struct WolvesResultUser {
committee: String,
wolves_id: String,
first_name: String,
last_name: String,
email: String,
student_id: Option<String>,
note: Option<String>,
expiry: String,
requested: String,
approved: String,
sitename: String,
domain: String,
}
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResult {
success: i8,
result: Vec<WolvesResultUser>,
}
let csv = format!("{}/{}", &config.home, &config.csv); async fn get_wolves(config: &Config) -> Vec<AccountWolves> {
println!("CSV: {:?}", &csv); if config.wolves_key.is_empty() {
if let Ok(mut rdr) = csv::Reader::from_path(csv) { return vec![];
for result in rdr.deserialize() { }
// Notice that we need to provide a type hint for automatic if config.wolves_url.is_empty() {
// deserialization. return vec![];
let record: RecordCSV = result?; }
if record.mem_id.is_empty() {
continue; // get wolves data
if let Ok(mut res) = surf::post(&config.wolves_url).header("X-AM-Identity", &config.wolves_key).await {
if let Ok(WolvesResult {
success,
result,
}) = res.body_json().await
{
if success != 1 {
return vec![];
} }
records.push(record);
return result.iter().map(AccountWolves::from).collect::<Vec<AccountWolves>>();
} }
} }
Ok(records) vec![]
} }
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) { async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {

View file

@ -180,7 +180,6 @@ pub struct Config {
pub ldap_admin_pw: String, pub ldap_admin_pw: String,
pub home: String, pub home: String,
pub database: String, pub database: String,
pub csv: String,
pub host_port: String, pub host_port: String,
pub mail_smtp: String, pub mail_smtp: String,
pub mail_user: String, pub mail_user: String,
@ -188,6 +187,8 @@ pub struct Config {
pub ssh_root: String, pub ssh_root: String,
pub auth_discord: String, pub auth_discord: String,
pub users_restricted: Vec<String>, pub users_restricted: Vec<String>,
pub wolves_url: String,
pub wolves_key: String,
} }
pub fn get_config() -> Config { pub fn get_config() -> Config {
@ -200,7 +201,6 @@ pub fn get_config() -> Config {
ldap_admin_pw: "".to_string(), ldap_admin_pw: "".to_string(),
home: ".".to_string(), home: ".".to_string(),
database: "database.db".to_string(), database: "database.db".to_string(),
csv: "wolves.csv".to_string(),
host_port: "127.0.0.1:8087".to_string(), host_port: "127.0.0.1:8087".to_string(),
mail_smtp: "".to_string(), mail_smtp: "".to_string(),
mail_user: "".to_string(), mail_user: "".to_string(),
@ -208,6 +208,8 @@ pub fn get_config() -> Config {
ssh_root: "skynet_old".to_string(), ssh_root: "skynet_old".to_string(),
auth_discord: "".to_string(), auth_discord: "".to_string(),
users_restricted: vec![], users_restricted: vec![],
wolves_url: "".to_string(),
wolves_key: "".to_string(),
}; };
if let Ok(x) = env::var("LDAP_HOST") { if let Ok(x) = env::var("LDAP_HOST") {
@ -225,9 +227,6 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("DATABASE") { if let Ok(x) = env::var("DATABASE") {
config.database = x.trim().to_string(); config.database = x.trim().to_string();
} }
if let Ok(x) = env::var("CSV") {
config.csv = x.trim().to_string();
}
if let Ok(x) = env::var("HOST_PORT") { if let Ok(x) = env::var("HOST_PORT") {
config.host_port = x.trim().to_string(); config.host_port = x.trim().to_string();
} }
@ -246,6 +245,12 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("LDAP_DISCORD_AUTH") { if let Ok(x) = env::var("LDAP_DISCORD_AUTH") {
config.auth_discord = x.trim().to_string(); config.auth_discord = x.trim().to_string();
} }
if let Ok(x) = env::var("WOLVES_URL") {
config.wolves_url = x.trim().to_string();
}
if let Ok(x) = env::var("WOLVES_KEY") {
config.wolves_key = x.trim().to_string();
}
if let Ok(x) = env::var("USERS_RESTRICTED") { if let Ok(x) = env::var("USERS_RESTRICTED") {
// usernames that are restricted // usernames that are restricted