$SSH_AUTH_SOCK is not being forwarded to docker
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 [email protected]"
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 [email protected]"
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 [email protected]: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.
Have you checked https://github.com/avsm/docker-ssh-agent-forward ? It seems to do what you want.
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.
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.
@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.
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 👎
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.
- up
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.
That forces the user to unlock the key in that Docker container, bypassing all local customization of the agent, password entry, etc.
Den fre 21 apr. 2017 09:46Josef Johansson [email protected] skrev:
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.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/docker/for-mac/issues/410#issuecomment-296112725, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAGPxeJ9C0joxfp1OancPQF2Ok4KpcQks5ryF7MgaJpZM4JrttX .
True, but the alternative is worse?
Anyway there not that much you can customize in the local ssh-agent, and the same customization is possible to add to the docker ssh-agent that way. A plus is that it is consistent across platforms. You can also have different ssh-agents for different purposes.
Presumably a workaround that doesn't lose local customisations would be to:
- Start a docker ssh server container with the necessary volume
- Use --volume-from instead of mounting the ssh-socket from localhost.
- SSH with agent-forwarding to the docker ssh server container
That way you use your local ssh-agent
copying / mounting a private key won't work if you store your ssh key on a hardware token such as yubikey.
Copying / mounting private keys is not a good idea. If you are going to, @andytson's approach is the best of the bunch.
at @andytson, since your solution seems to have consensus on its viability, would you mind elaborate a little bit on its steps for the less proficient docker users? Thanks!
@cecemel, I've been using https://github.com/uber-common/docker-ssh-agent-forward which seems to implement @andytson's solution. It provides an sshd container that you can forward the Darwin agent do (ssh -A ...), and exposes the agent as a socket in a docker volume that the target container can then use. A crude diagram:
+--------+ +--------+ +--------+ +-----------+
| | SSH | | Filesystem | Volume | Filesystem | Target |
| MacOSX | +----> | sshd | +---------> | With | +---------> | Container |
| | | | | Socket | | |
+--------+ +--------+ +--------+ +-----------+
There's also a solution using pure netcat, but I couldn't get it to work reliably for whatever reason.
If you want you cant mount a volume from ~/.ssh to /root/.ssh (you can replace root with your user)
If you want you cant mount a volume from ~/.ssh to /root/.ssh (you can replace root with your user)
Except it exposes all your keys to everything running in the container.
@mariusgrigaitis for the GitHub cloning part, a workaround would be to use a personal access token instead, till this will be solved. Hope this helps :)
Except it exposes all your keys to everything running in the container.
Not a big deal in development environments.
The example in your original issue works for me on Linux with the exception that in the container I make the agent simply /ssh-agent to avoid anything strange from creating non-existent folders plus to also simplify things.
Your error report lacks some key details. What happens when you try to ls on the key in docker, cat it, echo the variable to make sure it's good, etc. Have you tested that on another hosts, checked mounts, etc? I might be wrong but I assume both docker.sock and the agent file are the same thing aren't they? A special file pointing to a unix socket? If that is indeed the case and it works for docker.sock perhaps Mac has a different security set up for SSH agent sockets. For me on Linux it looks like they are all sockets and there's no immediately obvious reason why one should work and not another beyond permissions on the host. Are you also sure that agent forwarding is working at all on your host? SSH might be working in an outer shell if ssh it taking your private key directly.
There is some good advice here: https://gist.github.com/d11wtq/8699521
In the long run using the default auth sock is a problematic. If you do a normal docker run or docker-compose run it'll be fine to mount it. You have to keep in mind if what you want to do is something you log in, run, wait for it to finish or if it's something you fire and forget. Basically you're going to have a problem with anything unattended or that you want to initiate through an interface other than SSH.
If instead you do something that daemonises containers and then close the session you started them in then your ssh sock will disappear and those containers will no longer be able to connect to SSH using your keys.
Mounting personal keys to a docker container is a particularly extreme risk and almost defeats the entire point. It's like storing your raw meat in the lion cage. Agent forwarding carries its own risks but is a mitigation of the risk introduced by exposing your keys.
In some cases keys wont be on the machine in question. You'll ssh into a docker host using agent forwarding and in this case you really don't want ssh agent to have persistence. Given that it's dependent on your originating connection, then it shouldn't be possible to make it persistent.
These problems aren't simply docker issues but fundamental to the way SSH key security works.
My recommendation is one of the following:
- Create separate keys for development, builds, deploy, etc rather than having personal keys used. This wont always be perfect but at least you're not at risk of exposing your own personal keys.
- Avoid the need for having to ssh into things altogether at all in docker. There are any number of approaches to this (is authentication for read access even needed in a secure network for example). Mirroring such that the problem only exists in one place is another approach (one container managers fetching everything that needs credentials).
If it's genuinely a problem maybe you can do something nasty like you should be able to have a service to translate a domain sock to a TCP endpoint and vice versa. It's not that hard except you'll be publishing your SSH agent over an unsecure port and you'll also need some processes both hostside and container side to manage that.