Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal: Could not read Username for "https://github.com", No such device or address #1644

Closed
simonepri opened this issue Jan 10, 2018 · 9 comments

Comments

@simonepri
Copy link

Hello! :octocat:

Today I was doing the setup of a git automation inside a Docker container using hub and I ran into something that seems a bug.

Authentication method

Since the problem is related to an authentication problem let me give you some context before to talk about the error I have encountered.
In order to perform all the operations, I was using a personal token as mentioned in hub's man page

The part of the script that was configuring hub was looking like this:

hub config --global hub.protocol https
hub config --global user.email $email
hub config --global user.name $username
export GITHUB_TOKEN=$token

The problem

My automation was trying to perform the following operations on a public repository:

  • clone the repository ✅
  • fork the repository ✅
  • add some changes to the fork ✅
  • push the changes to the fork ❌
  • open a pull request on the original repository ❌

The problem was that the push and the pull-request commands both gave me this (meaningless) error:
fatal: could not read Username for "https://github.com", No such device or address

After a long struggling, I've then found a solution that seems a bit wired to me.

In order to be able to push on my fork I was needed to change this:

hub remote set-url upstream https://github.com/$owner/$repo.git
hub remote set-url origin https://github.com/$username/$repo.git

to this:

hub remote set-url upstream https://$token:x-oauth-basic@github.com/$owner/$repo.git
hub remote set-url origin https://$token:x-oauth-basic@github.com/$username/$repo.git

Why the $GITHUB_TOKEN environmental variable isn't enough in this situation?
The $GITHUB_TOKEN env var enables me to create a new repo (forking) on my account but does not give me the possibility to push on the same? Am I missing something?

Info to reproduce the error

Below you can find the "complete" script that lets you reproduce the error:

First (non-working) version of my script (click on Details):

# You must set these variables: $email, $username, $token, $repo, $owner 

hub config --global hub.protocol https
hub config --global user.email $email
hub config --global user.name $username
export GITHUB_TOKEN=$token

# Clones the repo
rm -rf ./$repo/
hub clone $owner/$repo
cd ./$repo/

# Fork the repo
hub fork

# Update remotes
hub remote rename origin upstream
hub remote rename $username origin
hub remote -v
hub pull --all

# Add something to the repo
echo "🌈" > 42
git add 42

# Push on our fork
hub push origin -f
# ^-- The push throws the error

Second (working) version of my script (click on Details):

# You must set these variables: $email, $username, $token, $repo, $owner 

hub config --global hub.protocol https
hub config --global user.email $email
hub config --global user.name $username
export GITHUB_TOKEN=$token

# Clones the repo
rm -rf ./$repo/
hub clone $owner/$repo
cd ./$repo/

# Fork the repo
hub fork

# Update remotes
hub remote rename origin upstream
hub remote rename $username origin
hub remote set-url upstream https://$token:x-oauth-basic@github.com/$owner/$repo.git
hub remote set-url origin https://$token:x-oauth-basic@github.com/$username/$repo.git
hub remote -v
hub pull --all

# Add something to the repo
echo "🌈" > 42
git add 42

# Push on our fork
hub push origin -f

Example of Dockerfile to run the script (click on Details):

FROM alpine:latest

RUN echo http://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
RUN apk update

RUN apk add git hub bash

# Copy the script
COPY script.sh .

RUN bash pr.sh

If interested you can find the whole script here.

Thank you!

@mislav
Copy link
Owner

mislav commented Jan 19, 2018

Hi thank you for the incredibly detailed report! ✨ ✨

This is something that is often a cause of confusion for hub users: when doing operations such as hub clone or hub fork … hub push, there are two authentication methods to GitHub in play, and they are in no way related! One may be fine, but the other one can be misconfigured (or vice versa).

  1. The first authentication method is the one git uses for pushing git commits to repos or cloning/fetching from private repos, and is in no way related or affected by hub. There are basically two authentication strategies for git operations:

  2. The second authentication method is the one hub uses to access the GitHub API. This is completely separate from git protocol authentication described above, for these reasons:

    • Even if there's an SSH key configured to be used for github.com, it can't be used by hub beause GitHub API can only be accessed over HTTPS.
    • Even if there's a password or Personal Access Token configured in git credential helper for use with HTTPS git remotes, it can't be used by hub in most cases due to the following:
      • Most of the GitHub API cannot be accessed with your actual GitHub password and instead must be accessed with an Oauth token (also known as Personal Access Token);
      • The Personal Access Token configured for cloning/pushing to git repos can't be used for all of the GitHub API operations since they might require permissions outside of the default repo scope that the tokens for git cloning are usually created with.

So you see, when you do hub clone owner/project, both authentication methods kick in: first hub checks the GitHub API, then git uses its own authentication method to fetch the repo. When hub users get an authentication error, they are often confused as to why they are getting it, because they have either sorted out git authentication or hub authentication, but they don't realize they need to configure both.

Ideas how to improve the user-friendliness of this is very welcome! Also see plans for #225.

@simonepri
Copy link
Author

Hi @mislav,
Thank you for the reply!

I think there are a lot of things to improve the user-friendliness, but maybe this issue is not the best place to discuss about.

My impression, is that using hub to achieve some simple operation seems just over-complicated.
For example, I would expect something like this to work:

