This commit is contained in:
6543 2023-02-09 19:14:53 +01:00
parent 75942990ac
commit 3c0ee7e8a3
9 changed files with 192 additions and 70 deletions

View file

@ -3,6 +3,7 @@ package database
import (
"errors"
"fmt"
"strings"
"github.com/rs/zerolog/log"
@ -27,6 +28,9 @@ func NewXormDB(dbType, dbConn string) (CertDB, error) {
if !supportedDriver(dbType) {
return nil, fmt.Errorf("not supported db type '%s'", dbType)
}
if dbConn == "" {
return nil, fmt.Errorf("no db connection provided")
}
e, err := xorm.NewEngine(dbType, dbConn)
if err != nil {
@ -46,31 +50,35 @@ func (x xDB) Close() error {
return x.engine.Close()
}
func (x xDB) Put(name string, cert *certificate.Resource) error {
log.Trace().Str("name", name).Msg("inserting cert to db")
c, err := toCert(name, cert)
func (x xDB) Put(domain string, cert *certificate.Resource) error {
log.Trace().Str("domain", cert.Domain).Msg("inserting cert to db")
c, err := toCert(domain, cert)
if err != nil {
return err
}
_, err = x.engine.Insert(c)
return err
}
func (x xDB) Get(name string) (*certificate.Resource, error) {
func (x xDB) Get(domain string) (*certificate.Resource, error) {
// TODO: do we need this or can we just go with domain name for wildcard cert
domain = strings.TrimPrefix(domain, ".")
cert := new(Cert)
log.Trace().Str("name", name).Msg("get cert from db")
if _, err := x.engine.ID(name).Get(&cert); err != nil {
log.Trace().Str("domain", domain).Msg("get cert from db")
if _, err := x.engine.ID(domain).Get(&cert); err != nil {
return nil, err
}
if cert == nil {
return nil, fmt.Errorf("%w: name='%s'", ErrNotFound, name)
return nil, fmt.Errorf("%w: name='%s'", ErrNotFound, domain)
}
return cert.Raw(), nil
}
func (x xDB) Delete(name string) error {
log.Trace().Str("name", name).Msg("delete cert from db")
_, err := x.engine.ID(name).Delete(new(Cert))
func (x xDB) Delete(domain string) error {
log.Trace().Str("domain", domain).Msg("delete cert from db")
_, err := x.engine.ID(domain).Delete(new(Cert))
return err
}
@ -93,7 +101,8 @@ func (x xDB) Items(page, pageSize int) ([]*Cert, error) {
// return all
certs := make([]*Cert, 0, 64)
return certs, x.engine.Find(&certs)
err := x.engine.Find(&certs)
return certs, err
}
// Supported database drivers