The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
170 lines
4.7 KiB
Go
170 lines
4.7 KiB
Go
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package certmanager
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.gearno.de/kit/pg"
|
|
"go.probo.inc/probo/pkg/coredata"
|
|
"go.probo.inc/probo/pkg/crypto/cipher"
|
|
)
|
|
|
|
type (
|
|
Selector struct {
|
|
pg *pg.Client
|
|
cache sync.Map
|
|
encryptionKey cipher.EncryptionKey
|
|
}
|
|
|
|
// NoSNIError is returned when a TLS client doesn't provide SNI (Server Name Indication)
|
|
NoSNIError struct{}
|
|
)
|
|
|
|
func (e *NoSNIError) Error() string {
|
|
return "no SNI provided"
|
|
}
|
|
|
|
func NewSelector(
|
|
pg *pg.Client,
|
|
encryptionKey cipher.EncryptionKey,
|
|
) *Selector {
|
|
return &Selector{
|
|
pg: pg,
|
|
encryptionKey: encryptionKey,
|
|
}
|
|
}
|
|
|
|
func (s *Selector) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
domain := hello.ServerName
|
|
|
|
// Empty domain, return error
|
|
if domain == "" {
|
|
return nil, &NoSNIError{}
|
|
}
|
|
|
|
if cached, ok := s.cache.Load(domain); ok {
|
|
if cert, ok := cached.(*tls.Certificate); ok {
|
|
return cert, nil
|
|
}
|
|
}
|
|
|
|
cert, err := s.loadFromDatabase(domain)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot load certificate from database: %w", err)
|
|
}
|
|
|
|
s.cache.Store(domain, cert)
|
|
|
|
return cert, nil
|
|
}
|
|
|
|
func (s *Selector) loadFromDatabase(domain string) (*tls.Certificate, error) {
|
|
ctx := context.Background()
|
|
|
|
var cert *tls.Certificate
|
|
|
|
err := s.pg.WithConn(
|
|
ctx,
|
|
func(ctx context.Context, conn pg.Querier) error {
|
|
var cache coredata.CachedCertificate
|
|
if err := cache.LoadByDomain(ctx, conn, domain); err != nil {
|
|
if err := s.rebuildCacheEntry(ctx, conn, domain); err != nil {
|
|
return fmt.Errorf("cannot rebuild cache entry: %w", err)
|
|
}
|
|
|
|
if err := cache.LoadByDomain(ctx, conn, domain); err != nil {
|
|
return fmt.Errorf("cannot load certificate from cache after rebuild: %w", err)
|
|
}
|
|
}
|
|
|
|
fullCertPEM := cache.CertificatePEM
|
|
if cache.CertificateChain != nil {
|
|
fullCertPEM += "\n" + *cache.CertificateChain
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(fullCertPEM), []byte(cache.PrivateKeyPEM))
|
|
if err != nil {
|
|
return fmt.Errorf("cannot parse certificate: %w", err)
|
|
}
|
|
|
|
cert = &tlsCert
|
|
|
|
return nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cert, nil
|
|
}
|
|
|
|
func (s *Selector) rebuildCacheEntry(ctx context.Context, conn pg.Querier, domain string) error {
|
|
var customDomain coredata.CustomDomain
|
|
if err := customDomain.LoadByDomain(ctx, conn, coredata.NewNoScope(), domain); err != nil {
|
|
return fmt.Errorf("cannot load domain: %w", err)
|
|
}
|
|
|
|
if customDomain.SSLStatus != coredata.CustomDomainSSLStatusActive {
|
|
return fmt.Errorf("domain does not have active SSL certificate")
|
|
}
|
|
|
|
if err := customDomain.ParseCertificate(s.encryptionKey); err != nil {
|
|
return fmt.Errorf("cannot parse certificate: %w", err)
|
|
}
|
|
|
|
if len(customDomain.SSLCertificatePEM) == 0 {
|
|
return fmt.Errorf("domain has no certificate PEM data")
|
|
}
|
|
|
|
if len(customDomain.EncryptedSSLPrivateKey) == 0 {
|
|
return fmt.Errorf("domain has no encrypted private key data")
|
|
}
|
|
|
|
privateKeyPEM, err := customDomain.DecryptPrivateKey(s.encryptionKey)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot decrypt private key: %w", err)
|
|
}
|
|
|
|
s.cache.Store(domain, customDomain.SSLCertificate)
|
|
|
|
cache := &coredata.CachedCertificate{
|
|
Domain: customDomain.Domain,
|
|
CertificatePEM: string(customDomain.SSLCertificatePEM),
|
|
PrivateKeyPEM: string(privateKeyPEM),
|
|
CertificateChain: customDomain.SSLCertificateChain,
|
|
ExpiresAt: *customDomain.SSLExpiresAt,
|
|
CachedAt: time.Now(),
|
|
CustomDomainID: customDomain.ID,
|
|
}
|
|
|
|
if err := cache.Upsert(ctx, conn); err != nil {
|
|
return fmt.Errorf("cannot insert cache entry: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|