Skip to content
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

Tidy up configuration files UNIX permissions #7983

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN apt update -y && \
libxcb-shape0-dev \
libxcb-xfixes0-dev \
libasound2-dev \
libpam0g-dev \
libpulse-dev \
make \
cmake \
Expand Down
36 changes: 35 additions & 1 deletion libs/hbb_common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,19 @@ pub fn load_path<T: serde::Serialize + serde::de::DeserializeOwned + Default + s

#[inline]
pub fn store_path<T: serde::Serialize>(path: PathBuf, cfg: T) -> crate::ResultType<()> {
Ok(confy::store_path(path, cfg)?)
#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
Ok(confy::store_path_perms(
path,
cfg,
fs::Permissions::from_mode(0o600),
)?)
}
#[cfg(windows)]
{
Ok(confy::store_path(path, cfg)?)
}
}

impl Config {
Expand Down Expand Up @@ -2214,4 +2226,26 @@ mod tests {
assert_eq!(cfg, Ok(cfg_to_compare), "Failed to test wrong_field_str");
}
}

#[test]
fn test_store_load() {
let peerconfig_id = "123456789";
let cfg: PeerConfig = Default::default();
cfg.store(&peerconfig_id);
assert_eq!(PeerConfig::load(&peerconfig_id), cfg);

#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
// ignore file type information by masking with 0o777 (see https://stackoverflow.com/a/50045872)
fs::metadata(PeerConfig::path(&peerconfig_id))
.expect("reading metadata failed")
.permissions()
.mode()
& 0o777,
0o600
);
}
}
}