pages-server/server/database/interface.go

73 lines
2.2 KiB
Go
Raw Normal View History

2021-12-03 04:15:48 +01:00
package database
2021-12-05 19:00:57 +01:00
import (
2023-02-09 17:52:30 +01:00
"fmt"
"github.com/go-acme/lego/v4/certcrypto"
2021-12-05 19:00:57 +01:00
"github.com/go-acme/lego/v4/certificate"
2023-02-09 17:52:30 +01:00
"github.com/rs/zerolog/log"
2021-12-05 19:00:57 +01:00
)
2021-12-03 04:15:48 +01:00
2021-12-05 17:42:53 +01:00
type CertDB interface {
Close() error
2021-12-05 19:00:57 +01:00
Put(name string, cert *certificate.Resource) error
Get(name string) (*certificate.Resource, error)
Delete(key string) error
2023-02-09 17:52:30 +01:00
Items(page, pageSize int) ([]*Cert, error)
// Compact deprecated // TODO: remove in next version
Compact() (string, error)
2021-12-03 04:15:48 +01:00
}
2023-02-09 15:19:16 +01:00
type Cert struct {
2023-02-09 17:52:30 +01:00
Name string `xorm:"pk NOT NULL 'name'"`
Domain string `xorm:" NOT NULL UNIQUE 'domain'"` // TODO: check: is name always same as domain?
Created int64 `xorm:"created NOT NULL DEFAULT 0 'created'"`
Updated int64 `xorm:"updated NOT NULL DEFAULT 0 'updated'"`
ValidTill int64 `xorm:" NOT NULL DEFAULT 0 'valid_till'"`
// certificate.Resource
certURL string `xorm:"'cert_url'"`
certStableURL string `xorm:"'cert_stable_url''"`
privateKey []byte `xorm:"'private_key'"`
certificate []byte `xorm:"'certificate'"`
issuerCertificate []byte `xorm:"'issuer_certificate'"` // TODO: dedup ?
csr []byte `xorm:"'csr'"`
}
func (c Cert) Raw() *certificate.Resource {
return &certificate.Resource{
Domain: c.Domain,
CertURL: c.certURL,
CertStableURL: c.certStableURL,
PrivateKey: c.privateKey,
Certificate: c.certificate,
IssuerCertificate: c.issuerCertificate,
CSR: c.csr,
}
}
func toCert(name string, c *certificate.Resource) (*Cert, error) {
tlsCertificates, err := certcrypto.ParsePEMBundle(c.Certificate)
if err != nil {
return nil, err
}
if len(tlsCertificates) != 1 || tlsCertificates[0] == nil {
err := fmt.Errorf("parsed cert resource has no or more than one cert")
log.Error().Err(err).Str("name", name).Msgf("cert: %v", c)
return nil, err
}
validTill := tlsCertificates[0].NotAfter.Unix()
return &Cert{
Name: name,
Domain: c.Domain,
ValidTill: validTill,
certURL: c.CertURL,
certStableURL: c.CertStableURL,
privateKey: c.PrivateKey,
certificate: c.Certificate,
issuerCertificate: c.IssuerCertificate,
csr: c.CSR,
}, nil
2023-02-09 15:19:16 +01:00
}