Reputation: 633
I am passing private-key
as a gitlab
environment variable. But when I echo it, it is altered, it is not really in the format of:
-----BEGIN RSA PRIVATE KEY-----
xxxxxxx
-----END RSA PRIVATE KEY-----
So I think the solution must be to encode
it again and then decode
it with base64
How can I do this with bash
shell?
What I tried is:
encode ./private-key
bash6d -d ./private-key
I have the doubt that this is not the way. Can someone please help me?
Upvotes: 10
Views: 36058
Reputation: 873
May be a delayed response, but hope it helps others. I had a similar situation of encoding the ssh private key, embed into kubernetes secret and using that in the mounted file inside the POD. Did as like below.
If you are using the linux environment, can encode the data with below command.
$ cat private_key.pem|base64
{Encoded_Data}
This output copied into secret and when mounted, I can find the decoded data was already with the same as it was encoded. But, in case if you want to decode this data, you can do with below command.
$cat encoded_private_key.pem|base64 --decode
Upvotes: 5
Reputation: 1328112
I prefer using openssl:
openssl enc -base64 -in my.key -out my.key.base64
Then, to decode it:
openssl enc -d -base64 -in my.key.base64 -out my.key
You can leave out the -out part if you want to see the resut on stdout.
And you can use -a
instead of -base64
(same option, shorter)
To test it:
openssl enc -a -in my.key | openssl enc -a -d
Upvotes: 13