Skip to content

Commit 692c826

Browse files
committed
Allow setting unix socket mode
Signed-off-by: Katalin Rebhan <[email protected]>
1 parent 209f180 commit 692c826

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 files in the repository root for full details.
66

77
use std::{
8+
fs,
89
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, ToSocketAddrs},
9-
os::unix::net::UnixListener,
1010
time::Duration,
11+
os::unix::{fs::PermissionsExt, net::UnixListener},
1112
};
1213

1314
use anyhow::Context;
@@ -385,8 +386,20 @@ pub fn build_listeners(
385386
listener.try_into()?
386387
}
387388

388-
HttpBindConfig::Unix { socket } => {
389+
HttpBindConfig::Unix { socket, mode } => {
389390
let listener = UnixListener::bind(socket).context("could not bind socket")?;
391+
392+
if let Some(mode) = mode {
393+
let mut permissions = fs::metadata(socket)
394+
.context("could not read socket metadata")?
395+
.permissions();
396+
let mode = u32::from_str_radix(mode, 8)
397+
.with_context(|| format!("could not parse mode: {mode}"))?;
398+
permissions.set_mode(mode);
399+
fs::set_permissions(socket, permissions)
400+
.context("could not set socket permissions")?;
401+
}
402+
390403
listener.try_into()?
391404
}
392405

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
@@ -901,6 +901,13 @@
901901
"socket": {
902902
"description": "Path to the socket",
903903
"type": "string"
904+
},
905+
"mode": {
906+
"description": "Socket file mode. A string representing UNIX permission bits, in octal integer format.",
907+
"examples": [
908+
"660"
909+
],
910+
"type": "string"
904911
}
905912
}
906913
},

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)