Skip to content

Commit 8ce065f

Browse files
zeripathguillep2k
andcommitted
Make CertFile and KeyFile relative to CustomPath (go-gitea#9868)
* Make CertFile and KeyFile relative to CustomPath The current code will absolute CertFile and KeyFile against the current working directory. This is quite unexpected for users. This code makes relative paths absolute against the CustomPath. Fix go-gitea#4196 * Improve error reporting when reading certificates * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
1 parent 8add1df commit 8ce065f

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

custom/conf/app.ini.sample

+3-2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ DISABLE_ROUTER_LOG = false
275275
; not forget to export the private key):
276276
; $ openssl pkcs12 -in cert.pfx -out cert.pem -nokeys
277277
; $ openssl pkcs12 -in cert.pfx -out key.pem -nocerts -nodes
278-
CERT_FILE = custom/https/cert.pem
279-
KEY_FILE = custom/https/key.pem
278+
; Paths are relative to CUSTOM_PATH
279+
CERT_FILE = https/cert.pem
280+
KEY_FILE = https/key.pem
280281
; Root directory containing templates and static files.
281282
; default is the path where Gitea is executed
282283
STATIC_ROOT_PATH =

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
181181
- `SSH_LISTEN_PORT`: **%(SSH\_PORT)s**: Port for the built-in SSH server.
182182
- `OFFLINE_MODE`: **false**: Disables use of CDN for static files and Gravatar for profile pictures.
183183
- `DISABLE_ROUTER_LOG`: **false**: Mute printing of the router log.
184-
- `CERT_FILE`: **custom/https/cert.pem**: Cert file path used for HTTPS.
185-
- `KEY_FILE`: **custom/https/key.pem**: Key file path used for HTTPS.
184+
- `CERT_FILE`: **https/cert.pem**: Cert file path used for HTTPS. From 1.11 paths are relative to `CUSTOM_PATH`.
185+
- `KEY_FILE`: **https/key.pem**: Key file path used for HTTPS. From 1.11 paths are relative to `CUSTOM_PATH`.
186186
- `STATIC_ROOT_PATH`: **./**: Upper level of template and static files path.
187187
- `STATIC_CACHE_TIME`: **6h**: Web browser cache time for static resources on `custom/`, `public/` and all uploaded avatars.
188188
- `ENABLE_GZIP`: **false**: Enables application-level GZIP support.

modules/graceful/server.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package graceful
77

88
import (
99
"crypto/tls"
10+
"io/ioutil"
1011
"net"
1112
"os"
1213
"strings"
@@ -99,12 +100,25 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string, serve ServeFuncti
99100
}
100101

101102
config.Certificates = make([]tls.Certificate, 1)
102-
var err error
103-
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
103+
104+
certPEMBlock, err := ioutil.ReadFile(certFile)
104105
if err != nil {
105106
log.Error("Failed to load https cert file %s for %s:%s: %v", certFile, srv.network, srv.address, err)
106107
return err
107108
}
109+
110+
keyPEMBlock, err := ioutil.ReadFile(keyFile)
111+
if err != nil {
112+
log.Error("Failed to load https key file %s for %s:%s: %v", keyFile, srv.network, srv.address, err)
113+
return err
114+
}
115+
116+
config.Certificates[0], err = tls.X509KeyPair(certPEMBlock, keyPEMBlock)
117+
if err != nil {
118+
log.Error("Failed to create certificate from cert file %s and key file %s for %s:%s: %v", certFile, keyFile, srv.network, srv.address, err)
119+
return err
120+
}
121+
108122
return srv.ListenAndServeTLSConfig(config, serve)
109123
}
110124

modules/setting/setting.go

+6
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,12 @@ func NewContext() {
554554
Protocol = HTTPS
555555
CertFile = sec.Key("CERT_FILE").String()
556556
KeyFile = sec.Key("KEY_FILE").String()
557+
if !filepath.IsAbs(CertFile) && len(CertFile) > 0 {
558+
CertFile = filepath.Join(CustomPath, CertFile)
559+
}
560+
if !filepath.IsAbs(KeyFile) && len(KeyFile) > 0 {
561+
KeyFile = filepath.Join(CustomPath, KeyFile)
562+
}
557563
case "fcgi":
558564
Protocol = FCGI
559565
case "fcgi+unix":

0 commit comments

Comments
 (0)