Skip to content

Commit 11e4607

Browse files
committed
fix: certificate dir cannot be created in some windows-docker setup #403
1 parent 8059345 commit 11e4607

File tree

2 files changed

+68
-31
lines changed

2 files changed

+68
-31
lines changed

api/certificate/issue.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package certificate
33
import (
44
"github.com/0xJacky/Nginx-UI/internal/cert"
55
"github.com/0xJacky/Nginx-UI/internal/logger"
6-
"github.com/0xJacky/Nginx-UI/internal/nginx"
76
"github.com/0xJacky/Nginx-UI/model"
87
"github.com/gin-gonic/gin"
98
"github.com/go-acme/lego/v4/certcrypto"
109
"github.com/gorilla/websocket"
1110
"net/http"
12-
"strings"
1311
)
1412

1513
const (
@@ -71,7 +69,6 @@ func IssueCert(c *gin.Context) {
7169
payload := &cert.ConfigPayload{}
7270

7371
err = ws.ReadJSON(payload)
74-
7572
if err != nil {
7673
logger.Error(err)
7774
return
@@ -122,14 +119,10 @@ func IssueCert(c *gin.Context) {
122119
return
123120
}
124121

125-
certDirName := strings.Join(payload.ServerName, "_") + "_" + string(payload.GetKeyType())
126-
sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
127-
sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")
128-
129122
err = certModel.Updates(&model.Cert{
130123
Domains: payload.ServerName,
131-
SSLCertificatePath: sslCertificatePath,
132-
SSLCertificateKeyPath: sslCertificateKeyPath,
124+
SSLCertificatePath: payload.GetCertificatePath(),
125+
SSLCertificateKeyPath: payload.GetCertificateKeyPath(),
133126
AutoCert: model.AutoCertEnabled,
134127
KeyType: payload.KeyType,
135128
ChallengeMethod: payload.ChallengeMethod,
@@ -152,8 +145,8 @@ func IssueCert(c *gin.Context) {
152145
err = ws.WriteJSON(IssueCertResponse{
153146
Status: Success,
154147
Message: "Issued certificate successfully",
155-
SSLCertificate: sslCertificatePath,
156-
SSLCertificateKey: sslCertificateKeyPath,
148+
SSLCertificate: payload.GetCertificatePath(),
149+
SSLCertificateKey: payload.GetCertificateKeyPath(),
157150
KeyType: payload.GetKeyType(),
158151
})
159152

internal/cert/payload.go

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ import (
1616
)
1717

1818
type ConfigPayload struct {
19-
CertID int `json:"cert_id"`
20-
ServerName []string `json:"server_name"`
21-
ChallengeMethod string `json:"challenge_method"`
22-
DNSCredentialID int `json:"dns_credential_id"`
23-
ACMEUserID int `json:"acme_user_id"`
24-
KeyType certcrypto.KeyType `json:"key_type"`
25-
Resource *model.CertificateResource `json:"resource,omitempty"`
26-
NotBefore time.Time
19+
CertID int `json:"cert_id"`
20+
ServerName []string `json:"server_name"`
21+
ChallengeMethod string `json:"challenge_method"`
22+
DNSCredentialID int `json:"dns_credential_id"`
23+
ACMEUserID int `json:"acme_user_id"`
24+
KeyType certcrypto.KeyType `json:"key_type"`
25+
Resource *model.CertificateResource `json:"resource,omitempty"`
26+
NotBefore time.Time `json:"-"`
27+
CertificateDir string `json:"-"`
28+
SSLCertificatePath string `json:"-"`
29+
SSLCertificateKeyPath string `json:"-"`
2730
}
2831

2932
func (c *ConfigPayload) GetACMEUser() (user *model.AcmeUser, err error) {
@@ -46,21 +49,38 @@ func (c *ConfigPayload) GetKeyType() certcrypto.KeyType {
4649
return helper.GetKeyType(c.KeyType)
4750
}
4851

49-
func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
50-
name := strings.Join(c.ServerName, "_")
51-
saveDir := nginx.GetConfPath("ssl/" + name + "_" + string(c.KeyType))
52-
if _, err := os.Stat(saveDir); os.IsNotExist(err) {
53-
err = os.MkdirAll(saveDir, 0755)
54-
if err != nil {
55-
errChan <- errors.Wrap(err, "mkdir error")
56-
return
52+
func (c *ConfigPayload) mkCertificateDir() (err error) {
53+
dir := c.getCertificateDirPath()
54+
if _, err = os.Stat(dir); os.IsNotExist(err) {
55+
err = os.MkdirAll(dir, 0755)
56+
if err == nil {
57+
return nil
58+
}
59+
}
60+
61+
// For windows, replace # with * (issue #403)
62+
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "#", "*")
63+
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
64+
err = os.MkdirAll(c.CertificateDir, 0755)
65+
if err == nil {
66+
return nil
5767
}
5868
}
5969

70+
return
71+
}
72+
73+
func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
74+
err := c.mkCertificateDir()
75+
if err != nil {
76+
errChan <- errors.Wrap(err, "make certificate dir error")
77+
return
78+
}
79+
6080
// Each certificate comes back with the cert bytes, the bytes of the client's
6181
// private key, and a certificate URL. SAVE THESE TO DISK.
6282
l.Println("[INFO] [Nginx UI] Writing certificate to disk")
63-
err := os.WriteFile(filepath.Join(saveDir, "fullchain.cer"),
83+
err = os.WriteFile(c.GetCertificatePath(),
6484
c.Resource.Certificate, 0644)
6585

6686
if err != nil {
@@ -69,7 +89,7 @@ func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
6989
}
7090

7191
l.Println("[INFO] [Nginx UI] Writing certificate private key to disk")
72-
err = os.WriteFile(filepath.Join(saveDir, "private.key"),
92+
err = os.WriteFile(c.GetCertificateKeyPath(),
7393
c.Resource.PrivateKey, 0644)
7494

7595
if err != nil {
@@ -84,7 +104,31 @@ func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
84104

85105
db := model.UseDB()
86106
db.Where("id = ?", c.CertID).Updates(&model.Cert{
87-
SSLCertificatePath: filepath.Join(saveDir, "fullchain.cer"),
88-
SSLCertificateKeyPath: filepath.Join(saveDir, "private.key"),
107+
SSLCertificatePath: c.GetCertificatePath(),
108+
SSLCertificateKeyPath: c.GetCertificateKeyPath(),
89109
})
90110
}
111+
112+
func (c *ConfigPayload) getCertificateDirPath() string {
113+
if c.CertificateDir != "" {
114+
return c.CertificateDir
115+
}
116+
c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
117+
return c.CertificateDir
118+
}
119+
120+
func (c *ConfigPayload) GetCertificatePath() string {
121+
if c.SSLCertificatePath != "" {
122+
return c.SSLCertificatePath
123+
}
124+
c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
125+
return c.SSLCertificatePath
126+
}
127+
128+
func (c *ConfigPayload) GetCertificateKeyPath() string {
129+
if c.SSLCertificateKeyPath != "" {
130+
return c.SSLCertificateKeyPath
131+
}
132+
c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
133+
return c.SSLCertificateKeyPath
134+
}

0 commit comments

Comments
 (0)