Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Visual Studio 2017 to work with ssh-agent / ssh-add for git

Whenever I "synchronize" (pull, push) my repository in Visual Studio, I notice the git-askpass.exe window pop up and ask me for my ssh private key passphrase for my git repository. That's fine, but I was wondering whether there is a way to get it to work with ssh-agent.exe.

When starting my git bash interpreter, I always also start its own ssh-agent.exe (it's in Program Files/git as opposed to Visual Studio's Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer/Git/mingw32/libexec/git-core) and ssh-add.exe my private key so that it asks me once for passphrase and then stops bothering me. From Visual Studio I always have to reenter the passphrase for my private key upon "sync-ing" the projects. Is there any way to slipstream this?

I was thinking of deleting that whole Visual Studio folder and creating a hard link towards the Git for Windows folder...

There's also Connect to Git repository with SSH using Visual Studio 2017 which doesn't feel like it covers my case.

like image 478
foxx1337 Avatar asked Feb 20 '18 12:02

foxx1337


1 Answers

Ok, I think i figured this one out.

Visual Studio 2017 launches git from C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd and git.exe from there attempts its own auth methods until one works.

The auth method I care about in my question deals with the already running ssh-agent process. Upon launching it, ssh-agent detects its environment and "conveniently" advises upon which "standard" env variables to set, such as:

SSH_AUTH_SOCK=/tmp/ssh-iTGtZyR9tAxO/agent.12088; export SSH_AUTH_SOCK;
SSH_AGENT_PID=972; export SSH_AGENT_PID;
#echo Agent pid 972;

Notice that the path is in "MINGW-speak", resolving to paths such as %LOCALAPPDATA%\Temp. It's actually OK, even outside of git bash to refer to paths like that (git.exe, ssh-agent.exe will be able to resolve them).

So I have 2 scripts, one that loads my private key, which is started by my .bashrc the first time I open a git bash:

10_ssh.sh:

#!/usr/bin/env bash

SSH_ENV=$HOME/.ssh/environment

function start_agent {
    echo "Initialising new SSH agent..."

    # run it with eval so that it sticks after terminal completion
    eval "/usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}"

    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null

    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . ${SSH_ENV} > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

and a new one I just created, that also puts into "Windows" those variables:

15_ssh_windows_hacks.sh:

#!/usr/bin/env bash

# This one makes sure that the Windows environment recognizes the ssh agent started before

if [[ `uname` == MINGW* ]]
then
    setx SSH_AUTH_SOCK $SSH_AUTH_SOCK > /dev/null
    setx SSH_AGENT_PID $SSH_AGENT_PID > /dev/null
fi

The only caveat is that Visual Studio has to be started after executing this script, because apparently it only reads its ENV once when it starts, and not every time it spawns the child git.exe process.

like image 102
foxx1337 Avatar answered Oct 21 '22 21:10

foxx1337