An installation used to be this one: the deploy scripts and the Ansible defaults carried one site's domain, hypervisor and login, the Proxmox side and the database were set up by hand, and deploying meant copying scripts and a tree of roles beside the binary and keeping them in step with it. The binary now carries the roles, the Terraform module, the schema and the catalogue (embed.go), and installs itself: - hpcod setup: a full-screen installer for administrators. It asks where clusters are built, signs in to Proxmox before asking anything else (root's password once, or tokens), offers nodes and datastores from what Proxmox has, checks every field as it is typed, runs every step with its output live, checks the result and offers a test cluster. -yes runs the same steps from a site file; make deploy uses that. - hpcod verify: each fact checked by using it. - hpcod selftest: the smallest real cluster, by the portal's own path, a job run on it, then taken down. - Closed networks: hpcod serves HTTPS itself from a CA name-constrained to the site's domains, issuing and renewing its own certificate, and answers DNS for the portal and every cluster name. - A simulated Proxmox: Proxmox VE in QEMU on the control plane host, on a private bridge with NAT, for trying it without one. Every step reads back what it changed and is safe to run again. The site's values live in one file; group_vars/all/site.yml is generated from it, and the playbooks refuse to run without it. ci_user and -ssh-user no longer default to a personal account. scripts/B10, B20 and C20 are gone: their work is the setup steps. Proven end to end on a simulated Proxmox (Proxmox VE 9 in a VM on a real host): the install, the Proxmox step by the password path, the head template, verify READY, and a test cluster that ran srun on its compute node and was destroyed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
6.4 KiB
Go
226 lines
6.4 KiB
Go
// Package pki is the certificate authority of an installation that has no
|
|
// public domain: one that runs on a closed network, under names like
|
|
// hpc.lab, where no public CA will issue a certificate.
|
|
//
|
|
// The CA is created once, by setup. It is name-constrained to the
|
|
// installation's own domains, so a user who trusts it has trusted nothing
|
|
// else: it cannot sign for their bank, whoever holds its key. The portal
|
|
// holds the key and issues its own server certificate from it, in memory,
|
|
// renewing it well before it expires - a certificate written to disk once
|
|
// is one that expires a year later on a weekend.
|
|
package pki
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// LeafLifetime is how long a server certificate lasts. Under 398 days,
|
|
// which is the most some browsers accept for any server certificate,
|
|
// private CA or not.
|
|
const LeafLifetime = 397 * 24 * time.Hour
|
|
|
|
// renewBefore is when a leaf is replaced.
|
|
const renewBefore = 30 * 24 * time.Hour
|
|
|
|
// NewCA makes a CA constrained to the given domains (each covers itself
|
|
// and every name under it). It returns the certificate and key as PEM.
|
|
func NewCA(domains []string) (certPEM, keyPEM []byte, err error) {
|
|
domains = Zones(domains)
|
|
if len(domains) == 0 {
|
|
return nil, nil, errors.New("a CA needs at least one domain to be constrained to")
|
|
}
|
|
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: serial(),
|
|
Subject: pkix.Name{CommonName: "hpcod CA for " + strings.Join(domains, ", "), Organization: []string{"hpcod"}},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().AddDate(10, 0, 0),
|
|
IsCA: true,
|
|
BasicConstraintsValid: true,
|
|
MaxPathLenZero: true,
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
PermittedDNSDomainsCritical: true,
|
|
PermittedDNSDomains: domains,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &k.PublicKey, k)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
kder, err := x509.MarshalPKCS8PrivateKey(k)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}),
|
|
pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: kder}), nil
|
|
}
|
|
|
|
// Zones reduces a list of domains to the ones not already under another,
|
|
// lower-cased and sorted: {"hpc.lab", "portal.hpc.lab"} is {"hpc.lab"}.
|
|
func Zones(domains []string) []string {
|
|
var out []string
|
|
for _, d := range domains {
|
|
d = strings.ToLower(strings.Trim(strings.TrimSpace(d), "."))
|
|
if d == "" {
|
|
continue
|
|
}
|
|
covered := false
|
|
for _, o := range domains {
|
|
o = strings.ToLower(strings.Trim(strings.TrimSpace(o), "."))
|
|
if o != d && strings.HasSuffix(d, "."+o) {
|
|
covered = true
|
|
}
|
|
}
|
|
if !covered && !contains(out, d) {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// Covers reports whether a CA's constraints cover every domain - whether
|
|
// it can still sign for a site whose domains may have changed.
|
|
func Covers(certPEM []byte, domains []string) (bool, error) {
|
|
c, err := parseCert(certPEM)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, d := range domains {
|
|
d = strings.ToLower(strings.Trim(d, "."))
|
|
ok := false
|
|
for _, p := range c.PermittedDNSDomains {
|
|
if d == p || strings.HasSuffix(d, "."+p) {
|
|
ok = true
|
|
}
|
|
}
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// Issuer signs the portal's server certificate and keeps it current.
|
|
type Issuer struct {
|
|
ca *x509.Certificate
|
|
key crypto.Signer
|
|
names []string
|
|
|
|
mu sync.Mutex
|
|
leaf *tls.Certificate
|
|
now func() time.Time
|
|
}
|
|
|
|
// NewIssuer loads a CA and the names the server answers to. A wildcard is
|
|
// written "*.hpc.lab".
|
|
func NewIssuer(certPEM, keyPEM []byte, names []string) (*Issuer, error) {
|
|
c, err := parseCert(certPEM)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b, _ := pem.Decode(keyPEM)
|
|
if b == nil {
|
|
return nil, errors.New("CA key is not PEM")
|
|
}
|
|
k, err := x509.ParsePKCS8PrivateKey(b.Bytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("CA key: %w", err)
|
|
}
|
|
signer, ok := k.(crypto.Signer)
|
|
if !ok {
|
|
return nil, errors.New("CA key cannot sign")
|
|
}
|
|
is := &Issuer{ca: c, key: signer, names: names, now: time.Now}
|
|
// Issued now, so a CA that cannot sign for these names fails the
|
|
// portal at start rather than the first browser that connects.
|
|
if _, err := is.current(); err != nil {
|
|
return nil, err
|
|
}
|
|
return is, nil
|
|
}
|
|
|
|
// GetCertificate is for tls.Config.
|
|
func (is *Issuer) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
return is.current()
|
|
}
|
|
|
|
func (is *Issuer) current() (*tls.Certificate, error) {
|
|
is.mu.Lock()
|
|
defer is.mu.Unlock()
|
|
if is.leaf != nil && is.leaf.Leaf.NotAfter.Sub(is.now()) > renewBefore {
|
|
return is.leaf, nil
|
|
}
|
|
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
now := is.now()
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: serial(),
|
|
Subject: pkix.Name{CommonName: is.names[0]},
|
|
DNSNames: is.names,
|
|
NotBefore: now.Add(-time.Hour),
|
|
NotAfter: now.Add(LeafLifetime),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, is.ca, &k.PublicKey, is.key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
leaf, err := x509.ParseCertificate(der)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Checked the way a browser will: against the CA, for every name.
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(is.ca)
|
|
for _, n := range is.names {
|
|
host := strings.Replace(n, "*", "x", 1)
|
|
if _, err := leaf.Verify(x509.VerifyOptions{Roots: pool, DNSName: host, CurrentTime: now}); err != nil {
|
|
return nil, fmt.Errorf("the CA cannot vouch for %s: %w", n, err)
|
|
}
|
|
}
|
|
is.leaf = &tls.Certificate{Certificate: [][]byte{der, is.ca.Raw}, PrivateKey: k, Leaf: leaf}
|
|
return is.leaf, nil
|
|
}
|
|
|
|
func parseCert(p []byte) (*x509.Certificate, error) {
|
|
b, _ := pem.Decode(p)
|
|
if b == nil {
|
|
return nil, errors.New("not a PEM certificate")
|
|
}
|
|
return x509.ParseCertificate(b.Bytes)
|
|
}
|
|
|
|
func serial() *big.Int {
|
|
n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 120))
|
|
return n
|
|
}
|
|
|
|
func contains(l []string, s string) bool {
|
|
for _, x := range l {
|
|
if x == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|