Skip to content

Instantly share code, notes, and snippets.

@linosteenkamp
Last active September 3, 2019 07:35
Show Gist options
  • Save linosteenkamp/1da2217ee544bb213f36d5b3a169b43c to your computer and use it in GitHub Desktop.
Save linosteenkamp/1da2217ee544bb213f36d5b3a169b43c to your computer and use it in GitHub Desktop.
Extract certificates from kubectl config
#!/bin/bash
CKD_KEY="client-key-data"
CCD_KEY="client-certificate-data"
CAD_KEY="certificate-authority-data"
json=$(kubectl config view --raw -o json)
#- Retrieve Certificates and Keys for all contexts
echo "$json" | jq -c '.contexts[].context' | while read line
do
user=$(echo "$line" | jq '.user')
cluster=$(echo "$line" | jq '.cluster')
fname=$(echo "$cluster" | sed -e 's/\"//g')
echo "$json" | jq ".users[] | select(.name == $user) | .user.\"$CKD_KEY\"" | sed -e 's/\"//g' | base64 -D > "$fname-$CKD_KEY.key"
echo "$json" | jq ".users[] | select(.name == $user) | .user.\"$CCD_KEY\"" | sed -e 's/\"//g' | base64 -D > "$fname-$CCD_KEY.pem"
echo "$json" | jq ".clusters[] | select(.name == $cluster) | .cluster.\"$CAD_KEY\"" | sed -e 's/\"//g' | base64 -D > "$fname-$CAD_KEY.pem"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment