

How to use different ssh deploy keys for multiple private github repositories wi...
source link: https://gist.github.com/jrapoport/d12f60029eef017354d0ec982b918258
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to use different ssh deploy keys for multiple private github repositories with Go Modules
Let's assume you are using Go Modules and have a go.mod
file that contains multiple private repos each with a different ssh key. How can you get go mod download
to do the right thing -- i.e. use ssh key A with private repo A and ssh key B with private repo B?
Ok, here we go!
Let's assume you have some github.com user with multiple private repos:
https://github.com/someuser/private-repo-1
https://github.com/someuser/private-repo-2
and a go.mod
file that looks something like this:
module github.com/alice/repo
go 1.11
require (
github.com/someuser/private-repo-1 v0.0.0
github.com/someuser/private-repo-2 v0.0.0
github.com/bob/public-repo v0.0.0
)
As part of an automated process (e.g. running go mod download
in a Docker container) you need to clone these private repos without using a password and make sure that you use the right key with its corresponding repo.
(I'm skipping over all the ways to try and get this to work that don't)
In order to access the repos we are going to use Github deploy keys. Because a given deploy key can only be added to one specific repo, you'll need to generate a keypair for each:
$ ssh-keygen -t ecdsa -C "[email protected]"
...
> private1_id_ecdsa, private1_id_ecdsa.pub # keys for private-repo-1
$ ssh-keygen -t ecdsa -C "[email protected]"
...
> private2_id_ecdsa, private2_id_ecdsa.pub # keys for private-repo-2
Next you'll add each key to its corresponding repo via Settings > Deploy keys
in Github:
private1_id_ecdsa.pub => github.com/someuser/private-repo-1
private2_id_ecdsa.pub => github.com/someuser/private-repo-2
Now you'll need to tell your ssh client what private key to use for which repo. Open up ~/.ssh/config
and add:
Host private1.github.com
HostName github.com
IdentityFile ~/.ssh/private1_id_ecdsa
IdentitiesOnly yes
Host private2.github.com
HostName github.com
IdentityFile ~/.ssh/private2_id_ecdsa
IdentitiesOnly yes
This tells ssh when it sees a repo at private1.github.com/foo
to use the private1_id_ecdsa
deploy key, and likewise a repo at private2.github.com/bar
should use the private2_id_ecdsa
deploy key.
Ok great, but your repos are github.com/someuser/private-repo-1
and github.com/someuser/private-repo-1
, so how can we make this work correctly with go mod download
?
Bring the Magic
Edit your ~/.gitconfig
file and add:
[url "[email protected]:someuser/private-repo-1"]
insteadOf = https://github.com/someuser/private-repo-1
[url "[email protected]:someuser/private-repo-2"]
insteadOf = https://github.com/someuser/private-repo-2
Now when go mod download
encounters a url in your mod file like https://github.com/someuser/private-repo-1
it will automagicallly rewrite the host so your ~/.ssh/config
picks it up correctly and uses the right key. \:D/ Yay!
p.s.
You won't need to use GOPRIVATE
when calling go mod
-- it will just work -- but you might want to anyway if you're concerned about info leakage.
Recommend
-
53
Whether you are working on a JavaScript project or a Rails one with Webpack you might come to the point where you need to reference a private GitHub repository as your dependency in package.json . Here ar...
-
14
New fro...
-
13
GitHub Discussions now available for private repositories In December 2020, we launched the public beta of GitHub Discussions, a collaborative c...
-
11
How to use GitHub Actions and private repositories to deploy a Hugo static siteFor quite some time I have wanted to build a site where I could share links to the stuff I read online. There must be already plenty of sites to solve this but non...
-
5
Table of contents Introduction Overview git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command. You give git-xargs:
-
4
Multiple SSH Keys settings for different github account create different public key create different ssh key according the article Mac Set-Up Git $ ssh-keyg...
-
7
Introduction By using GitHub deploy keys you can safely push to GitHub repositories, or read private GitHub repositories. Configuration Let’s assume that: your GitHub user name / organization name i...
-
5
Kris Chau June 17, 2022 2 minute read ...
-
10
Deploy Applications from private Repositories using Flux GitOps and Azure Arc Posted Sep 25, 2022 by By Wolfgang Ofner 10 min read
-
4
Slack suffered a security incident over the holidays affecting some...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK