Skip to content

Allow configuring ssh host and optionally do account detection based on it #9740

Description

@titaniumtraveler

Describe the feature or problem you’d like to solve

I use multiple github accounts with isolated ssh-keys that are configured based
on the ssh-host.
My ssh config looks something like this:

Host gh1
    User git
    HostName github.com
    IdentityFile <path-to-the-ssh-key-gh1>

Host gh2
    User git
    HostName github.com
    IdentityFile <path-to-the-ssh-key-gh2>

Host gh3
    User git
    HostName github.com
    IdentityFile <path-to-the-ssh-key-gh3>

While gh works mostly as expected with this setup, when cloning or forking a
repo, the remotes use [email protected] as ssh-host, which means I then have to
rename those to work with my system.

Proposed solution

It would be great if gh supported this use case by adding a setting to
configure an ssh-host that is then being used for operations that
clone repos or add new remotes like gh repo clone or gh repo fork.
(This new setting should be per-account, else that would be defeating the
purpose.)

Furthermore that ssh-host could then be used to automatically select the correct
user, which would make using multiple accounts pretty much seamless.

Additional context

Prior Art

gitea's CLI tea has the option to configure an ssh-host and uses that both
when cloning a new repo and for detecting which repository it is being called
from. (I'm not sure if they do account detection based on that though.)

Alternatives

Of cause this issue could be solved by wrapping gh in a shellscript that as
pre-execution hook selects the right account and as post-execution hook fixes up
the remotes, but that would likely cause some subtle bugs for more complex
operations like gh pr create that both create a fork and then push to that
fork.
So first party support would definitely be preferable.

Activity

  1. andyfeller commented on Oct 15, 2024

    @andyfeller
    Contributor

    @titaniumtraveler : I want to follow up internally because I don't think exactly what gh supports in terms of SSH when working with repositories is well documented.

    Unfortunately gh is oriented around GitHub hosts, not around user accounts. And while we do have multi-account support added, I'm unsure of the ramifications of gh attempting to manage SSH configuration for users.

    Additionally, did you mean to use the same IdentityFile for each host or were they intended to be separate?

  2. added
    discussFeature changes that require discussion primarily among the GitHub CLI team
    gh-reporelating to the gh repo command
    gh-authrelating to the gh auth command
    on Oct 15, 2024
  3. titaniumtraveler commented on Oct 18, 2024

    @titaniumtraveler
    Author

    Unfortunately gh is oriented around GitHub hosts, not around user accounts. And while we do have multi-account support added, I'm unsure of the ramifications of gh attempting to manage SSH configuration for users.

    That is sad, but I guess I should have guessed that based on the structure of
    the hosts.yml file.

    @titaniumtraveler : I want to follow up internally because I don't think exactly what gh supports in terms of SSH when working with repositories is well documented.

    * [`pkg/ssh/ssh.go` in `cli/go-gh`](https://github.com/cli/go-gh/blob/trunk/pkg/ssh/ssh.go) was designed to handle translating aliases like the ones above into hostnames
    
    * [`pkg/repository/repository.go` in `cli/go-gh`](https://github.com/cli/go-gh/blob/71770357e0cb12867d3e3e288854c0aa09d440b7/pkg/repository/repository.go#L117-L158) is used to translate local repository fetch and push URLs
    
    * [`context/remote.go` in `cli/cli`](https://github.com/cli/cli/blob/c657cfac370ef33696cd19aaca187abbf6b3a9f9/context/remote.go#L105-L123) similarly handles translating local git remote URLs
    

    These links are definitely helpful!
    I kind of was planning to look how the multi-account-support is implemented
    anyway, so having an entry point makes that easier!

    I am not too versed in go, but I guess having the opportunity to learn
    something is never a bad thing.

    Additionally, did you mean to use the same IdentityFile for each host or were they intended to be separate?

    No, that was not my intention. Thank you for pointing it out.
    Fixed it. 😄

  4. kako654 commented on Nov 14, 2024

    @kako654
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

    discussFeature changes that require discussion primarily among the GitHub CLI teamenhancementa request to improve CLIgh-authrelating to the gh auth commandgh-reporelating to the gh repo commandneeds-triageneeds to be reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions