1

I tried using Nodegit in the same method, but I got an error message saying "Clone.clone, stack: Error: Method clone has caused an error." or "Github authentitation failed."

I have tried it but it gives me below error message

Error: Method clone has thrown an error. {errno: -1, errorFunction: 'Clone.clone', stack: 'Error: Method clone has thrown an error.', message: 'Method clone has thrown an error.'}

class GitClient {
    constructor(realname, email, token, username, repoName, branch, local) {
        this.config = {
            branch,
            remote: "SSH URL",
            local,
            username,
            realname,
            email,
            token
        };
        this.cloneOpts = {
            callbacks: {
                certificateCheck: () => { return 0; },
                credentials: (url, username) => {
                    return NodeGit.Cred.sshKeyNew(
                        username,
                        path.join(this.config.local, '.ssh/id_rsa.pub'),
                        path.join(this.config.local, '.ssh/id_rsa'),
                        ''
                    );
                    // return NodeGit.Cred.sshKeyFromAgent(username);
                }
            }
        };
        this.cloneOpts.fetchOpts = { callbacks: this.cloneOpts.callbacks };
    }

    async clone(options) {
        this.cloneOpts.checkoutBranch = options.branch;
        return NodeGit.Clone(options.remote, options.local, this.cloneOpts).then((data) => {
            console.log(data)
            return data;
        }).catch(err => {
            console.log(err);
        });
    }
}
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 27 '22 at 21:27

1 Answers1

1

It depends on your code, and on the type of SSH keys used.

If you are using OpenSSH one, as shown in nodegit/nodegit issue 1594, then it should work with:

const Git = require('nodegit');

const cloneURL = "git@URL:PATH_TO_GIT.git";
var local_publickey = local("/ssh/ssh-public-manual.pub");
var local_privatekey = local("/ssh/openssh-private-key");
var tmpGitPath = "./tmp";

var opts = {
    fetchOpts: {
        callbacks: {
            certificateCheck: () => 0,
            credentials: function(url, userName) {
                return Git.Cred.sshKeyNew(
                    userName,
                    local_publickey,
                    local_privatekey,
                    "SECRET_OF_PRIVATE_KEY"
                );
            }
        }
    }
};

Git.Clone.clone(cloneURL,tmpGitPath,opts)
    .then(function(repo) {
        if(repo instanceof Git.Repository) {
            console.log("Good!");
        } else {
            console.log("Bad!");
        }
        console.log('Cloning DONE');
    })
    .catch(function(err) {
        console.log('/!\\ ERROR /!\\');
        console.log(err);
    });
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have tried the same thing but it gives me below error message `Error: Method clone has thrown an error. {errno: -1, errorFunction: 'Clone.clone', stack: 'Error: Method clone has thrown an error.', message: 'Method clone has thrown an error.'}` – Nirav Chauhan Mar 28 '22 at 06:14
  • @NiravChauhan Is a manual `ssh -Tv git@github.com` working? (outside of any program, directly in command-line) – VonC Mar 28 '22 at 06:47
  • Yes it gave me `You've successfully authenticated, but GitHub does not provide shell access.` Now I am able to successfully clone the REPO using above code actually path that I had given was wrong. Thanks @VonC – Nirav Chauhan Mar 28 '22 at 08:26
  • One more question, Which is the method to generate those keys programmatically in Nodejs and use it in Git.Cred.sshKeyNew() (file path - public and private) function for above code I have added it manually using command line now I want to do it using **NODEJS** – Nirav Chauhan Mar 28 '22 at 08:29
  • Sure, So what I need here is to generate public and private keys and save them to a local file so I can use them in Git.Cred.sshKeyNew() function for filepath – Nirav Chauhan Mar 28 '22 at 09:36
  • @NiravChauhan You would need https://www.npmjs.com/package/ssh-keygen + https://docs.github.com/en/rest/reference/users#create-a-public-ssh-key-for-the-authenticated-user (to register the public key to the user profile), or call [`gh ssh-key add`](https://cli.github.com/manual/gh_ssh-key_add) from node, assuming you have installed the `gh` cli. – VonC Mar 28 '22 at 12:25
  • Great help ! Thanks As per Github [link](https://github.blog/2021-09-01-improving-git-protocol-security-github/) So how can we use **ed2551** instead of RSA – Nirav Chauhan Mar 28 '22 at 14:05
  • @NiravChauhan Good point, I agree. I mentioned that (ed25519) in https://stackoverflow.com/a/70767367/6309, and https://stackoverflow.com/a/65370706/6309, but mostly in https://stackoverflow.com/a/70734156/6309. – VonC Mar 28 '22 at 14:47
  • I tried ssh-keygen npm package to generate keys But it gives me below output. _Keys created!_ _private key: undefined_ _public key: undefined_ – Nirav Chauhan Mar 29 '22 at 06:57
  • @NiravChauhan That would be better addressed in a separate question, with a more complete and detailed error output. – VonC Mar 29 '22 at 06:58
  • Sure kindly refer [ssh-keygen](https://stackoverflow.com/questions/71657784/ssh-keygen-npm-package-gives-undefined-public-and-private-keys) – Nirav Chauhan Mar 29 '22 at 07:19