Skip to content

Commit

Permalink
Use enum type for clarity & separation of logic
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Dec 16, 2023
1 parent d8dff29 commit 62a0d69
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/nix_health/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ anyhow = { version = "1.0.75" }
colored = { version = "2.0" }
which = { version = "4.4.2" }
bytesize.workspace = true
itertools = "0.12.0"
20 changes: 7 additions & 13 deletions crates/nix_health/src/check/trusted_users.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use itertools::{Either, Itertools};
use nix_rs::config::TrustedUserValue;
use serde::{Deserialize, Serialize};

use crate::traits::*;
Expand Down Expand Up @@ -52,16 +52,10 @@ fn is_current_user_trusted(nix_info: &nix_rs::info::NixInfo) -> bool {
let current_user = &nix_info.nix_env.current_user;
let current_user_groups: HashSet<&String> =
nix_info.nix_env.current_user_groups.iter().collect();
let val = &nix_info.nix_config.trusted_users.value;
// In nix.conf, groups are prefixed with '@'. '*' means all users are
// trusted.
if val.contains(&"*".to_string()) {
return true;
}
let (val_groups, val_users): (Vec<String>, Vec<String>) =
val.iter().partition_map(|x| match x.strip_prefix('@') {
Some(x) => Either::Left(x.to_string()),
None => Either::Right(x.clone()),
});
val_users.contains(current_user) || val_groups.iter().any(|x| current_user_groups.contains(&x))
let trusted_user_values = nix_info.nix_config.get_trusted_users_vals();
trusted_user_values.iter().any(|x| match x {
TrustedUserValue::Group(x) => current_user_groups.contains(&x),
TrustedUserValue::User(x) => x == current_user,
TrustedUserValue::All => true,
})
}
38 changes: 38 additions & 0 deletions crates/nix_rs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,44 @@ impl NixConfig {
.await?;
Ok(v)
}

pub fn get_trusted_users_vals(&self) -> Vec<TrustedUserValue> {
self.trusted_users
.value
.iter()
.map(|s| TrustedUserValue::from_str(s))
.collect()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrustedUserValue {
/// All users are trusted
All,
/// A specific user is trusted
User(String),
/// Users belonging to a specific group are trusted
Group(String),
}

impl TrustedUserValue {
fn from_str(s: &str) -> Self {
// In nix.conf, groups are prefixed with '@'. '*' means all users are
// trusted.
if s == "*" {
return Self::All;
}
match s.strip_prefix('@') {
Some(s) => Self::Group(s.to_string()),
None => Self::User(s.to_string()),
}
}
}

impl From<String> for TrustedUserValue {
fn from(s: String) -> Self {
Self::from_str(&s)
}
}

#[tokio::test]
Expand Down

0 comments on commit 62a0d69

Please sign in to comment.