1

Edit: I'm changing the question to suit my current understanding of the problem which has changed significantly.

Original Title: Nodegit seems to be asking for wrong credentials on push

When trying to push using nothing seems to work on Windows (while they work fine on Linux).

  • Using SSH
    • sshKeyFromAgent - error authenticating: failed connecting agent
    • sshKeyNew - credentials callback is repeatedly (looks like an infinite loop but I can't be sure)
    • sshKeyMemoryNew: credentials is called twice and then node exits with no diagnostic (the exit and beforeExit events on process aren't signalled)
  • Using HTTPS
    • userpassPlaintextNew: [Error: unknown certificate check failure] errno: -17

Original question follows.


I'm trying to get nodegit to push and the following question seems to address this situation. However I'm not able to get it to work.

I've cloned a repository using SSH and when I try to push, my credentials callback is being called with user git and not motti (which is the actual git user).

try {
    const remote = await repository.getRemote("origin");
    await remote.push(["refs/head/master:refs/heads/master"], {
        callbacks: {
            credentials: (url, user) => {
                console.log(`Push asked for credentials for '${user}' on ${url}`);
                return git.Cred.sshKeyFromAgent(user);
            }
        }
    });
}
catch(err) {
    console.log("Error:", err);
}

I get the following output:

Push asked for credentials for 'git' on git@github.[redacted].net:motti/tmp.git
Error: { Error: error authenticating: failed connecting agent errno: -1, errorFunction: 'Remote.push' }

If I try to hardcode motti to the sshKeyFromAgent function the error changes to:

Error: { Error: username does not match previous request errno: -1, errorFunction: 'Remote.push' }

This my first time trying to programmatically use git so I may be missing something basic...

Answer for some questions from comments:

  • I'm running on windows 10
  • node v8.9.4
  • git version 2.15.0.windows.1
  • nodegit version 0.24.1
  • the user running node is my primary user which when I use for git in command line works correctly
Motti
  • 110,860
  • 49
  • 189
  • 262
  • What is the output of `git remote -v`. It should say motti@URL. If not, set it with `git remote set-url origin motti@URL` – EncryptedWatermelon Apr 29 '19 at 14:32
  • @EncryptedWatermelon, you're right, I updated the remote URL, now I'm getting asked for the correct username but I still get the same error. Also in my regular git repositories it says git@URL but when I commit and push from command line it uses the correct user, any ideas why? – Motti Apr 29 '19 at 14:45
  • `git@URL` is correct, change that back, that's nothing to do with your github user account. – steadweb Apr 29 '19 at 14:49
  • Are you running this locally? Does the user you're running as have a key that has access to the repo you're trying to push to – steadweb Apr 29 '19 at 14:57
  • @steadweb, yes and yes. – Motti Apr 29 '19 at 14:58
  • linux, osx, or windows? – steadweb Apr 29 '19 at 15:06
  • @steadweb windows – Motti Apr 29 '19 at 16:09
  • @Motti what's your dev setup? git-bash, WSL or native windows? Take a look at https://github.com/nodegit/nodegit/issues/1040 - I know sshagent on linux works fine, but I can't speak for Windows unfortunately. – steadweb Apr 29 '19 at 16:16

2 Answers2

1

Instead of using git.Cred.sshKeyFromAgent - you could use git.Cred.sshKeyNew and pass your username / keys along.

const fs = require('fs');

// ...

const username = "git";
const publickey = fs.readFileSync("PATH TO PUBLIC KEY").toString();
const privatekey = fs.readFileSync("PATH TO PRIVATE KEY").toString();
const passphrase = "YOUR PASSPHRASE IF THE KEY HAS ONE";
const cred = await Git.Cred.sshKeyMemoryNew(username, publickey, privatekey, passphrase);

const remote = await repository.getRemote("origin");
await remote.push(["refs/head/master:refs/heads/master"], {
    callbacks: {
        credentials: (url, user) => cred
    }
});

steadweb
  • 15,364
  • 3
  • 33
  • 47
  • Thanks for all your help, when I try what you suggested I get error "username does not match previous request", if I change the remote (as @EncryptedWatermelon suggested) I get: "Failed to authenticate SSH session: Unable to open public key file" so I changed the private and public key from the content of the files to the path to the files and then I get the `creadentials` callback called in what seems to be an infinite loop. – Motti Apr 30 '19 at 08:59
  • Try `git` as the username instead of your github username – steadweb Apr 30 '19 at 16:32
  • Is the path correct to your public / private key? (Re: Failed to authenticate SSH session: Unable to open public key file) – steadweb Apr 30 '19 at 16:33
  • Yes the path is correct (I verified the content of the file is read correctly in the initial version), I'll try the `git` option tomorrow (as you probably figured out we're in different time zones :)) – Motti Apr 30 '19 at 18:24
  • Sorry to inform that using the requested username (git) also calls `credentials` repeatedly. – Motti May 01 '19 at 07:42
  • I'll dig out the code for this, haven't forgotten @Motti :) - are you suggesting it's still in a loop and won't continue to auth? – steadweb May 02 '19 at 22:21
  • @Motti try `git.Cred.sshKeyMemoryNew` instead please – steadweb May 03 '19 at 09:42
  • Just tried it, when using contents of the public/private keys it gets stuck calling `credentials` repeatedly, when using the path I get an invalid public key data error (so I assume passing the contents is correct). Do you know how I can verify that I have my gpg keys set up correctly? – Motti May 05 '19 at 07:31
  • GPG is used to sign commit, not auth. If you use `sshKeyMemoryNew` it should be the contents of the keys, if you use `sshKeyNew` it should be the path of the keys. – steadweb May 07 '19 at 08:39
  • @Motti I forgot `sshKeyMemoryNew `is async, so you have to use `await` then pass it back. Can you try the edited code, apologise for this back and fourth. Happy to jump in a chat (if SO offers it) – steadweb May 07 '19 at 08:42
  • You have nothing to apologise for, thanks for putting in so much effort! I made the change and it's very strange... a couple of times I got a segmentation fault but now I get the `credentials` called (exactly) twice and then the process exits. The flow doesn't reach the code after the `push` nor does it reach the `catch` block of the `try` that wraps the `push` (I've never seen anything like that in node) – Motti May 07 '19 at 09:21
  • Neither the `exit` nor `beforeExit` events on the `process` object are being called :( – Motti May 07 '19 at 09:54
  • Unfortunately segfaults happen with this library (from experience) :( - very strange events aren't being called. I have the code that's in prod for one of my clients, and the only change now is using `const remote = await git.Remote.lookup(params.repo, 'origin');` instead of `const remote = await repository.getRemote("origin");` – steadweb May 07 '19 at 10:00
  • `git.Remote.lookup` behaves the same as `repository.getRemote` for me. I'll try on a different system, perhaps my dev machine has bit rot. – Motti May 07 '19 at 10:05
  • @Motti any updates? Did you try a different system? – steadweb May 08 '19 at 22:39
  • Nope, I'm on vacation, I'll update next week. Thanks for the interest – Motti May 09 '19 at 17:23
  • I setup an Ubuntu virtual machine and the findings are as follows: `sshKeyFromAgent` -> Works!, `sshKeyNew` -> infinite loop on credentials, `sshKeyMemoryNew` -> credentials called twice and then **double free or corruption (out) \n Aborted (core dumped)**. – Motti May 13 '19 at 08:15
  • 1
    The `sshKeyFromAgent` plays nicely with Linux. The other two functions should work though, assuming the values passed are correct. I have this workon on an OSX macbook. – steadweb May 13 '19 at 13:03
-1

You need to run an ssh agent locally and save your password there. Follow these steps to make it work:

  1. Enable the ssh agent locally (automatically runs on OS X): https://code.visualstudio.com/docs/remote/troubleshooting#_setting-up-the-ssh-agent
  2. Run 'ssh-add' in the same CLI as you're running your nodegit actions and enter your passphrase

I hope this helps because I also struggled a lot with it and it can be very frustrating.

patwis
  • 136
  • 1
  • 13