Skip to content

Allow setting unix socket mode #4344

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

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions crates/cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Please see LICENSE in the repository root for full details.

use std::{
fs,
future::ready,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, ToSocketAddrs},
os::unix::net::UnixListener,
os::unix::{fs::PermissionsExt, net::UnixListener},
};

use anyhow::Context;
Expand Down Expand Up @@ -377,8 +378,20 @@ pub fn build_listeners(
listener.try_into()?
}

HttpBindConfig::Unix { socket } => {
HttpBindConfig::Unix { socket, mode } => {
let listener = UnixListener::bind(socket).context("could not bind socket")?;

if let Some(mode) = mode {
let mut permissions = fs::metadata(socket)
.context("could not read socket metadata")?
.permissions();
let mode = u32::from_str_radix(mode, 8)
.with_context(|| format!("could not parse mode: {mode}"))?;
permissions.set_mode(mode);
fs::set_permissions(socket, permissions)
.context("could not set socket permissions")?;
}
Comment on lines +384 to +393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid any potential race conditions, I would appreciate if we used the opened file descriptor directly.

This is a little bit annoying to do, but basically, you can:

This way we're sure that if for some reason the socket gets deleted, gets moved or whatever, we don't end up doing operations on the wrong file. It only ever gets opened once

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That unfortunately doesn't work, calling fchmod on a socket fd does nothing. See https://stackoverflow.com/a/15450698

(Still, change here: 2xsaiko@push-looktxnmnmsw)


listener.try_into()?
}

Expand Down
9 changes: 9 additions & 0 deletions crates/config/src/sections/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ fn http_address_example_4() -> &'static str {
"0.0.0.0:8080"
}

fn unix_mode_example() -> Option<String> {
Some("660".to_string())
}

#[cfg(not(any(feature = "docker", feature = "dist")))]
fn http_listener_assets_path_default() -> Utf8PathBuf {
"./frontend/dist/".into()
Expand Down Expand Up @@ -124,6 +128,11 @@ pub enum BindConfig {
/// Path to the socket
#[schemars(with = "String")]
socket: Utf8PathBuf,

/// Socket file mode. A string representing UNIX permission bits, in octal
/// integer format.
#[schemars(example = "unix_mode_example")]
mode: Option<String>,
},

/// Accept connections on file descriptors passed by the parent process.
Expand Down
7 changes: 7 additions & 0 deletions docs/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,13 @@
"socket": {
"description": "Path to the socket",
"type": "string"
},
"mode": {
"description": "Socket file mode. A string representing UNIX permission bits, in octal integer format.",
"examples": [
"660"
],
"type": "string"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ http:

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

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