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

Unable to pass the example with private key #431

Closed
meetme2meat opened this issue Dec 21, 2021 · 3 comments
Closed

Unable to pass the example with private key #431

meetme2meat opened this issue Dec 21, 2021 · 3 comments

Comments

@meetme2meat
Copy link

Following is my example file

package main

import (
	"context"
	"fmt"

	git "github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/transport"

	// "github.com/go-git/go-git/v5/plumbing/transport/client"
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)

func main() {

	// cfg := cssh.ClientConfig{HostKeyCallback: cssh.InsecureIgnoreHostKey()}
	// c := ssh.NewClient(&cfg)
	// client.InstallProtocol("ssh", c)
	fmt.Println(cloneRepo(context.Background()))
}

func cloneRepo(ctx context.Context) error {
	url := "github.com:ringsq/access-policies.git"
	auth, err := auth("./keys/id_rsa", "")
	if err != nil {
		return fmt.Errorf("failed to create git auth credentials: %w", err)
	}

	fmt.Println(auth.String())
	opts := &git.CloneOptions{
		URL:           url,
		Auth:          auth,
		ReferenceName: plumbing.NewBranchReferenceName("master"),
		SingleBranch:  true,
	}

	fmt.Printf("Cloning git repo from %s", "")

	if _, err := git.PlainCloneContext(ctx, "./", false, opts); err != nil {
		return fmt.Errorf("failed to clone from %s to %s: %w", url, "master", err)
	}

	return nil
}

func auth(privateKey string, password string) (transport.AuthMethod, error) {
	return ssh.NewPublicKeysFromFile("git", privateKey, password)
}

Every time I run this I get the following error

failed to clone from github.com:ringsq/access-policies.git to master: ssh: handshake failed: known hosts: the key is unknown

But I'm able to git-clone using the git utility. once the git utility (or using ssh -T git@github.com) writes the known_hosts entry the script above seems to work. Well, it seems workable solution on a local machine but doing this on production.

@meetme2meat
Copy link
Author

I'm able to pass it by setting the HostKeyCallbackHelper for *PublicKeys

func auth(privateKey string, password string) (transport.AuthMethod, error) {
       	pub, err := ssh.NewPublicKeysFromFile("git", privateKey, password)
	if err == nil {
		pub.HostKeyCallbackHelper = ssh.HostKeyCallbackHelper{
			HostKeyCallback: cssh.InsecureIgnoreHostKey(),
		}
	}
        return pub, error

@charithe
Copy link

I believe the root cause for this issue is #411.

@go101
Copy link

go101 commented Jan 7, 2023

It looks the PublicKeys.HostKeyCallbackHelper field is removed since v5.5.0, which broke modules compatibility.
On the other hand, it looks this field is not needed any more since v5.5.0, which makes user code become cleaner.

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

No branches or pull requests

3 participants