6

I want to set a gitlab variable so it can be consumed by the gitlab-ci.yaml

The variable I want to set is a ssh private key and it is being copied to a server.

Problem: - When I set the private key as below, it changes the structure of that private key which leads to a incorrect key.

enter image description here

Private key before: (I will only put a half of the key for security reasons)

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAmJwjZmGw7sitfoCb88xpn6bfgoSKVat5j8rvyGgOkX/Eavag
32izNxWbZvQ249nE1rOwpWAmxtN+JyFEo6IlVhfbrwfRGen+RFboARAe5s5/6ETI
c70s68xFmZxHJYJlt+lJoTOz+YnoK4QaIFbFoh/keRTSnIr7gMlDjv7VFH4VBhJ4
Jpp9TtmK5hsmpnESWYameePdFuo9vZFssrqLZXBGISGEOafGuZhT6eh/iG3sNeeK
.
.
.
-----END RSA PRIVATE KEY-----

Private key after it is being saved to server from the pipeline:

-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAz26/7zOELE87ColkJBhNWBQcvL7DjhdRCeWmHeeSPZ2PSjZS KP1+a/J0eHuBHvVIOrZB8oyPnfXfpOlga5y2TIvxLTgNNFzbzGScBIYHNQ9sBzAB w3IEiHzkJCYYY3rWuUMlJtLIyWnaIS8CBYQvO/DlcB70V5z9kWuu5VJDvV8esOnH +Pe2pk/08R2Q610UwOm4blJi0UAVd89Cd3/kPuC5xln+wumdqtQ8ZiAKRvZiXUgK 
.
.
.
b4ewuT1RZXkO6WyP/CSdFjEvYi9pxErXYMMY5hFFJCnzQcEQPXqW -----END RSA PRIVATE KEY-----

How can I avoid this disorientation?

1 Answer 1

8

Use base64 encoding before you put text in the variable and then decode variable back into key using base64 as well.

I.e.,

base64 -w0 your_key_file > key.base64

Then put contents of key.base64 into your env variable.

Then in your pipeline do

echo -n $PRIVATEKEY | base64 -d > key

Which would put your original key into the key file.

Side note: be very careful with adding private ssh keys to pipelines. This may pose significant security risk.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.