0

Good afternoon, I have a private ssh key, I first write it to a variable (so far for tests in bash, it is assumed that the variable will be in gitlab), then I rewrite it to a file:

ID_RSA="-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----"

echo $ID_RSA > ~/.ssh/id_rsa

But this does not work, because the variable writes the entire key to a string and transfers the string to a file. But as I experimented, I need a format not in one line, but with line breaks. But I don't know how to edit the file with a command. I tried to wrap lines after spaces, but this is not the right option, since the results are also wrong:

sed 's/ /\n/g' ~/.ssh/id_rsa

The result is almost perfect, but not quite)

-----BEGIN 
RSA 
PRIVATE 
KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END 
RSA 
PRIVATE 
KEY-----"

And you need it like this:

-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

With the following option, I experimented in the console, but caught an error when executing the command. The command is like this:

ssh-add - <<< "${SSH_PRIVATE_KEY}"

As for the hashicorp vault, for some reason my colleagues want to refuse it in principle, therefore, I don’t consider this option either (I mean storing not a variable, but a file with subsequent encryption).

  • try : echo "${ID_RSA}" ? Might work - dont have the ability to test right now. (note the quotes). – FreudianSlip Jan 26 '23 at 07:12
  • Yes! Thank you very much for your answer, it worked! Plus, I didn't notice that you can store variables as a file in gitlab - I think that should also make storing keys easier. – Andrew Metelkin Jan 26 '23 at 09:03

0 Answers0