How to store SSH keys with the Gitpod environment variables
For some months, I only used Gitpod for almost everything, even creating and handling VM, Kubernetes clusters, etc... As a workspace is stateless and rebuilt from a git repository (according to me, Gitpod is like a personal computer as code), I needed a way to store my ssh keys. One solution was to keep my repositories private and commit the keys. But I'm not very satisfied by this, nor proud in fact, and I would like to use public projects. It's time to use a clever way. We'll use the Gitpod environment variables.
Handling ssh keys
Generate keys and keep the format
To generate my ssh keys, I used the command below:
ssh-keygen -t rsa -b 2048 -C "riker"
My first try was to copy-paste the content of the two keys in 2 environment variables of Gitpod (go to https://gitpod.io/variables):
RIKER_ID_RSA_PUB
RIKER_ID_RSA
But every time I tried to regenerate the keys from the environment variables and want to use them, I got a message saying that the format of my private key was "bad".
The trick is to encode the content of the keys before saving them to environment variables, and then you will keep the format of the keys when you decode them.
Encode the public and private keys
These are the commands to encode the public and private keys before storing the values in the environment variables of your Gitpod profile https://gitpod.io/variables:
base64 /home/gitpod/.ssh/id_rsa.pub
# and store it to RIKER_ID_RSA_PUB
base64 /home/gitpod/.ssh/id_rsa
# and store it to RIKER_ID_RSA
Then you'll need to restart the workspace.
Decode the public and private keys
Now, when I need my keys, I use this:
echo ${RIKER_ID_RSA_PUB} > ./id_rsa.pub
echo ${RIKER_ID_RSA} > ./id_rsa
base64 --decode --ignore-garbage id_rsa.pub > /home/gitpod/.ssh/id_rsa.pub
base64 --decode --ignore-garbage id_rsa > /home/gitpod/.ssh/id_rsa
chmod 644 /home/gitpod/.ssh/id_rsa.pub
chmod 600 /home/gitpod/.ssh/id_rsa
chmod 700 /home/gitpod/.ssh
Simple and useful.
👋
- If you loved this "post" (or not), don't forget to use the emojis reactions
- Don't hesitate to add comments and/or ask questions
- You can subscribe to the Rss feed