-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add credential loader helper #98
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::{env, ffi::OsStr, fs, io, path::PathBuf}; | ||
|
||
/// Credential loader for units. | ||
/// | ||
/// Credentials are read by systemd on unit startup and exported by their ID. | ||
/// | ||
/// **Note**: only the user associated with the unit and the superuser may access credentials. | ||
/// | ||
/// More documentation: <https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Credentials> | ||
#[derive(Clone, Debug)] | ||
pub struct CredentialLoader { | ||
dir: PathBuf, | ||
} | ||
|
||
impl CredentialLoader { | ||
/// Attempt to initiate a loader, returning [`None`] if no credentials are available. | ||
pub fn new() -> Option<Self> { | ||
let dir: PathBuf = env::var_os("CREDENTIALS_DIRECTORY")?.into(); | ||
|
||
if dir.is_dir() { | ||
Some(Self { dir }) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Get a credential by its ID. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// use libsystemd::CredentialLoader; | ||
/// | ||
/// if let Some(loader) = CredentialLoader::new() { | ||
/// let key = "token"; | ||
/// match loader.get("token") { | ||
/// Ok(val) => println!("{}: {}", key, String::from_utf8_lossy(&val)), | ||
/// Err(e) => println!("couldn't retreive {}: {}", key, e), | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn get<K: AsRef<OsStr>>(&self, id: K) -> io::Result<Vec<u8>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be happier if we return a |
||
self._get(id.as_ref()) | ||
} | ||
|
||
fn _get(&self, id: &OsStr) -> io::Result<Vec<u8>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note that the systemd docs says |
||
let path: PathBuf = [self.dir.as_ref(), id].iter().collect(); | ||
|
||
fs::read(path) | ||
} | ||
|
||
/// An iterator over this units credentials. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// use libsystemd::CredentialLoader; | ||
/// | ||
/// if let Some(loader) = CredentialLoader::new() { | ||
/// for entry in loader.iter() { | ||
/// match entry { | ||
/// Ok(entry) => { | ||
/// let key = entry.file_name(); | ||
/// println!("{:?}: {}", key, String::from_utf8_lossy(&loader.get(&key)?)) | ||
/// } | ||
/// Err(e) => println!("couldn't list some credential: {}", e), | ||
/// } | ||
/// } | ||
/// } | ||
/// # Ok::<(), std::io::Error>(()) | ||
pub fn iter(&self) -> fs::ReadDir { | ||
self.dir.read_dir().expect("path exists") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need this kind of listing primitive, then let's store a https://docs.rs/nix/latest/nix/dir/struct.Dir.html internally and directly wire our method to |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
/// Interfaces for socket-activated services. | ||
pub mod activation; | ||
mod credential; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this |
||
/// Interfaces for systemd-aware daemons. | ||
pub mod daemon; | ||
/// Error handling. | ||
|
@@ -34,3 +35,5 @@ pub mod logging; | |
pub mod sysusers; | ||
/// Helpers for working with systemd units. | ||
pub mod unit; | ||
|
||
pub use credential::CredentialLoader; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be called
open()
and return aResult<Self>
instead.To check whether the credentials store is available or not, let's add a dedicated
CredentialLoader::credentials_directory() -> Option<PathBuf>
on the side.