Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract certificate from the kubernetes config.
require 'optparse'
require 'yaml'
require 'base64'
options = {
config_path: File.join(ENV['HOME'], '.kube', 'config'),
write_dir: File.join(ENV['HOME'], '.kube')
}
OptionParser.new do |opts|
opts.banner = "Usage: extract_crt.rb [options]"
opts.on('-s', '--source FILE_PATH', 'Path to the kube config') { |v| options[:config_path] = v }
opts.on('-d', '--destination DIR', 'Path to directory where save key and certs') { |v| options[:write_dir] = v }
end.parse!
kube_path = options[:write_dir]
file_config = File.read options[:config_path]
config = YAML.load file_config
ca = Base64.decode64 config["clusters"][0]["cluster"]["certificate-authority-data"]
File.open(File.join(kube_path, 'ca.crt'), File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
f.write(ca)
end
client_crt = Base64.decode64 config["users"][0]["user"]["client-certificate-data"]
File.open(File.join(kube_path, 'kubecfg.crt'), File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
f.write(client_crt)
end
client_key = Base64.decode64 config["users"][0]["user"]["client-key-data"]
File.open(File.join(kube_path, 'kubecfg.key'), File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
f.write(client_key)
end
#!/bin/bash
# Would ask for password to encrypt the key
openssl pkcs12 -export -clcerts -inkey ~/.kube/kubecfg.key -in ~/.kube/kubecfg.crt -out ~/.kube/kubecfg.p12 -name "kubernetes-client"
open ~/.kube/kubecfg.p12
@xueshanf
Copy link

xueshanf commented May 28, 2017

@ has a shell version of it: https://gist.github.com/xueshanf/71f188c58553c82bda16f80483e71918. Thanks for the inspiration!

@carlosonunez
Copy link

carlosonunez commented Jul 27, 2018

Here's a shorter shell version of it:

sudo cat $KUBECONFIG_PATH | grep client-certificate-data | cut -f2 -d : | tr -d ' ' | base64 -d | openssl x509 -text -in -

@bjethwan
Copy link

bjethwan commented Apr 6, 2019

@carlosonunez
I think you were trying to write "-out -" in place of "-in -"?

sudo cat $KUBECONFIG_PATH | grep client-certificate-data | cut -f2 -d : | tr -d ' ' | base64 -d | openssl x509 -text -out -

@chris-cmsoft
Copy link

chris-cmsoft commented Jun 24, 2019

Late chime in here.

# minify for current
# raw for full output
kubectl config view --minify --raw --output 'jsonpath={..cluster.certificate-authority-data}'

@risentveber
Copy link

risentveber commented Jul 3, 2019

Seems full version is like that:

kubectl config view --minify --raw --output 'jsonpath={..cluster.certificate-authority-data}' | base64 -D | openssl x509 -text -out -

@wahmedswl
Copy link

wahmedswl commented Oct 16, 2020

Seems like there is mistake, it should be like

kubectl config view --minify --raw --output 'jsonpath={..cluster.certificate-authority-data}' | base64 -d | openssl x509 -text -out -

@migounette
Copy link

migounette commented Apr 1, 2021

A small update with the user part... thanks for your valuable inputs...

# Extract the Cluster Certificate Authorithy
$ kubectl config view --minify --raw --output 'jsonpath={..cluster.certificate-authority-data}' | base64 -d | openssl x509 -text -out -
...

# Extract the Client Certificate
$kubectl config view --minify --raw --output 'jsonpath={..user.client-certificate-data}' | base64 -d | openssl x509 -text -out -
...

# Extract the Client Private Key
$ kubectl config view --minify --raw --output 'jsonpath={..user.client-key-data}' | base64 -d
...

For Windows user

choco install base64
choco install openssl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment