feat(egress): persistent per-daemon CA issuing cached leaf certificates
This commit is contained in:
1 parent
888cc5239c
commit
8a3933ded7
2 files changed
+257
No files matched your search
@@ -0,0 +1,173 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
caCertFile = "ca.pem"
|
||||
caKeyFile = "ca-key.pem"
|
||||
|
||||
caLifetime = 10 * 365 * 24 * time.Hour
|
||||
leafLifetime = 7 * 24 * time.Hour
|
||||
)
|
||||
|
||||
// CA is the daemon's egress certificate authority. Sandbox containers trust
|
||||
// it so the proxy can terminate their TLS; nothing outside a sandbox does.
|
||||
// It persists across restarts because every running container was started
|
||||
// trusting this exact certificate.
|
||||
type CA struct {
|
||||
cert *x509.Certificate
|
||||
certPEM []byte
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
mu sync.Mutex
|
||||
leaves map[string]*tls.Certificate
|
||||
}
|
||||
|
||||
// LoadOrCreateCA loads the CA from dir, creating it on first use. A CA that
|
||||
// exists but will not load is an error: minting a replacement silently
|
||||
// would break trust in every running container.
|
||||
func LoadOrCreateCA(dir string) (*CA, error) {
|
||||
certPath, keyPath := filepath.Join(dir, caCertFile), filepath.Join(dir, caKeyFile)
|
||||
certPEM, certErr := os.ReadFile(certPath)
|
||||
keyPEM, keyErr := os.ReadFile(keyPath)
|
||||
switch {
|
||||
case certErr == nil && keyErr == nil:
|
||||
return parseCA(certPEM, keyPEM)
|
||||
case errors.Is(certErr, fs.ErrNotExist) && errors.Is(keyErr, fs.ErrNotExist):
|
||||
return createCA(dir, certPath, keyPath)
|
||||
case certErr != nil && !errors.Is(certErr, fs.ErrNotExist):
|
||||
return nil, fmt.Errorf("read egress CA certificate: %w", certErr)
|
||||
case keyErr != nil && !errors.Is(keyErr, fs.ErrNotExist):
|
||||
return nil, fmt.Errorf("read egress CA key: %w", keyErr)
|
||||
default:
|
||||
return nil, fmt.Errorf("egress CA in %s is incomplete: one of %s and %s is missing", dir, caCertFile, caKeyFile)
|
||||
}
|
||||
}
|
||||
|
||||
func createCA(dir, certPath, keyPath string) (*CA, error) {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now()
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: serial(),
|
||||
Subject: pkix.Name{CommonName: "archie egress CA"},
|
||||
NotBefore: now.Add(-time.Hour),
|
||||
NotAfter: now.Add(caLifetime),
|
||||
IsCA: true,
|
||||
BasicConstraintsValid: true,
|
||||
MaxPathLenZero: true,
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyDER, err := x509.MarshalECPrivateKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
|
||||
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(keyPath, keyPEM, 0o600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(certPath, certPEM, 0o644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseCA(certPEM, keyPEM)
|
||||
}
|
||||
|
||||
func parseCA(certPEM, keyPEM []byte) (*CA, error) {
|
||||
certBlock, _ := pem.Decode(certPEM)
|
||||
if certBlock == nil {
|
||||
return nil, errors.New("egress CA certificate is not PEM")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse egress CA certificate: %w", err)
|
||||
}
|
||||
keyBlock, _ := pem.Decode(keyPEM)
|
||||
if keyBlock == nil {
|
||||
return nil, errors.New("egress CA key is not PEM")
|
||||
}
|
||||
key, err := x509.ParseECPrivateKey(keyBlock.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse egress CA key: %w", err)
|
||||
}
|
||||
if !key.PublicKey.Equal(cert.PublicKey) {
|
||||
return nil, errors.New("egress CA key does not match its certificate")
|
||||
}
|
||||
return &CA{cert: cert, certPEM: certPEM, key: key, leaves: map[string]*tls.Certificate{}}, nil
|
||||
}
|
||||
|
||||
// CertPEM is the certificate a sandbox container installs as trusted.
|
||||
func (c *CA) CertPEM() []byte { return c.certPEM }
|
||||
|
||||
// leaf returns a certificate for host signed by the CA, issuing it once and
|
||||
// reusing it until it nears expiry.
|
||||
func (c *CA) leaf(host string) (*tls.Certificate, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if cached, ok := c.leaves[host]; ok && time.Until(cached.Leaf.NotAfter) > leafLifetime/2 {
|
||||
return cached, nil
|
||||
}
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now()
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: serial(),
|
||||
Subject: pkix.Name{CommonName: host},
|
||||
NotBefore: now.Add(-time.Hour),
|
||||
NotAfter: now.Add(leafLifetime),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
template.IPAddresses = []net.IP{ip}
|
||||
} else {
|
||||
template.DNSNames = []string{host}
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, template, c.cert, &key.PublicKey, c.key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsed, err := x509.ParseCertificate(der)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
issued := &tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: parsed}
|
||||
c.leaves[host] = issued
|
||||
return issued, nil
|
||||
}
|
||||
|
||||
func serial() *big.Int {
|
||||
n, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func verifyLeaf(t *testing.T, ca *CA, leaf *tls.Certificate, name string) error {
|
||||
t.Helper()
|
||||
pool := x509.NewCertPool()
|
||||
if !pool.AppendCertsFromPEM(ca.CertPEM()) {
|
||||
t.Fatal("CertPEM is not a PEM certificate")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(leaf.Certificate[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = cert.Verify(x509.VerifyOptions{DNSName: name, Roots: pool})
|
||||
return err
|
||||
}
|
||||
|
||||
func TestCAPersistsAcrossLoads(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first, err := LoadOrCreateCA(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := LoadOrCreateCA(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(first.CertPEM()) != string(second.CertPEM()) {
|
||||
t.Fatal("reloading the CA produced a different certificate; every container would stop trusting the proxy")
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(dir, caKeyFile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if perm := info.Mode().Perm(); perm != 0o600 {
|
||||
t.Fatalf("CA key is mode %o, want 600", perm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCAIssuesVerifiableLeaves(t *testing.T) {
|
||||
ca, err := LoadOrCreateCA(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, host := range []string{"api.example.com", "10.0.0.5"} {
|
||||
leaf, err := ca.leaf(host)
|
||||
if err != nil {
|
||||
t.Fatalf("leaf(%q): %v", host, err)
|
||||
}
|
||||
if err := verifyLeaf(t, ca, leaf, host); err != nil {
|
||||
t.Fatalf("leaf for %q does not verify: %v", host, err)
|
||||
}
|
||||
if err := verifyLeaf(t, ca, leaf, "other.example.com"); err == nil {
|
||||
t.Fatalf("leaf for %q also verifies for another host", host)
|
||||
}
|
||||
again, err := ca.leaf(host)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if again != leaf {
|
||||
t.Fatalf("leaf for %q is re-signed on every connection", host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCARejectsATamperedKey(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, err := LoadOrCreateCA(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, caKeyFile), []byte("not a key"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := LoadOrCreateCA(dir); err == nil {
|
||||
t.Fatal("a corrupt CA key loaded; the proxy must fail closed rather than mint a new CA silently")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user