Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Support UNIX sockets for HTTP interfaces #4975

Closed
adrianheine opened this issue Mar 30, 2019 · 15 comments · Fixed by #15708
Closed

Support UNIX sockets for HTTP interfaces #4975

adrianheine opened this issue Mar 30, 2019 · 15 comments · Fixed by #15708
Labels
T-Enhancement New features, changes in functionality, improvements in performance, or user-facing enhancements. Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution z-p2 (Deprecated Label)

Comments

@adrianheine
Copy link

Should probably be implemented in a wrapper around

def listen_tcp(bind_addresses, port, factory, reactor=reactor, backlog=50):

See https://twistedmatrix.com/documents/8.2.0/api/twisted.internet.interfaces.IReactorUNIX.html.

@neilisfragile neilisfragile added enhancement z-p2 (Deprecated Label) labels Apr 1, 2019
@richvdh
Copy link
Member

richvdh commented May 30, 2019

support for what? the CS-API? I'm struggling to see how that would be useful.

@adrianheine
Copy link
Author

It would be useful for people who don't want to open a localhost TCP port when they have a proxy in front of synapse.

@richvdh
Copy link
Member

richvdh commented May 30, 2019

Fair enough. I wasn't aware that http-over-unix-sockets was even a thing.

@richvdh richvdh changed the title Support UNIX sockets Support UNIX sockets for HTTP interfaces May 30, 2019
@syndicalist
Copy link

+1 to this request. It's really common practice for app servers like gunicorn, to skip local networking and just use sockets between the frontend (like nginx) and the app server, e.g. http://docs.gunicorn.org/en/stable/deploy.html

@wvffle
Copy link

wvffle commented May 31, 2020

I'm also looking forward to see this getting implemented.

@erikjohnston erikjohnston added the Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution label Jun 1, 2020
@ilmaisin
Copy link

ilmaisin commented Jul 20, 2020

This would be cool. I am not really a fan of using unprivileged TCP ports for loopbacks. Any unprivileged local user could seize the port if the server shuts down for a while. This issue could perhaps therefore be tagged with the "security" tag?

@1848
Copy link

1848 commented Aug 14, 2020

This is a quick hack I am using currently.
Seems to work but I did not do any testing...

--- a/synapse/app/_base.py
+++ b/synapse/app/_base.py
@@ -163,7 +163,10 @@ def listen_tcp(bind_addresses, port, factory, reactor=reactor, backlog=50):
     r = []
     for address in bind_addresses:
         try:
-            r.append(reactor.listenTCP(port, factory, backlog, address))
+            if address[0] == "/":
+                r.append(reactor.listenUNIX(address, factory, backlog))
+            else:
+                r.append(reactor.listenTCP(port, factory, backlog, address))
         except error.CannotListenError as e:
             check_bind_error(e, address, bind_addresses)

@auscompgeek
Copy link
Contributor

Nice hack.

It probably doesn't make a terrible amount of sense to require all the TCP options for a UNIX listener. Having thought about it for a bit, here's what I imagine would happen:

  1. Change parse_listener_def and ListenerConfig in synapse.config.server for a listener config that only has path, type, and http.
    • Specifying any of the TCP options is an error.
    • Split the TCP fields from ListenerConfig into a TcpListenerConfig?
    • (We could make the manhole support UNIX sockets too, but baby steps.)
  2. Change start_listening and _listen_http to take the new ListenerConfig and listen on TCP or a UNIX socket as appropriate.

If this seems sensible I might try working on this in the next few days.

@clokep
Copy link
Member

clokep commented Aug 14, 2020

That sounds like a reasonable approach on the face of it. Not sure how gnarly it'll end up being.

I suspect the worst part is figuring out how to do the configuration without bloating the config (and being backwards compatible).

@1848
Copy link

1848 commented Aug 14, 2020

My suggestion for config modifications:

Dont Change:

  • bind_address is good enough, no need to change it to path imo.

Drop (ignore or throw error if set):

  • tls
  • port

Add:

  • bool: socket_abstract (useful?)
  • int: socket_mode (socket permissions)
  • str: socket_user (owning user, if process has privileges to chown it)
  • str: socket_group

Another idea, allow to set ipaddrs and unix_paths side by side in bind_addresses...

@auscompgeek
Copy link
Contributor

auscompgeek commented Aug 17, 2020

@1848 I don't see a good reason to allow listening on multiple paths and/or TCP sockets for a UNIX listener. Surely if you're specifying a UNIX socket to listen on then you intend on using a reverse proxy, right?

@1848
Copy link

1848 commented Aug 20, 2020

@auscompgeek I am with you. I could imagine corner cases where it would make sense (e.g. multiple frontends, some of them unable to use unix sockets) but yeah, corner cases...

@kevincox
Copy link

kevincox commented Sep 3, 2021

Yeah, you could give your metric scraper access to one socket and your reverse proxy access to the others. I can definitely see the use cases.

Ideally it would be supported to pass these in from systemd (or similar runners) so that synapse doesn't need to worry about permissions and socket options and just let the service manager deal with that. This also allows hitless restarts, socket activation and other niceties.

@erikjohnston erikjohnston added T-Enhancement New features, changes in functionality, improvements in performance, or user-facing enhancements. and removed z-enhancement labels Sep 22, 2021
@DMRobertson
Copy link
Contributor

Do we consider this closed by #15353?

@realtyem
Copy link
Contributor

realtyem commented Apr 3, 2023

Do we consider this closed by #15353?

Almost. There's a little more to do yet.
Edit: Docs for one, a little more testing hookup, and replication listener enablement.

Note for those watching, metrics won't happen at this time. That's a completely separate problem this won't help with.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Enhancement New features, changes in functionality, improvements in performance, or user-facing enhancements. Z-Help-Wanted We know exactly how to fix this issue, and would be grateful for any contribution z-p2 (Deprecated Label)
Projects
None yet