Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private ssh key from one line environment var

I have my privkey.pem private key in the form

-----BEGIN RSA PRIVATE KEY-----
xxx
-----END RSA PRIVATE KEY-----

I need to make a Bamboo task to connect to a server via ssh using this key. Unfortunately I see that Bamboo can store only variables in one line, so if I paste the key all the "\n" get stripped out so this doesn't work:

eval $(ssh-agent -s)
echo "${bamboo.sshKey}" | ssh-add - > /dev/null
scp -vvv -o StrictHostKeyChecking=no .....

Is there a way to have a private key in a "one line" format readable from ssh-add? Or, is there a clean way to reparse the key to get again the stripped "\n"?

like image 689
edoedoedo Avatar asked Sep 19 '25 02:09

edoedoedo


1 Answers

I realize I am a bit late but solutions with sed and tr didn't work well for me. I ended up using base64 and base64 -d, e.g.

First, convert your private SSH key on your Linux machine like this, into base64 without any newlines:

 base64 ~/.ssh/yourkey.id_rsa | tr -d '\n'

Second, copy and paste that one line string into a new plan variable named "sshkey".

Finally, create a Script task that decodes the key and uses it.

echo "${bamboo.sshkey}" | base64 -d > id_rsa
chmod 600 id_rsa
export GIT_SSH_COMMAND='ssh -i id_rsa -o StrictHostKeyChecking=no'
....
rm id_rsa

Using a variable name like sshkey makes Bamboo mask it with ******** so it is somewhat casually obscured. It's not secure, because you can unhide the value easily.

Be careful not to "edit' the sshkey, because if you edit the key and it shows ******** and then save it, the new key will literally be a bunch of asterisks. (not your fault, that's a UI failure)

like image 183
Dariop Avatar answered Sep 23 '25 13:09

Dariop