Using SSH to authenticate with your repositories

Tutorials

SSH is an fast & secure way to communicate with your Git & Mercurial repositories in Codebase. When you choose to use SSH, all your traffic is encrypted and your are authenticated using your own set of SSH keys.

Understanding SSH keys

An SSH keypair consists of a public and a private key. These are generated on your computer and you just need to send us the public key. The private key is kept on your computer and should never be sent to anyone else.

To generate a new key pair, just open up your terminal of choice and run ssh-keygen. You'll be guided through a number of steps which will help you create your key. The defaults are fine if this is your first time creating a new key. When asked to add a passphrase, it's strongly recommended to protect your key with a passphrase in case you lose it.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/adam/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/adam/.ssh/id_rsa.
Your public key has been saved in /Users/adam/.ssh/id_rsa.pub.
The key fingerprint is:
5a:73:dd:c7:c7:97:01:4a:5c:42:76:d8:cd:e0:1b:ee adam@adam-mpb.local
The key's randomart image is:
+--[ RSA 2048]----+
|         o=+=+   |
|          oo=..o |
|           . o . |
|           ...ooo|
|        S . .o..*|
|       o o  .  .o|
|      .      E   |
|                 |
|                 |
n+-----------------+

The output from this command, will tell you where you can find your public key. In this instance, it is in /Users/adam/.ssh/id_rsa.pub.

$ cat /Users/adam/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvtNKd/IHPXiiZNLftQ1f487HR1uMjec4/1S3aoBKVE2XGd6tF8HgC3jit\r\nmP4LFyzn0AsLPK0Wf0A6zh9qQxk4qtquDEfHblfd20ws7DSKlqweZwIbBqIHeIw26FEOJiurp2i5dAGZcHq4kYPgJn14z\r\nvX6mSd3rcPYk+6PdUNdI5eDR0pFuW+VG37J6+7gN8PAXDhieCh9YcX2aqiJD09NZZgMoxK2FDw/7gj3oBW9G4Dff7C/m6\r\nZjtlPb61d1HUmNkPJNq9cf04mKrWU9NsXX8gj7wSnmQouYTec9Gv57Z5iRAd5Qa627f9Ta6rsO89aykMFcvle/XY5Hkc3\r\nBcIxPQ==

Once you've got this key, just head over to your Codebase account. Select Settings and then My Profile from the menu in the top right. Then you can select Add new SSH public key and paste your newly generated public key into this field.

New public key

Pushing/pulling your code

Pushing & pulling your repositories happens in exactly the same way as when you use HTTP. Any SSH repository URLs will look like this:

git@codebasehq.com:your-account/project-name/repo-name.git

To clone your repository, just enter your usual clone command followed by your SSH URL. You can find your SSH transport URL on any repository page in the Codebase interface.

SSH clone URL

Converting a repository from HTTP to SSH

If you've already got a repository which uses HTTP, you can easily switch to SSH using the command below.

$ cd path/to/repo
$ git remote origin set-url git@codebasehq.com:your-account/project-name/repo-name.git
$ git push origin
# Counting objects: 3, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (3/3), done.
# Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
# Total 3 (delta 2), reused 0 (delta 0)
# To git@codebasehq.com:atechmedia/qoffee/app.git
#    81556e7..493cabd  master -> master

Once you've run this you'll notice that you're pushing to your new SSH URL.

Dealing with errors

There are a couple of issues which may occur when authenticating using SSH. The first error you might experience is:

$ git clone blah@codebasehq.com:viaduct/viaduct/app.git test
# Cloning into 'test'...
# Permission denied (publickey).
  • Check that you've uploaded your SSH key into your Codebase account
  • Check that you're SSH client is using the correct private key for the request. You can do this by running the command below and examining the output. It should list all the SSH keys which are being provided. If you don't see your key, you may need to run ssh-add.
$ ssh git@codebasehq.com -v 2>&1 | grep Offering
# debug1: Offering RSA public key: /Users/adam/.ssh/id_rsa

Another error which is quite common is this one:

$ git clone git@codebasehq.com:viaduct/viaduct/app.git test
# Cloning into 'test'...
# Error: You do not have access to this repository.

This means that your key was sent successfully but you do not have access to the repository which you have requested.

  • Ensure that you have entered the URL to the repository correctly.
  • Check that you have access to the repository in the web interface.
  • Check with the account owner to ensure that you should have access.

Summary

We hope you've found this guide helpful in outlining how to set up SSH authentication to our Codebase repositories. If you have any questions about this or any other aspect of Codebase, please don't hesitate to get in touch.

A little bit about the author

Adam is the Head of Software at Krystal. Adam looks after our software engineering team by day and, when not doing that, enjoys hobbies including LEGO, collecting playing cards, building mechanical keyboards and home automation.