aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2022-01-13 23:52:24 +0800
committerKunoiSayami <[email protected]>2022-01-13 23:52:24 +0800
commit7662f028360370f0e4f3bd3770965e39c1458ae1 (patch)
treef9a91c9fb15d06370475ef067f8eada475d7ecaf
parentff968cf5c39652d9153aa3dcf5563e6d83ea0e6c (diff)
feat: Add some useful output in logv4.0.0-alpha.2
Signed-off-by: KunoiSayami <[email protected]>
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml4
-rw-r--r--README.md2
-rw-r--r--src/database.rs5
-rw-r--r--src/datastructures.rs37
-rw-r--r--src/main.rs25
-rw-r--r--src/test.rs6
7 files changed, 53 insertions, 28 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6c128c4..88fb293 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -346,7 +346,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cgit-simple-authentication"
-version = "4.0.0-alpha.1"
+version = "4.0.0-alpha.2"
dependencies = [
"anyhow",
"argon2",
diff --git a/Cargo.toml b/Cargo.toml
index 8ed2ef1..361b6cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,11 +1,11 @@
[package]
name = "cgit-simple-authentication"
-version = "4.0.0-alpha.1"
+version = "4.0.0-alpha.2"
authors = ["KunoiSayami <[email protected]>"]
edition = "2018"
[dependencies]
-log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
+log = { version = "0.4", features = ["max_level_trace", "release_max_level_info"] }
env_logger = "0.8"
tokio = { version = "1", features = ["full"] }
serde_json = "1"
diff --git a/README.md b/README.md
index 4391b66..d94063a 100644
--- a/README.md
+++ b/README.md
@@ -81,7 +81,7 @@ Most of the ideas come from: https://github.com/varphone/cgit-gogs-auth-filter
[![](https://www.gnu.org/graphics/agplv3-155x51.png)](https://www.gnu.org/licenses/agpl-3.0.txt)
-Copyright (C) 2021 KunoiSayami
+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 the Free Software Foundation, either version 3 of the License, or any later version.
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");