aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/database.rs5
-rw-r--r--src/datastructures.rs37
-rw-r--r--src/main.rs25
-rw-r--r--src/test.rs6
4 files changed, 49 insertions, 24 deletions
diff --git a/src/database.rs b/src/database.rs
index 5854e81..5ea93c4 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -1,8 +1,5 @@
/*
- ** Copyright (C) 2021 KunoiSayami
- **
- ** This file is part of cgit-simple-authentication and is released under
- ** the AGPL v3 License: https://www.gnu.org/licenses/agpl-3.0.txt
+ ** Copyright (C) 2021-2022 KunoiSayami
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published by
diff --git a/src/datastructures.rs b/src/datastructures.rs
index f581653..f84b3de 100644
--- a/src/datastructures.rs
+++ b/src/datastructures.rs
@@ -1,8 +1,5 @@
/*
- ** Copyright (C) 2021 KunoiSayami
- **
- ** This file is part of cgit-simple-authentication and is released under
- ** the AGPL v3 License: https://www.gnu.org/licenses/agpl-3.0.txt
+ ** Copyright (C) 2021-2022 KunoiSayami
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published by
@@ -28,10 +25,11 @@ use rand_core::OsRng;
use serde::{Deserialize, Serialize};
use sqlx::ConnectOptions;
use std::borrow::{BorrowMut, Cow};
-use std::fmt::Formatter;
+use std::fmt::{Debug, Formatter};
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use std::str::FromStr;
+use log::error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use url::form_urlencoded;
@@ -138,7 +136,7 @@ impl Config {
Self::load_from_path(DEFAULT_CONFIG_LOCATION)
}
- pub fn load_from_path<P: AsRef<Path>>(path: P) -> Self {
+ pub fn load_from_path<P: AsRef<Path> + Debug>(path: P) -> Self {
let file = read_to_string(&path).unwrap_or_default();
let mut cookie_ttl: u64 = DEFAULT_COOKIE_TTL;
@@ -339,7 +337,7 @@ struct ProtectSettings {
}
impl ProtectSettings {
- pub fn from_path<P: AsRef<Path>>(
+ pub fn from_path<P: AsRef<Path> + Debug>(
protect_enabled: bool,
protect_white_list_mode: bool,
path: P,
@@ -355,8 +353,20 @@ impl ProtectSettings {
}
}
- fn load_repos_from_path<P: AsRef<Path>>(white_list_mode: bool, path: P) -> Vec<String> {
- let context = read_to_string(path).unwrap();
+ // TODO: Use Result<> to return
+ fn load_repos_from_path<P: AsRef<Path> + Debug>(white_list_mode: bool, path: P) -> Vec<String> {
+ let context = read_to_string(&path);
+ let context = match context {
+ Ok(context) => context,
+ Err(e) => {
+ error!("Got error while reading {:?}, {:?}", path, e);
+ if let std::io::ErrorKind::NotFound = e.kind() {
+ error!("File not found, did you forget to perform initialization?");
+ }
+ panic!();
+ }
+
+ };
Self::load_repos_from_context(white_list_mode, &context)
}
@@ -614,6 +624,15 @@ pub enum AuthorizerType {
Password,
}
+impl std::fmt::Display for AuthorizerType {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", match self {
+ AuthorizerType::PAM => "PAM",
+ AuthorizerType::Password => "PASSWORD",
+ })
+ }
+}
+
#[async_trait::async_trait]
pub trait Authorizer {
fn method(&self) -> AuthorizerType {
diff --git a/src/main.rs b/src/main.rs
index 217255e..784837b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,5 @@
/*
- ** Copyright (C) 2021 KunoiSayami
- **
- ** This file is part of cgit-simple-authentication and is released under
- ** the AGPL v3 License: https://www.gnu.org/licenses/agpl-3.0.txt
+ ** Copyright (C) 2021-2022-2022 KunoiSayami
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published by
@@ -20,6 +17,7 @@
mod database;
mod datastructures;
+#[cfg(test)]
mod test;
use crate::datastructures::{AuthorizerType, Config, Cookie, FormData, TestSuite, WrapConfigure};
@@ -56,6 +54,7 @@ impl<R: BufRead, W: Write> IOModule<R, W> {
let data = datastructures::FormData::from(buffer);
let cfg = WrapConfigure::from(cfg);
+ log::trace!("Method is {}", cfg.get_authorizer().method());
let ret = verify_login(&cfg, &data).await;
if let Err(ref e) = ret {
@@ -134,11 +133,15 @@ async fn cmd_authenticate_cookie(matches: &ArgMatches<'_>, cfg: Config) -> Resul
let redis_key = format!("cgit_repo_{}", repo);
if !repo.is_empty() && !conn.exists(&redis_key).await? {
- let mut sql_conn = SqliteConnectOptions::from_str(cfg.get_database_location())?
+ let sql_conn = SqliteConnectOptions::from_str(cfg.get_database_location())?
.read_only(true)
.disable_statement_logging()
.connect()
- .await?;
+ .await;
+ if let Err(ref e) = sql_conn {
+ log::error!("Got error while open sqlite connection: {:?}\nDatabase location: {}", e, cfg.get_database_location());
+ }
+ let mut sql_conn = sql_conn?;
if let Some((users,)) =
sqlx::query_as::<_, (String,)>(r#"SELECT "users" FROM "repos" WHERE "repo" = ? "#)
.bind(repo)
@@ -151,6 +154,7 @@ async fn cmd_authenticate_cookie(matches: &ArgMatches<'_>, cfg: Config) -> Resul
}
if let Ok(Some(cookie)) = Cookie::load_from_request(cookies) {
+ log::debug!("Cookie is {:?}", &cookie);
if let Ok(r) = conn
.get::<_, String>(format!("cgit_auth_{}", cookie.get_key()))
.await
@@ -782,11 +786,18 @@ fn process_arguments() -> Result<()> {
}
fn main() -> Result<()> {
+ let logfile_path = env::var("LOG_FILE").unwrap_or_else(|_| "/var/cache/cgit/auth.log".to_string());
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(
"{d(%Y-%m-%d %H:%M:%S)}- {h({l})} - {m}{n}",
)))
- .build(env::var("LOG_FILE").unwrap_or_else(|_| "/var/cache/cgit/auth.log".to_string()))?;
+ .build(&logfile_path);
+ let logfile = match logfile {
+ Ok(f) => f,
+ Err(e) => {
+ return Err(anyhow::Error::msg(format!("Got error while append to {}: {:?}", &logfile_path, e)))
+ }
+ };
let config = log4rs::Config::builder()
.appender(Appender::builder().build("logfile", Box::new(logfile)))
diff --git a/src/test.rs b/src/test.rs
index bdc66dc..cf08105 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -1,8 +1,5 @@
/*
- ** Copyright (C) 2021 KunoiSayami
- **
- ** This file is part of cgit-simple-authentication and is released under
- ** the AGPL v3 License: https://www.gnu.org/licenses/agpl-3.0.txt
+ ** Copyright (C) 2021-2022 KunoiSayami
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published by
@@ -368,6 +365,7 @@ mod core {
.unwrap();
}
+ #[ignore]
#[test]
fn test_pam() {
let service = option_env!("pam_service").unwrap_or("system-auth");