Skip to content

Commit 7dbcbc5

Browse files
committed
Allow setting unix socket mode
Signed-off-by: Katalin Rebhan <me@dblsaiko.net>
1 parent 2590b7d commit 7dbcbc5

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

crates/cli/src/server.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
// Please see LICENSE in the repository root for full details.
66

77
use std::{
8+
fs,
89
future::ready,
910
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, ToSocketAddrs},
10-
os::unix::net::UnixListener,
11+
os::unix::{fs::PermissionsExt, net::UnixListener},
1112
};
1213

1314
use anyhow::Context;
@@ -377,8 +378,20 @@ pub fn build_listeners(
377378
listener.try_into()?
378379
}
379380

380-
HttpBindConfig::Unix { socket } => {
381+
HttpBindConfig::Unix { socket, mode } => {
381382
let listener = UnixListener::bind(socket).context("could not bind socket")?;
383+
384+
if let Some(mode) = mode {
385+
let mut permissions = fs::metadata(socket)
386+
.context("could not read socket metadata")?
387+
.permissions();
388+
let mode = u32::from_str_radix(mode, 8)
389+
.with_context(|| format!("could not parse mode: {mode}"))?;
390+
permissions.set_mode(mode);
391+
fs::set_permissions(socket, permissions)
392+
.context("could not set socket permissions")?;
393+
}
394+
382395
listener.try_into()?
383396
}
384397

crates/config/src/sections/http.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ fn http_address_example_4() -> &'static str {
3636
"0.0.0.0:8080"
3737
}
3838

39+
fn unix_mode_example() -> Option<String> {
40+
Some("660".to_string())
41+
}
42+
3943
#[cfg(not(any(feature = "docker", feature = "dist")))]
4044
fn http_listener_assets_path_default() -> Utf8PathBuf {
4145
"./frontend/dist/".into()
@@ -124,6 +128,11 @@ pub enum BindConfig {
124128
/// Path to the socket
125129
#[schemars(with = "String")]
126130
socket: Utf8PathBuf,
131+
132+
/// Socket file mode. A string representing UNIX permission bits, in octal
133+
/// integer format.
134+
#[schemars(example = "unix_mode_example")]
135+
mode: Option<String>,
127136
},
128137

129138
/// Accept connections on file descriptors passed by the parent process.

docs/config.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,13 @@
893893
"socket": {
894894
"description": "Path to the socket",
895895
"type": "string"
896+
},
897+
"mode": {
898+
"description": "Socket file mode. A string representing UNIX permission bits, in octal integer format.",
899+
"examples": [
900+
"660"
901+
],
902+
"type": "string"
896903
}
897904
}
898905
},

docs/reference/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ http:
5858

5959
# Third option: listen on the given UNIX socket
6060
- socket: /tmp/mas.sock
61+
mode: "660" # permissions to set on the socket, optional
6162

6263
# Fourth option: grab an already open file descriptor given by the parent process
6364
# This is useful when using systemd socket activation

0 commit comments

Comments
 (0)