Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is flutter pub get not finding my ssh-key for github?

My flutter App depends on a module privately hosted on github.
When I run pub get from Powershell, i get:

Git error. Command: `git clone --mirror ssh://[email protected]:dirkbo/repo-name.git C:\src\flutter\.pub-cache\git\cache\repo-name-123ba`
stdout:
stderr: Cloning into bare repository 'C:\src\flutter\.pub-cache\git\cache\repo-name-123ba'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
exit code: 128

When I copy the failed command and run directly in the same powershell:

git clone --mirror [email protected]:dirkbo/repo-name.git C:\src\flutter\.pub-cache\git\cache\repo-name-123ba

Everything works fine:

Cloning into bare repository 'C:\src\flutter\.pub-cache\git\cache\repo-name-123ba'...
Enter passphrase for key '/c/Users/dirkb/.ssh/id_rsa':
remote: Enumerating objects: 229, done.
remote: Counting objects: 100% (229/229), done.
remote: Compressing objects: 100% (132/132), done.
remote: Total 229 (delta 13), reused 229 (delta 13), pack-reused 0
Receiving objects: 100% (229/229), 19.32 MiB | 1.45 MiB/s, done.
Resolving deltas: 100% (13/13), done.

In my pubspec.yaml I tried:

repo:
    git: ssh://[email protected]/dirkbo/repo-name.git

(result see above)

and

repo:
    git: [email protected]:dirkbo/repo-name.git

which gives me:

Git error. Command: `git fetch`
stdout:
stderr: [email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
exit code: 128

It seems like the batch command running pub get can't find my ssh key.

like image 528
dirkbo Avatar asked Sep 10 '20 09:09

dirkbo


People also ask

How do I find my SSH key in GitHub?

Login to github.com and bring up your account settings by clicking the tools icon. Select SSH Keys from the side menu, then click the Add SSH key button. Name your key something whatever you like, and paste the contents of your clipboard into the Key text box. Finally, hit Add key to save.


3 Answers

The problem was that Windows did't use the correct ssh programm and instead of openssh was using it's internal ssh program, which of course didn't know about my keys in openssh.

git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

So after configuring git to use OpenSSH, flutter pub get finds my keys, asks for a password and pulls the package correctly.

Thanks to https://github.com/desktop/desktop/issues/5641#issuecomment-421801704

like image 126
dirkbo Avatar answered Oct 17 '22 02:10

dirkbo


In my case, my repository is public & the package in the subfolder. I set the path by the subfolder name where package located.

dependencies:
  flutter:
    sdk: flutter

  flutter_pi:
    git:
      url: [email protected]:{username}/flutter-packages.git
      path: flutter_pi
      ref: master
like image 1
Prantik Avatar answered Oct 17 '22 03:10

Prantik


In my case, flutter pub get was running well, but flutter pub upgrade was erroring with

Permission denied (publickey)

The answer provided by @dirkbo solved only part of the problem for me, since there still was a conflict in my system between C:\Windows\System32\OpenSSH\ssh.exe from Windows and usr\bin\ssh.exe from Git Bash. Here I'm picking OpenSSH to handle all the operations:

  1. Running the PowerShell console as administrator, enable the OpenSSH service:
Set-Service ssh-agent -StartupType Manual
  1. Running a new command prompt, start the OpenSSH service:
ssh-agent
  1. Then check if the keys you want to use are listed here:
ssh-add -L
  1. If they aren't, add them using ssh-add, inserting their respective password when prompted:
ssh-add C:\Users\%YOUR_USER%\.ssh\id_github_home
ssh-add C:\Users\%YOUR_USER%\.ssh\id_github_work
  1. Tell Git to always use OpenSSH:
git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

Then I could get to run flutter pub upgrade normally!

I think this already solves the problem, but I've also added an environment variable named GIT_SSH:

set GIT_SSH=C:\Windows\System32\OpenSSH\ssh.exe

Note that every time start your computer, you need to run step 2 before running flutter pub upgrade.

like image 1
enzo Avatar answered Oct 17 '22 03:10

enzo