Skip to content

Mount unix socket into Exec #3850

Closed

Description

@shykes

Problem

Exec cannot mount unix sockets into the container. This prevents two frequently requested use cases: SSH agent forwarding and connecting to a docker engine.

Solution

Expand the Dagger API to allow mounting unix sockets from the host into a container. Design this in a such a way that:

  1. it doesn't break the Dagger security model
  2. it can be expanded later (for example to mount unix sockets into the container from sources other than the host filesystem)

Design proposal

As proposed and amended by @aluzzardi :

  • container.WithUnixSocket()
  • host.UnixSocket()

Example:

  • SSH Agent forwarding
    • container.WithUnixSocket("/ssh-agent.sock", c.Host().UnixSocket(os.Getenv("SSH_AUTH_SOCK")))
  • Docker forwarding
    • container.WithUnixSocket("/var/run/docker.sock", c.Host().UnixSocket("/var/run/docker.sock"))

Discussion topics

  • In the future, we could use the same mechanism to forward TCP connections to/from the container (using the shim + engine sessions)
    • Especially useful with services

Activity

  1. aluzzardi commented on Nov 15, 2022

    @aluzzardi
  2. shykes commented on Nov 15, 2022

    @shykes
    Author
  3. aluzzardi commented on Nov 15, 2022

    @aluzzardi
  4. shykes commented on Nov 15, 2022

    @shykes
    Author
  5. shykes commented on Nov 15, 2022

    @shykes
    ContributorAuthor

    @aluzzardi continuing our API design / bikeshedding discussion here to keep better records:

    container-to-host: processes running inside the container can connect to a tcp port, udp port or unix socket, and the traffic is forwarded to a given address in the host network stack

    2 parts:

    • Host() API exposes unix/tcp/udp sockets
    • Container() API allows to pass unix/tcp/udp sockets
    c.Host().UnixSocket(path string) *dagger.Socket
    c.Host().TCPSocket(port int) *dagger.Socket
    c.Host().UDPSocket(port int) *dagger.Socket

    First reaction: TCPSocket and UDPSocket don't make it clear if we're listening or connecting.

    Technically it's not clear with UnixSocket either, but one can make an educated guess based on most common use cases: probably this is for connecting to a docker engine or ssh agent, so probably we're connecting. But you still have to guess.

    I think dagger.Socket might suffer from the same problem: is it a "listener", a "connecter", or both?

    container.WithUnixSocket(path string, socket *dagger.Socket)
    container.WithTCPSocket(port int, socket *dagger.Socket)
    container.WithUDPSocket(port int, socket *dagger.Socket)

    Same problem as above: are we proxying connections inbound (to container) or outbound (from container)?

    Does this make sense for anything but services?

    Regular Execs are:

    • short lived
    • eventual (when do you connect to the socket? might be right away, might be in 10 mins)
    • cached (you might never be able to connect if the Exec is cached)

    In that case, mirror the Host() API in Service():

    c.Container().WithTCPSocket(8080, myService.TCPSocket(8080))

    I know @vito did a lot of this with bass. There's a lot of considerations to take into account, especially around not busting the cache.

    I think the answer might be different from inbound and outbound (which again are hard to distinguish in the proposed API).

    • Outbound: useful both in short-lived containers and long-lived services. Connect to docker engine; to ssh agent; to sql database; to api endpoint... Short-lived containers already do all those things: outbound proxying just gives them more endpoints they can connect to.

    • Inbound: most useful for long-lived services. It would be reasonable to not support them in short-lived containers IMO. But we have to be prepared to change our minds if users show us a reasonable use case we hadn't thought about (or if they just use horrible hacks to bypass the limitation).

    host-to-container: processes running inside the container can listen on a tcp, udp or unix address, and traffic is forwarded from a given listening address on the host

    Same question as above -- does it make sense for non-services? I think not, for the same reasons.

    Same answer as container-to-container. Different for inbound and outbound.

  6. vito commented on Nov 28, 2022

    @vito
    Contributor

    I know @vito did a lot of this with bass. There's a lot of considerations to take into account, especially around not busting the cache.

    I'll fill in some gaps with what I learned from/did with Bass. The main thing I learned is that this kind of feature intersects many tricky subjects and the key thing will be figuring out where to embrace constraints.

    Note that all of the following is really only relevant for TCP/UDP. I haven't considered Unix socket support yet. But it makes sense to discuss them all together anyway.

    DNS + bridge networking

    Bass containers reach services by addressing them over a bridge network, using DNS to resolve to container hostnames to IPs at runtime.

    DNS was my solution for not busting the cache. The key point is to avoid having ephemeral container IPs ever enter into LLB. I got DNS to work by configuring Buildkit to use CNI and installing dnsmasq and the dnsname plugin:

    Bass uses the thunk 'name' as the DNS address, which is a hash of the thunk like GRLO5DSIPLJPQ. It's not possible to set your own name. I didn't want the user to have to deal with collisions, so having content-addressed addresses made sense here, especially since Buildkit also handled deduping two thunks that represent the same service.

    Compared to Dagger: this model is pretty different than what @aluzzardi has proposed so far, which seems more along the lines of forwarding sockets in to local addresses within the container. Do I have that right?

    Thunk addresses

    Bass forces thunks to be self-contained by using thunk addrs as the primitive for passing addresses to commands.

    Rather than something stateful (start service with hostname foo, wait for it to start, use it for a bit, and then stop it), thunk addrs contain the service that it's reaching, and Bass automatically runs it when needed. Buildkit handles deduping equivalent services (e.g. same thunk addr used by multiple clients) and stopping them when all clients go away.

    (defn echo [msg]
      (let [server (http-server msg)]
        (from (linux/alpine)
          ($ wget -O- (addr server :http "http://$host:$port")))))
    
    (echo "Hello, world!")

    Thunk addrs contain a string template where $host and $port are replaced with the actual host and port of the destination service at runtime. But this is obviously TCP specific.

    Compared to Dagger: thunk addrs seem unlikely to be the right approach for Dagger, since our spec dictates that env/args are simple String!s. But I don't think it needs to work the same way in Dagger. If our approach is to proxy addresses into localhost:SOME_PORT I can make this work easily by just resolving $host to localhost and $port to whatever container-side port they chose.

    edit to add: I think we should definitely try to maintain the spirit of this design though: having hard dependencies between the client + service so the whole plan is self-contained and stateless.

    Health/liveness checks

    Some really common cludgy code to write is "start this service and then do a while loop checking for it to be running before continuing on."

    I opted to just have Bass do this for you, since services are started on-demand. When a thunk uses with-port this tells Bass to poll for that port to be bound prior to running the thunk that depends on it.

    Note that this health check literally just opens a connection on the port and closes it. Other systems allow you to configure more sophisticated checks, but I haven't bothered with all that jazz.

    Compared to Dagger: this still makes sense to have IMO. Having to perform health checks yourself seems really clunky.

    TLS & certificate generation

    My "north star" for services in Bass was to be able to run a registry as a service and use it for later thunks. This forced me to figure out how to do TLS, because using DNS or even container IP addresses for Buildkit's image fetching requires the registry to use TLS.

    I decided to have Bass generate and manage its own CA and use it to mint certificates for any thunks that request a TLS cert/path using with-tls. Bass's shim then injects the CA as a trusted cert for the container, so any thunks that use the service will automatically trust it.

    This worked out really nicely! It made it really easy to use TLS for any service.

    Compared to Dagger: it's unclear how we would expect TLS to work if we're forwarding sockets into containers instead of having containers directly address one another.


    I think that covers the interesting bits, but I may have missed/glossed over some parts. AMA!


    @shykes For what it's worth, I don't share the same confusion with inbound/outbound; it feels pretty intuitive to me if I draw parallels to Host Directory access.

    I see Host.(Unix|TCP|UDP)Socket as something that gives you a reference to an existing address on the host, and Container.With(Unix|TCP|UDP)Socket as something that makes a socket reachable inside the container's local filesystem/network on the specified path/port. The same way that Host.Directory gives you a Directory which can be mounted in to a container.

    Replace Host with Container (for a container that runs a service) and I'd interpret it the same way, but drawing parallels to Container.Directory instead.

    So to me this design feels pretty clean, but my only question is whether we can really have TCP addresses work by forwarding sockets to container-local addresses, or whether we could/should use a bridge network + TLS + DNS + etc. instead.

  7. assigned and unassigned on Nov 29, 2022
  8. gerhard commented on Nov 29, 2022

    @gerhard
    Contributor

    We talked to @grouville about this today.

    Updating the assigned person to reflect reality: #4025 💪

  9. vito commented on Nov 29, 2022

    @vito
    Contributor

    Ah thanks for assigning, should have made that more clear! 🙂

  10. shykes commented on Nov 30, 2022

    @shykes
    ContributorAuthor

    As proposed and amended by @aluzzardi :

    • container.WithUnixSocket()
    • host.UnixSocket()

    Example:

    • SSH Agent forwarding

      • container.WithUnixSocket("/ssh-agent.sock", c.Host().UnixSocket(os.Getenv("SSH_AUTH_SOCK")))
    • Docker forwarding

      • container.WithUnixSocket("/var/run/docker.sock", c.Host().UnixSocket("/var/run/docker.sock"))

    Roll call on this. Who is OK to move forward with the above for Unix sockets, without waiting for a complete solution on TCP and UDP forwarding?

    cc @sipsma @vito @aluzzardi @grouville @samalba @mircubed

  11. sipsma commented on Nov 30, 2022

    @sipsma
    Contributor

    Roll call on this. Who is OK to move forward with the above for Unix sockets, without waiting for a complete solution on TCP and UDP forwarding?

    SGTM, unix socket support is relatively straightforward, unblocks a lot of valuable use cases and I can easily see how we can layer on the tcp/udp forwarding later in a backwards compatible way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions