Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5a166ad
Add custom logs
Arshdeep54 Dec 19, 2024
6f9ca23
correct log file path
Arshdeep54 Dec 19, 2024
1772f4f
fix cargo test
Arshdeep54 Dec 19, 2024
6dd0690
fix log implementation
Arshdeep54 Dec 20, 2024
f46758a
adds debug statements as logs
Arshdeep54 Dec 22, 2024
d30e0b2
add debug flag in logging
Arshdeep54 Feb 26, 2025
518c01f
Implement projects as groups
Arshdeep54 Mar 22, 2025
e856086
Add watchdog update function
Arshdeep54 Apr 10, 2025
7d5e978
Restrict user to only login to his account
Arshdeep54 Apr 11, 2025
9b3b9e1
Update pam_ruser to pam_user
Arshdeep54 Apr 12, 2025
9dac407
Update validate user function
Arshdeep54 Apr 13, 2025
29b2cc7
Update ssh message with threads
Arshdeep54 Apr 13, 2025
badc2ed
Implement logging with fern
Arshdeep54 Apr 15, 2025
abcac32
Add logs filtering
Arshdeep54 Apr 15, 2025
80da0a0
Update handle_update to add new users
Arshdeep54 Apr 15, 2025
86ed573
Update logs and create_user function
Arshdeep54 Apr 17, 2025
e090e78
Fix fetching from master branch
Arshdeep54 Apr 18, 2025
c8b4a11
Add constant.rs and enums
Arshdeep54 Apr 21, 2025
94f9e0a
Update user's bashrc with group's bashrc
Arshdeep54 Apr 21, 2025
c116d0b
Update add to sudo and wheel group
Arshdeep54 Apr 25, 2025
ac21dab
Add auto-update feature in handle_auth
Arshdeep54 May 3, 2025
0f91224
Fix multiple appends in .bashrc
Arshdeep54 May 3, 2025
f7dd03c
Log sudo command on slack
Arshdeep54 May 3, 2025
045c7bc
Update based on last commit
Arshdeep54 May 16, 2025
6189738
Fix base_url in handle_update
Arshdeep54 May 16, 2025
cc23e54
Fix verbose logging
Arshdeep54 May 16, 2025
a17cf10
Add verbose level in config
Arshdeep54 May 18, 2025
b84fd19
Add default logs to watchdog.logs
Arshdeep54 May 18, 2025
58d6f92
Add dependencies logs in dependencies.logs
Arshdeep54 May 18, 2025
3d02069
Fix HOME env in update_user_bashrc
Arshdeep54 Jul 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,061 changes: 1,310 additions & 751 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ clap = "2.19"
error-chain="0.12.1"
toml_edit="0.1.5"
nix = "0.9.0"
openssl = "0.10"
openssl-sys = "0.9.58"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see these used anywhere. Is there a reason for including openssl?

chrono = "0.4"
log = "0.4"
fern = "0.6"
lazy_static = "1.5.0"
regex = "1.11.1"

[[ bin ]]
name = "watchdog"
path = "src/main.rs"

[lib]
name = "lib"
path = "src/lib/lib.rs"
path = "src/lib/lib.rs"

[features]
default = []
auto-update = []
5 changes: 5 additions & 0 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ mkdir -p /opt/watchdog/logs
touch /opt/watchdog/logs/sudo.logs
touch /opt/watchdog/logs/su.logs
touch /opt/watchdog/logs/ssh.logs
mkdir -p /opt/watchdog/custom-logs
touch /opt/watchdog/custom-logs/ssh.logs
touch /opt/watchdog/custom-logs/sudo.logs
touch /opt/watchdog/custom-logs/su.logs
touch /opt/watchdog/custom-logs/auth.logs

cp ../target/release/watchdog /opt/watchdog/bin/watchdog
chown root /opt/watchdog/bin/watchdog
Expand Down
9 changes: 8 additions & 1 deletion sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ token = 'secret_token'
# Make an incoming hook to your Slack workspace from this
# app(https://slack.com/apps/A0F7XDUAZ-incoming-webhooks)
# and paste the hook URL here. You can customize the icon and name as you like.
slack = 'https://hooks.slack.com/services/ABCDEFGHI/ABCDEFGHI/abcdefghijklmnopqrstuvwx'
token = 'bot_token'
channel = 'C0123456789'

# Logging
[logging]
debug='true'
offset='+5:30'
verbosity='v'
44 changes: 29 additions & 15 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;

use lib::logger::LogTarget;
use log::{error, info};
use nix::unistd::{fork, ForkResult};

use lib::config::read_config;
Expand All @@ -8,42 +8,56 @@ use lib::init::init;
use lib::keyhouse::{get_name, validate_user};
use lib::notifier;

#[cfg(feature = "auto-update")]
use crate::update::handle_update;

