Skip to content

Instantly share code, notes, and snippets.

@zaenk
Created March 22, 2024 07:31
Show Gist options
  • Save zaenk/a109c9a649451235818254569d79ea6c to your computer and use it in GitHub Desktop.
Save zaenk/a109c9a649451235818254569d79ea6c to your computer and use it in GitHub Desktop.
Encode decode base64 in simple kubernetes secrets yaml files

Goal

These scripts encode-decode yaml files, e.g. from

secret:
  greeting: aGVsbG8=

using ksd test.yaml it will overwrite the file contents to

secret:
  greeting: hello

using kse test.yaml will convert it back.

Code

Prerequisites:

Place this code into your .bash_profile or .zshrc or into the rc file of your terminal of choice.

function ksd() {
    file=$1

    if [ -z "$file" ]; then
        echo "decodes base64 string in kubernetes secrets like file"
        echo 
        echo "Usage: $0 <file>"
        echo
        return 1
    fi

    yq -y -i '.secret[] |= @base64d' $file
}

function kse() {
    file=$1

    if [ -z "$file" ]; then
        echo "encodes strings to base64 in kubernetes config map like file"
        echo 
        echo "Usage: $0 <file>"
        echo
        return 1
    fi

    yq -y -i '.secret[] |= @base64' $file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment