mislav / hub

A command-line tool that makes git easier to use with GitHub.
https://hub.github.com/
MIT License
22.83k stars 2.2k forks source link

With multiple github accouts configured with openssh hub does not recognize repository #2270

Closed Vlad-Herus closed 5 years ago

Vlad-Herus commented 5 years ago

I'm using openssh on windows to authenticate my git calls for two different github accounts. I've setup my openssh config according to https://gist.github.com/jexchan/2351996 . As a result my repository origin is git@github.com-sshUser:gitUser/repository.git instead of usual git@github.com:gitUser/repository.git.

With this change hub refuses to recognize my repository with following message : Aborted: could not find any git remote pointing to a GitHub repository.

When I switch my ssh config to have only one github user and switch my repository origin not to include sshUser - hub works fine.

mislav commented 5 years ago

Hi thank you for writing in! This might be related or identical to https://github.com/github/hub/issues/1934.

  1. What is your hub version?
  2. What is the contents of your ~/.ssh/config (at least the part that pertains to github.com hosts)?
  3. What is the hub command you've tried to run?
Vlad-Herus commented 5 years ago
  1. 2.12.3

Host github.com-devops HostName github.com User git IdentityFile ~/.ssh/github_devops

Host github.com-vlad HostName github.com User git IdentityFile ~/.ssh/github_vlad

  1. hub pr list
mislav commented 5 years ago

Thank you for additional info. I cannot reproduce this. I've changed my git remote url to start with: git@github.com-devops:

and I've added to ~/.ssh/config:

Host github.com-devops
HostName github.com
User git
IdentityFile ~/.ssh/github_devops

and after that hub pr list correctly recognizes the host as GitHub.com and displays the list of pull requests.

We also have a test that SSH hostname aliases are properly expanded: https://github.com/github/hub/blob/4aba5854179b8e381bebd08ec9b2d49a3613eb15/features/browse.feature#L200-L209

Can you try poke more in your configuration, or build hub from source so you can add debugging statements? Note that setting HUB_VERBOSE=1 enables verbose mode, altough in this case I don't think it would provide relevant extra information.

Vlad-Herus commented 5 years ago

Thanks for detailed reply, I switched my ssh configuration back and forth a couple of times - still able to consistently reproduce the issue. Can you : 1 make sure that your openssh config does not contain default github host, only aliased? 2 call git remote -v to make sure that only aliased git remote shows up?

I've looked through the source code of the hub and put in a couple of trace outputs to help diagnose the issue, you can find them in here. With those changes here's output of the hub : image

It compares github.com-vlad to the list of known hosts in hosts.go::knownGitHubHostsInclude and obviously fails. I tried to look for the code that addresses test case you listed but couldn't find it.

I've made a pull request with my understanding of how to address this issue.

I'm somewhat new to git, github, ssh, go. So all comments suggestions are appreciated.

mislav commented 5 years ago

Thanks for looking into this!

I still can't reproduce. It's possible that something is wrong with the SSHConfig parser. If you apply the following patch, hub will print the parsed hostname aliases from ~/.ssh/config when it processes the git remote URL configuration

diff --git a/git/url.go b/git/url.go
index f3f4adf..f19aa90 100644
--- a/git/url.go
+++ b/git/url.go
@@ -1,6 +1,7 @@
 package git

 import (
+       "fmt"
        "net/url"
        "regexp"
        "strings"
@@ -44,6 +45,7 @@ func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) {
                u.Host = u.Host[0:idx]
        }

+       fmt.Printf("%#v\n", p.SSHConfig)
        sshHost := p.SSHConfig[u.Host]
        // ignore replacing host that fixes for limited network
        // https://help.github.com/articles/using-ssh-over-the-https-port

it should show something like

$ hub pr list -L1                                                                                          [43/1473]
git.SSHConfig{"github.com-devops":"github.com"}
   #2272  fixes #2270 resolving host name with openssh alias

This demonstrates how, on my system, the github.com-devops host is correctly translated into github.com and the API of that host is then queried.

Vlad-Herus commented 5 years ago

I see, I was looking in github folder only.

with your change here's output of the pr list : image

I've added more traces here. It seems it tries to read .ssh\config(located in user home dir) when current directory is my repo folder.

here's output with my traces : image

And here's output after I copies .ssh folder into my git repo folder : image

Not sure what is the right way to fix this.

Vlad-Herus commented 5 years ago

whoops, just noticed that I attached same image twice. the middle one was supposed to show that it tries to find .ssh\config and is unable to and git.SSHConfig is empty (as the first image shows)

When you were reproducing the issue were you on windows or linux?

mislav commented 5 years ago

@VladimirUAZ Ah thanks to your debugging I see now that .ssh/config was looked up in the wrong location. Can you try https://github.com/github/hub/pull/2274 please?

Vlad-Herus commented 5 years ago

2274 works. Thanks!