certlist.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package certs
  14. import (
  15. "crypto"
  16. "crypto/x509"
  17. "github.com/pkg/errors"
  18. certutil "k8s.io/client-go/util/cert"
  19. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  20. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
  22. )
  23. type configMutatorsFunc func(*kubeadmapi.InitConfiguration, *pkiutil.CertConfig) error
  24. // KubeadmCert represents a certificate that Kubeadm will create to function properly.
  25. type KubeadmCert struct {
  26. Name string
  27. LongName string
  28. BaseName string
  29. CAName string
  30. // Some attributes will depend on the InitConfiguration, only known at runtime.
  31. // These functions will be run in series, passed both the InitConfiguration and a cert Config.
  32. configMutators []configMutatorsFunc
  33. config pkiutil.CertConfig
  34. }
  35. // GetConfig returns the definition for the given cert given the provided InitConfiguration
  36. func (k *KubeadmCert) GetConfig(ic *kubeadmapi.InitConfiguration) (*pkiutil.CertConfig, error) {
  37. for _, f := range k.configMutators {
  38. if err := f(ic, &k.config); err != nil {
  39. return nil, err
  40. }
  41. }
  42. k.config.PublicKeyAlgorithm = ic.ClusterConfiguration.PublicKeyAlgorithm()
  43. return &k.config, nil
  44. }
  45. // CreateFromCA makes and writes a certificate using the given CA cert and key.
  46. func (k *KubeadmCert) CreateFromCA(ic *kubeadmapi.InitConfiguration, caCert *x509.Certificate, caKey crypto.Signer) error {
  47. cfg, err := k.GetConfig(ic)
  48. if err != nil {
  49. return errors.Wrapf(err, "couldn't create %q certificate", k.Name)
  50. }
  51. cert, key, err := pkiutil.NewCertAndKey(caCert, caKey, cfg)
  52. if err != nil {
  53. return err
  54. }
  55. err = writeCertificateFilesIfNotExist(
  56. ic.CertificatesDir,
  57. k.BaseName,
  58. caCert,
  59. cert,
  60. key,
  61. cfg,
  62. )
  63. if err != nil {
  64. return errors.Wrapf(err, "failed to write or validate certificate %q", k.Name)
  65. }
  66. return nil
  67. }
  68. // 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.
  69. func (k *KubeadmCert) CreateAsCA(ic *kubeadmapi.InitConfiguration) (*x509.Certificate, crypto.Signer, error) {
  70. cfg, err := k.GetConfig(ic)
  71. if err != nil {
  72. return nil, nil, errors.Wrapf(err, "couldn't get configuration for %q CA certificate", k.Name)
  73. }
  74. caCert, caKey, err := pkiutil.NewCertificateAuthority(cfg)
  75. if err != nil {
  76. return nil, nil, errors.Wrapf(err, "couldn't generate %q CA certificate", k.Name)
  77. }
  78. err = writeCertificateAuthorityFilesIfNotExist(
  79. ic.CertificatesDir,
  80. k.BaseName,
  81. caCert,
  82. caKey,
  83. )
  84. if err != nil {
  85. return nil, nil, errors.Wrapf(err, "couldn't write out %q CA certificate", k.Name)
  86. }
  87. return caCert, caKey, nil
  88. }
  89. // CertificateTree is represents a one-level-deep tree, mapping a CA to the certs that depend on it.
  90. type CertificateTree map[*KubeadmCert]Certificates
  91. // CreateTree creates the CAs, certs signed by the CAs, and writes them all to disk.
  92. func (t CertificateTree) CreateTree(ic *kubeadmapi.InitConfiguration) error {
  93. for ca, leaves := range t {
  94. cfg, err := ca.GetConfig(ic)
  95. if err != nil {
  96. return err
  97. }
  98. var caKey crypto.Signer
  99. caCert, err := pkiutil.TryLoadCertFromDisk(ic.CertificatesDir, ca.BaseName)
  100. if err == nil {
  101. // Cert exists already, make sure it's valid
  102. if !caCert.IsCA {
  103. return errors.Errorf("certificate %q is not a CA", ca.Name)
  104. }
  105. // Try and load a CA Key
  106. caKey, err = pkiutil.TryLoadKeyFromDisk(ic.CertificatesDir, ca.BaseName)
  107. if err != nil {
  108. // If there's no CA key, make sure every certificate exists.
  109. for _, leaf := range leaves {
  110. cl := certKeyLocation{
  111. pkiDir: ic.CertificatesDir,
  112. baseName: leaf.BaseName,
  113. uxName: leaf.Name,
  114. }
  115. if err := validateSignedCertWithCA(cl, caCert); err != nil {
  116. return errors.Wrapf(err, "could not load expected certificate %q or validate the existence of key %q for it", leaf.Name, ca.Name)
  117. }
  118. }
  119. continue
  120. }
  121. // CA key exists; just use that to create new certificates.
  122. } else {
  123. // CACert doesn't already exist, create a new cert and key.
  124. caCert, caKey, err = pkiutil.NewCertificateAuthority(cfg)
  125. if err != nil {
  126. return err
  127. }
  128. err = writeCertificateAuthorityFilesIfNotExist(
  129. ic.CertificatesDir,
  130. ca.BaseName,
  131. caCert,
  132. caKey,
  133. )
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. for _, leaf := range leaves {
  139. if err := leaf.CreateFromCA(ic, caCert, caKey); err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. return nil
  145. }
  146. // CertificateMap is a flat map of certificates, keyed by Name.
  147. type CertificateMap map[string]*KubeadmCert
  148. // CertTree returns a one-level-deep tree, mapping a CA cert to an array of certificates that should be signed by it.
  149. func (m CertificateMap) CertTree() (CertificateTree, error) {
  150. caMap := make(CertificateTree)
  151. for _, cert := range m {
  152. if cert.CAName == "" {
  153. if _, ok := caMap[cert]; !ok {
  154. caMap[cert] = []*KubeadmCert{}
  155. }
  156. } else {
  157. ca, ok := m[cert.CAName]
  158. if !ok {
  159. return nil, errors.Errorf("certificate %q references unknown CA %q", cert.Name, cert.CAName)
  160. }
  161. caMap[ca] = append(caMap[ca], cert)
  162. }
  163. }
  164. return caMap, nil
  165. }
  166. // Certificates is a list of Certificates that Kubeadm should create.
  167. type Certificates []*KubeadmCert
  168. // AsMap returns the list of certificates as a map, keyed by name.
  169. func (c Certificates) AsMap() CertificateMap {
  170. certMap := make(map[string]*KubeadmCert)
  171. for _, cert := range c {
  172. certMap[cert.Name] = cert
  173. }
  174. return certMap
  175. }
  176. // GetDefaultCertList returns all of the certificates kubeadm requires to function.
  177. func GetDefaultCertList() Certificates {
  178. return Certificates{
  179. &KubeadmCertRootCA,
  180. &KubeadmCertAPIServer,
  181. &KubeadmCertKubeletClient,
  182. // Front Proxy certs
  183. &KubeadmCertFrontProxyCA,
  184. &KubeadmCertFrontProxyClient,
  185. // etcd certs
  186. &KubeadmCertEtcdCA,
  187. &KubeadmCertEtcdServer,
  188. &KubeadmCertEtcdPeer,
  189. &KubeadmCertEtcdHealthcheck,
  190. &KubeadmCertEtcdAPIClient,
  191. }
  192. }
  193. // GetCertsWithoutEtcd returns all of the certificates kubeadm needs when etcd is hosted externally.
  194. func GetCertsWithoutEtcd() Certificates {
  195. return Certificates{
  196. &KubeadmCertRootCA,
  197. &KubeadmCertAPIServer,
  198. &KubeadmCertKubeletClient,
  199. // Front Proxy certs
  200. &KubeadmCertFrontProxyCA,
  201. &KubeadmCertFrontProxyClient,
  202. }
  203. }
  204. var (
  205. // KubeadmCertRootCA is the definition of the Kubernetes Root CA for the API Server and kubelet.
  206. KubeadmCertRootCA = KubeadmCert{
  207. Name: "ca",
  208. LongName: "self-signed Kubernetes CA to provision identities for other Kubernetes components",
  209. BaseName: kubeadmconstants.CACertAndKeyBaseName,
  210. config: pkiutil.CertConfig{
  211. Config: certutil.Config{
  212. CommonName: "kubernetes",
  213. },
  214. },
  215. }
  216. // KubeadmCertAPIServer is the definition of the cert used to serve the Kubernetes API.
  217. KubeadmCertAPIServer = KubeadmCert{
  218. Name: "apiserver",
  219. LongName: "certificate for serving the Kubernetes API",
  220. BaseName: kubeadmconstants.APIServerCertAndKeyBaseName,
  221. CAName: "ca",
  222. config: pkiutil.CertConfig{
  223. Config: certutil.Config{
  224. CommonName: kubeadmconstants.APIServerCertCommonName,
  225. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  226. },
  227. },
  228. configMutators: []configMutatorsFunc{
  229. makeAltNamesMutator(pkiutil.GetAPIServerAltNames),
  230. },
  231. }
  232. // KubeadmCertKubeletClient is the definition of the cert used by the API server to access the kubelet.
  233. KubeadmCertKubeletClient = KubeadmCert{
  234. Name: "apiserver-kubelet-client",
  235. LongName: "certificate for the API server to connect to kubelet",
  236. BaseName: kubeadmconstants.APIServerKubeletClientCertAndKeyBaseName,
  237. CAName: "ca",
  238. config: pkiutil.CertConfig{
  239. Config: certutil.Config{
  240. CommonName: kubeadmconstants.APIServerKubeletClientCertCommonName,
  241. Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
  242. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  243. },
  244. },
  245. }
  246. // KubeadmCertFrontProxyCA is the definition of the CA used for the front end proxy.
  247. KubeadmCertFrontProxyCA = KubeadmCert{
  248. Name: "front-proxy-ca",
  249. LongName: "self-signed CA to provision identities for front proxy",
  250. BaseName: kubeadmconstants.FrontProxyCACertAndKeyBaseName,
  251. config: pkiutil.CertConfig{
  252. Config: certutil.Config{
  253. CommonName: "front-proxy-ca",
  254. },
  255. },
  256. }
  257. // KubeadmCertFrontProxyClient is the definition of the cert used by the API server to access the front proxy.
  258. KubeadmCertFrontProxyClient = KubeadmCert{
  259. Name: "front-proxy-client",
  260. BaseName: kubeadmconstants.FrontProxyClientCertAndKeyBaseName,
  261. LongName: "certificate for the front proxy client",
  262. CAName: "front-proxy-ca",
  263. config: pkiutil.CertConfig{
  264. Config: certutil.Config{
  265. CommonName: kubeadmconstants.FrontProxyClientCertCommonName,
  266. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  267. },
  268. },
  269. }
  270. // KubeadmCertEtcdCA is the definition of the root CA used by the hosted etcd server.
  271. KubeadmCertEtcdCA = KubeadmCert{
  272. Name: "etcd-ca",
  273. LongName: "self-signed CA to provision identities for etcd",
  274. BaseName: kubeadmconstants.EtcdCACertAndKeyBaseName,
  275. config: pkiutil.CertConfig{
  276. Config: certutil.Config{
  277. CommonName: "etcd-ca",
  278. },
  279. },
  280. }
  281. // KubeadmCertEtcdServer is the definition of the cert used to serve etcd to clients.
  282. KubeadmCertEtcdServer = KubeadmCert{
  283. Name: "etcd-server",
  284. LongName: "certificate for serving etcd",
  285. BaseName: kubeadmconstants.EtcdServerCertAndKeyBaseName,
  286. CAName: "etcd-ca",
  287. config: pkiutil.CertConfig{
  288. Config: certutil.Config{
  289. // TODO: etcd 3.2 introduced an undocumented requirement for ClientAuth usage on the
  290. // server cert: https://github.com/coreos/etcd/issues/9785#issuecomment-396715692
  291. // Once the upstream issue is resolved, this should be returned to only allowing
  292. // ServerAuth usage.
  293. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
  294. },
  295. },
  296. configMutators: []configMutatorsFunc{
  297. makeAltNamesMutator(pkiutil.GetEtcdAltNames),
  298. setCommonNameToNodeName(),
  299. },
  300. }
  301. // KubeadmCertEtcdPeer is the definition of the cert used by etcd peers to access each other.
  302. KubeadmCertEtcdPeer = KubeadmCert{
  303. Name: "etcd-peer",
  304. LongName: "certificate for etcd nodes to communicate with each other",
  305. BaseName: kubeadmconstants.EtcdPeerCertAndKeyBaseName,
  306. CAName: "etcd-ca",
  307. config: pkiutil.CertConfig{
  308. Config: certutil.Config{
  309. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
  310. },
  311. },
  312. configMutators: []configMutatorsFunc{
  313. makeAltNamesMutator(pkiutil.GetEtcdPeerAltNames),
  314. setCommonNameToNodeName(),
  315. },
  316. }
  317. // KubeadmCertEtcdHealthcheck is the definition of the cert used by Kubernetes to check the health of the etcd server.
  318. KubeadmCertEtcdHealthcheck = KubeadmCert{
  319. Name: "etcd-healthcheck-client",
  320. LongName: "certificate for liveness probes to healthcheck etcd",
  321. BaseName: kubeadmconstants.EtcdHealthcheckClientCertAndKeyBaseName,
  322. CAName: "etcd-ca",
  323. config: pkiutil.CertConfig{
  324. Config: certutil.Config{
  325. CommonName: kubeadmconstants.EtcdHealthcheckClientCertCommonName,
  326. Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
  327. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  328. },
  329. },
  330. }
  331. // KubeadmCertEtcdAPIClient is the definition of the cert used by the API server to access etcd.
  332. KubeadmCertEtcdAPIClient = KubeadmCert{
  333. Name: "apiserver-etcd-client",
  334. LongName: "certificate the apiserver uses to access etcd",
  335. BaseName: kubeadmconstants.APIServerEtcdClientCertAndKeyBaseName,
  336. CAName: "etcd-ca",
  337. config: pkiutil.CertConfig{
  338. Config: certutil.Config{
  339. CommonName: kubeadmconstants.APIServerEtcdClientCertCommonName,
  340. Organization: []string{kubeadmconstants.SystemPrivilegedGroup},
  341. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  342. },
  343. },
  344. }
  345. )
  346. func makeAltNamesMutator(f func(*kubeadmapi.InitConfiguration) (*certutil.AltNames, error)) configMutatorsFunc {
  347. return func(mc *kubeadmapi.InitConfiguration, cc *pkiutil.CertConfig) error {
  348. altNames, err := f(mc)
  349. if err != nil {
  350. return err
  351. }
  352. cc.AltNames = *altNames
  353. return nil
  354. }
  355. }
  356. func setCommonNameToNodeName() configMutatorsFunc {
  357. return func(mc *kubeadmapi.InitConfiguration, cc *pkiutil.CertConfig) error {
  358. cc.CommonName = mc.NodeRegistration.Name
  359. return nil
  360. }
  361. }