CA delegation: name-constrained intermediates, root rotation, ACME node certificates
New roots carry path length 1. `rollcall ca intermediate` signs a CSR (or a generated key) as an intermediate limited to the permitted domains and address ranges; a root with path length 0 refuses and `rollcall ca rotate` replaces it. deploy/tags/acme-cert.yaml gets a node certificate from an ACME server under such an intermediate and renews it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcbb07721b
commit
77894b7d57
6 files changed
+311
-2
No files matched your search
@@ -58,6 +58,10 @@ publish a tag that has no section here.
|
||||
Network page and, compact, on the Overview. Overview redesigned: fleet state bar, per-rack counts,
|
||||
attention list incl. IPAM sync, capacity and free pool, OS, hardware, BMC drivers, locations, tags,
|
||||
recent deployments.
|
||||
- CA delegation: new roots allow one level of intermediates; `rollcall ca intermediate NAME -permit
|
||||
DOMAIN,CIDR [-csr FILE]` signs a name-constrained intermediate (for an ACME server such as Caddy's
|
||||
`acme_server`); `rollcall ca rotate -yes` replaces a root that cannot delegate; tag fragment
|
||||
`deploy/tags/acme-cert.yaml` fetches and renews a node certificate with certbot.
|
||||
- `rollcall version`; CD-built binary carries `git describe`.
|
||||
- Release notes from `CHANGELOG.md`.
|
||||
|
||||
|
||||
@@ -430,6 +430,18 @@ plane is: `ca_dir` (default `/etc/rollcall/ca`) holds `ca.pem` and
|
||||
unverified), stateless overlays get it as `.CA`. `rollcall ca issue NAME
|
||||
-host h1,h2 [-client]` issues leaves for anything else (Slurm REST, a
|
||||
registry, a web front end).
|
||||
* **Delegation.** The root may sign one level of intermediate CAs, each
|
||||
limited by name constraints to the domains and address ranges it serves:
|
||||
`rollcall ca intermediate caddy -permit int.example.com,10.213.0.0/16
|
||||
-csr caddy.csr` signs a CSR (the key stays on the host that made it). An ACME
|
||||
server under that intermediate (Caddy's `acme_server`, step-ca) then issues
|
||||
short-lived certificates to nodes with certbot or acme.sh; everything that
|
||||
trusts the Rollcall CA trusts them, and nothing outside the permitted names
|
||||
can be issued. Roots created before this (path length 0) cannot delegate:
|
||||
`rollcall ca rotate -yes` replaces the root, keeping the old one as a
|
||||
backup; rack controllers then join again and clients trust the new
|
||||
fingerprint. `deploy/tags/acme-cert.yaml` is a tag that fetches a node
|
||||
certificate at first boot and renews it.
|
||||
|
||||
## Power drivers
|
||||
|
||||
|
||||
+183
-2
@@ -40,6 +40,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -76,7 +77,7 @@ func newCA(cn string) (*CA, error) {
|
||||
}
|
||||
tmpl := &x509.Certificate{SerialNumber: serial, Subject: pkix.Name{CommonName: cn, Organization: []string{"rollcall"}},
|
||||
NotBefore: time.Now().Add(-backdate), NotAfter: time.Now().Add(caValidity),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, BasicConstraintsValid: true, IsCA: true, MaxPathLenZero: true}
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, BasicConstraintsValid: true, IsCA: true, MaxPathLen: 1}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -189,6 +190,58 @@ func (ca *CA) sign(pub crypto.PublicKey, cn string, hosts []string, usage ...x50
|
||||
return pemEncode("CERTIFICATE", der), cert, err
|
||||
}
|
||||
|
||||
// canDelegate: the root may sign intermediate CAs (roots before path length 1 cannot).
|
||||
func (ca *CA) canDelegate() bool {
|
||||
return !(ca.Cert.MaxPathLen == 0 && ca.Cert.MaxPathLenZero)
|
||||
}
|
||||
|
||||
// parsePermits splits "int.example.com,10.213.0.0/16" into DNS and IP constraints.
|
||||
func parsePermits(list string) (dns []string, nets []*net.IPNet, err error) {
|
||||
for _, p := range strings.Split(list, ",") {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if _, n, e := net.ParseCIDR(p); e == nil {
|
||||
nets = append(nets, n)
|
||||
continue
|
||||
}
|
||||
if net.ParseIP(p) != nil {
|
||||
return nil, nil, fmt.Errorf("%s: give an address range as CIDR", p)
|
||||
}
|
||||
dns = append(dns, strings.TrimPrefix(strings.ToLower(p), "*."))
|
||||
}
|
||||
return dns, nets, nil
|
||||
}
|
||||
|
||||
// intermediate signs a CA certificate for pub that may issue only below the
|
||||
// permitted DNS names and inside the permitted address ranges, one level deep.
|
||||
func (ca *CA) intermediate(pub crypto.PublicKey, cn string, dns []string, nets []*net.IPNet, validity time.Duration) ([]byte, *x509.Certificate, error) {
|
||||
if !ca.canDelegate() {
|
||||
return nil, nil, errors.New("this root was created with path length 0 and cannot sign intermediate CAs; `rollcall ca rotate` replaces it with one that can")
|
||||
}
|
||||
serial, err := randomSerial()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if validity <= 0 || validity > caValidity {
|
||||
validity = 365 * 24 * time.Hour
|
||||
}
|
||||
if na := ca.Cert.NotAfter; time.Now().Add(validity).After(na) {
|
||||
validity = time.Until(na)
|
||||
}
|
||||
tmpl := &x509.Certificate{SerialNumber: serial, Subject: pkix.Name{CommonName: cn, Organization: []string{"rollcall"}},
|
||||
NotBefore: time.Now().Add(-backdate), NotAfter: time.Now().Add(validity),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, BasicConstraintsValid: true, IsCA: true, MaxPathLenZero: true,
|
||||
PermittedDNSDomainsCritical: len(dns)+len(nets) > 0, PermittedDNSDomains: dns, PermittedIPRanges: nets}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.Cert, pub, ca.Key)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cert, err := x509.ParseCertificate(der)
|
||||
return pemEncode("CERTIFICATE", der), cert, err
|
||||
}
|
||||
|
||||
// issue makes a key and a certificate for it.
|
||||
func (ca *CA) issue(cn string, hosts []string, usage ...x509.ExtKeyUsage) (certPEM, keyPEM []byte, err error) {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
@@ -569,7 +622,12 @@ func caCmd(args []string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if args[0] != "issue" || len(args) < 2 {
|
||||
switch {
|
||||
case args[0] == "intermediate" && len(args) >= 2:
|
||||
return caIntermediateCmd(cfg, args[1], args[2:])
|
||||
case args[0] == "rotate":
|
||||
return caRotateCmd(cfg, args[1:])
|
||||
case args[0] != "issue" || len(args) < 2:
|
||||
usage()
|
||||
}
|
||||
fs := newFlags("ca issue")
|
||||
@@ -604,3 +662,126 @@ func caCmd(args []string) error {
|
||||
fmt.Printf("wrote %s.pem (with the CA) and %s-key.pem in %s, valid until %s\n", args[1], args[1], *out, time.Now().Add(leafValidity).Format("2006-01-02"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollcall ca intermediate NAME -permit DOMAIN,CIDR [-csr FILE] [-days N] [-out DIR]
|
||||
func caIntermediateCmd(cfg *Config, name string, args []string) error {
|
||||
fs := newFlags("ca intermediate")
|
||||
permit := fs.String("permit", "", "comma-separated DNS domains and CIDRs it may issue for (required)")
|
||||
csrFile := fs.String("csr", "", "sign this CSR (the key stays where it was made); without it a key is generated")
|
||||
days := fs.Int("days", 365, "validity in days")
|
||||
out := fs.String("out", ".", "directory for NAME.pem (+ NAME-key.pem without -csr)")
|
||||
unconstrained := fs.Bool("unconstrained", false, "allow an intermediate without name constraints")
|
||||
_ = fs.Parse(args)
|
||||
dns, nets, err := parsePermits(*permit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dns)+len(nets) == 0 && !*unconstrained {
|
||||
return errors.New("an intermediate needs -permit (e.g. -permit int.example.com,10.213.0.0/16); -unconstrained to override")
|
||||
}
|
||||
ca, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var pub crypto.PublicKey
|
||||
var keyPEM []byte
|
||||
if *csrFile != "" {
|
||||
b, err := os.ReadFile(*csrFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
blk, _ := pem.Decode(b)
|
||||
if blk == nil {
|
||||
return fmt.Errorf("%s: not PEM", *csrFile)
|
||||
}
|
||||
csr, err := x509.ParseCertificateRequest(blk.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := csr.CheckSignature(); err != nil {
|
||||
return fmt.Errorf("CSR signature: %w", err)
|
||||
}
|
||||
pub = csr.PublicKey
|
||||
} else {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pub = &key.PublicKey
|
||||
kder, _ := x509.MarshalECPrivateKey(key)
|
||||
keyPEM = pemEncode("EC PRIVATE KEY", kder)
|
||||
}
|
||||
certPEM, cert, err := ca.intermediate(pub, name, dns, nets, time.Duration(*days)*24*time.Hour)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if keyPEM != nil {
|
||||
if err := os.WriteFile(filepath.Join(*out, name+"-key.pem"), keyPEM, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(*out, name+".pem"), certPEM, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
var permits []string
|
||||
permits = append(permits, dns...)
|
||||
for _, n := range nets {
|
||||
permits = append(permits, n.String())
|
||||
}
|
||||
fmt.Printf("wrote %s.pem in %s: intermediate CA %q until %s, may issue for %s\nroot: %s (%s)\n", name, *out, name,
|
||||
cert.NotAfter.Format("2006-01-02"), orDash(strings.Join(permits, " ")), filepath.Join(cfg.CADir, "ca.pem"), ca.Fingerprint())
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollcall ca rotate -yes: a new root (the old one is kept as a backup). Racks
|
||||
// must join again and clients trust the new fingerprint.
|
||||
func caRotateCmd(cfg *Config, args []string) error {
|
||||
fs := newFlags("ca rotate")
|
||||
yes := fs.Bool("yes", false, "really replace the root")
|
||||
_ = fs.Parse(args)
|
||||
old, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !*yes {
|
||||
return fmt.Errorf("this replaces the root %s (delegates: %v); rack controllers then join again and clients trust the new one. Run with -yes", old.Fingerprint(), old.canDelegate())
|
||||
}
|
||||
bak := cfg.CADir + ".bak-" + time.Now().Format("20060102-150405")
|
||||
if err := os.Rename(cfg.CADir, bak); err != nil {
|
||||
return err
|
||||
}
|
||||
ca, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
_ = os.Rename(bak, cfg.CADir)
|
||||
return err
|
||||
}
|
||||
if st, err := os.Stat(bak); err == nil {
|
||||
_ = os.Chmod(cfg.CADir, st.Mode().Perm())
|
||||
if own, ok := fileOwner(bak); ok {
|
||||
_ = chownTree(cfg.CADir, own[0], own[1])
|
||||
}
|
||||
}
|
||||
fmt.Printf("new root %s\nold root kept in %s\nnext: restart rollcall; on each rack controller `rollcall rack join` with a new join code; clients trust the new fingerprint\n", ca.Fingerprint(), bak)
|
||||
return nil
|
||||
}
|
||||
|
||||
// fileOwner returns uid, gid of path (unix).
|
||||
func fileOwner(path string) ([2]int, bool) {
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return [2]int{}, false
|
||||
}
|
||||
if s, ok := st.Sys().(*syscall.Stat_t); ok {
|
||||
return [2]int{int(s.Uid), int(s.Gid)}, true
|
||||
}
|
||||
return [2]int{}, false
|
||||
}
|
||||
|
||||
func chownTree(root string, uid, gid int) error {
|
||||
return filepath.Walk(root, func(p string, _ os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Lchown(p, uid, gid)
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
@@ -101,3 +104,64 @@ func TestSignRackCSR(t *testing.T) {
|
||||
t.Errorf("subject %s, SANs %v %v", cert.Subject.CommonName, cert.IPAddresses, cert.DNSNames)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntermediate(t *testing.T) {
|
||||
root, err := newCA("test root")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !root.canDelegate() {
|
||||
t.Fatal("new roots must be able to delegate one level")
|
||||
}
|
||||
dns, nets, err := parsePermits("int.example.com, 10.213.0.0/16")
|
||||
if err != nil || len(dns) != 1 || len(nets) != 1 {
|
||||
t.Fatalf("permits: %v %v %v", dns, nets, err)
|
||||
}
|
||||
ikey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
_, icert, err := root.intermediate(&ikey.PublicKey, "caddy", dns, nets, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !icert.IsCA || !icert.MaxPathLenZero || !icert.PermittedDNSDomainsCritical {
|
||||
t.Fatalf("intermediate shape: %+v", icert)
|
||||
}
|
||||
sub := &CA{Cert: icert, Key: ikey}
|
||||
roots, inters := x509.NewCertPool(), x509.NewCertPool()
|
||||
roots.AddCert(root.Cert)
|
||||
inters.AddCert(icert)
|
||||
verify := func(hosts ...string) error {
|
||||
_, leaf, err := sub.sign(&ikey.PublicKey, hosts[0], hosts, x509.ExtKeyUsageServerAuth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = leaf.Verify(x509.VerifyOptions{Roots: roots, Intermediates: inters, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}})
|
||||
return err
|
||||
}
|
||||
if err := verify("node001.int.example.com", "10.213.77.50"); err != nil {
|
||||
t.Errorf("leaf inside the constraints: %v", err)
|
||||
}
|
||||
if err := verify("www.google.com"); err == nil {
|
||||
t.Error("leaf outside the DNS constraint verified")
|
||||
}
|
||||
if err := verify("node001.int.example.com", "192.168.1.1"); err == nil {
|
||||
t.Error("leaf outside the IP constraint verified")
|
||||
}
|
||||
// a second level below the intermediate is refused by path length 0
|
||||
k2, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
_, c2, err := sub.intermediate(&k2.PublicKey, "deeper", dns, nil, time.Hour)
|
||||
if err == nil {
|
||||
inters.AddCert(c2)
|
||||
_, leaf, _ := (&CA{Cert: c2, Key: k2}).sign(&k2.PublicKey, "x.int.example.com", []string{"x.int.example.com"}, x509.ExtKeyUsageServerAuth)
|
||||
if _, err := leaf.Verify(x509.VerifyOptions{Roots: roots, Intermediates: inters}); err == nil {
|
||||
t.Error("two levels of intermediates verified")
|
||||
}
|
||||
}
|
||||
// old roots (path length 0) refuse to delegate
|
||||
old := *root
|
||||
oc := *root.Cert
|
||||
oc.MaxPathLen, oc.MaxPathLenZero = 0, true
|
||||
old.Cert = &oc
|
||||
if _, _, err := old.intermediate(&ikey.PublicKey, "x", dns, nil, time.Hour); err == nil {
|
||||
t.Error("path-length-0 root signed an intermediate")
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func usage() {
|
||||
rollcall rack [-config /etc/rollcall/rack.json] a rack controller next to the machines
|
||||
rollcall rack join CODE -url http://ADDR [-name N] [-listen ADDR:80] enroll a rack controller (writes rack.json + certificate)
|
||||
rollcall ca show | issue NAME -host h1,h2 [-client] [-out DIR] the installation's certificate authority (region host)
|
||||
rollcall ca intermediate NAME -permit DOMAIN,CIDR [-csr FILE] [-days 365] | rotate -yes delegate to an ACME CA / replace the root
|
||||
rollcall version
|
||||
rollcall keygen
|
||||
rollcall list
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#cloud-config
|
||||
# Rollcall tag fragment: a TLS certificate for the node from the site's ACME CA
|
||||
# (an ACME server under a Rollcall intermediate, see README "Delegation"), renewed automatically.
|
||||
# Register with: rollcall tags add acme-cert -cloud-config deploy/tags/acme-cert.yaml
|
||||
# Edit the two lines in /etc/rollcall-acme.conf for your site. Deployments already trust the
|
||||
# Rollcall CA (cloud-init ca_certs), so certbot can talk to the ACME server over TLS.
|
||||
# Result: /etc/letsencrypt/live/<fqdn>/{fullchain,privkey}.pem; log /var/log/rollcall-acme.log.
|
||||
write_files:
|
||||
- path: /etc/rollcall-acme.conf
|
||||
permissions: "0644"
|
||||
content: |
|
||||
ACME_DIRECTORY=https://acme.int.mynodes.xyz/acme/rollcall/directory
|
||||
DOMAIN=int.mynodes.xyz
|
||||
- path: /usr/local/sbin/rollcall-acme-cert
|
||||
permissions: "0755"
|
||||
content: |
|
||||
#!/bin/sh
|
||||
# Gets <host>.<DOMAIN> from the ACME server (http-01 on port 80) and sets up renewal.
|
||||
set -eu
|
||||
exec >>/var/log/rollcall-acme.log 2>&1
|
||||
. /etc/rollcall-acme.conf
|
||||
. /etc/os-release
|
||||
fqdn="$(hostname -s).$DOMAIN"
|
||||
echo "== $(date -Is) certificate for $fqdn from $ACME_DIRECTORY"
|
||||
case "$ID" in
|
||||
debian|ubuntu) bundle=/etc/ssl/certs/ca-certificates.crt
|
||||
command -v certbot >/dev/null || { apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install -y certbot; } ;;
|
||||
rocky|almalinux|centos|rhel) bundle=/etc/pki/tls/certs/ca-bundle.crt
|
||||
command -v certbot >/dev/null || { dnf -y install epel-release || true; dnf -y install certbot; } ;;
|
||||
*) echo "unsupported distro $ID"; exit 1 ;;
|
||||
esac
|
||||
export REQUESTS_CA_BUNDLE="$bundle"
|
||||
# renewals run from certbot's own timer; it needs the same trust and server
|
||||
for unit in certbot.service certbot-renew.service; do
|
||||
mkdir -p "/etc/systemd/system/$unit.d"
|
||||
printf '[Service]\nEnvironment=REQUESTS_CA_BUNDLE=%s\n' "$bundle" > "/etc/systemd/system/$unit.d/rollcall-acme.conf"
|
||||
done
|
||||
systemctl daemon-reload
|
||||
certbot certonly --standalone --non-interactive --agree-tos --register-unsafely-without-email \
|
||||
--server "$ACME_DIRECTORY" --cert-name "$fqdn" -d "$fqdn" --keep-until-expiring
|
||||
# short-lived certificates: renew when a third of the lifetime is left, not 30 days before
|
||||
sed -i 's/^#\? *renew_before_expiry.*/renew_before_expiry = 2 days/' "/etc/letsencrypt/renewal/$fqdn.conf"
|
||||
grep -q '^renew_before_expiry' "/etc/letsencrypt/renewal/$fqdn.conf" || echo 'renew_before_expiry = 2 days' >> "/etc/letsencrypt/renewal/$fqdn.conf"
|
||||
systemctl enable --now certbot.timer 2>/dev/null || systemctl enable --now certbot-renew.timer
|
||||
openssl x509 -in "/etc/letsencrypt/live/$fqdn/cert.pem" -noout -subject -issuer -enddate
|
||||
runcmd:
|
||||
- [/usr/local/sbin/rollcall-acme-cert]
|
||||
Reference in new issue
Block a user