Giter Site home page Giter Site logo

ensody / ssh-agent-inject Goto Github PK

View Code? Open in Web Editor NEW
23.0 2.0 3.0 39 KB

[Note: Not needed with VS Code anymore.] Forwards the host's ssh-agent into a Docker container on Windows and macOS hosts.

License: MIT License

Dockerfile 2.50% Shell 18.38% Go 79.13%
docker ssh ssh-key ssh-agent ssh-agent-forwarding windows openssh-client macos osx linux

ssh-agent-inject's Introduction

ssh-agent-inject

Build Status

Forwards the host's ssh-agent into a Docker container. This is especially useful when working with the Visual Studio Code Remote - Containers extension and Git repos cloned via SSH.

Why this is needed

While you can bind-mount the SSH_AUTH_SOCK from a Linux host, this is not possible from a macOS or Windows host. Also, none of the existing solutions is cross-platform and easy. The recommended solution is to copy the SSH key from the host to the container, but then you have to manually add the key (assuming you've setup ssh-agent within the container) and enter the password within the container.

With ssh-agent-inject you can skip those annoyances and simply reuse your host's ssh-agent.

Usage

Download ssh-agent-inject for your platform. Make sure ssh-agent-inject runs in the background or just launch it on-demand.

Add the following to your Dockerfile:

ENV SSH_AUTH_SOCK=/tmp/.ssh-auth-sock
LABEL com.ensody.ssh-agent-inject=

Alternatively, you can run an arbitrary container directly:

docker run -e SSH_AUTH_SOCK=/tmp/.ssh-auth-sock -l com.ensody.ssh-agent-inject ...

Note that this project is itself using ssh-agent-inject with VS Code (see .devcontainer/).

How it works

This project consists of two applications that communicate through stdio: ssh-agent-inject and ssh-agent-pipe which is embedded within the ssh-agent-inject binary (that's why you don't see it in the release archive).

The ssh-agent-inject command runs on the host and

  • watches Docker for containers having the com.ensody.ssh-agent-inject label
  • copies the embedded ssh-agent-pipe binary into those containers
  • runs ssh-agent-pipe within each container via docker exec
  • connects to the host's ssh-agent (one connection per container)
  • forwards the host's ssh-agent to ssh-agent-pipe via stdio

The ssh-agent-pipe command runs in the container and

  • listens on a UNIX socket at $SSH_AUTH_SOCK
  • handles parallel connections on that UNIX socket
  • serializes all socket<->stdio communication (handles one request-response pair at a time)

The apps communicate via stdio because this keeps the attack surface small and makes it easier to ensure that nobody else can connect to your ssh-agent (assuming you can trust the Docker container, of course).

Building

All required dependencies are contained in a Docker image defined in .devcontainer/, which can be automatically used with Visual Studio Code (or manually via Docker build & run). That way your host system stays clean and the whole environment is automated, exactly defined, isolated from the host, and easily reproducible. This saves time and prevents mistakes (wrong version, interference with other software installed on host, etc.).

Run ./build.sh to build binaries for all platforms.

Releasing

  • Update CHANGELOG.md.
  • Add a tag (e.g. git tag v1.2.3) and push it.
  • The CI system will deploy a draft release to GitHub.
  • Edit the release description and publish it.

Note: Only tags that look like a version number and start with "v" will be deployed to GitHub.

ssh-agent-inject's People

Contributors

wkornewald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ssh-agent-inject's Issues

socket: too many open files

During a docker build which requires a RUN with --mount=type=ssh, I end up with the error below printed by ssh-agent-inject process.

Error fetching containers error during connect: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.25/containers/json?filters=%7B%22label%22%3A%7B%22com.ensody.ssh-agent-inject%22%3Atrue%7D%7D&limit=0: dial unix /var/run/docker.sock: socket: too many open files

Error connecting to agent from entrypoint

I have a Dockerfile with an entrypoint script that expects the agent to be forwarded. But it does not work in the entrypoint when I run the container. It works when I run the same afterwards from the shell.
docker run -it mydockerimage sh
Error connecting to agent: No such file or directory - thats from the entrypoint

But when the shell opens

# ssh-add -l
3072 SHA256:BLAHBLAHBLAH mylogin (RSA)
#

EDIT: Adding some sleep into the entrypoint helped. Would be nice if it somehow waited automatically if possible

BTW thanks a lot for this package

Permission Denied when running as non root

Typically my dev containers run as a non-root user in VSCode and when trying to interact with the agent, I get Permission Denied.
Both ssh-add -l and simply using ssh get permission denied when interacting with the agent. The /tmp/.ssh-auth-sock permissions are srwxr-xr-x root:rooot. If I run the same container as root with:

docker run -ti --rm -e SSH_AUTH_SOCK=/tmp/.ssh-auth-sock -l com.ensody.ssh-agent-inject= --entrypoint=/bin/bash vsc-mw-docker-base-image-updater-integration-393441b99ce096875fdf31c15bd6ca7f-uid 

I can use the agent socket just fine.

Copy from agent to sock failed

I'm getting this error on macOS Mojave 10.14.6 when trying to install php composer dependencies from private repositories while building a docker image with ssh-agent-inject support.

173860a65acb7d82ba855ab25069a8fb890f6a0be9412bab63d5ca0bba087eb8 Copy from agent to sock failed: read unix ->/private/tmp/com.apple.launchd.FLrERP3sdn/Listeners: use of closed network connection

The same image builds properly on Ubuntu 18.04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.