While developing one of our recent projects, as a team, we decided to implement our microservices application in the mono repository. This idea allows us to sync versions between services easily, even in one commit. However, we’re not keeping this solution for ourselves. Seeing great value in it, we open-sourced one of the microservices for the community. 

We decided to change our private GitHub repository to be open to the public when we were ready, but before we had to find answers to questions like: 

  • How to authorize other services to pull dependencies from private repositories, both in local and Docker environments?
  • How to make it work with our CI system - CircleCI?

Keep reading to find out how we nailed it. In the article, you will find a step-by-step guide to implementing our solution. 

Moreover, here you can check our Go development services.

Problem statement

NOTE: At the beginning of each paragraph there are links to GitHub repositories containing fully working example solutions at given stage.

monorepo@before-split

In our project, we are creating a system which consists of multiple microservices, most of which are written in Go. For easier and faster development we decided to start with monorepo (hosted by GitHub), with the following structure.

1.
2├── authorizer
3│ ├── cmd
4│ │ └── authorizer
5│ │ └── main.go
6│ ├── Dockerfile
7│ ├── go.mod
8│ ├── go.sum
9│ └── pkg
10│ └── app
11│ └── app.go
12├── backend
13│ ├── cmd
14│ │ └── backend
15│ │ └── main.go
16│ ├── Dockerfile
17│ ├── go.mod
18│ ├── go.sum
19│ └── pkg
20│ └── app
21│ └── app.go
22├── log
23│ ├── go.mod
24│ ├── go.sum
25│ └── log.go
26├── Makefile
27├── README.md
28└── server
29 ├── go.mod
30 ├── go.sum
31 └── server.go

There are two microservices - ‘authorizer’ and ’backend’. There are also two libraries - ‘log’ and ‘server’. Authorizer depends only on the ‘log’ module, while the backend depends on ‘log’ and ‘server’. Dependencies use the ‘replace’ directive in Go mod to work properly.

1replace (
2 example.org/log => ../log
3 example.org/server => ../server
4)

At some point during development, we decided that the ’server’ and ‘log’ packages would be open source. As a first step, we wanted to extract them as a separate (but still private repo) and then after some testing go public.

Requirements

In our development process, we build our services in 3 ways:

  1. Build on local dev machine
  2. Containerized build on local dev machine
  3. Containerized build in CircleCI

At each of those stages we need to address the following issues:

  1. How to access dependency code from a private GitHub repository?
  2. How to handle custom (‘example.org’) domain names?

These sound easy, but during the repo split we discovered that this matter is far from trivial. In the following paragraphs we will walk through the implementation process describing how to solve issues emerging at each of the levels.

Working on a local environment

monorepo@after-split-local-dev

goprivate-blog/log

goprivate-blog/server

There are no surprises at this level. To get this to work there are only three steps needed:

  1. Configure the GitHub SSH key which has access to all the required repositories.
  2. Configure Git to use SSH instead of HTTPS for GitHub:
    git config --global url.ssh://git@github.com/.insteadOf https://github.com/
  3. Set GOPRIVATE=github.com/codilime/ to tell go get that those repositories need to be accessed directly without Go module mirrors.

Working on a local environment with Docker

monorepo@after-split-local-dev-with-docker

goprivate-blog/log

goprivate-blog/server

The main challenge when using private repositories in Docker is to keep the SSH keys used to access repositories safe. We can’t just copy them to the container as someone unauthorized might access them.

Docker allows us to copy files to the container which live only in the context of one ‘RUN’. That allows us to inject the SSH key only for the dependencies download. This key won’t be present in the final image.

1FROM golang:1.18-buster AS builder
2WORKDIR /src
3COPY authorizer/ .
4​
5RUN git config --global url.ssh://git@github.com/.insteadOf https://github.com/
6RUN mkdir /root/.ssh && ssh-keyscan github.com >> /root/.ssh/known_hosts
7ENV GOPRIVATE github.com/codilime
8​
9RUN --mount=type=secret,id=sshKey,dst=/root/.ssh/id_ecdsa go mod download
10​
11RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /authorizer cmd/authorizer/main.go
12​
13FROM alpine:3.15.0
14COPY --from=builder /authorizer /authorizer
15CMD ["/authorizer"]

Makefile:

1TAG=$(shell git rev-parse --short HEAD)
2​
3build_backend:
4 docker build \
5 -t backend:$(TAG) \
6 -f backend/Dockerfile \
7 --secret id=sshKey,src=${HOME}/.ssh/id_ecdsa \
8 .
9​
10build_authorizer:
11 docker build \
12 -t authorizer:$(TAG) \
13 -f authorizer/Dockerfile \
14 --secret id=sshKey,src=${HOME}/.ssh/id_ecdsa \
15 .

Working in CircleCI with Docker

blog/monorepo@after-split-circleci

goprivate-blog/log

goprivate-blog/server

Unlike in the local dev environment, in CircleCI we would like to have more granular control when accessing GitHub repositories. To achieve this we can use deploy keys, which are limited to a single repository and may be even configured to be read only, which is the expected permission for CI jobs.

Unfortunately, GitHub does not allow adding the same deploy key for multiple repositories. We need to improve the solution from the previous step to make it work.

First, let’s generate and add SSH deploy keys to each of the repositories.