hub auth $GITHUB_TOKEN      <--- This should take care of all the stuffs related to the auth
hub fork $owner/$repo       <--- This should clone, fork and set remotes correctly
# some edits
hub push       <--- Should work without any type of configs if the token have enough rights.

Anyway let's talk about the auth error:
git should not be transparent while using hub. I mean. Hub should predict misconfigurations instead of letting git fail in strange ways.
I believe that hub should provide the same easy-to-use experience of the GitHub website inside the terminal.

Can you show me what is correct way to setup both hub and git authentications using the Personal Access Token ($GITHUB_TOKEN)?
I did not found any good example.

@mislav
Copy link
Owner

mislav commented Jan 22, 2018

The correct way to auth with both git and hub:

  1. For git HTTPS auth, you want to configure the credentials helper (this is platform-specific) to store the username/token combo. This is an example how to do it with osxkeychain on Mac OS

  2. For hub, doing export GITHUB_TOKEN=$token and configuring hub to always use HTTPS is enough: git config --global hub.protocol https.

@simonepri
Copy link
Author

simonepri commented Jan 22, 2018

What about adding those information somewhere inside the hub's documentation?

Also I believe that the community would appreciate if we add some examples on how to perform some basic operations (authentication, create a fork, open a pull-request, etc) with hub.

In case I would be happy to help.

@mislav
Copy link
Owner

mislav commented Jan 23, 2018

Adding extra documentation about this would be great, but actually implementing something like hub auth $GITHUB_TOKEN that you've proposed would be even better. See #225 for plans to make a command like that. In the future, hub should have a dedicated auth command, switch to HTTPS protocol by default, and help you set up the git credential helper in case you haven't already.

Any help around implementing this would be welcome!

@mislav
Copy link
Owner

mislav commented May 29, 2018

Closing this in favor of the referenced issue. Authentication should be easier and more friendly once a dedicated command for that is written.

@mislav mislav closed this as completed May 29, 2018
JLLeitschuh added a commit to JLLeitschuh/hub that referenced this issue Feb 6, 2020
There's a lot of scattered information about how to properly configure hub to be used on GitHub actions.
This attempts to condense this to a simplified bit of information captured in the README.

See:
 - mislav#1644
 - mislav#225
@dimyme
Copy link

dimyme commented Apr 23, 2020

This is something that is often a cause of confusion for hub users:

🏖️ very true words indeed! 🥇

@nickboldt
Copy link

nickboldt commented Apr 29, 2020

Working in a jenkinsfile, the only thing that worked to be able to push changes back to the origin was this bit of groovy & embedded shell script:

SOURCE_REPO="eclipse/che"
GITHUB_TOKEN="mytokenhere"

sh '''
  export GITHUB_TOKEN=''' + GITHUB_TOKEN + ''' # echo "''' + GITHUB_TOKEN + '''"
  git config --global hub.protocol https
  git remote set-url origin https://\$GITHUB_TOKEN:x-oauth-basic@github.com/''' + SOURCE_REPO + '''.git
  git commit ...
  git push
'''

Thanks for the hint!

karczex pushed a commit to karczex/pmemkv that referenced this issue Aug 25, 2020
Add GITHUB_TOKEN to GHA environment

 * GITHUB_TOKEN variable is needed by run_doc_update.sh, but GHA provides
	variable secrets.GITHUB_TOKEN
 * update build.sh to pass doc_update_github_token
 * Setup GITHUB_TOKEN env variable in run-doc-update
 * use https for hub app
 * apply x-oauth workaroud: mislav/hub#1644
lluiscampos added a commit to lluiscampos/integration that referenced this issue Dec 16, 2022
This commit partially reverts 7bb7fc0. It uses a different token because
the previous one only had repo:status permissions.

It took me a while to realize that we actually need both the token and
the SSH key. See this comment:

See here:
* mislav/hub#1644 (comment)

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>
lluiscampos added a commit to lluiscampos/integration that referenced this issue Dec 16, 2022
This commit partially reverts 7bb7fc0. It uses a different token because
the previous one only had repo:status permissions.

It took me a while to realize that we actually need both the token and
the SSH key. See this comment:
* mislav/hub#1644 (comment)

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>
lluiscampos added a commit to lluiscampos/integration that referenced this issue Dec 19, 2022
This commit partially reverts 7bb7fc0. It uses a different token because
the previous one only had repo:status permissions.

It took me a while to realize that we actually need both the token and
the SSH key. See this comment:
* mislav/hub#1644 (comment)

Additionally, fix the hub command: a pull request needs to be launched
from a different fork or branch.

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>
lluiscampos added a commit to lluiscampos/integration that referenced this issue Dec 19, 2022
This commit partially reverts 7bb7fc0. It uses a different token because
the previous one only had repo:status permissions.

It took me a while to realize that we actually need both the token and
the SSH key. See this comment:
* mislav/hub#1644 (comment)

Additionally, fix the hub command: a pull request needs to be launched
from a different fork or branch.

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>
@lucasgherculano
Copy link

lucasgherculano commented Apr 21, 2023

This worked:

    - name: Configure Git 
      run: |
        echo "machine github.com login $username password ${GH_TOKEN}" > ~/.netrc
        git config --global url."https://github.com/".insteadOf "git://github.com/"
        git config --global advice.detachedHead false
      env:
        GH_TOKEN: ${{ secrets.GH_TOKEN }}

change $username to your username

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants