danday74
danday74

Reputation: 57116

Node.js permission denied public key

I'm trying to run a Git command via node over SSH but I keep getting the error:

Permission denied (publickey)

I guess this is because Node does not have access to my SSH keys since it is running in a child process?

How can I get past this?

Upvotes: 3

Views: 1135

Answers (2)

danday74
danday74

Reputation: 57116

I fixed this running a Bash script from node as follows:

"scripts": {
  "start": "npm-run-all -p server update",
  "server": "dyson rest 7070",
  "update": "sh update.sh"
}

update.sh looks like:

#!/usr/bin/env bash

set -e
set -o pipefail

SSH_KEY=/path/.ssh/id_rsa

function update {
  eval $(ssh-agent -s)
  ssh-add ${SSH_KEY}
  git submodule update --recursive --remote
}

update

The main thing is to start the ssh-agent in Node and then add your ssh key to the agent before running your git command.

UPDATE: The above works but I found a better answer. The reason it was failing is because inside the executing script the HOME environment variable was not pointing to the same HOME when outside the script. I fixed this by putting my ssh keys in both HOME folders. Then the script was able to find the right key and voilà.

PS You can determine the value of the HOME variable in Node by logging process.env.HOME or in a shell script with echo "${HOME}".

Upvotes: 1

Rahul
Rahul

Reputation: 952

On windows, if your credentials are correct then it should work. I guess you are running something like this -

require('child_process').spawn('git', ['push', 'origin', 'master']);

It works for me in both cases, ssh and https.

Upvotes: 1

Related Questions