pub fn handle_auth(ssh_host_username: &str, ssh_key: &str) -> Result<()> {
let config = read_config()?;
init(&config)?;
info!(target: LogTarget::AUTH.as_str(), "ssh_key in handle_auth: {}", ssh_key);

#[cfg(feature = "auto-update")]
{
match handle_update() {
Ok(_) => {
info!(target: LogTarget::UPDATE.as_str(), "Update handled successfully");
}
Err(e) => {
error!(target: LogTarget::UPDATE.as_str(), "Error handling update: {}", e);
return Err(e);
}
}
}

match validate_user(&config, ssh_host_username.to_string(), ssh_key) {
Ok(true) => {
let data = format!(
"ssh_host_username = '{}'\nssh_key = '{}'\n",
ssh_host_username, ssh_key
);

fs::write("/opt/watchdog/ssh_env", data)
.chain_err(|| "Cannot write temporary environment file. Please check if the watchdog `auth_keys_cmd` is run by the root user")?;

info!(target: LogTarget::AUTH.as_str(), "User validated by handle auth");
println!("{}", ssh_key);
Ok(())
}

Ok(false) => {
info!(target: LogTarget::AUTH.as_str(), "User not validated");
let name = get_name(&config, ssh_key)?;

info!(target: LogTarget::AUTH.as_str(), "Logging failed");
match fork() {
Ok(ForkResult::Parent { .. }) => {}
Ok(ForkResult::Child) => {
notifier::post_ssh_summary(
&config,
false,
name,
ssh_host_username.to_string(),
&name,
&ssh_host_username.to_string(),
)?;
std::process::exit(0);
}
Err(_) => println!("Fork failed"),
}
Ok(())
}

Err(e) => Err(e).chain_err(|| "Error while validating user from keyhouse"),
Err(e) => {
error!(target: LogTarget::AUTH.as_str(), "Error while validating user from keyhouse");
Err(e).chain_err(|| "Error while validating user from keyhouse")
}
}
}
43 changes: 34 additions & 9 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use serde_derive::Deserialize;
use toml_edit::{value, Document};

use crate::errors::*;
use crate::{constants::CONFIG_PATH, errors::*};

#[derive(Deserialize, Clone)]
pub struct KeyhouseConf {
Expand All @@ -13,24 +13,33 @@ pub struct KeyhouseConf {

#[derive(Deserialize, Clone)]
pub struct NotifiersConf {
pub slack: String,
pub token: String,
pub channel: String,
}

#[derive(Deserialize, Clone)]
pub struct LoggingConf {
pub debug: String,
pub offset: String,
pub verbosity: String,
}

#[derive(Deserialize, Clone)]
pub struct Config {
pub hostname: String,
pub keyhouse: KeyhouseConf,
pub notifiers: NotifiersConf,
pub logging: LoggingConf,
}

pub fn read_config() -> Result<Config> {
let toml_str = fs::read_to_string("/opt/watchdog/config.toml")?;
let toml_str = fs::read_to_string(CONFIG_PATH)?;
let config: Config = toml::from_str(&toml_str)?;
Ok(config)
}

pub fn set_config_value(key: &str, val: &str) -> Result<()> {
let toml_str = fs::read_to_string("/opt/watchdog/config.toml")?;
let toml_str = fs::read_to_string(CONFIG_PATH)?;
let mut doc = toml_str.parse::<Document>().chain_err(|| {
"Invalid TOML file. Please reverify if /opt/watchdog/config.toml is a valid toml file."
})?;
Expand All @@ -44,27 +53,43 @@ pub fn set_config_value(key: &str, val: &str) -> Result<()> {
"keyhouse.token" => {
doc["keyhouse"]["token"] = value(val);
}
"notifiers.slack" => {
doc["notifiers"]["slack"] = value(val);
"notifiers.token" => {
doc["notifiers"]["token"] = value(val);
}
"notifiers.channel" => {
doc["notifiers"]["channel"] = value(val);
}
"logging.debug" => {
doc["logging"]["debug"] = value(val);
}
"logging.offset" => {
doc["logging"]["offset"] = value(val);
}
"logging.verbosity" => {
doc["logging"]["verbosity"] = value(val);
}
_ => {
return Err("Invalid Key passed".into());
}
}
fs::write("/opt/watchdog/config.toml", doc.to_string())?;
fs::write(CONFIG_PATH, doc.to_string())?;
Ok(())
}

pub fn get_config_value(key: &str) -> Result<String> {
let toml_str = fs::read_to_string("/opt/watchdog/config.toml")?;
let toml_str = fs::read_to_string(CONFIG_PATH)?;
let doc = toml_str.parse::<Document>().chain_err(|| {
"Invalid TOML file. Please reverify if /opt/watchdog/config.toml is a valid toml file."
})?;
let val = match key {
"hostname" => doc["hostname"].as_str(),
"keyhouse.base_url" => doc["keyhouse"]["base_url"].as_str(),
"keyhouse.token" => doc["keyhouse"]["token"].as_str(),
"notifiers.slack" => doc["notifiers"]["slack"].as_str(),
"notifiers.token" => doc["notifiers"]["token"].as_str(),
"notifiers.channel" => doc["notifiers"]["channel"].as_str(),
"logging.debug" => doc["logging"]["debug"].as_str(),
"logging.offset" => doc["logging"]["offset"].as_str(),
"logging.verbosity" => doc["logging"]["verbosity"].as_str(),
_ => {
return Err("Invalid Key passed".into());
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const BASE_COMMIT_PATH: &str = "/opt/watchdog/base_commit.txt";
pub const CONFIG_PATH: &str = "/opt/watchdog/config.toml";
pub const LOG_PATH: &str = "/opt/watchdog/custom-logs";
pub const HOME_DIR: &str = "/opt/watchdog/users";
Loading