|
@@ -10,7 +10,7 @@ import (
|
|
func UpdateCert(certID int64, name string, subject string) error {
|
|
func UpdateCert(certID int64, name string, subject string) error {
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
var cert CertRecord
|
|
var cert CertRecord
|
|
- err := tx.Model(&CertRecord{}).Where("name = ?", name).First(&cert).Error
|
|
|
|
|
|
+ err := tx.Model(&CertRecord{}).Where("name = ?", name).Order("created_at desc").First(&cert).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
cert = CertRecord{
|
|
cert = CertRecord{
|
|
CertID: certID,
|
|
CertID: certID,
|
|
@@ -56,13 +56,13 @@ func UpdateCert(certID int64, name string, subject string) error {
|
|
func UpdateDomain(certID int64, name string, subject string, domain string) error {
|
|
func UpdateDomain(certID int64, name string, subject string, domain string) error {
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
var cert CertRecord
|
|
var cert CertRecord
|
|
- err := tx.Model(&CertRecord{}).Where("name = ?", name).First(&cert).Error
|
|
|
|
|
|
+ err := tx.Model(&CertRecord{}).Where("name = ?", name).Order("created_at desc").First(&cert).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
- return fmt.Errorf("update CDN domain SSL record failed: cert record not found")
|
|
|
|
|
|
+ return fmt.Errorf("update CDN domain SSL record to SQLite failed: cert record not found")
|
|
} else if err != nil {
|
|
} else if err != nil {
|
|
return err
|
|
return err
|
|
} else if cert.CertID != certID || cert.Name != name || cert.Subject != subject {
|
|
} else if cert.CertID != certID || cert.Name != name || cert.Subject != subject {
|
|
- logger.Errorf("Update CDN domain SSL record failed: information does not match (sqlite: cert-id=%d; name=%s; subject=%s) (be given: cert-id=%d; name=%s; subject=%s)", cert.CertID, cert.Name, cert.Subject, certID, name, subject)
|
|
|
|
|
|
+ logger.Errorf("Update CDN domain SSL record to SQLite failed: information does not match (sqlite: cert-id=%d; name=%s; subject=%s) (be given: cert-id=%d; name=%s; subject=%s)", cert.CertID, cert.Name, cert.Subject, certID, name, subject)
|
|
}
|
|
}
|
|
|
|
|
|
record := DomainRecord{
|
|
record := DomainRecord{
|
|
@@ -71,9 +71,10 @@ func UpdateDomain(certID int64, name string, subject string, domain string) erro
|
|
Name: cert.Name,
|
|
Name: cert.Name,
|
|
Subject: cert.Subject,
|
|
Subject: cert.Subject,
|
|
Domain: domain,
|
|
Domain: domain,
|
|
|
|
+ CertUpdateAt: cert.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
|
|
- err = tx.Save(&record).Error
|
|
|
|
|
|
+ err = tx.Create(&record).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -86,3 +87,44 @@ func UpdateDomain(certID int64, name string, subject string, domain string) erro
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func CheckNeedUpdateDomain(certName string, domainName string) (cr *CertRecord, res bool, err error) {
|
|
|
|
+ defer func() {
|
|
|
|
+ if err != nil {
|
|
|
|
+ res = false
|
|
|
|
+ cr = nil
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ var cert CertRecord
|
|
|
|
+
|
|
|
|
+ err = db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
+ err := tx.Model(&CertRecord{}).Where("name = ?", certName).Order("created_at desc").First(&cert).Error
|
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
+ return fmt.Errorf("check CDN domain SSL record from SQLite failed: cert record not found")
|
|
|
|
+ } else if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var domain DomainRecord
|
|
|
|
+ err = tx.Model(&DomainRecord{}).Where("domain = ?", domainName).Order("created_at desc").First(&domain).Error
|
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
+ res = true
|
|
|
|
+ return nil
|
|
|
|
+ } else if err != nil {
|
|
|
|
+ res = false
|
|
|
|
+ return err
|
|
|
|
+ } else if cert.UpdatedAt.After(domain.CertUpdateAt) {
|
|
|
|
+ res = true
|
|
|
|
+ return nil
|
|
|
|
+ } else {
|
|
|
|
+ res = false
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, false, fmt.Errorf("check CDN domain SSL record from SQLite failed: %s", err.Error())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return &cert, res, nil
|
|
|
|
+}
|