2017-10-31 13:55:35 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-10-31 13:55:35 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-10-31 13:55:35 +00:00
|
|
|
"k8s.io/client-go/util/cert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CertificatePKI struct {
|
2017-11-10 02:39:10 +00:00
|
|
|
Certificate *x509.Certificate
|
|
|
|
Key *rsa.PrivateKey
|
|
|
|
Config string
|
|
|
|
Name string
|
|
|
|
CommonName string
|
|
|
|
OUName string
|
|
|
|
EnvName string
|
|
|
|
Path string
|
|
|
|
KeyEnvName string
|
|
|
|
KeyPath string
|
|
|
|
ConfigEnvName string
|
|
|
|
ConfigPath string
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartCertificatesGeneration ...
|
2018-01-16 23:10:14 +00:00
|
|
|
func StartCertificatesGeneration(ctx context.Context, cpHosts, etcdHosts []*hosts.Host, clusterDomain, localConfigPath string, KubernetesServiceIP net.IP) (map[string]CertificatePKI, error) {
|
2018-02-01 21:28:31 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating kubernetes certificates")
|
2018-01-16 23:10:14 +00:00
|
|
|
certs, err := generateCerts(ctx, cpHosts, etcdHosts, clusterDomain, localConfigPath, KubernetesServiceIP)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
2017-11-02 10:07:10 +00:00
|
|
|
return nil, err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
return certs, nil
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
func generateCerts(ctx context.Context, cpHosts, etcdHosts []*hosts.Host, clusterDomain, localConfigPath string, KubernetesServiceIP net.IP) (map[string]CertificatePKI, error) {
|
2017-10-31 13:55:35 +00:00
|
|
|
certs := make(map[string]CertificatePKI)
|
|
|
|
// generate CA certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating CA kubernetes certificates")
|
2017-10-31 13:55:35 +00:00
|
|
|
caCrt, caKey, err := generateCACertAndKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[CACertName] = ToCertObject(CACertName, "", "", caCrt, caKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
|
|
|
// generate API certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating Kubernetes API server certificates")
|
2017-11-21 19:25:08 +00:00
|
|
|
kubeAPIAltNames := GetAltNames(cpHosts, clusterDomain, KubernetesServiceIP)
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeAPICrt, kubeAPIKey, err := GenerateSignedCertAndKey(caCrt, caKey, true, KubeAPICertName, kubeAPIAltNames, nil, nil)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[KubeAPICertName] = ToCertObject(KubeAPICertName, "", "", kubeAPICrt, kubeAPIKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
|
|
|
// generate Kube controller-manager certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating Kube Controller certificates")
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeControllerCrt, kubeControllerKey, err := GenerateSignedCertAndKey(caCrt, caKey, false, getDefaultCN(KubeControllerCertName), nil, nil, nil)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[KubeControllerCertName] = ToCertObject(KubeControllerCertName, "", "", kubeControllerCrt, kubeControllerKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
|
|
|
// generate Kube scheduler certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating Kube Scheduler certificates")
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeSchedulerCrt, kubeSchedulerKey, err := GenerateSignedCertAndKey(caCrt, caKey, false, getDefaultCN(KubeSchedulerCertName), nil, nil, nil)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[KubeSchedulerCertName] = ToCertObject(KubeSchedulerCertName, "", "", kubeSchedulerCrt, kubeSchedulerKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
|
|
|
// generate Kube Proxy certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating Kube Proxy certificates")
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeProxyCrt, kubeProxyKey, err := GenerateSignedCertAndKey(caCrt, caKey, false, getDefaultCN(KubeProxyCertName), nil, nil, nil)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[KubeProxyCertName] = ToCertObject(KubeProxyCertName, "", "", kubeProxyCrt, kubeProxyKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
2017-11-01 21:46:43 +00:00
|
|
|
// generate Kubelet certificate and key
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating Node certificate")
|
2018-01-16 23:10:14 +00:00
|
|
|
nodeCrt, nodeKey, err := GenerateSignedCertAndKey(caCrt, caKey, false, KubeNodeCommonName, nil, nil, []string{KubeNodeOrganizationName})
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
certs[KubeNodeCertName] = ToCertObject(KubeNodeCertName, KubeNodeCommonName, KubeNodeOrganizationName, nodeCrt, nodeKey)
|
2017-11-01 21:46:43 +00:00
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
// generate Admin certificate and key
|
2018-02-01 21:28:31 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating admin certificates and kubeconfig")
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeAdminCrt, kubeAdminKey, err := GenerateSignedCertAndKey(caCrt, caKey, false, KubeAdminCertName, nil, nil, []string{KubeAdminOrganizationName})
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
2018-01-16 23:10:14 +00:00
|
|
|
return nil, err
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
kubeAdminConfig := GetKubeConfigX509WithData(
|
|
|
|
"https://"+cpHosts[0].Address+":6443",
|
|
|
|
KubeAdminCertName,
|
|
|
|
string(cert.EncodeCertPEM(caCrt)),
|
|
|
|
string(cert.EncodeCertPEM(kubeAdminCrt)),
|
|
|
|
string(cert.EncodePrivateKeyPEM(kubeAdminKey)))
|
|
|
|
|
|
|
|
kubeAdminCertObj := ToCertObject(KubeAdminCertName, KubeAdminCertName, KubeAdminOrganizationName, kubeAdminCrt, kubeAdminKey)
|
|
|
|
kubeAdminCertObj.Config = kubeAdminConfig
|
|
|
|
kubeAdminCertObj.ConfigPath = localConfigPath
|
|
|
|
certs[KubeAdminCertName] = kubeAdminCertObj
|
|
|
|
|
|
|
|
etcdAltNames := GetAltNames(etcdHosts, clusterDomain, KubernetesServiceIP)
|
|
|
|
for _, host := range etcdHosts {
|
2018-02-01 21:28:31 +00:00
|
|
|
log.Infof(ctx, "[certificates] Generating etcd-%s certificate and key", host.InternalAddress)
|
2018-01-16 23:10:14 +00:00
|
|
|
etcdCrt, etcdKey, err := GenerateSignedCertAndKey(caCrt, caKey, true, EtcdCertName, etcdAltNames, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
etcdName := GetEtcdCrtName(host.InternalAddress)
|
|
|
|
certs[etcdName] = ToCertObject(etcdName, "", "", etcdCrt, etcdKey)
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
return certs, nil
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
func RegenerateEtcdCertificate(
|
|
|
|
ctx context.Context,
|
|
|
|
crtMap map[string]CertificatePKI,
|
|
|
|
etcdHost *hosts.Host,
|
|
|
|
etcdHosts []*hosts.Host,
|
|
|
|
clusterDomain string,
|
|
|
|
KubernetesServiceIP net.IP) (map[string]CertificatePKI, error) {
|
2017-10-31 13:55:35 +00:00
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
log.Infof(ctx, "[certificates] Regenerating new etcd-%s certificate and key", etcdHost.InternalAddress)
|
|
|
|
caCrt := crtMap[CACertName].Certificate
|
|
|
|
caKey := crtMap[CACertName].Key
|
|
|
|
etcdAltNames := GetAltNames(etcdHosts, clusterDomain, KubernetesServiceIP)
|
2017-10-31 13:55:35 +00:00
|
|
|
|
2018-01-16 23:10:14 +00:00
|
|
|
etcdCrt, etcdKey, err := GenerateSignedCertAndKey(caCrt, caKey, true, EtcdCertName, etcdAltNames, nil, nil)
|
2017-11-26 22:29:52 +00:00
|
|
|
if err != nil {
|
2018-01-16 23:10:14 +00:00
|
|
|
return nil, err
|
2017-11-10 02:39:10 +00:00
|
|
|
}
|
2018-01-16 23:10:14 +00:00
|
|
|
etcdName := GetEtcdCrtName(etcdHost.InternalAddress)
|
|
|
|
crtMap[etcdName] = ToCertObject(etcdName, "", "", etcdCrt, etcdKey)
|
|
|
|
log.Infof(ctx, "[certificates] Successfully generated new etcd-%s certificate and key", etcdHost.InternalAddress)
|
|
|
|
return crtMap, nil
|
2017-11-10 02:39:10 +00:00
|
|
|
}
|