mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
make db interface more generic and add memdb
This commit is contained in:
parent
e5af66b2cd
commit
bc3d3befee
5 changed files with 78 additions and 16 deletions
|
@ -61,7 +61,7 @@ func removeCert(ctx *cli.Context) error {
|
||||||
|
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
fmt.Printf("Removing domain %s from the database...\n", domain)
|
fmt.Printf("Removing domain %s from the database...\n", domain)
|
||||||
if err := keyDatabase.Delete([]byte(domain)); err != nil {
|
if err := keyDatabase.Delete(domain); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,7 +193,7 @@ func (a AcmeHTTPChallengeProvider) CleanUp(domain, token, _ string) error {
|
||||||
|
|
||||||
func retrieveCertFromDB(sni, mainDomainSuffix []byte, dnsProvider string, acmeUseRateLimits bool, certDB database.CertDB) (tls.Certificate, bool) {
|
func retrieveCertFromDB(sni, mainDomainSuffix []byte, dnsProvider string, acmeUseRateLimits bool, certDB database.CertDB) (tls.Certificate, bool) {
|
||||||
// parse certificate from database
|
// parse certificate from database
|
||||||
res, err := certDB.Get(sni)
|
res, err := certDB.Get(string(sni))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) // TODO: no panic
|
panic(err) // TODO: no panic
|
||||||
}
|
}
|
||||||
|
@ -406,7 +406,7 @@ func SetupAcmeConfig(acmeAPI, acmeMail, acmeEabHmac, acmeEabKID string, acmeAcce
|
||||||
|
|
||||||
func SetupCertificates(mainDomainSuffix []byte, dnsProvider string, acmeConfig *lego.Config, acmeUseRateLimits, enableHTTPServer bool, challengeCache cache.SetGetKey, certDB database.CertDB) error {
|
func SetupCertificates(mainDomainSuffix []byte, dnsProvider string, acmeConfig *lego.Config, acmeUseRateLimits, enableHTTPServer bool, challengeCache cache.SetGetKey, certDB database.CertDB) error {
|
||||||
// getting main cert before ACME account so that we can fail here without hitting rate limits
|
// getting main cert before ACME account so that we can fail here without hitting rate limits
|
||||||
mainCertBytes, err := certDB.Get(mainDomainSuffix)
|
mainCertBytes, err := certDB.Get(string(mainDomainSuffix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cert database is not working")
|
return fmt.Errorf("cert database is not working")
|
||||||
}
|
}
|
||||||
|
@ -478,7 +478,7 @@ func MaintainCertDB(ctx context.Context, interval time.Duration, mainDomainSuffi
|
||||||
|
|
||||||
tlsCertificates, err := certcrypto.ParsePEMBundle(res.Certificate)
|
tlsCertificates, err := certcrypto.ParsePEMBundle(res.Certificate)
|
||||||
if err != nil || !tlsCertificates[0].NotAfter.After(now) {
|
if err != nil || !tlsCertificates[0].NotAfter.After(now) {
|
||||||
err := certDB.Delete(key)
|
err := certDB.Delete(string(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] Deleting expired certificate for %s failed: %s", string(key), err)
|
log.Printf("[ERROR] Deleting expired certificate for %s failed: %s", string(key), err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -491,15 +491,15 @@ func MaintainCertDB(ctx context.Context, interval time.Duration, mainDomainSuffi
|
||||||
log.Printf("[INFO] Removed %d expired certificates from the database", expiredCertCount)
|
log.Printf("[INFO] Removed %d expired certificates from the database", expiredCertCount)
|
||||||
|
|
||||||
// compact the database
|
// compact the database
|
||||||
result, err := certDB.Compact()
|
msg, err := certDB.Compact()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] Compacting key database failed: %s", err)
|
log.Printf("[ERROR] Compacting key database failed: %s", err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[INFO] Compacted key database (%+v)", result)
|
log.Printf("[INFO] Compacted key database (%s)", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// update main cert
|
// update main cert
|
||||||
res, err := certDB.Get(mainDomainSuffix)
|
res, err := certDB.Get(string(mainDomainSuffix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msgf("could not get cert for domain '%s'", mainDomainSuffix)
|
log.Err(err).Msgf("could not get cert for domain '%s'", mainDomainSuffix)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
type CertDB interface {
|
type CertDB interface {
|
||||||
Close() error
|
Close() error
|
||||||
Put(name string, cert *certificate.Resource) error
|
Put(name string, cert *certificate.Resource) error
|
||||||
Get(name []byte) (*certificate.Resource, error)
|
Get(name string) (*certificate.Resource, error)
|
||||||
Delete(key []byte) error
|
Delete(key string) error
|
||||||
Compact() (pogreb.CompactionResult, error)
|
Compact() (string, error)
|
||||||
Items() *pogreb.ItemIterator
|
Items() *pogreb.ItemIterator
|
||||||
}
|
}
|
||||||
|
|
58
server/database/mock.go
Normal file
58
server/database/mock.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/OrlovEvgeny/go-mcache"
|
||||||
|
"github.com/akrylysov/pogreb"
|
||||||
|
"github.com/go-acme/lego/v4/certificate"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ CertDB = tmpDB{}
|
||||||
|
|
||||||
|
type tmpDB struct {
|
||||||
|
ctx context.Context
|
||||||
|
intern *mcache.CacheDriver
|
||||||
|
ttl time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Close() error {
|
||||||
|
_ = p.intern.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Put(name string, cert *certificate.Resource) error {
|
||||||
|
p.intern.Set(name, cert, p.ttl)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Get(name string) (*certificate.Resource, error) {
|
||||||
|
cert, has := p.intern.Get(name)
|
||||||
|
if !has {
|
||||||
|
return nil, fmt.Errorf("cert for '%s' not found", name)
|
||||||
|
}
|
||||||
|
return cert.(*certificate.Resource), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Delete(key string) error {
|
||||||
|
p.intern.Remove(key)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Compact() (string, error) {
|
||||||
|
p.intern.Truncate()
|
||||||
|
return "Turncate done", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p tmpDB) Items() *pogreb.ItemIterator {
|
||||||
|
panic("ItemIterator not implemented for tmpDB")
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTmpDB() (CertDB, error) {
|
||||||
|
return &tmpDB{
|
||||||
|
intern: mcache.New(),
|
||||||
|
ttl: time.Minute,
|
||||||
|
}, nil
|
||||||
|
}
|
|
@ -35,9 +35,9 @@ func (p aDB) Put(name string, cert *certificate.Resource) error {
|
||||||
return p.intern.Put([]byte(name), resGob.Bytes())
|
return p.intern.Put([]byte(name), resGob.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p aDB) Get(name []byte) (*certificate.Resource, error) {
|
func (p aDB) Get(name string) (*certificate.Resource, error) {
|
||||||
cert := &certificate.Resource{}
|
cert := &certificate.Resource{}
|
||||||
resBytes, err := p.intern.Get(name)
|
resBytes, err := p.intern.Get([]byte(name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -50,12 +50,16 @@ func (p aDB) Get(name []byte) (*certificate.Resource, error) {
|
||||||
return cert, nil
|
return cert, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p aDB) Delete(key []byte) error {
|
func (p aDB) Delete(key string) error {
|
||||||
return p.intern.Delete(key)
|
return p.intern.Delete([]byte(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p aDB) Compact() (pogreb.CompactionResult, error) {
|
func (p aDB) Compact() (string, error) {
|
||||||
return p.intern.Compact()
|
result, err := p.intern.Compact()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%+v", result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p aDB) Items() *pogreb.ItemIterator {
|
func (p aDB) Items() *pogreb.ItemIterator {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue