Skip to content

Commit 36f0bf0

Browse files
committed
chore(web): Improve error message for invalid SSL configuration
1 parent 4a116ad commit 36f0bf0

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/cmd/grafana-server/server.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"time"
99

10+
"gopkg.in/macaron.v1"
11+
1012
"golang.org/x/sync/errgroup"
1113

1214
"github.com/grafana/grafana/pkg/api"
@@ -89,7 +91,7 @@ func (g *GrafanaServerImpl) startHttpServer() {
8991
case setting.HTTP:
9092
err = http.ListenAndServe(listenAddr, m)
9193
case setting.HTTPS:
92-
err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
94+
err = ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
9395
default:
9496
g.log.Error("Invalid protocol", "protocol", setting.Protocol)
9597
g.Shutdown(1, "Startup failed")
@@ -113,6 +115,26 @@ func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
113115
os.Exit(code)
114116
}
115117

118+
func ListenAndServeTLS(listenAddr, certfile, keyfile string, m *macaron.Macaron) error {
119+
if certfile == "" {
120+
return fmt.Errorf("cert_file cannot be empty when using HTTPS")
121+
}
122+
123+
if keyfile == "" {
124+
return fmt.Errorf("cert_key cannot be empty when using HTTPS")
125+
}
126+
127+
if _, err := os.Stat(setting.CertFile); os.IsNotExist(err) {
128+
return fmt.Errorf(`Cannot find SSL cert_file at %v`, setting.CertFile)
129+
}
130+
131+
if _, err := os.Stat(setting.KeyFile); os.IsNotExist(err) {
132+
return fmt.Errorf(`Cannot find SSL key_file at %v`, setting.KeyFile)
133+
}
134+
135+
return http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
136+
}
137+
116138
// implement context.Context
117139
func (g *GrafanaServerImpl) Deadline() (deadline time.Time, ok bool) {
118140
return g.context.Deadline()

0 commit comments

Comments
 (0)