SSH auth: Add custom username support (#536)

* Add support for custom username when using SSH auth
Previously, the user "git" was hard-coded
This commit also adds support for ssh:// URLs

* Add comment to explain username parsing
This commit is contained in:
Tomy Guichard authored and GitHub committed 2021-05-05 12:30:12 -04:00
1 parent 9ab424172d
commit 25c3e92c2a
1 file changed
+17 -5
+17 -5
View File
@@ -144,8 +144,8 @@ func (opts Options) CloneOptions() (*git.CloneOptions, error) {
var auth transport.AuthMethod var auth transport.AuthMethod
if strings.HasPrefix(opts.RepoURL, "git") { if strings.HasPrefix(opts.RepoURL, "ssh://") || (!strings.Contains(opts.RepoURL, "://") && strings.Contains(opts.RepoURL, ":")) {
// using git protocol so needs ssh auth // using ssh:// url or scp-like syntax
auth, err = SSHAuth(opts) auth, err = SSHAuth(opts)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -176,17 +176,29 @@ func (opts Options) CloneOptions() (*git.CloneOptions, error) {
// SSHAuth tried to generate ssh public keys based on what was passed via cli. If no // SSHAuth tried to generate ssh public keys based on what was passed via cli. If no
// path was passed via cli then this will attempt to retrieve keys from the default // path was passed via cli then this will attempt to retrieve keys from the default
// location for ssh keys, $HOME/.ssh/id_rsa. This function is only called if the // location for ssh keys, $HOME/.ssh/id_rsa. This function is only called if the
// repo url using the git:// protocol. // repo url using the ssh:// protocol or scp-like syntax.
func SSHAuth(opts Options) (*ssh.PublicKeys, error) { func SSHAuth(opts Options) (*ssh.PublicKeys, error) {
params := strings.Split(opts.RepoURL, "@")
if len(params) != 2 {
return nil, fmt.Errorf("user must be specified in the URL")
}
// the part of the RepoURL before the "@" (params[0]) can be something like:
// - "ssh://user" if RepoURL is an ssh:// URL
// - "user" if RepoURL uses scp-like syntax
// we must strip the protocol if it is present so that we only have "user"
username := strings.Replace(params[0], "ssh://", "", 1)
if opts.SSH != "" { if opts.SSH != "" {
return ssh.NewPublicKeysFromFile("git", opts.SSH, "") return ssh.NewPublicKeysFromFile(username, opts.SSH, "")
} }
c, err := user.Current() c, err := user.Current()
if err != nil { if err != nil {
return nil, err return nil, err
} }
defaultPath := fmt.Sprintf("%s/.ssh/id_rsa", c.HomeDir) defaultPath := fmt.Sprintf("%s/.ssh/id_rsa", c.HomeDir)
return ssh.NewPublicKeysFromFile("git", defaultPath, "") return ssh.NewPublicKeysFromFile(username, defaultPath, "")
} }
// OpenLocal checks what options are set, if no remote targets are set // OpenLocal checks what options are set, if no remote targets are set