Skip to content

plumbing: transport/ssh allow different AuthMethod basd on user/host #1509

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

Open
wants to merge 1 commit into
base: previous-main
Choose a base branch
from

Conversation

tjamet
Copy link
Contributor

@tjamet tjamet commented Apr 12, 2025

When using the plain git with SSH, git reads the SSH config and can consider all the relevant SSH options depending on the hostname.

With go-git, it is impossible to use different configurations like IdentityFiles based on the hostname or other configurations as the information is not provided to the ssh auth config.

This commit provides such information while keeping the backward compatibility with older auth methods which do not.

I expect this will help fix #218 and #509

@Copilot Copilot AI review requested due to automatic review settings April 12, 2025 09:07
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

plumbing/transport/ssh/auth_method.go:41

  • [nitpick] The naming between AuthMethod and LegacyAuthMethod is very similar and may cause confusion. Consider using a more distinct name (e.g., LegacyToAuthAdapter) to clearly differentiate the legacy conversion adapter.
type AuthMethodFromLegacy struct {

plumbing/transport/ssh/auth_method.go:76

  • [nitpick] It appears there is no test covering the new ClientConfig behavior for KeyboardInteractive. Consider adding tests to verify that the user/host parameters are appropriately handled or documented as intentionally unused.
func (a *KeyboardInteractive) ClientConfig(string, string) (*ssh.ClientConfig, error) {

@tjamet
Copy link
Contributor Author

tjamet commented Apr 12, 2025

I have tested this change with the auth method which allowed me to consider both ssh agents and individual identity files for a given host.

import (
	// ssh-client-config is a WIP project to create a working ssh config
	// as configured in the various ssh config files
	sshclientconfig "github.com/tjamet/ssh-client-config"
)

type GitAuth struct {
}

func (a *GitAuth) SetAuth(r *http.Request) {
}

func (a *GitAuth) Name() string {
	return "GitAuth"
}

func (a *GitAuth) String() string {
	return "GitAuth"
}
func (a *GitAuth) ClientConfig(user string, host string) (*ssh.ClientConfig, error) {
	return sshclientconfig.NewSSHClientConfig("").SSHClientConfig(host, func(cc *ssh.ClientConfig) {
		// go git already implements the HostKeyCallback
		// so far, sshclientconfig does not at the moment
		cc.HostKeyCallback = nil
		cc.User = user
	})
}

When using the plain git with SSH, git reads the ssh config and is able to consider
all the relevant SSH options depending on the hostname.

With go-git, it is not possible to use different configurations like IdentityFiles
based on the hostname or other configurations as the information is not provided to the
ssh auth config.

This commit provides such an information while keeping the backward compatibility with
older auth methods which do not.
@tjamet tjamet force-pushed the feat/plumbing/ssh/config-with-user-host branch from bbe6fa2 to 8255f10 Compare April 12, 2025 09:22
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

Successfully merging this pull request may close these issues.

ssh private key not being picked up
1 participant