Search code examples
windowsgitvisual-studio-codegitlabmulti-factor-authentication

Solving VS Code PAC with a private Gitlab domain that inforces 2-FA


So I've more or less scoured the whole internet to try and solve this issue but I can't seem to get to the bottom of it and I may either be 1. the only one in existence to experience this exact problem or 2. the only one who just cannot simply solve it.

Background:

on my Windows 10 pc using VS Code I manage my work on a private Gitlab domain that enforces 2-FA. My usual workflow is authenticating though a ssh key, I have not been able to get the personal access token working despite my best efforts and this is what I'd like to solve.

I've added the personal access token (that has all permissions check within Gitlab) to VS code with my url to the Gitlab instance being https://gitlab.private.domain/. now when I try doing any action git action within VS code that requires authentication to the remote repository I get the following error

[email protected]: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.

I've tried many things and have nothing to show for it. All help is very appreciated. Additionally attempting to start a ssh-agent to preserve my passhrase does not work either for my regular workflow using the git commands in a terminal.

bonus question:

In a Golang project using go modules, running go get in a package that requires a version from other repos from the GitLab domain fails with a spectacularly useless fashion by looping with a git credential manager pop-up asking for a username and password for the private GitLab domain https://gitlab.private.domain, after putting my username and password the credential window presumably fails with a 401 and pops back up with the same prompt, running go get -v offers no additional insight


Solution

  • If the error message says git@your-gitlab-host it means that it's trying to use the "git" protocol to connect to the remote, which is just SSH but specifically for connecting to git remotes. It shouldn't be that if you're using HTTP (which for the last few years in Gitlab and Github requires a personal access token instead of your password).

    I'm not sure how to check the remote in VS Code, but from a command prompt you can do git remote show origin (or whatever the remote is called other than origin) from your project directory. If it looks like [email protected]:group/project.git that's the git ssh protocol so your personal access token will not work (or even be used). That's why you're getting the error about publickey, it's trying to use your ssh keys.

    To change your remote from git/ssh to https, go to your project's page in Gitlab and copy the HTTPS remote (on the page, it'll be the area that tells you how to clone the repo. For the Gitlab project itself (https://gitlab.com/gitlab-org/gitlab), it's the blue "Clone" button, then the url under "Clone with HTTPS". Then switch your remote:

    # remove the existing origin:
    git remote remove origin # or whatever the name is
    
    # add the new origin with HTTPS
    git remote add origin https://your-gitlab-host.com/group/project.git # or whatever you want the remote name to be
    

    Then your access token should work.