Packages
k8s
0.2.4
2.8.0
2.7.0
2.6.2
2.6.1
2.6.0
2.5.0
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.6
2.0.0-rc.5
2.0.0-rc.4
2.0.0-rc.3
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc1
0.5.2
0.5.1
0.5.0
0.5.0-rc.2
0.5.0-rc.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
retired
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
Kubernetes API Client for Elixir
Current section
Files
Jump to
Current section
Files
lib/k8s/conf/pki.ex
defmodule K8s.Conf.PKI do
@moduledoc """
Retrieves information from certificates
"""
alias K8s.Conf
alias K8s.Conf.PKI
@private_key_atoms [
:RSAPrivateKey,
:DSAPrivateKey,
:ECPrivateKey,
:PrivateKeyInfo
]
@doc """
Reads the certificate from PEM file or base64 encoding
"""
@spec cert_from_map(map(), String.t()) :: binary
def cert_from_map(%{"certificate-authority-data" => data}, _) when not is_nil(data),
do: PKI.cert_from_base64(data)
def cert_from_map(%{"certificate-authority" => file_name}, base_path) do
file_name
|> Conf.resolve_file_path(base_path)
|> PKI.cert_from_pem()
end
# Handles the case for docker-for-desktop kubernetes cluster as there is no ca
def cert_from_map(_, _), do: nil
@doc """
Reads the certificate from a PEM file
"""
@spec cert_from_pem(String.t()) :: nil | binary
def cert_from_pem(nil), do: nil
def cert_from_pem(file) do
file
|> File.read!()
|> decode_cert_data()
end
@doc """
Decodes the certificate from a base64 encoded string
"""
@spec cert_from_base64(String.t()) :: nil | binary
def cert_from_base64(nil), do: nil
def cert_from_base64(data) do
case Base.decode64(data) do
{:ok, cert_data} -> decode_cert_data(cert_data)
_ -> nil
end
end
defp decode_cert_data(cert_data) do
cert_data
|> :public_key.pem_decode()
|> Enum.find_value(fn
{:Certificate, data, _} -> data
_ -> nil
end)
end
@spec private_key_from_pem(String.t()) :: nil | {atom, binary}
def private_key_from_pem(nil), do: nil
@doc """
Reads private key from a PEM file
"""
def private_key_from_pem(file) do
file
|> File.read!()
|> decode_private_key_data
end
@doc """
Decodes private key from a base64 encoded string
"""
@spec private_key_from_base64(String.t()) :: nil | {atom, binary}
def private_key_from_base64(nil), do: nil
def private_key_from_base64(encoded_key) do
case Base.decode64(encoded_key) do
{:ok, data} -> decode_private_key_data(data)
_ -> nil
end
end
defp decode_private_key_data(private_key_data) do
private_key_data
|> :public_key.pem_decode()
|> Enum.find_value(fn
{type, data, _} when type in @private_key_atoms -> {type, data}
_ -> false
end)
end
end