This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
summer-utils/tables/certificate/certificate-detail.go
2023-04-16 11:56:17 +01:00

52 lines
1.2 KiB
Go

package certificate
import (
"code.mrmelon54.com/melon/certgen"
"code.mrmelon54.com/melon/summer/pkg/utils"
"crypto/tls"
"github.com/pkg/errors"
"time"
)
type CertificateData struct {
DataId uint64 `xorm:"pk autoincr"`
MetaId uint64
PreviousCert uint64
CertificateBytes []byte
KeyBytes []byte
NotAfter time.Time
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
Ready *bool
}
func (c *CertificateData) SortOutDefaultValues() {
if c.CertificateBytes == nil {
c.CertificateBytes = make([]byte, 0)
}
if c.KeyBytes == nil {
c.KeyBytes = make([]byte, 0)
}
if c.Ready == nil {
c.Ready = utils.PBool(false)
}
}
func (c *CertificateData) GetCertificate() (*tls.Certificate, error) {
cert, err := tls.X509KeyPair(c.CertificateBytes, c.KeyBytes)
return &cert, err
}
func (c *CertificateData) UpdateNotAfter() error {
certBase, err := c.GetCertificate()
if err != nil {
return errors.Errorf("Invalid certificate: %s", err)
}
certLeaf := certgen.TlsLeaf(certBase)
if certLeaf == nil {
return errors.Errorf("Failed to get leaf for certificate")
}
c.NotAfter = certLeaf.NotAfter
return nil
}