2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-02 09:58:19 -04:00
|
|
|
package kubernetesca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2022-07-15 03:33:11 -04:00
|
|
|
"errors"
|
2022-06-02 09:58:19 -04:00
|
|
|
"fmt"
|
2022-07-15 03:33:11 -04:00
|
|
|
"strings"
|
2022-06-02 09:58:19 -04:00
|
|
|
"time"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-07-15 03:33:11 -04:00
|
|
|
kubeconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
2022-06-02 09:58:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
caCertFilename = "/etc/kubernetes/pki/ca.crt"
|
|
|
|
caKeyFilename = "/etc/kubernetes/pki/ca.key"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KubernetesCA handles signing of certificates using the Kubernetes root CA.
|
|
|
|
type KubernetesCA struct {
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-06-02 09:58:19 -04:00
|
|
|
file file.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new KubernetesCA.
|
2022-06-28 10:51:30 -04:00
|
|
|
func New(log *logger.Logger, fileHandler file.Handler) *KubernetesCA {
|
2022-06-02 09:58:19 -04:00
|
|
|
return &KubernetesCA{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-06-02 09:58:19 -04:00
|
|
|
file: fileHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:48:01 -05:00
|
|
|
// GetNodeNameFromCSR extracts the node name from a CSR.
|
|
|
|
func (c KubernetesCA) GetNodeNameFromCSR(csr []byte) (string, error) {
|
|
|
|
certRequest, err := x509.ParseCertificateRequest(csr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(certRequest.Subject.CommonName, kubeconstants.NodesUserPrefix) {
|
|
|
|
return "", fmt.Errorf("certificate request must have common name prefix %q but is %q", kubeconstants.NodesUserPrefix, certRequest.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimPrefix(certRequest.Subject.CommonName, kubeconstants.NodesUserPrefix), nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 09:58:19 -04:00
|
|
|
// GetCertificate creates a certificate for a node and signs it using the Kubernetes root CA.
|
2022-07-15 03:33:11 -04:00
|
|
|
func (c KubernetesCA) GetCertificate(csr []byte) (cert []byte, err error) {
|
2022-06-28 10:51:30 -04:00
|
|
|
c.log.Debugf("Loading Kubernetes CA certificate")
|
2022-06-02 09:58:19 -04:00
|
|
|
parentCertRaw, err := c.file.Read(caCertFilename)
|
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
2022-08-19 06:26:29 -04:00
|
|
|
parentCert, err := crypto.PemToX509Cert(parentCertRaw)
|
2022-06-02 09:58:19 -04:00
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
c.log.Debugf("Loading Kubernetes CA private key")
|
2022-06-02 09:58:19 -04:00
|
|
|
parentKeyRaw, err := c.file.Read(caKeyFilename)
|
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
parentKeyPEM, _ := pem.Decode(parentKeyRaw)
|
|
|
|
var parentKey any
|
|
|
|
switch parentKeyPEM.Type {
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
parentKey, err = x509.ParseECPrivateKey(parentKeyPEM.Bytes)
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
parentKey, err = x509.ParsePKCS1PrivateKey(parentKeyPEM.Bytes)
|
|
|
|
case "PRIVATE KEY":
|
|
|
|
parentKey, err = x509.ParsePKCS8PrivateKey(parentKeyPEM.Bytes)
|
|
|
|
default:
|
2022-08-19 06:26:29 -04:00
|
|
|
return nil, fmt.Errorf("unsupported key type %q", parentKeyPEM.Type)
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 03:33:11 -04:00
|
|
|
certRequest, err := x509.ParseCertificateRequest(csr)
|
2022-06-02 09:58:19 -04:00
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
2022-07-15 03:33:11 -04:00
|
|
|
if err := certRequest.CheckSignature(); err != nil {
|
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
c.log.Infof("Creating kubelet certificate")
|
2022-07-15 03:33:11 -04:00
|
|
|
if len(certRequest.Subject.Organization) != 1 {
|
|
|
|
return nil, errors.New("certificate request must have exactly one organization")
|
|
|
|
}
|
|
|
|
if certRequest.Subject.Organization[0] != kubeconstants.NodesGroup {
|
|
|
|
return nil, fmt.Errorf("certificate request must have organization %q but has %q", kubeconstants.NodesGroup, certRequest.Subject.Organization[0])
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(certRequest.Subject.CommonName, kubeconstants.NodesUserPrefix) {
|
|
|
|
return nil, fmt.Errorf("certificate request must have common name prefix %q but is %q", kubeconstants.NodesUserPrefix, certRequest.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
serialNumber, err := crypto.GenerateCertificateSerialNumber()
|
2022-06-02 09:58:19 -04:00
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
// Create the kubelet certificate
|
|
|
|
// For a reference on the certificate fields, see: https://kubernetes.io/docs/setup/best-practices/certificates/
|
|
|
|
certTmpl := &x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
NotBefore: now.Add(-2 * time.Hour),
|
|
|
|
NotAfter: now.Add(24 * 365 * time.Hour),
|
2022-07-15 03:33:11 -04:00
|
|
|
Subject: certRequest.Subject,
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
2022-06-02 09:58:19 -04:00
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
2022-07-15 03:33:11 -04:00
|
|
|
x509.ExtKeyUsageServerAuth,
|
2022-06-02 09:58:19 -04:00
|
|
|
},
|
|
|
|
IsCA: false,
|
|
|
|
BasicConstraintsValid: true,
|
2022-07-15 03:33:11 -04:00
|
|
|
IPAddresses: certRequest.IPAddresses,
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
2022-07-15 03:33:11 -04:00
|
|
|
|
|
|
|
certRaw, err := x509.CreateCertificate(rand.Reader, certTmpl, parentCert, certRequest.PublicKey, parentKey)
|
2022-06-02 09:58:19 -04:00
|
|
|
if err != nil {
|
2022-07-15 03:33:11 -04:00
|
|
|
return nil, err
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
kubeletCert := pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: certRaw,
|
|
|
|
})
|
|
|
|
|
2022-07-15 03:33:11 -04:00
|
|
|
return kubeletCert, nil
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|