You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO: etcd 3.2 introduced an undocumented requirement for ClientAuth usage on the
Copyright2018TheKubernetes Authors.
LicensedundertheApacheLicense, Version2.0 (the"License");
youmaynotusethisfileexceptincompliancewiththe License.
YoumayobtainacopyoftheLicenseathttp://www.apache.org/licenses/LICENSE-2.0Unlessrequiredbyapplicablelaworagreedtoinwriting, softwaredistributedundertheLicenseisdistributedonan"AS IS"BASIS,
WITHOUTWARRANTIESORCONDITIONSOFANYKIND, eitherexpressor implied.
SeetheLicenseforthespecificlanguagegoverningpermissionsandlimitationsundertheLicense.
*/packagecertsimport (
"crypto""crypto/x509""fmt""io""github.com/pkg/errors"certutil"k8s.io/client-go/util/cert"kubeadmapi"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"kubeadmconstants"k8s.io/kubernetes/cmd/kubeadm/app/constants""k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
)
const (
errInvalid="invalid argument"errExist="file already exists"
)
typeconfigMutatorsFuncfunc(*kubeadmapi.InitConfiguration, *pkiutil.CertConfig) error// KubeadmCert represents a certificate that Kubeadm will create to function properly.typeKubeadmCertstruct {
NamestringLongNamestringBaseNamestringCANamestring// Some attributes will depend on the InitConfiguration, only known at runtime.// These functions will be run in series, passed both the InitConfiguration and a cert Config.configMutators []configMutatorsFuncconfig pkiutil.CertConfig
}
// GetConfig returns the definition for the given cert given the provided InitConfigurationfunc (k*KubeadmCert) GetConfig(ic*kubeadmapi.InitConfiguration) (*pkiutil.CertConfig, error) {
for_, f:=rangek.configMutators {
iferr:=f(ic, &k.config); err!=nil {
returnnil, err
}
}
k.config.PublicKeyAlgorithm=ic.ClusterConfiguration.PublicKeyAlgorithm()
return&k.config, nil
}
// CreateFromCA makes and writes a certificate using the given CA cert and key.func (k*KubeadmCert) CreateFromCA(ic*kubeadmapi.InitConfiguration, caCert*x509.Certificate, caKey crypto.Signer) error {
cfg, err:=k.GetConfig(ic)
iferr!=nil {
returnerrors.Wrapf(err, "couldn't create %q certificate", k.Name)
}
cert, key, err:=pkiutil.NewCertAndKey(caCert, caKey, cfg)
iferr!=nil {
returnerr
}
err=writeCertificateFilesIfNotExist(
ic.CertificatesDir,
k.BaseName,
caCert,
cert,
key,
cfg,
)
iferr!=nil {
returnerrors.Wrapf(err, "failed to write or validate certificate %q", k.Name)
}
returnnil
}
// CreateAsCA creates a certificate authority, writing the files to disk and also returning the created CA so it can be used to sign child certs.func (k*KubeadmCert) CreateAsCA(ic*kubeadmapi.InitConfiguration) (*x509.Certificate, crypto.Signer, error) {
cfg, err:=k.GetConfig(ic)
iferr!=nil {
returnnil, nil, errors.Wrapf(err, "couldn't get configuration for %q CA certificate", k.Name)
}
caCert, caKey, err:=pkiutil.NewCertificateAuthority(cfg)
iferr!=nil {
returnnil, nil, errors.Wrapf(err, "couldn't generate %q CA certificate", k.Name)
}
err=writeCertificateAuthorityFilesIfNotExist(
ic.CertificatesDir,
k.BaseName,
caCert,
caKey,
)
iferr!=nil {
returnnil, nil, errors.Wrapf(err, "couldn't write out %q CA certificate", k.Name)
}
returncaCert, caKey, nil
}
// CertificateTree is represents a one-level-deep tree, mapping a CA to the certs that depend on it.typeCertificateTreemap[*KubeadmCert]Certificates// CreateTree creates the CAs, certs signed by the CAs, and writes them all to disk.func (tCertificateTree) CreateTree(ic*kubeadmapi.InitConfiguration) error {
forca, leaves:=ranget {
cfg, err:=ca.GetConfig(ic)
iferr!=nil {
returnerr
}
varcaKey crypto.SignercaCert, err:=pkiutil.TryLoadCertFromDisk(ic.CertificatesDir, ca.BaseName)
iferr==nil {
// Validate periodCheckCertificatePeriodValidity(ca.BaseName, caCert)
// Cert exists already, make sure it's validif!caCert.IsCA {
returnerrors.Errorf("certificate %q is not a CA", ca.Name)
}
// Try and load a CA KeycaKey, err=pkiutil.TryLoadKeyFromDisk(ic.CertificatesDir, ca.BaseName)
iferr!=nil {
// If there's no CA key, make sure every certificate exists.for_, leaf:=rangeleaves {
cl:=certKeyLocation{
pkiDir: ic.CertificatesDir,
baseName: leaf.BaseName,
uxName: leaf.Name,
}
iferr:=validateSignedCertWithCA(cl, caCert); err!=nil {
returnerrors.Wrapf(err, "could not load expected certificate %q or validate the existence of key %q for it", leaf.Name, ca.Name)
}
}
continue
}
// CA key exists; just use that to create new certificates.
} else {
// CACert doesn't already exist, create a new cert and key.caCert, caKey, err=pkiutil.NewCertificateAuthority(cfg)
iferr!=nil {
returnerr
}
err=writeCertificateAuthorityFilesIfNotExist(
ic.CertificatesDir,
ca.BaseName,
caCert,
caKey,
)
iferr!=nil {
returnerr
}
}
for_, leaf:=rangeleaves {
iferr:=leaf.CreateFromCA(ic, caCert, caKey); err!=nil {
returnerr
}
}
}
returnnil
}
// CertificateMap is a flat map of certificates, keyed by Name.typeCertificateMapmap[string]*KubeadmCert// CertTree returns a one-level-deep tree, mapping a CA cert to an array of certificates that should be signed by it.func (mCertificateMap) CertTree() (CertificateTree, error) {
caMap:=make(CertificateTree)
for_, cert:=rangem {
ifcert.CAName=="" {
if_, ok:=caMap[cert]; !ok {
caMap[cert] = []*KubeadmCert{}
}
} else {
ca, ok:=m[cert.CAName]
if!ok {
returnnil, errors.Errorf("certificate %q references unknown CA %q", cert.Name, cert.CAName)
}
caMap[ca] =append(caMap[ca], cert)
}
}
returncaMap, nil
}
// Certificates is a list of Certificates that Kubeadm should create.typeCertificates []*KubeadmCert// AsMap returns the list of certificates as a map, keyed by name.func (cCertificates) AsMap() CertificateMap {
certMap:=make(map[string]*KubeadmCert)
for_, cert:=rangec {
certMap[cert.Name] =cert
}
returncertMap
}
// GetDefaultCertList returns all of the certificates kubeadm requires to function.funcGetDefaultCertList() Certificates {
returnCertificates{
KubeadmCertRootCA(),
KubeadmCertAPIServer(),
KubeadmCertKubeletClient(),
// Front Proxy certsKubeadmCertFrontProxyCA(),
KubeadmCertFrontProxyClient(),
// etcd certsKubeadmCertEtcdCA(),
KubeadmCertEtcdServer(),
KubeadmCertEtcdPeer(),
KubeadmCertEtcdHealthcheck(),
KubeadmCertEtcdAPIClient(),
}
}
// GetCertsWithoutEtcd returns all of the certificates kubeadm needs when etcd is hosted externally.funcGetCertsWithoutEtcd() Certificates {
returnCertificates{
KubeadmCertRootCA(),
KubeadmCertAPIServer(),
KubeadmCertKubeletClient(),
// Front Proxy certsKubeadmCertFrontProxyCA(),
KubeadmCertFrontProxyClient(),
}
}
// KubeadmCertRootCA is the definition of the Kubernetes Root CA for the API Server and kubelet.funcKubeadmCertRootCA() *KubeadmCert {
return&KubeadmCert{
Name: "ca",
LongName: "self-signed Kubernetes CA to provision identities for other Kubernetes components",
BaseName: kubeadmconstants.CACertAndKeyBaseName,
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: "kubernetes",
},
},
}
}
// KubeadmCertAPIServer is the definition of the cert used to serve the Kubernetes API.funcKubeadmCertAPIServer() *KubeadmCert {
return&KubeadmCert{
Name: "apiserver",
LongName: "certificate for serving the Kubernetes API",
BaseName: kubeadmconstants.APIServerCertAndKeyBaseName,
CAName: "ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: kubeadmconstants.APIServerCertCommonName,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
},
},
configMutators: []configMutatorsFunc{
makeAltNamesMutator(pkiutil.GetAPIServerAltNames),
},
}
}
// KubeadmCertKubeletClient is the definition of the cert used by the API server to access the kubelet.funcKubeadmCertKubeletClient() *KubeadmCert {
return&KubeadmCert{
Name: "apiserver-kubelet-client",
LongName: "certificate for the API server to connect to kubelet",
BaseName: kubeadmconstants.APIServerKubeletClientCertAndKeyBaseName,
CAName: "ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: kubeadmconstants.APIServerKubeletClientCertCommonName,
Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
},
}
}
// KubeadmCertFrontProxyCA is the definition of the CA used for the front end proxy.funcKubeadmCertFrontProxyCA() *KubeadmCert {
return&KubeadmCert{
Name: "front-proxy-ca",
LongName: "self-signed CA to provision identities for front proxy",
BaseName: kubeadmconstants.FrontProxyCACertAndKeyBaseName,
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: "front-proxy-ca",
},
},
}
}
// KubeadmCertFrontProxyClient is the definition of the cert used by the API server to access the front proxy.funcKubeadmCertFrontProxyClient() *KubeadmCert {
return&KubeadmCert{
Name: "front-proxy-client",
BaseName: kubeadmconstants.FrontProxyClientCertAndKeyBaseName,
LongName: "certificate for the front proxy client",
CAName: "front-proxy-ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: kubeadmconstants.FrontProxyClientCertCommonName,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
},
}
}
// KubeadmCertEtcdCA is the definition of the root CA used by the hosted etcd server.funcKubeadmCertEtcdCA() *KubeadmCert {
return&KubeadmCert{
Name: "etcd-ca",
LongName: "self-signed CA to provision identities for etcd",
BaseName: kubeadmconstants.EtcdCACertAndKeyBaseName,
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: "etcd-ca",
},
},
}
}
// KubeadmCertEtcdServer is the definition of the cert used to serve etcd to clients.funcKubeadmCertEtcdServer() *KubeadmCert {
return&KubeadmCert{
Name: "etcd-server",
LongName: "certificate for serving etcd",
BaseName: kubeadmconstants.EtcdServerCertAndKeyBaseName,
CAName: "etcd-ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
// TODO: etcd 3.2 introduced an undocumented requirement for ClientAuth usage on the// server cert: https://github.com/coreos/etcd/issues/9785#issuecomment-396715692// Once the upstream issue is resolved, this should be returned to only allowing// ServerAuth usage.Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
},
},
configMutators: []configMutatorsFunc{
makeAltNamesMutator(pkiutil.GetEtcdAltNames),
setCommonNameToNodeName(),
},
}
}
// KubeadmCertEtcdPeer is the definition of the cert used by etcd peers to access each other.funcKubeadmCertEtcdPeer() *KubeadmCert {
return&KubeadmCert{
Name: "etcd-peer",
LongName: "certificate for etcd nodes to communicate with each other",
BaseName: kubeadmconstants.EtcdPeerCertAndKeyBaseName,
CAName: "etcd-ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
},
},
configMutators: []configMutatorsFunc{
makeAltNamesMutator(pkiutil.GetEtcdPeerAltNames),
setCommonNameToNodeName(),
},
}
}
// KubeadmCertEtcdHealthcheck is the definition of the cert used by Kubernetes to check the health of the etcd server.funcKubeadmCertEtcdHealthcheck() *KubeadmCert {
return&KubeadmCert{
Name: "etcd-healthcheck-client",
LongName: "certificate for liveness probes to healthcheck etcd",
BaseName: kubeadmconstants.EtcdHealthcheckClientCertAndKeyBaseName,
CAName: "etcd-ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: kubeadmconstants.EtcdHealthcheckClientCertCommonName,
Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
},
}
}
// KubeadmCertEtcdAPIClient is the definition of the cert used by the API server to access etcd.funcKubeadmCertEtcdAPIClient() *KubeadmCert {
return&KubeadmCert{
Name: "apiserver-etcd-client",
LongName: "certificate the apiserver uses to access etcd",
BaseName: kubeadmconstants.APIServerEtcdClientCertAndKeyBaseName,
CAName: "etcd-ca",
config: pkiutil.CertConfig{
Config: certutil.Config{
CommonName: kubeadmconstants.APIServerEtcdClientCertCommonName,
Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
},
}
}
funcmakeAltNamesMutator(ffunc(*kubeadmapi.InitConfiguration) (*certutil.AltNames, error)) configMutatorsFunc {
returnfunc(mc*kubeadmapi.InitConfiguration, cc*pkiutil.CertConfig) error {
altNames, err:=f(mc)
iferr!=nil {
returnerr
}
cc.AltNames=*altNamesreturnnil
}
}
funcsetCommonNameToNodeName() configMutatorsFunc {
returnfunc(mc*kubeadmapi.InitConfiguration, cc*pkiutil.CertConfig) error {
cc.CommonName=mc.NodeRegistration.Namereturnnil
}
}
// leafCertificates returns non-CA certificates from the supplied Certificates.funcleafCertificates(cCertificates) (Certificates, error) {
certTree, err:=c.AsMap().CertTree()
iferr!=nil {
returnnil, err
}
varoutCertificatesfor_, leafCertificates:=rangecertTree {
out=append(out, leafCertificates...)
}
returnout, nil
}
funccreateKeyAndCSR(kubeadmConfig*kubeadmapi.InitConfiguration, cert*KubeadmCert) error {
ifkubeadmConfig==nil {
returnerrors.Errorf("%s: kubeadmConfig was nil", errInvalid)
}
ifcert==nil {
returnerrors.Errorf("%s: cert was nil", errInvalid)
}
certDir:=kubeadmConfig.CertificatesDirname:=cert.BaseNameifpkiutil.CSROrKeyExist(certDir, name) {
returnerrors.Errorf("%s: key or CSR %s/%s", errExist, certDir, name)
}
cfg, err:=cert.GetConfig(kubeadmConfig)
iferr!=nil {
returnerr
}
csr, key, err:=pkiutil.NewCSRAndKey(cfg)
iferr!=nil {
returnerr
}
err=pkiutil.WriteKey(certDir, name, key)
iferr!=nil {
returnerr
}
err=pkiutil.WriteCSR(certDir, name, csr)
iferr!=nil {
returnerr
}
returnnil
}
// CreateDefaultKeysAndCSRFiles is used in ExternalCA mode to create key files// and adjacent CSR files.funcCreateDefaultKeysAndCSRFiles(out io.Writer, config*kubeadmapi.InitConfiguration) error {
certificates, err:=leafCertificates(GetDefaultCertList())
iferr!=nil {
returnerr
}
ifout!=nil {
fmt.Fprintf(out, "generating keys and CSRs in %s\n", config.CertificatesDir)
}
for_, cert:=rangecertificates {
iferr:=createKeyAndCSR(config, cert); err!=nil {
returnerr
}
ifout!=nil {
fmt.Fprintf(out, " %s\n", cert.BaseName)
}
}
returnnil
}
ewfilemode100644ndex0000000000000..c1d99bc85a4b2++b/cmd/kubeadm/app/phases/certs/certlist_test.go
648d2a27acbfbb6f5dd08b469dee83875589628e
The text was updated successfully, but these errors were encountered:
etcd 3.2 introduced an undocumented requirement for ClientAuth usage on the
server cert: etcd-io/etcd#9785 (comment)
Once the upstream issue is resolved, this should be returned to only allowing
ServerAuth usage.
kubernetes/cmd/kubeadm/app/phases/certs/certlist.go
Line 350 in f444ffd
648d2a27acbfbb6f5dd08b469dee83875589628e
The text was updated successfully, but these errors were encountered: