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

Handle cases when hub api operation needs additional OAuth scopes #2147

Closed
mastoj opened this issue May 27, 2019 · 7 comments · Fixed by #2446
Closed

Handle cases when hub api operation needs additional OAuth scopes #2147

mastoj opened this issue May 27, 2019 · 7 comments · Fixed by #2446
Labels

Comments

@mastoj
Copy link

mastoj commented May 27, 2019

I want to make it easier for our devs, data engineers and data scientist to work with gpg keys without know too much about them. One step in that would be to automate the upload of their gpg keys to github, or at least make a simple command they can run.

As of know I can't get it to work with hub.

Here's the command I want to run:

gpg --armor --export <email> | ./hub api user/gpg_keys -F "armored_public_key=@-"

I'm not sure why it doesn't work. I did download the repo and added some logging trying to figure out where it goes wrong, but failed to do so. The response I get is

{"message":"Not Found","documentation_url":"https://developer.github.com/v3/users/gpg_keys/#create-a-gpg-key"}

The actual request that hub generates looks the same as the one I created and tested with curl that works:

curl -v --request POST -H "Authorization: token $GH_TEST_TOKEN" https://api.github.com/user/gpg_keys --data '{"armored_public_key": "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n....-----END PGP PUBLIC KEY BLOCK-----"}'

Also asked on SO: https://stackoverflow.com/questions/56324233/how-to-post-a-message-to-the-github-api-using-hub-api

@mastoj
Copy link
Author

mastoj commented May 28, 2019

It seems to be something weird with authentication. If I modified the code and used a hard coded token like

req.Header.Set("Authorization", "token <token>")

in http.go file it worked. Not sure why it doesn't work with the token I got when logging in using hub.

@mastoj
Copy link
Author

mastoj commented May 28, 2019

I think I found the reason to why I can't upload gpg key. This line here: https://github.com/github/hub/blob/master/github/client.go#L889 is explicitly setting the scope to repo. What is the reasoning behind this? Doesn't it make sense that you get more access as the default from an automation perspective. Or at least that you can provide the scope as argument and a log in command.

@mislav
Copy link
Owner

mislav commented May 29, 2019

@mastoj Thanks for discovering this, and I'm sorry you had trouble. I guess your hub token would need the write:gpg_key for this to work. You can manually edit scopes for your "hub" token at https://github.com/settings/tokens, but I'm guessing you need a solution that lends itself better to automation.

Here are things we could improve around this:

  • When a hub api command has failed due to insufficient OAuth token scopes, we should tell the user that this happened, tell them which exact scope they need to enable, and present them with the URL to edit the scopes at.

    Challenges: As you might have noticed from the error message, GitHub API isn't really friendly in reporting token scope problems. There is nothing in the “Not Found” error to indicate that it was your token that lacked insufficient scopes. Other endpoints might return similarly vague error messages. I'm not sure how we can implement this from within hub.

  • Support requesting a hub token in the first place that has more scopes than just repo.

    Challenges: it would be great it there was a hub login, hub auth or a similar command to handle authentication in an interactive way that you could pass the additional scopes to via command-line parameters, but alas such a command doesn't exist yet, even though it was planned for a long time Dedicated command for authentication & configuration #225.

What are your ideas?

@mislav mislav changed the title Can't post to gpg_keys Handle cases when hub api operation needs additional OAuth scopes May 29, 2019
@mislav mislav added the feature label May 29, 2019
@mastoj
Copy link
Author

mastoj commented May 31, 2019 via email

@jamesstidard
Copy link

Hi,

I've just ran into this same problem, and had thought I was using the cli incorrectly before finding this ticket. The 404 is indeed a little misleading.

I'm also looking to automate setups of fresh machines and want to generate keys (ssh in my case) and upload them to the users GitHub account.

A login command (#225) would be good, though this would leave the hub oath token with these write keys permissions on the users machine which maybe is to privileged of a state. It would be nice to have the option for the login command to, instead of storing, hand back a auth token that can be stored in a temporary script variable to auth these types of high privileged commands, which can then be deleted at the end of the session. Leaving the system with no oath on the cli or the normal repos access rights.

@SuperSandro2000
Copy link

I also run into this problem when running hub fork for the first time and having already a token set via GITHUB_TOKEN env without the repo scope?

@kwhite
Copy link

kwhite commented Jan 18, 2020

+1 for this; Like @jamesstidard I am trying to automate provisioning new machines for my organization )(also with SSH keys) and would like to generate keys and upload them to the user's account.

I do like the idea of providing this as a temporary authorization so that hub isn't overprivileged on the machine for everyday operations. But less is more, and anything that gets me closer to being able to automate this process would be a win to my mind. :)

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

Successfully merging a pull request may close this issue.

5 participants