mislav / hub

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

Use hub pull-request with SSH #978

Open tybenz opened 8 years ago

tybenz commented 8 years ago

I am trying to use hub with GitHub Enterprise. My company's enterprise account is set up to only use SSH keys when pulling/pushing. I think they've disabled basic auth altogether. When using hub pull-request, hub prompts me for my username/password for GitHub Enterprise, which I do not have. Our web portal requires a username + 2FA token. Tried to use that to no avail.

Is there a way for hub to use the GitHub API and use an SSH key to authenticate itself?

Or am I out of luck?

nightpool commented 8 years ago

You can't auth to the API over an ssh key, but you can get a oauth token and put that into hub manually.

nightpool commented 8 years ago

see https://help.github.com/articles/creating-an-access-token-for-command-line-use/

mislav commented 8 years ago

Hub accesses GitHub API via HTTPS, so it needs some kind of authentication such as Basic Auth or OAuth. SSH keys won't help because they're only used by git when pushing/pulling.

Because you don't have a password you can authenticate with, only 2FA token, that means you will probably need to generate a Personal access token manually. You can do that from your settings in the web interface. Then, add that token to ~/.config/hub like so:

---
myenterprise.com:
- protocol: https
  user: tybenz
  oauth_token: YOURTOKEN
rayrrr commented 7 years ago

This is a very old Issue thread with incredible search engine placement. I searched just for "hub ssh," ended up here, tried this and it didn't work. Looking at the docs here it seems the proper way to do this nowadays is to simply set an environment variable with the name GITHUB_TOKEN containing the value of your GitHub Personal Access Token. That worked for me!

youcandanch commented 6 years ago

whoa @rayrrr what are you doing here

miohtama commented 6 years ago

I have two-factor authentication enabled. hub ends up asking password even if I put manually generated token to ~/.hub/config or GITHUB_TOKEN environment variable.

mislav commented 6 years ago
  1. When ~/.config/hub (mind the actual file name) already contains the user and oauth_token keys (perhaps you've pasted in a manually generated token; that's ok), you will never be prompted for password. Hub will try to use that oauth token and fail with an error message if the token is invalid.

  2. You can set HUB_VERBOSE=1 to have hub output extra debugging information.

levithomason commented 6 years ago

Can this get added to the docs or README?

holdenk commented 6 years ago

It also checks for an env variable called GITHUB_TOKEN which was surprising for me since all of the docs pointed to deleting the config value -- just incase anyone else gets stuck on this like I do.

chrowe commented 5 years ago

The documentations on https://hub.github.com/hub.1.html seems to still indicate that git is the default with git config --global hub.protocol https being an override. Unfortunately git and ssh options do not seem to work.

In the short term, using the full git@github.com/... url works for some things

christopher-francisco commented 5 years ago

Can we get some more explicit documentation? Specifically on these topics

  1. I intend to use only SSH key for Git operations, NO OAuth2. Do I still need to set GITHUB_TOKEN? If yes, a brief explanation would help people understand
  2. What's the difference of git config --global hub.protocol vs HUB_PROTOCOL? In what use cases I would pick one over the other?
  3. Is this required git config --global --add hub.host MY.GIT.ORG? I'm pretty sure I set mine up without this line, but only the GITHUB_HOST and it worked
mislav commented 5 years ago

Until there is better documentation:

  1. I intend to use only SSH key for Git operations, NO OAuth2. Do I still need to set GITHUB_TOKEN?

Yes, because hub needs access to the GitHub API, which can only be accessed via HTTP(S) Oauth2 and not SSH.

2. What's the difference of git config --global hub.protocol vs HUB_PROTOCOL?

There is no difference. Use the one you're more comforable with.

3. Is this required git config --global --add hub.host MY.GIT.ORG?

It's required in case GITHUB_HOST isn't set.

mislav commented 5 years ago

Reopening until there are better docs.

More about the SSH vs. HTTPS communication during hub operations: https://github.com/github/hub/issues/1644#issuecomment-359002547

azec-pdx commented 4 years ago

In my ~/.zshrc, I have added:

GITHUB_TOKEN=<***REDACTED***>

and inside ~/.config/hub , I have only:

---
github.com
    - protocol: https
      user: azec-pdx
      oauth_token: <***REDACTED***>

If I try to do hub pr list in local dir of my repo, I am still asked for username and password. What am I missing ?

azec-pdx commented 4 years ago

I added also

export HUB_CONFIG=~/.config/hub

to my ~/.zshrc and started new session and it is still asking for uname/password when I run git pr list

azec-pdx commented 4 years ago

Finally this worked for me....

I am sure that there is some minimal version of this config that I could just keep , but there seems to be biggest confusion caused by hub.protocol parameter for which any of https|ssh|git are valid options.

I believe that hub.protocol setting is used to select flavor of authentication against regular git operations, and I prefer SSH for this, because as I said, all my repos are checked out with SSH. But the counter-intuitive part is that if you have git configured to work with your GitHub via SSH (before introducing hub in your workflow) , then the right setting is hub.protocol=git and not hub.protocol=ssh or hub.protocol.git . I absolutely don't want https even though it is recommended by GitHub, because it forces me to store username/pass in MacOS keychain in the org laptop and in the case of leaving org, it would be much easier to revoke SSH key as well as OAuth token from my GitHub profile.

The second part of the auth is GITHUB_TOKEN that is a must-have if you want hub to talk to GitHub APIs , that are not git - protocol operations.

mislav commented 4 years ago

I'm confused: protocol: git is literally unsupported in ~/.config/hub. The only possible values are https and http because these are the only protocols that GitHub API is accessible over. (Note that this configuration option is NOT for git clone/push/pull protocol!)

When it comes to git config, why did hub.protocol=ssh not work out for you? What were the particular problems you've encountered with that setting?

jtakalai commented 4 years ago

For me, just the ~/.config/hub file was enough (using https and access token).