Base64 and “echo” command

As we saw in the previous post, secrets in Kubernetes are passed and stored in base64 form. However when i was trying to do so, I run in a lot of problems, such as not escaped characters, wrong username, wrong password ,etc. At the end I found out the reason and I think is nice to report it in this quick post. So, lets see…

One nice way to encode the values in base64 is using the base64 tool available in any linux distribution (even GIT bash in windows has it). So you can issue the following command to convert a value in base64:

$ echo "admin" | base64 -w0

However if you do that you will notice that the output is “YWRtaW4K” and not “YWRtaW4=” (which is the expected output) . The reason is because echo command issue a new line character automatically, and thus will be appended, passed and encoded in base64 too, causing all sort of problems.

The solution is to append -n to echo to prevent the new line to be added. The correct command is

 $ echo -n "admin" | base64 -w0

This will output the correct value.

Also for decoding you can easily do that by:

 $ echo "YWRtaW4=" | base64 -d

 

I hope this quick post was useful!

Click to rate this post!
[Total: 1 Average: 5]

Leave a Reply

Your email address will not be published. Required fields are marked *