From 9f02350ac04d6a1e0fa808e7761e846f6e0abd80 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Sat, 25 May 2024 01:00:17 +0200 Subject: [PATCH 01/15] update usage --- book/src/chsr/README.md | 6 ++++-- src/chsr/cli/usage.rs | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/book/src/chsr/README.md b/book/src/chsr/README.md index f7a6ca46..9b8df0a8 100644 --- a/book/src/chsr/README.md +++ b/book/src/chsr/README.md @@ -84,11 +84,13 @@ chsr options path [operation] Environment options: chsr options env [operation] setpolicy [policy] Specify the policy for environment settings (delete-all, keep-all, inherit). - set [key=value,...] Set the policy as delete-all and the key-value map to enforce. + set [key=value,...] Set variables to enforce. + keep-only [key,...] Set the policy as delete-all and the key map to keep. + delete-only [key,...] Set the policy as keep-all and the key map to delete. whitelist, wl [listing] Manage the whitelist for environment settings. blacklist, bl [listing] Manage the blacklist for environment settings. checklist, cl [listing] Manage the checklist for environment settings. (Removed if contains unsafe chars) - + setlist, sl [listing] Manage the setlist for environment settings. (define environment variables) Timeout options: chsr options timeout [operation] diff --git a/src/chsr/cli/usage.rs b/src/chsr/cli/usage.rs index 1cd92fc3..4ede9508 100644 --- a/src/chsr/cli/usage.rs +++ b/src/chsr/cli/usage.rs @@ -101,10 +101,13 @@ chsr options path [operation] const RAR_USAGE_OPTIONS_ENV :&str = formatcp!("{UNDERLINE}{BOLD}Environment options:{RST} chsr options env [operation] {BOLD}setpolicy{RST} [policy] Specify the policy for environment settings (delete-all, keep-all, inherit). - {BOLD}set{RST} [key=value,...] Set the policy as delete-all and the key-value map to enforce. + {BOLD}set{RST} [key=value,...] Set variables to enforce. + {BOLD}keep-only{RST} [key,...] Set the policy as delete-all and the key map to keep. + {BOLD}delete-only{RST} [key,...] Set the policy as keep-all and the key map to delete. {BOLD}whitelist, wl{RST} [listing] Manage the whitelist for environment settings. {BOLD}blacklist, bl{RST} [listing] Manage the blacklist for environment settings. {BOLD}checklist, cl{RST} [listing] Manage the checklist for environment settings. (Removed if contains unsafe chars) + {BOLD}setlist, sl{RST} [listing] Manage the setlist for environment settings. (define environment variables) ",UNDERLINE=UNDERLINE, BOLD=BOLD, RST=RST); const RAR_USAGE_OPTIONS_TIMEOUT: &str = formatcp!( From c75b2f888704100cac31ba68ea571e84741b84a8 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:39:35 +0200 Subject: [PATCH 02/15] chore: rename rar-common to rootasrole-core for crates.io --- Cargo.toml | 5 +++-- build.rs | 3 +++ capable | 2 +- rar-common/Cargo.toml | 6 +++--- xtask/Cargo.toml | 1 + 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f7dfc38a..c37a9782 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = ["xtask", "rar-common"] [package] -name = "RootAsRole" +name = "rootasrole" # The project version is managed on json file in resources/rootasrole.json version = "3.0.0" rust-version = "1.76.0" @@ -17,6 +17,7 @@ keywords = ["sudo", "capabilities", "rbac", "linux", "security"] categories = ["command-line-utilities", "os::linux-apis", "config"] exclude = ["sudoers-reader/*", "book/*"] + [badges] maintainance ={ status = "actively-maintained", badge = "https://img.shields.io/badge/maintenance-actively%20maintained-brightgreen.svg" } @@ -58,7 +59,7 @@ serde_json = "1.0.116" toml = "0.8.13" [dependencies] -rar-common = { path = "rar-common" } +rar-common = { path = "rar-common", version = "3.0.0", package = "rootasrole-core" } tracing = "0.1.40" tracing-subscriber = "0.3.18" libc = "0.2.155" diff --git a/build.rs b/build.rs index 8a77ea39..ac6a09c4 100644 --- a/build.rs +++ b/build.rs @@ -94,6 +94,9 @@ fn main() { if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { eprintln!("cargo:warning={}", err); } + if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } if let Err(err) = set_readme_version(package_version, "README.md") { eprintln!("cargo:warning={}", err); } diff --git a/capable b/capable index 3d019032..42fff41a 160000 --- a/capable +++ b/capable @@ -1 +1 @@ -Subproject commit 3d0190322feb6245a5d17165cdfa2fb5843714b6 +Subproject commit 42fff41a5d814a5fe1666fd285a9d5a5646be4d7 diff --git a/rar-common/Cargo.toml b/rar-common/Cargo.toml index 3e700bb0..8a00e93b 100644 --- a/rar-common/Cargo.toml +++ b/rar-common/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "rar-common" -version = "0.1.0" +name = "rootasrole-core" +version = "3.0.0" edition = "2021" [dependencies] @@ -39,4 +39,4 @@ pcre2 = ["dep:pcre2"] finder = ["dep:glob"] [lints.rust] -unexpected_cfgs = { level = "allow", check-cfg = ['cfg(tarpaulin_include)'] } \ No newline at end of file +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(tarpaulin_include)'] } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 4079b74c..4836af4c 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -3,6 +3,7 @@ name = "xtask" # The project version is managed on json file in resources/rootasrole.json version = "3.0.0" edition = "2021" +publish = false [[bin]] name = "postinst" From 8a97a5c55844407db58b828f1abe2f306278e611 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:39:46 +0200 Subject: [PATCH 03/15] chore: Update TS_LOCATION path for test environment --- src/sr/timeout.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sr/timeout.rs b/src/sr/timeout.rs index ee4d805c..6475a613 100644 --- a/src/sr/timeout.rs +++ b/src/sr/timeout.rs @@ -159,7 +159,10 @@ fn write_lockfile(lockfile_path: &Path) { .expect("Failed to write to lockfile"); } +#[cfg(not(test))] const TS_LOCATION: &str = "/var/run/rar/ts"; +#[cfg(test)] +const TS_LOCATION: &str = "target/ts"; fn read_cookies(user: &Cred) -> Result, Box> { let path = Path::new(TS_LOCATION).join(user.user.uid.as_raw().to_string()); From d04fb3f8037c6d674f83bc09dc394038e545a218 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:50:47 +0200 Subject: [PATCH 04/15] chore: Update rar-common to rootasrole-core for crates.io --- rar-common/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rar-common/Cargo.toml b/rar-common/Cargo.toml index 8a00e93b..1e88f15b 100644 --- a/rar-common/Cargo.toml +++ b/rar-common/Cargo.toml @@ -2,6 +2,8 @@ name = "rootasrole-core" version = "3.0.0" edition = "2021" +description = "This core crate contains the RBAC and main features for the RootAsRole project." +license = "GPL-3.0-or-later" [dependencies] tracing = "0.1.40" From 73e35116f456a2988718be19b5fc3da277846634 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:51:49 +0200 Subject: [PATCH 05/15] chore: Update env_logger dependency to version 0.11.5 for crates.io --- rar-common/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rar-common/Cargo.toml b/rar-common/Cargo.toml index 1e88f15b..b35940f7 100644 --- a/rar-common/Cargo.toml +++ b/rar-common/Cargo.toml @@ -30,7 +30,7 @@ once_cell = "1.19.0" hex = "0.4.3" [dev-dependencies] -env_logger = "*" +env_logger = "0.11.5" test-log = { version = "0.2.12", features = ["trace"] } tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt"] } From 364fcc6fd5066696afbaf1fd4b9b9579d2c33d48 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:52:58 +0200 Subject: [PATCH 06/15] chore: Update env_logger dependency to version 0.11.5 in root toml for crates.io --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c37a9782..4251c1e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,7 +93,7 @@ const_format = "0.2.32" hex = "0.4.3" [dev-dependencies] -env_logger = "*" +env_logger = "0.11.5" test-log = { version = "0.2.12", features = ["trace"] } tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt"] } From d262423615f1c05b37719adc0bd6d945244c51d2 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 13:57:43 +0200 Subject: [PATCH 07/15] chore: Add conditions on sub-crates version updating --- build.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index ac6a09c4..0dd9656b 100644 --- a/build.rs +++ b/build.rs @@ -91,11 +91,15 @@ fn main() { eprintln!("cargo:warning={}", err); } } - if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { - eprintln!("cargo:warning={}", err); + if Path::new("xtask").is_dir() { + if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } } - if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { - eprintln!("cargo:warning={}", err); + if Path::new("rar-common").is_dir() { + if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } } if let Err(err) = set_readme_version(package_version, "README.md") { eprintln!("cargo:warning={}", err); From 3cb1363fb63ec16071130db0cddbd9d19547cd97 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 14:40:50 +0200 Subject: [PATCH 08/15] chore: Update build.rs to comply with crates.io publishing --- build.rs | 96 ++++++++++++++++++++++++++------------------------------ 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/build.rs b/build.rs index 0dd9656b..a2c44318 100644 --- a/build.rs +++ b/build.rs @@ -1,16 +1,19 @@ use serde_json::Value; use std::error::Error; -use std::fs::File; +use std::fs::{self, File}; use std::io::{BufRead, BufReader, Write}; - use std::path::Path; -fn write_version<'a>(f: &'a mut File, doc: &'a Value) -> Result<&'a str, Box> { - let package_version = doc - .get("version") - .ok_or("No version found")? - .as_str() - .unwrap(); + +fn package_version(json: &Value) -> Result<&str, Box> { + json + .get("version") + .ok_or("No version found")? + .as_str() + .ok_or("Version is not a string".into()) +} + +fn write_version<'a>(f: &'a mut File, package_version: &'a str) -> Result<&'a str, Box> { f.write_all( format!( "pub const PACKAGE_VERSION: &'static str = \"{}\";\n", @@ -63,60 +66,49 @@ fn main() { let json: Value = include_str!("resources/rootasrole.json") .parse() .expect("Failed to parse rootasrole.json"); + let package_version = package_version(&json).expect("Failed to get package version"); let dest_path = std::path::Path::new("src").join("version.rs"); + if dest_path.exists() && fs::read_to_string(&dest_path).unwrap().ends_with(&format!("\"{}\";\n",package_version)) { + return; + } let mut f = File::create(dest_path).unwrap(); f.write_all(b"// This file is generated by build.rs\n") .unwrap(); f.write_all(b"// Do not edit this file directly\n").unwrap(); f.write_all(b"// Instead edit build.rs and run cargo build\n") .unwrap(); - match write_version(&mut f, &json) { - Ok(package_version) => { - if let Err(err) = set_cargo_version(package_version, "Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - //if folder capable/ exists - if Path::new("capable/capable").is_dir() { - if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - if let Err(err) = - set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml") - { - eprintln!("cargo:warning={}", err); - } - if let Err(err) = - set_cargo_version(package_version, "capable/capable-common/Cargo.toml") - { - eprintln!("cargo:warning={}", err); - } - } - if Path::new("xtask").is_dir() { - if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - } - if Path::new("rar-common").is_dir() { - if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { - eprintln!("cargo:warning={}", err); - } - } - if let Err(err) = set_readme_version(package_version, "README.md") { - eprintln!("cargo:warning={}", err); - } - //if let Err(err) = set_pkgbuild_version(package_version, "PKGBUILD") { - //eprintln!("cargo:warning={}", err); - //} + write_version(&mut f, &package_version).expect("Failed to write version"); + if let Err(err) = set_cargo_version(package_version, "Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + //if folder capable/ exists + if Path::new("capable/capable").is_dir() { + if let Err(err) = set_cargo_version(package_version, "capable/capable/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + if let Err(err) = + set_cargo_version(package_version, "capable/capable-ebpf/Cargo.toml") + { + eprintln!("cargo:warning={}", err); } - Err(err) => { + if let Err(err) = + set_cargo_version(package_version, "capable/capable-common/Cargo.toml") + { eprintln!("cargo:warning={}", err); } } - - // let xml = include_str!("resources/rootasrole.xml"); - // if let Err(err) = write_dtd(&mut f, xml) { - // eprintln!("cargo:warning={}", err); - // } - + if Path::new("xtask").is_dir() { + if let Err(err) = set_cargo_version(package_version, "xtask/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + } + if Path::new("rar-common").is_dir() { + if let Err(err) = set_cargo_version(package_version, "rar-common/Cargo.toml") { + eprintln!("cargo:warning={}", err); + } + } + if let Err(err) = set_readme_version(package_version, "README.md") { + eprintln!("cargo:warning={}", err); + } f.flush().unwrap(); } From 6e59833a6b877fab9ddd91bf44c6ad7a600c5b21 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 15:35:40 +0200 Subject: [PATCH 09/15] chore: Update faq.md with cargo install explanation and add avoid cargo install in build.rs --- book/src/faq.md | 6 ++++++ build.rs | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/book/src/faq.md b/book/src/faq.md index 363fd897..36731616 100644 --- a/book/src/faq.md +++ b/book/src/faq.md @@ -2,6 +2,12 @@ This page contains known issues and solutions for RootAsRole project. +## Why I cannot do `cargo install rootasrole` command ? + +The `cargo install` command is primarily designed to install Rust binaries into a user’s local environment, specifically into the `.cargo/bin` directory in the user’s home directory. The philosophy behind this is to keep the installation process simple and unprivileged, avoiding system-wide changes that would require root or admin permissions. + +In this context, RootAsRole is a privilege escalation tool, so it requires privileges to work. As the `cargo install` command is not designed to install system-wide binaries, so RootAsRole won't work as expected this way. + ## capable does not work on my OS, what can I do ? capable is a tool based on eBPF features, so it requires a Linux kernel version 4.1 or later. Additionnally you need many kernel features enabled, [described here](https://github.com/iovisor/bcc/blob/master/INSTALL.md#kernel-configuration). It is also, possible that the program cannot allocate memory, in this case you may consider to add CAP_SYS_RESOURCE capability to the program, but this may not solve completely the issue. diff --git a/build.rs b/build.rs index a2c44318..11ca8b5e 100644 --- a/build.rs +++ b/build.rs @@ -3,6 +3,7 @@ use std::error::Error; use std::fs::{self, File}; use std::io::{BufRead, BufReader, Write}; use std::path::Path; +use std::process::Command; fn package_version(json: &Value) -> Result<&str, Box> { @@ -63,6 +64,11 @@ fn main() { println!("cargo:rerun-if-changed=resources/rootasrole.json"); println!("cargo:rerun-if-changed=build.rs"); + let is_install = std::env::var("CARGO_INSTALL_ROOT").is_ok(); + if is_install { + panic!("This crate is not meant to be installed with cargo install, please download .deb or .rpm and install it with your package manager.\nSee: https://lechatp.github.io/RootAsRole/faq.html"); + } + let json: Value = include_str!("resources/rootasrole.json") .parse() .expect("Failed to parse rootasrole.json"); @@ -71,6 +77,7 @@ fn main() { if dest_path.exists() && fs::read_to_string(&dest_path).unwrap().ends_with(&format!("\"{}\";\n",package_version)) { return; } + let mut f = File::create(dest_path).unwrap(); f.write_all(b"// This file is generated by build.rs\n") .unwrap(); From 00730b2593916ceb2ece00f547b5dead17705247 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Mon, 23 Sep 2024 16:02:07 +0200 Subject: [PATCH 10/15] chore: Update faq.md with improved cargo install explanation --- book/src/faq.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/src/faq.md b/book/src/faq.md index 36731616..81e3f7e1 100644 --- a/book/src/faq.md +++ b/book/src/faq.md @@ -4,9 +4,9 @@ This page contains known issues and solutions for RootAsRole project. ## Why I cannot do `cargo install rootasrole` command ? -The `cargo install` command is primarily designed to install Rust binaries into a user’s local environment, specifically into the `.cargo/bin` directory in the user’s home directory. The philosophy behind this is to keep the installation process simple and unprivileged, avoiding system-wide changes that would require root or admin permissions. +The `cargo install` command is primarily designed to install Rust binaries into a user’s local environment, specifically into the `.cargo/bin` directory in the user’s home directory. The philosophy behind this is to keep the installation process simple and unprivileged, avoiding system-wide changes that would require administrative privileges. -In this context, RootAsRole is a privilege escalation tool, so it requires privileges to work. As the `cargo install` command is not designed to install system-wide binaries, so RootAsRole won't work as expected this way. +RootAsRole is a privilege escalation tool that is designed to be system-wide and with administrative privileges. Therefore, it is not suitable for installation via `cargo install`. Instead, you should install it with the package manager of your distribution or by compiling it from source. The main reason for RootAsRole being on crates.io is to claim the name and warn users when they try to install it via `cargo install`. ## capable does not work on my OS, what can I do ? From 94986433bdef7fe57c30f1ebd579a2335f22c398 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Wed, 25 Sep 2024 10:12:18 +0200 Subject: [PATCH 11/15] chore: Bump version to 3.0.1, ignore unimplemented config updates. --- Cargo.toml | 3 +- README.md | 2 +- build.rs | 68 +++++++------------------- rar-common/Cargo.toml | 4 +- rar-common/src/database/migration.rs | 2 + rar-common/src/database/mod.rs | 7 ++- rar-common/src/database/versionning.rs | 24 --------- rar-common/src/lib.rs | 15 ++++-- rar-common/src/version.rs | 2 +- src/version.rs | 4 -- xtask/Cargo.toml | 2 +- 11 files changed, 44 insertions(+), 89 deletions(-) delete mode 100644 src/version.rs diff --git a/Cargo.toml b/Cargo.toml index 4251c1e0..2365d4dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["xtask", "rar-common"] [package] name = "rootasrole" # The project version is managed on json file in resources/rootasrole.json -version = "3.0.0" +version = "3.0.1" rust-version = "1.76.0" authors = ["Eddie Billoir "] edition = "2021" @@ -100,6 +100,7 @@ tracing-subscriber = { version = "0.3.16", default-features = false, features = pest-test-gen = "0.1.7" pest-test = "0.1.6" lazy_static = "1.4.0" +toml = "0.8.19" [package.metadata.deb] diff --git a/README.md b/README.md index dbe19358..6483289f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ -# RootAsRole (V3.0.0) : A memory-safe and security-oriented alternative to sudo/su commands +# RootAsRole (V3.0.1) : A memory-safe and security-oriented alternative to sudo/su commands **RootAsRole** is a project to allow Linux/Unix administrators to delegate their administrative tasks access rights to users. Its main features are : diff --git a/build.rs b/build.rs index 11ca8b5e..3f49feb1 100644 --- a/build.rs +++ b/build.rs @@ -1,23 +1,18 @@ -use serde_json::Value; -use std::error::Error; -use std::fs::{self, File}; -use std::io::{BufRead, BufReader, Write}; -use std::path::Path; -use std::process::Command; +use std::{error::Error, fs::{self, File}, io::{BufRead, BufReader, Write}, path::Path}; +use toml::Table; -fn package_version(json: &Value) -> Result<&str, Box> { - json - .get("version") - .ok_or("No version found")? - .as_str() - .ok_or("Version is not a string".into()) + +fn package_version>(path : P) -> Result> { + let cargo_toml = fs::read_to_string(path)?; + let value: Table = cargo_toml.parse::()?; + Ok(value["package"]["version"].as_str().map(|s| s.to_string()).expect("Failed to get package version")) } fn write_version<'a>(f: &'a mut File, package_version: &'a str) -> Result<&'a str, Box> { f.write_all( format!( - "pub const PACKAGE_VERSION: &'static str = \"{}\";\n", + "pub const PACKAGE_VERSION: &str = \"{}\";\n", package_version ) .as_bytes(), @@ -61,7 +56,7 @@ fn set_readme_version(package_version: &str, file: &str) -> Result<(), Box Migration { while migrated == ChangeResult::UpgradeIndirect || migrated == ChangeResult::DowngradeIndirect { + migrated = ChangeResult::None; for migration in migrations { match migration.change(doc, &from, &to)? { ChangeResult::UpgradeDirect | ChangeResult::DowngradeDirect => { @@ -123,6 +124,7 @@ impl Migration { /// If the version is already the current version, nothing is done. /// If the version is older, the database is upgraded. /// If the version is newer, the database is downgraded. + /// Returns true if the database was migrated, false if it was already at the current version. pub fn migrate( version: &Version, doc: &mut T, diff --git a/rar-common/src/database/mod.rs b/rar-common/src/database/mod.rs index 793ab580..e61025c4 100644 --- a/rar-common/src/database/mod.rs +++ b/rar-common/src/database/mod.rs @@ -8,6 +8,7 @@ use chrono::Duration; use linked_hash_set::LinkedHashSet; use serde::{de, Deserialize, Serialize}; use tracing::debug; +use tracing_subscriber::field::debug; use self::{migration::Migration, options::EnvKey, structs::SConfig, versionning::Versioning}; @@ -69,12 +70,14 @@ pub fn read_json_config( )?; let versionned_config: Versioning>> = serde_json::from_reader(file)?; let config = versionned_config.data; - if Migration::migrate( + if let Ok(true) = Migration::migrate( &versionned_config.version, &mut *config.as_ref().borrow_mut(), versionning::JSON_MIGRATIONS, - )? { + ) { save_json(settings.clone(), config.clone())?; + } else { + debug!("No migrations needed"); } make_weak_config(&config); Ok(config) diff --git a/rar-common/src/database/versionning.rs b/rar-common/src/database/versionning.rs index 787dc01f..f98a1df1 100644 --- a/rar-common/src/database/versionning.rs +++ b/rar-common/src/database/versionning.rs @@ -34,31 +34,7 @@ impl Default for Versioning { } pub(crate) const JSON_MIGRATIONS: &[Migration] = &[ - Migration { - from: || Version::parse("3.0.0-alpha.4").unwrap(), - to: || Version::parse("3.0.0-alpha.5").unwrap(), - up: |_, _| Ok(()), - down: |_, _| Ok(()), - }, - Migration { - from: || Version::parse("3.0.0-alpha.5").unwrap(), - to: || Version::parse("3.0.0").unwrap(), - up: |_, _| Ok(()), - down: |_, _| Ok(()), - }, ]; pub(crate) const SETTINGS_MIGRATIONS: &[Migration] = &[ - Migration { - from: || Version::parse("3.0.0-alpha.4").unwrap(), - to: || Version::parse("3.0.0-alpha.5").unwrap(), - up: |_, _| Ok(()), - down: |_, _| Ok(()), - }, - Migration { - from: || Version::parse("3.0.0-alpha.5").unwrap(), - to: || Version::parse("3.0.0").unwrap(), - up: |_, _| Ok(()), - down: |_, _| Ok(()), - }, ]; diff --git a/rar-common/src/lib.rs b/rar-common/src/lib.rs index be1f9af3..6c904e42 100644 --- a/rar-common/src/lib.rs +++ b/rar-common/src/lib.rs @@ -256,12 +256,12 @@ where read_effective(false).or(dac_override_effective(false))?; debug!("{}", serde_json::to_string_pretty(&value)?); let settingsfile = rc_refcell!(value.data); - if Migration::migrate( + if let Ok(true) = Migration::migrate( &value.version, &mut *settingsfile.as_ref().borrow_mut(), SETTINGS_MIGRATIONS, - )? { - Migration::migrate( + ) { + if let Ok(true) = Migration::migrate( &value.version, &mut *settingsfile .as_ref() @@ -270,8 +270,13 @@ where .as_ref() .borrow_mut(), JSON_MIGRATIONS, - )?; - save_settings(settingsfile.clone())?; + ) { + save_settings(settingsfile.clone())?; + } else { + debug!("No config migrations needed"); + } + } else { + debug!("No settings migrations needed"); } Ok(settingsfile) } diff --git a/rar-common/src/version.rs b/rar-common/src/version.rs index ce6ba159..a54afb71 100644 --- a/rar-common/src/version.rs +++ b/rar-common/src/version.rs @@ -1,4 +1,4 @@ // This file is generated by build.rs // Do not edit this file directly // Instead edit build.rs and run cargo build -pub const PACKAGE_VERSION: &str = "3.0.0-alpha.5"; +pub const PACKAGE_VERSION: &'static str = "3.0.1"; diff --git a/src/version.rs b/src/version.rs deleted file mode 100644 index e7239f23..00000000 --- a/src/version.rs +++ /dev/null @@ -1,4 +0,0 @@ -// This file is generated by build.rs -// Do not edit this file directly -// Instead edit build.rs and run cargo build -pub const PACKAGE_VERSION: &'static str = "3.0.0"; diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 4836af4c..9b56b751 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xtask" # The project version is managed on json file in resources/rootasrole.json -version = "3.0.0" +version = "3.0.1" edition = "2021" publish = false From 115b558b8b96dc1003d964b50cd640596e778ed1 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Wed, 25 Sep 2024 10:12:36 +0200 Subject: [PATCH 12/15] feat: Change directory to git root before building and installing --- xtask/src/installer/build.rs | 3 +++ xtask/src/installer/install.rs | 3 ++- xtask/src/util.rs | 10 +++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/xtask/src/installer/build.rs b/xtask/src/installer/build.rs index aa96d931..30f88da3 100644 --- a/xtask/src/installer/build.rs +++ b/xtask/src/installer/build.rs @@ -3,6 +3,8 @@ use std::{fs, os::unix, process::Command}; use anyhow::Context; use tracing::debug; +use crate::util::change_dir_to_git_root; + use super::BuildOptions; fn build_binary( @@ -22,6 +24,7 @@ fn build_binary( } pub fn build(options: &BuildOptions) -> Result<(), anyhow::Error> { + change_dir_to_git_root()?; if options.clean_before { Command::new("cargo") .arg("clean") diff --git a/xtask/src/installer/install.rs b/xtask/src/installer/install.rs index a722f084..6411202c 100644 --- a/xtask/src/installer/install.rs +++ b/xtask/src/installer/install.rs @@ -13,7 +13,7 @@ use strum::EnumIs; use tracing::{debug, error, info}; use crate::installer::Profile; -use crate::util::{detect_priv_bin, BOLD, RED, RST}; +use crate::util::{change_dir_to_git_root, detect_priv_bin, BOLD, RED, RST}; use anyhow::{anyhow, Context}; use super::{CHSR_DEST, SR_DEST}; @@ -199,6 +199,7 @@ pub fn install( .unwrap() )) })?; + change_dir_to_git_root()?; // change to the root of the project before elevating privileges env::set_var("ROOTASROLE_INSTALLER_NESTED", "1"); tracing::warn!("Elevating privileges..."); std::process::Command::new(priv_exe) diff --git a/xtask/src/util.rs b/xtask/src/util.rs index 9de1f866..9d82c42e 100644 --- a/xtask/src/util.rs +++ b/xtask/src/util.rs @@ -2,7 +2,7 @@ use std::{ fs::{self, File}, io, os::{fd::AsRawFd, unix::fs::MetadataExt}, - path::Path, + path::Path, process::Command, }; use anyhow::{anyhow, Context}; @@ -119,6 +119,14 @@ fn read_or_dac_override(effective: bool) -> Result<(), capctl::Error> { Ok(()) } +pub fn change_dir_to_git_root() -> Result<(), anyhow::Error> { + let output = Command::new("git").args(&["rev-parse", "--show-toplevel"]).output()?; + let git_root = String::from_utf8(output.stdout)?.trim().to_string(); + debug!("Changing directory to git root: {}", git_root); + std::env::set_current_dir(git_root)?; + Ok(()) +} + /// Set or unset the immutable flag on a file /// # Arguments /// * `file` - The file to set the immutable flag on From d9e4de802c04a1919f510027640a714c26d23899 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Wed, 25 Sep 2024 10:14:11 +0200 Subject: [PATCH 13/15] chore: Fmt and fix --- build.rs | 27 +++++++++++++++++++------- rar-common/src/database/mod.rs | 1 - rar-common/src/database/versionning.rs | 6 ++---- xtask/src/util.rs | 7 +++++-- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/build.rs b/build.rs index 3f49feb1..c814e564 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,19 @@ -use std::{error::Error, fs::{self, File}, io::{BufRead, BufReader, Write}, path::Path}; +use std::{ + error::Error, + fs::{self, File}, + io::{BufRead, BufReader, Write}, + path::Path, +}; use toml::Table; - -fn package_version>(path : P) -> Result> { +fn package_version>(path: P) -> Result> { let cargo_toml = fs::read_to_string(path)?; let value: Table = cargo_toml.parse::
()?; - Ok(value["package"]["version"].as_str().map(|s| s.to_string()).expect("Failed to get package version")) + Ok(value["package"]["version"] + .as_str() + .map(|s| s.to_string()) + .expect("Failed to get package version")) } fn write_version<'a>(f: &'a mut File, package_version: &'a str) -> Result<&'a str, Box> { @@ -65,8 +72,14 @@ fn main() { } let package_version = package_version("Cargo.toml").expect("Failed to get package version"); - let dest_path = std::path::Path::new("rar-common").join("src").join("version.rs"); - if dest_path.exists() && fs::read_to_string(&dest_path).unwrap().ends_with(&format!("\"{}\";\n",package_version)) { + let dest_path = std::path::Path::new("rar-common") + .join("src") + .join("version.rs"); + if dest_path.exists() + && fs::read_to_string(&dest_path) + .unwrap() + .ends_with(&format!("\"{}\";\n", package_version)) + { return; } let mut f = File::create(dest_path).unwrap(); @@ -88,4 +101,4 @@ fn main() { if let Err(err) = set_readme_version(&package_version, "README.md") { eprintln!("cargo:warning={}", err); } -} \ No newline at end of file +} diff --git a/rar-common/src/database/mod.rs b/rar-common/src/database/mod.rs index e61025c4..0fdadcbd 100644 --- a/rar-common/src/database/mod.rs +++ b/rar-common/src/database/mod.rs @@ -8,7 +8,6 @@ use chrono::Duration; use linked_hash_set::LinkedHashSet; use serde::{de, Deserialize, Serialize}; use tracing::debug; -use tracing_subscriber::field::debug; use self::{migration::Migration, options::EnvKey, structs::SConfig, versionning::Versioning}; diff --git a/rar-common/src/database/versionning.rs b/rar-common/src/database/versionning.rs index f98a1df1..f46ec9f5 100644 --- a/rar-common/src/database/versionning.rs +++ b/rar-common/src/database/versionning.rs @@ -33,8 +33,6 @@ impl Default for Versioning { } } -pub(crate) const JSON_MIGRATIONS: &[Migration] = &[ -]; +pub(crate) const JSON_MIGRATIONS: &[Migration] = &[]; -pub(crate) const SETTINGS_MIGRATIONS: &[Migration] = &[ -]; +pub(crate) const SETTINGS_MIGRATIONS: &[Migration] = &[]; diff --git a/xtask/src/util.rs b/xtask/src/util.rs index 9d82c42e..22a43bdf 100644 --- a/xtask/src/util.rs +++ b/xtask/src/util.rs @@ -2,7 +2,8 @@ use std::{ fs::{self, File}, io, os::{fd::AsRawFd, unix::fs::MetadataExt}, - path::Path, process::Command, + path::Path, + process::Command, }; use anyhow::{anyhow, Context}; @@ -120,7 +121,9 @@ fn read_or_dac_override(effective: bool) -> Result<(), capctl::Error> { } pub fn change_dir_to_git_root() -> Result<(), anyhow::Error> { - let output = Command::new("git").args(&["rev-parse", "--show-toplevel"]).output()?; + let output = Command::new("git") + .args(&["rev-parse", "--show-toplevel"]) + .output()?; let git_root = String::from_utf8(output.stdout)?.trim().to_string(); debug!("Changing directory to git root: {}", git_root); std::env::set_current_dir(git_root)?; From a870559270fa3094349500b3e385feca54191055 Mon Sep 17 00:00:00 2001 From: LeChatP Date: Wed, 25 Sep 2024 10:48:10 +0200 Subject: [PATCH 14/15] fix: publish regression --- build.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index c814e564..c5648460 100644 --- a/build.rs +++ b/build.rs @@ -70,7 +70,9 @@ fn main() { if is_install { panic!("This crate is not meant to be installed with cargo install, please download .deb or .rpm and install it with your package manager.\nSee: https://lechatp.github.io/RootAsRole/faq.html"); } - + if !std::path::Path::new("rar-common").exists() { + return; + } let package_version = package_version("Cargo.toml").expect("Failed to get package version"); let dest_path = std::path::Path::new("rar-common") .join("src") From dfc01a56e5f0ef450fc9ab1b8163a5f510b0805e Mon Sep 17 00:00:00 2001 From: LeChatP Date: Fri, 27 Sep 2024 13:00:50 +0200 Subject: [PATCH 15/15] chore: Update README.md with smaller image size --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6483289f..ffd4719c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@

- +