aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 05:28:26 +0100
committertv <tv@krebsco.de>2026-10-31 05:28:26 +0100
commitc76fd5b9b0a3bba6c8ffa847e5fc818661e800d6 (patch)
tree56441e7534fc4d25381c8d368f98d9528551f1df /src
parent8802997e3adfe43b2e084f3695f8620fdfc36759 (diff)
move warn_if_readable_by_others to files module
Diffstat (limited to 'src')
-rw-r--r--src/files.rs21
-rw-r--r--src/identity.rs2
-rw-r--r--src/swarm/auth.rs23
-rw-r--r--src/transport/libp2p/config.rs2
4 files changed, 24 insertions, 24 deletions
diff --git a/src/files.rs b/src/files.rs
index e27bde6..cd2f38c 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -167,6 +167,27 @@ fn sync_parent(path: &Path) {
}
}
+/// A key is a secret: a file others may read is worth a warning (not an error: a
+/// group may be deliberate, e.g. shared with a deployment tool). `what`: which key.
+pub(crate) fn warn_if_readable_by_others(path: &Path, what: &str) {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ if let Ok(metadata) = std::fs::metadata(path) {
+ let mode = metadata.permissions().mode();
+ if mode & 0o077 != 0 {
+ tracing::warn!(
+ path = %path.display(),
+ mode = format!("{:o}", mode & 0o777),
+ "The {what} file is readable by others than its owner; chmod 600 it"
+ );
+ }
+ }
+ }
+ #[cfg(not(unix))]
+ let _ = (path, what);
+}
+
fn with_suffix(path: &Path, suffix: &str) -> PathBuf {
let mut name = path.as_os_str().to_owned();
name.push(suffix);
diff --git a/src/identity.rs b/src/identity.rs
index 96e524e..8efe30f 100644
--- a/src/identity.rs
+++ b/src/identity.rs
@@ -48,7 +48,7 @@ impl NodeKey {
.with_context(|| format!("Reading the node key from {}", path.display()));
}
};
- crate::swarm::auth::warn_if_readable_by_others(path, "node key");
+ crate::files::warn_if_readable_by_others(path, "node key");
let Ok(seed) = <[u8; KEY_BYTES]>::try_from(bytes.as_slice()) else {
bail!(
"The node key in {} has {} bytes; a node key has {KEY_BYTES}",
diff --git a/src/swarm/auth.rs b/src/swarm/auth.rs
index 50e2fae..534a64d 100644
--- a/src/swarm/auth.rs
+++ b/src/swarm/auth.rs
@@ -40,27 +40,6 @@ struct Rejections {
suppressed: u64,
}
-/// A key is a secret: a file others may read is worth a warning (not an error: a
-/// group may be deliberate, e.g. shared with a deployment tool). `what`: which key.
-pub(crate) fn warn_if_readable_by_others(path: &Path, what: &str) {
- #[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- if let Ok(metadata) = std::fs::metadata(path) {
- let mode = metadata.permissions().mode();
- if mode & 0o077 != 0 {
- tracing::warn!(
- path = %path.display(),
- mode = format!("{:o}", mode & 0o777),
- "The {what} file is readable by others than its owner; chmod 600 it"
- );
- }
- }
- }
- #[cfg(not(unix))]
- let _ = (path, what);
-}
-
/// At most one rejection log line per this interval.
const REJECTION_LOG_INTERVAL: Duration = Duration::from_secs(10);
@@ -70,7 +49,7 @@ impl SwarmKey {
pub(crate) fn from_file(path: &Path) -> Result<Self> {
let key = std::fs::read(path)
.with_context(|| format!("Reading the swarm key from {}", path.display()))?;
- warn_if_readable_by_others(path, "swarm key");
+ crate::files::warn_if_readable_by_others(path, "swarm key");
if key.len() < 16 {
bail!(
"The swarm key in {} has {} bytes; use at least 16 (32 random bytes recommended)",
diff --git a/src/transport/libp2p/config.rs b/src/transport/libp2p/config.rs
index 012999d..719f2e8 100644
--- a/src/transport/libp2p/config.rs
+++ b/src/transport/libp2p/config.rs
@@ -103,7 +103,7 @@ impl Libp2pConfig {
};
let mut bytes = std::fs::read(path)
.with_context(|| format!("Reading the libp2p key from {}", path.display()))?;
- crate::swarm::auth::warn_if_readable_by_others(path, "libp2p key");
+ crate::files::warn_if_readable_by_others(path, "libp2p key");
if bytes.len() != 32 {
bail!(
"The libp2p key in {} has {} bytes; it has 32",