| 1 | package edge |
| 2 | |
| 3 | // Ported from titogo internal/carlos/certcache.go (itself adapted from |
| 4 | // Eleven's certcache.go): the certificate-cache seam. Edge boxes in a pool |
| 5 | // must eventually SHARE issued certs, so every issuance artifact goes through |
| 6 | // one autocert.Cache chosen here, never direct file I/O. autocert already |
| 7 | // defines exactly the interface a remote backend needs (Get/Put/Delete with |
| 8 | // ctx, ErrCacheMiss for absent), so there is no parallel abstraction: an S3 |
| 9 | // backend is a drop-in behind this constructor. |
| 10 | |
| 11 | import ( |
| 12 | "fmt" |
| 13 | "os" |
| 14 | "strings" |
| 15 | |
| 16 | "golang.org/x/crypto/acme/autocert" |
| 17 | ) |
| 18 | |
| 19 | // newCertCache returns the certificate cache selected by CARLOS_CERT_CACHE. |
| 20 | // Unset (or "dir") is the on-disk layout — autocert.DirCache over dir, cache |
| 21 | // keys as plain file names — so an existing cache keeps working over a |
| 22 | // restart with no migration. "s3" is reserved for a pooled-edge backend; |
| 23 | // until it exists it refuses to start, because failing loudly beats silently |
| 24 | // minting certs into a local dir the rest of a pool can't see. |
| 25 | // |
| 26 | // Sharing arrived as a WRAPPER around what this returns, not as a backend |
| 27 | // selected here: certsync.go's publishingCache keeps the local cache as the |
| 28 | // durable copy and publishes every entry to the control bucket under |
| 29 | // control/certs/, which is what the multi-region spec's "one issuer, every |
| 30 | // edge serves" needs — a pure remote backend would put a network round trip |
| 31 | // on the issuance path and leave a box with nothing local when the bucket |
| 32 | // blips. "s3" therefore stays reserved and still refuses. |
| 33 | func newCertCache(dir string) (autocert.Cache, error) { |
| 34 | switch backend := strings.TrimSpace(os.Getenv("CARLOS_CERT_CACHE")); backend { |
| 35 | case "", "dir": |
| 36 | return autocert.DirCache(dir), nil |
| 37 | case "s3": |
| 38 | return nil, fmt.Errorf("CARLOS_CERT_CACHE=s3: reserved, not implemented yet") |
| 39 | default: |
| 40 | return nil, fmt.Errorf("CARLOS_CERT_CACHE=%q: want unset, \"dir\", or \"s3\"", backend) |
| 41 | } |
| 42 | } |
| 43 | |