1$ ssh-keygen -t ecdsa
2Generating public/private ecdsa key pair.
3Enter file in which to save the key (/home/krzysztof/.ssh/id_ecdsa): log_github_key
4Enter passphrase (empty for no passphrase):
5Enter same passphrase again:
6Your identification has been saved in log_github_key
7Your public key has been saved in log_github_key.pub
8<snip>
9~/.../github.com/goprivate-blog$ ssh-keygen -t ecdsa
10Generating public/private ecdsa key pair.
11Enter file in which to save the key (/home/krzysztof/.ssh/id_ecdsa): server_github_key
12Enter passphrase (empty for no passphrase):
13Enter same passphrase again:
14Your identification has been saved in server_github_key
15Your public key has been saved in server_github_key.pub
16<snip>

Adding to GitHub

Fig.1: Adding SSH keys to GitHub

Adding to CircleCI 

To distinguish keys we add a prefix to github.com. Don’t worry that they are not real hostnames, we will override them.

Fig.2: Adding SSH keys to CircleCI
Fig.3: Additional SSH keys

Next, we need to create fake SSH endpoints, to tell the SSH client which key needs to be used to access each of the repositories. To make it reusable, both in Dockerfiles and in CircleCI config, we can wrap it in a simple bash script.

1#!/usr/bin/env bash
2set -euo pipefail
3set -x
4​
5log_key_path=${1:-/root/.ssh/log.pub}
6server_key_path=${2:-/root/.ssh/server.pub}
7​
8mkdir -p ~/.ssh
9# Add github to known hosts
10ssh-keyscan github.com >>~/.ssh/known_hosts
11​
12# Add "fake" SSH hosts per each private repo
13cat <<EOT >>~/.ssh/config
14 Host log.github.com
15 HostName github.com
16 IdentityFile ${log_key_path}
17 IdentitiesOnly yes
18 Host server.github.com
19 HostName github.com
20 IdentityFile ${server_key_path}
21 IdentitiesOnly yes
22EOT
23​
24# Configure git to use "fake" SSH hosts
25git config \
26 --global url."git@log.github.com:codilime/goprivate-blog-log".insteadOf \
27 "https://github.com/codilime/goprivate-blog-log"
28git config \
29 --global url."git@server.github.com:codilime/goprivate-blog-server".insteadOf \
30 "https://github.com/codilime/goprivate-blog-server"
31​
32export GOPRIVATE=github.com/codilime

Now, change Dockerfiles to:

1FROM golang:1.18-buster AS builder
2​
3WORKDIR /src
4COPY authorizer/ .
5​
6RUN --mount=type=secret,id=ssh-script,dst=/root/go_mod_ssh_config.sh \
7 --mount=type=secret,id=log-key,dst=/root/.ssh/log \
8 --mount=type=secret,id=server-key,dst=/root/.ssh/server \
9 --mount=type=ssh <<-EOF
10 bash /root/go_mod_ssh_config.sh
11 go mod download
12 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /authorizer cmd/authorizer/main.go
13EOF
14​
15FROM alpine:3.15.0
16COPY --from=builder /authorizer /authorizer
17CMD ["/authorizer"]

And update CircleCI config to:

1version: 2
2jobs:
3 build:
4 docker:
5 - image: cimg/base:2022.08
6 steps:
7 - checkout
8 - setup_remote_docker:
9 version: 20.10.14
10 - add_ssh_keys:
11 fingerprints:
12 - "c5:cd:b9:00:1e:4c:99:c0:60:45:57:1d:d2:41:8c:d2" # log.github.com
13 - "6f:2d:3c:ae:08:6f:d4:f0:15:5f:b7:4a:89:60:d2:13" # server.github.com
14 - run:
15 name: Configure private repos for go mod
16 command: |
17 ./.circleci/scripts/go_mod_ssh_config.sh \
18 ~/.ssh/id_rsa_c5cdb9001e4c99c06045571dd2418cd2 \
19 ~/.ssh/id_rsa_6f2d3cae086fd4f0155fb74a8960d213
20 - run:
21 name: Build backend Docker image
22 command: |
23 make build_backend
24 - run:
25 name: Build authorizer Docker image
26 command: |
27 make build_authorizer

Working with passphrase-protected SSH keys

monorepo@after-split-passphrase-protected-keys

goprivate-blog/log

goprivate-blog/server

When the SSH key is protected with a passphrase this solution won’t work. In such a case, we would need to use SSH Agent to manage our keys. To do so we need to pass the existing SSH Agent socket into the image and point the SSH config to public keys instead of private ones.

The only issue here is that CircleCI only stores private keys, we need to generate private ones in CircleCI:

1 - run:
2 name: Generate public keys
3 command: |
4 ssh-keygen -y -f ~/.ssh/id_rsa_c5cdb9001e4c99c06045571dd2418cd2 \
5 > ~/.ssh/id_rsa_c5cdb9001e4c99c06045571dd2418cd2.pub
6 ssh-keygen -y -f ~/.ssh/id_rsa_6f2d3cae086fd4f0155fb74a8960d213 \
7 > ~/.ssh/id_rsa_6f2d3cae086fd4f0155fb74a8960d213.pub

Summary

Our goal was to make this solution secure and reusable across multiple services. As all this information could not be found in a single place on the internet, this post gathers all the information in one place and explains the entire process we went through to make it work. 

Hopefully, you will benefit from our guide next time you have to deal with private repositories, Go modules, Docker and CircleCI at once.