Set up git authentication when logging in to gh#2449
Conversation
|
This looks great so far! Just flagging this for later: |
|
I've updated this branch to solve what to me was a pretty hard problem: what do we do if the user selects "HTTPS" authentication, but does not have any The credential helper is a program that stores usernames & passwords for git operations over the HTTPS protocol. Git allows this to be configurable because people have different preferences for how they want to securely store authentication info. The default credential helper on macOS is We cannot really configure or recommend any single 3rd-party program as a credential helper because we do not know what program would be available and reliable for their platform. Git itself ships with two built-in credential helpers:
However, what if gh itself was a credential helper? The git credential protocol is not complex, and gh already has access to an authentication token that can be used for cloning. In the absence of a credential helper, gh now configures this in the user's The new (hidden) @ampinsk Yes, let's think about alternatives! We do have to ask something, because this operation should never initiate unprompted. The gist of what we need to ask is, even though most |
vilmibm
left a comment
There was a problem hiding this comment.
Thrilled with this! I am a fan of the credential store hack 👍
pkg/cmd/auth/login/login.go
Outdated
| if !isOurCredentialHelper(helper) { | ||
| var primeCredentials bool | ||
| err = prompt.SurveyAskOne(&survey.Confirm{ | ||
| Message: "Set up git for passwordless push/pull operations?", |
There was a problem hiding this comment.
Was thinking something similar to @ampinsk - curious how something like this feels.
| Message: "Set up git for passwordless push/pull operations?", | |
| Message: "Also set up Git authentication for GitHub CLI operations that leverage Git?", |
There was a problem hiding this comment.
@billygriffin The idea is that more git operations than just ones initiated by gh are affected. For example, after doing this step, you can safely git clone <url> (bypassing GitHub CLI) and having the credentials that you've established during gh auth login be automatically used without your intervention.
Alternatively, we could ensure that only gh-initiated git operations (e.g. git clone within gh repo clone, git push within gh pr create) are properly authenticated, but that regular git operations that you initiate outside of gh are left intact (and possibly un-authenticated). This approach would be simpler to achieve, but I think it would be worse for the user experience, because GitHub CLI often relies on regular git operations to be combined with it during everyday workflows.
Something that I've noticed in both hub and GitHub CLI: after users log into GitHub from the command line, they expect to be logged into GitHub for all their GitHub-related operations. To meet their expectations, I think that in this case it's worth leaving the boundaries of our own tool and making sure things work for them even when they were not explicitly using our tool.
There was a problem hiding this comment.
How about Authenticate Git with your GitHub credentials?
Since GitHub CLI now offers to authenticate your Git as well, the token we request here will be used for git pushes. Since we do anticipate our users making edits to their GitHub Actions workflow files, we want them to be able to push their changes, and this scope allows that.
samcoe
left a comment
There was a problem hiding this comment.
This looks great to me! I left a couple comments/questions, but nothing blocking.
| cmd := &cobra.Command{ | ||
| Use: "git-credential", | ||
| Args: cobra.ExactArgs(1), | ||
| Short: "Implements git credential helper protocol", |
There was a problem hiding this comment.
Are we worried at all about this protocol updating and us having to maintain/upkeep this implementation?
There was a problem hiding this comment.
The oldest reference to this specific format of the git credential protocol that I could find is from 2012, and it hasn't changed since. It's also very unlikely that git will ever change it—at least not in a backwards-incompatible way—because it's in git's strong interest to avoid breaking current programs that have implemented the protocol.
| func helperRun(opts *CredentialOptions) error { | ||
| if opts.Operation == "store" { | ||
| // We pretend to implement the "store" operation, but do nothing since we already have a cached token. | ||
| return cmdutil.SilentError |
There was a problem hiding this comment.
Rather than a silent error here is it possible to send back a response saying that the user should use gh auth login/refresh to do this operation? This seems like it would only be called if a user is explicitly trying set credentials from the git command, right?
There was a problem hiding this comment.
For a user, that would make sense! But the user doesn't ever interact with this helper directly. Instead, git invokes it internally when git credential is interacted with, and the user typically doesn't even interact with git credential themselves. So I don't think a more helpful error would be useful here.
Furthermore, when multiple credential helpers are configured, git currently uses them in a sort of a chained sequence, taking credentials from one and storing them into all others. This is why I felt it was important to implement store, but also to discard any attempt to actually store credentials, since those unrelated to GitHub we don't want to store, and those for GitHub we already have.
This sets up git HTTPS credentials after successfully logging in to GitHub CLI. As a proof of concept, observe the last step in the flow:
On a fresh system, the user is now able to clone/push/pull from GitHub.com with no extra intervention, provided that they have a git credential helper defined (likely already the case on macOS and Windows).
Fixes #1434
TODO:
If the user selected SSH protocol, offer to upload their public keys- followupEnable non-interactive credential priming via flag- decided againstauth refreshup to parity with credential handling inauth loginInclude checking git credential status in- cannot safely do. Since we do not know which credential helper the user has configured, usingauth statusgit credential approveto read their stored credential might cause their credential helper to interactively prompt the user for credentials, and that's something we cannot allow duringauth status.