Skip to content

Unable to retrieve username during multi-account migration due to invalid/expired token #8441

Open

Description

@jcasale

Describe the bug

I have some Windows machines that have an old version of %APPDATA%\GitHub CLI\config.yml and %APPDATA%\GitHub CLI\hosts.yml that breaks all invocations of the cli with the following error:

failed to migrate configuration: failed to migrate config: cowardly refusing to continue with multi account migration: couldn't get user name for "github.com": non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"

If I remove all files, the problem will be resolved.

If I remove the hosts.yml file, the config.yml file is changed by adding a line with version: "1", and the problem is resolved.

I can even restore the old hosts.yml once the config.yml has been updated and the cli will continue to run.

I am running version 2.40.0.

Steps to reproduce the behavior

  1. Type this gh config list (all commands I have tried fail, even help)
  2. View the output failed to migrate configuration: failed to migrate config... (see above)
  3. See error

Expected vs actual behavior

I am not clear what the migration code attempts to achieve, but it fails on some cases as described above.

Logs

See above.

Activity

  1. added
    bug
    on Dec 9, 2023
  2. added
    needs-triage
    on Dec 9, 2023
  3. williammartin commented on Dec 9, 2023

    @williammartin
    Member

    Hey @jcasale, sorry for the inconvenience.

    The migration exists to support multi account. We're failing the migration because we're unable to resolve a username for some token that we found because the server is returning 401. The idea here was that we wanted to err on the side of caution and have people take remediation rather than migrate data we didn't have confidence in. In retrospect, I'm not 100% sure this was the right choice here but we'll talk about it as a team on Monday.

    I can even restore the old hosts.yml once the config.yml has been updated and the cli will continue to run.

    It will continue to run but later if you log into another account on any host you will replace your current user rather than it being an additive operation the first time. After that things should work as expected. This may not be a big issue for you.

    To help us get confidence in this diagnosis, I would love to know the output of gh auth status.

  4. jcasale commented on Dec 9, 2023

    @jcasale
    Author

    Hi @williammartin no worries, the tool is brilliant ;)

    After removing the config files and executing gh auth login, the output of gh auth status is:

    github.com
      ✓ Logged in to github.com account jcasale (keyring)
      - Active account: true
      - Git operations protocol: https
      - Token: gho_************************************
      - Token scopes: 'gist', 'read:org', 'repo'
    

    If I misunderstood what you needed, please forgive me and straighten me out.

    I am not sure if it matters, but these hosts previously may have had gh auth login executed for troubleshooting purposes, but they all run with the GH_TOKEN environment variable set.

    The broken hosts.yml file is:

    github.com:
        oauth_token: ghp_************************************
    

    and the new hosts.yml file created above is:

    github.com:
        git_protocol: https
        users:
            jcasale:
        user: jcasale
    
  5. williammartin commented on Dec 9, 2023

    @williammartin
    Member

    Hi @williammartin no worries, the tool is brilliant ;)

    Thanks! We try our best not to break but in this case, the multi-account feature work was so large I'm not surprised we overlooked something.

    I'd love to see gh auth status for the old hosts.yml, I suspect it will say something like The github.com token in .config/gh/hosts.yml is invalid

  6. jcasale commented on Dec 9, 2023

    @jcasale
    Author

    The gh auth status command fails to run with the old files in place:

    failed to migrate configuration: failed to migrate config: cowardly refusing to continue with multi account migration: couldn't get user name for "github.com": non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"
    

    Just to be thorough in case I still misunderstood, I tried the following:

    1. Removed the config files.
    2. Executed gh auth login .
    3. Verified it with gh auth status .
    4. Restored the old config files.
    5. Executed gh auth status again.
    6. Same error as above.
  7. williammartin commented on Dec 9, 2023

    @williammartin
    Member

    Haha sorry it's late here and I wasn't clear at all. You would need to run gh auth status with a version earlier than v2.40.0 if you copy the old config.yml back in place. I think it should work with v2.40.0 if you only copy the hosts.yml back.

  8. jcasale commented on Dec 9, 2023

    @jcasale
    Author

    lol, well now that makes sense;)

    C:\>gh.exe --version
    gh version 2.39.2 (2023-11-27)
    https://github.com/cli/cli/releases/tag/v2.39.2
    
    C:\>gh.exe auth status
    github.com
      X github.com: authentication failed
      - The github.com token in C:\Users\***\AppData\Roaming\GitHub CLI\hosts.yml is invalid.
      - To re-authenticate, run: gh auth login -h github.com
      - To forget about this host, run: gh auth logout -h github.com
    

    Which is not surprising, the token is 15 months old.

  9. williammartin commented on Dec 9, 2023

    @williammartin
    Member

    Yep that all makes sense. So what I think happened is that at some point you logged in using gh auth login --with-token and we wrote an entry with just an oauth_token and no user field. For example, I just picked v2.10.0 since it was about 15 months old, we can see that happen here.

    Then in the migration code if there is no user entry in the config we try to fetch a username with the token. Obviously since the token is not valid we blow up.

    Like I said above, we cowardly fail here because we weren't sure about migrating data that we didn't have full confidence in but maybe that was the wrong choice here. Need to think about it some more on Monday.

    Thank you for your help triaging this, it was extremely useful!

  10. changed the title [-]Invalid `%APPDATA%\GitHub CLI\config.yml` and `%APPDATA%\GitHub CLI\hosts.yml` file breaks cli[/-] [+]Unable to retrieve username during multi-account migration due to invalid/expired token[/+] on Dec 11, 2023
  11. added
    priority-3
    and removed
    needs-triage
    on Dec 11, 2023
  12. Deleplace commented on Dec 27, 2023

    @Deleplace

    I'm getting this message for all gh commands, on Ubuntu linux:

    failed to migrate config: cowardly refusing to continue with multi account migration: couldn't migrate oauth token for "github.com": timeout while trying to set secret in keyring
    
  13. Deleplace commented on Dec 27, 2023

    @Deleplace

    It got better after rm ~/.config/gh/*

  14. kdere commented on Mar 8, 2024

    @kdere

    that also worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions