Skip to content

Unable to pass the example with private key #431

@meetme2meat

Description

@meetme2meat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions