-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rsjudge-runner): ✨ add require_caps function, remove repetition …
…from user module
- Loading branch information
1 parent
ed32e0a
commit 564bebe
Showing
5 changed files
with
85 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::convert::identity; | ||
|
||
use caps::{errors::CapsError, has_cap, Capability}; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
pub fn require_caps<I>(caps: I) -> Result<()> | ||
where | ||
I: IntoIterator<Item = Capability>, | ||
{ | ||
let missing_caps = caps | ||
.into_iter() | ||
.map(|cap| match has_cap(None, caps::CapSet::Effective, cap) { | ||
Ok(has_cap) => Ok((!has_cap).then_some(cap)), | ||
Err(e) => Err(e), | ||
}) | ||
.collect::<Result<Vec<_>, CapsError>>()? | ||
.into_iter() | ||
.filter_map(identity) | ||
.collect::<Vec<_>>(); | ||
|
||
if missing_caps.is_empty() { | ||
Ok(()) | ||
} else { | ||
Err(Error::CapsRequired { | ||
caps: missing_caps.into_boxed_slice(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
use caps::Capability; | ||
use caps::{errors::CapsError, Capability}; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("{cap} is required to run as another user.")] | ||
CapsRequired { cap: Capability }, | ||
#[error("User '{name}' not found")] | ||
UserNotFound { name: String }, | ||
/// Capabilities required but not set. | ||
#[error("{caps:?} required but not set.")] | ||
CapsRequired { caps: Box<[Capability]> }, | ||
|
||
#[error("I/O error: {0}")] | ||
/// The requested user is not found. | ||
#[error("User '{username}' not found")] | ||
UserNotFound { username: &'static str }, | ||
|
||
/// A wrapper for `std::io::Error`. | ||
#[error(transparent)] | ||
Io(#[from] std::io::Error), | ||
#[error("{0}")] | ||
CapsError(#[from] caps::errors::CapsError), | ||
|
||
/// A wrapper for `caps::errors::CapsError`. | ||
#[error(transparent)] | ||
CapsError(#[from] CapsError), | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
pub type Result<T, E = Error> = std::result::Result<T, E>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
//! Functions to get user instances. | ||
//! | ||
//! All functions return a reference to a static instance of [`uzers::User`] if succeeded. | ||
use std::sync::OnceLock; | ||
|
||
use uzers::{get_user_by_name, User}; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
pub static SUPERVISOR: OnceLock<Option<User>> = OnceLock::new(); | ||
pub static BUILDER: OnceLock<Option<User>> = OnceLock::new(); | ||
pub static RUNNER: OnceLock<Option<User>> = OnceLock::new(); | ||
|
||
pub fn supervisor<'a>() -> Result<&'a User> { | ||
SUPERVISOR | ||
.get_or_init(|| get_user_by_name("rsjudge-supervisor")) | ||
.as_ref() | ||
.ok_or_else(|| Error::UserNotFound { | ||
name: "rsjudge-supervisor".to_string(), | ||
}) | ||
} | ||
|
||
pub fn builder<'a>() -> Result<&'a User> { | ||
BUILDER | ||
.get_or_init(|| get_user_by_name("rsjudge-builder")) | ||
.as_ref() | ||
.ok_or_else(|| Error::UserNotFound { | ||
name: "rsjudge-builder".to_string(), | ||
}) | ||
/// Generate functions to get user instances. | ||
macro_rules! users { | ||
($($vis:vis fn $id:ident() => $name:literal);* $(;)?) => { | ||
$( | ||
#[doc = concat!("Get an instance of user `", $name, "`.")] | ||
/// | ||
/// # Errors | ||
/// Returns an error if the user is not found. | ||
pub fn $id() -> Result<&'static User> { | ||
static INNER: OnceLock<Option<User>> = OnceLock::new(); | ||
INNER | ||
.get_or_init(|| get_user_by_name($name)) | ||
.as_ref() | ||
.ok_or_else(|| Error::UserNotFound { username: $name }) | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
pub fn runner<'a>() -> Result<&'a User> { | ||
RUNNER | ||
.get_or_init(|| get_user_by_name("rsjudge-runner")) | ||
.as_ref() | ||
.ok_or_else(|| Error::UserNotFound { | ||
name: "rsjudge-runner".to_string(), | ||
}) | ||
users! { | ||
pub fn supervisor() => "rsjudge-supervisor"; | ||
pub fn builder() => "rsjudge-builder"; | ||
pub fn runner() => "rsjudge-runner"; | ||
} |