Skip to content

Instantly share code, notes, and snippets.

@pilgrim2go
Forked from miry/01_extract_crt.rb
Created April 15, 2021 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pilgrim2go/3a330f2cd6e76ea5e9b7b0366c8a3523 to your computer and use it in GitHub Desktop.
Save pilgrim2go/3a330f2cd6e76ea5e9b7b0366c8a3523 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment