Skip to content

Commit 7776276

Browse files
fix: Resolve issue where bulk certificate setup creates duplicate server certificates
1 parent 279a16c commit 7776276

File tree

3 files changed

+76
-57
lines changed

3 files changed

+76
-57
lines changed

agent/app/service/website.go

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package service
33
import (
44
"bytes"
55
"context"
6-
"crypto/x509"
76
"encoding/base64"
8-
"encoding/pem"
97
"errors"
108
"fmt"
119
"net"
@@ -975,64 +973,10 @@ func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteH
975973
websiteSSL = *websiteModel
976974
}
977975
if req.Type == constant.SSLManual {
978-
var (
979-
certificate string
980-
privateKey string
981-
)
982-
switch req.ImportType {
983-
case "paste":
984-
certificate = req.Certificate
985-
privateKey = req.PrivateKey
986-
case "local":
987-
fileOp := files.NewFileOp()
988-
if !fileOp.Stat(req.PrivateKeyPath) {
989-
return nil, buserr.New("ErrSSLKeyNotFound")
990-
}
991-
if !fileOp.Stat(req.CertificatePath) {
992-
return nil, buserr.New("ErrSSLCertificateNotFound")
993-
}
994-
if content, err := fileOp.GetContent(req.PrivateKeyPath); err != nil {
995-
return nil, err
996-
} else {
997-
privateKey = string(content)
998-
}
999-
if content, err := fileOp.GetContent(req.CertificatePath); err != nil {
1000-
return nil, err
1001-
} else {
1002-
certificate = string(content)
1003-
}
1004-
}
1005-
1006-
privateKeyCertBlock, _ := pem.Decode([]byte(privateKey))
1007-
if privateKeyCertBlock == nil {
1008-
return nil, buserr.New("ErrSSLKeyFormat")
1009-
}
1010-
1011-
certBlock, _ := pem.Decode([]byte(certificate))
1012-
if certBlock == nil {
1013-
return nil, buserr.New("ErrSSLCertificateFormat")
1014-
}
1015-
cert, err := x509.ParseCertificate(certBlock.Bytes)
976+
websiteSSL, err = getManualWebsiteSSL(req)
1016977
if err != nil {
1017978
return nil, err
1018979
}
1019-
websiteSSL.ExpireDate = cert.NotAfter
1020-
websiteSSL.StartDate = cert.NotBefore
1021-
websiteSSL.Type = cert.Issuer.CommonName
1022-
if len(cert.Issuer.Organization) > 0 {
1023-
websiteSSL.Organization = cert.Issuer.Organization[0]
1024-
} else {
1025-
websiteSSL.Organization = cert.Issuer.CommonName
1026-
}
1027-
if len(cert.DNSNames) > 0 {
1028-
websiteSSL.PrimaryDomain = cert.DNSNames[0]
1029-
websiteSSL.Domains = strings.Join(cert.DNSNames, ",")
1030-
}
1031-
websiteSSL.Provider = constant.Manual
1032-
websiteSSL.PrivateKey = privateKey
1033-
websiteSSL.Pem = certificate
1034-
websiteSSL.Status = constant.SSLReady
1035-
1036980
res.SSL = websiteSSL
1037981
}
1038982

agent/app/service/website_op.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ func (w WebsiteService) BatchSetHttps(ctx context.Context, req request.BatchWebs
9191
HttpsPorts: req.HttpsPorts,
9292
Http3: req.Http3,
9393
}
94+
if req.Type == constant.SSLManual {
95+
websiteSSL, err := getManualWebsiteSSL(websiteHttpsOp)
96+
if err != nil {
97+
return err
98+
}
99+
if err = websiteSSLRepo.Create(ctx, &websiteSSL); err != nil {
100+
return err
101+
}
102+
websiteHttpsOp.Type = constant.SSLExisted
103+
websiteHttpsOp.WebsiteSSLID = websiteSSL.ID
104+
}
94105
opWebsiteTask := func(t *task.Task) error {
95106
for _, web := range websites {
96107
if web.Type == constant.Stream {

agent/app/service/website_utils.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package service
22

33
import (
44
"context"
5+
"crypto/x509"
56
"encoding/json"
7+
"encoding/pem"
68
"fmt"
79
"log"
810
"net"
@@ -1707,3 +1709,65 @@ func getNginxUpstreamServers(upstreamServers []*components.UpstreamServer) []dto
17071709
}
17081710
return servers
17091711
}
1712+
1713+
func getManualWebsiteSSL(req request.WebsiteHTTPSOp) (model.WebsiteSSL, error) {
1714+
var websiteSSL model.WebsiteSSL
1715+
var (
1716+
certificate string
1717+
privateKey string
1718+
)
1719+
switch req.ImportType {
1720+
case "paste":
1721+
certificate = req.Certificate
1722+
privateKey = req.PrivateKey
1723+
case "local":
1724+
fileOp := files.NewFileOp()
1725+
if !fileOp.Stat(req.PrivateKeyPath) {
1726+
return websiteSSL, buserr.New("ErrSSLKeyNotFound")
1727+
}
1728+
if !fileOp.Stat(req.CertificatePath) {
1729+
return websiteSSL, buserr.New("ErrSSLCertificateNotFound")
1730+
}
1731+
if content, err := fileOp.GetContent(req.PrivateKeyPath); err != nil {
1732+
return websiteSSL, err
1733+
} else {
1734+
privateKey = string(content)
1735+
}
1736+
if content, err := fileOp.GetContent(req.CertificatePath); err != nil {
1737+
return websiteSSL, err
1738+
} else {
1739+
certificate = string(content)
1740+
}
1741+
}
1742+
1743+
privateKeyCertBlock, _ := pem.Decode([]byte(privateKey))
1744+
if privateKeyCertBlock == nil {
1745+
return websiteSSL, buserr.New("ErrSSLKeyFormat")
1746+
}
1747+
1748+
certBlock, _ := pem.Decode([]byte(certificate))
1749+
if certBlock == nil {
1750+
return websiteSSL, buserr.New("ErrSSLCertificateFormat")
1751+
}
1752+
cert, err := x509.ParseCertificate(certBlock.Bytes)
1753+
if err != nil {
1754+
return websiteSSL, err
1755+
}
1756+
websiteSSL.ExpireDate = cert.NotAfter
1757+
websiteSSL.StartDate = cert.NotBefore
1758+
websiteSSL.Type = cert.Issuer.CommonName
1759+
if len(cert.Issuer.Organization) > 0 {
1760+
websiteSSL.Organization = cert.Issuer.Organization[0]
1761+
} else {
1762+
websiteSSL.Organization = cert.Issuer.CommonName
1763+
}
1764+
if len(cert.DNSNames) > 0 {
1765+
websiteSSL.PrimaryDomain = cert.DNSNames[0]
1766+
websiteSSL.Domains = strings.Join(cert.DNSNames, ",")
1767+
}
1768+
websiteSSL.Provider = constant.Manual
1769+
websiteSSL.PrivateKey = privateKey
1770+
websiteSSL.Pem = certificate
1771+
websiteSSL.Status = constant.SSLReady
1772+
return websiteSSL, nil
1773+
}

0 commit comments

Comments
 (0)