softprops / hyperlocal

🔌 ✨rustlang hyper bindings for local unix domain sockets
MIT License
229 stars 46 forks source link

Socket permissions. #8

Closed janderholm closed 6 years ago

janderholm commented 6 years ago

Hi!

I have a scenario in which Apache is running as a reverse proxy to a hyper server through a UDS. Things seem to work well enough but there is an issue with the permissions of the socket. I need it to be writable by both user and group as I don't want to run both apache and the hyper server under the same user.

I tried to simply open the socket as a file and use "set_permissions". This doesn't work however as the socket file isn't created until server.run() is executed and then it's too late for me to do it in the program.

I'm sure it's doable in some way but I can't seem to figure it out. Preferably the socket should be created early enough that I could launch the program as root, create the socket and set whatever permission I need, drop any permissions and finish of by starting to listen to the socket.

Any ideas?

janderholm commented 6 years ago

Seems like I did something wrong. I tried changing permissions between new() and run() again:

let _ = fs::remove_file(&addr);
let server = Http::new()
    .bind(&addr, NewHandlerService::new(|| Ok(say_hello)))
    .unwrap();

let mut permissions = fs::metadata(&addr)
    .expect("Failed to get metadata from socket")
    .permissions();
permissions.set_mode(0o666);

fs::set_permissions(&addr, permissions)
    .expect("Could not set mode on socket");

println!("Listen on unix://{}", &addr);
server.run().unwrap();

As you would expert it works just fine!

softprops commented 6 years ago

Sorry I missed your first message. Would this be something helpful to add to the this crate's documentation?