2020-12-18 08:01:19 +00:00
|
|
|
// Package telemetry provides telemetry information for SFTPGo, such as:
|
|
|
|
// - health information (for health checks)
|
|
|
|
// - metrics
|
|
|
|
// - profiling information
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
2020-12-18 15:04:42 +00:00
|
|
|
"crypto/tls"
|
2020-12-18 08:01:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-12-18 15:04:42 +00:00
|
|
|
"path/filepath"
|
2020-12-18 08:01:19 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
|
2020-12-18 15:04:42 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2020-12-18 08:01:19 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2020-12-18 15:04:42 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-12-18 08:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "telemetry"
|
|
|
|
metricsPath = "/metrics"
|
|
|
|
pprofBasePath = "/debug"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-18 15:04:42 +00:00
|
|
|
router *chi.Mux
|
|
|
|
httpAuth common.HTTPAuthProvider
|
|
|
|
certMgr *common.CertManager
|
2020-12-18 08:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Conf telemetry server configuration.
|
|
|
|
type Conf struct {
|
2020-12-18 08:47:22 +00:00
|
|
|
// The port used for serving HTTP requests. 0 disable the HTTP server. Default: 10000
|
2020-12-18 08:01:19 +00:00
|
|
|
BindPort int `json:"bind_port" mapstructure:"bind_port"`
|
|
|
|
// The address to listen on. A blank value means listen on all available network interfaces. Default: "127.0.0.1"
|
|
|
|
BindAddress string `json:"bind_address" mapstructure:"bind_address"`
|
2020-12-18 15:04:42 +00:00
|
|
|
// Enable the built-in profiler.
|
|
|
|
// The profiler will be accessible via HTTP/HTTPS using the base URL "/debug/pprof/"
|
|
|
|
EnableProfiler bool `json:"enable_profiler" mapstructure:"enable_profiler"`
|
|
|
|
// Path to a file used to store usernames and password for basic authentication.
|
|
|
|
// This can be an absolute path or a path relative to the config dir.
|
|
|
|
// We support HTTP basic authentication and the file format must conform to the one generated using the Apache
|
|
|
|
// htpasswd tool. The supported password formats are bcrypt ($2y$ prefix) and md5 crypt ($apr1$ prefix).
|
|
|
|
// If empty HTTP authentication is disabled
|
|
|
|
AuthUserFile string `json:"auth_user_file" mapstructure:"auth_user_file"`
|
|
|
|
// If files containing a certificate and matching private key for the server are provided the server will expect
|
|
|
|
// HTTPS connections.
|
|
|
|
// Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
|
|
|
|
// "paramchange" request to the running service on Windows.
|
|
|
|
CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
|
|
|
|
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
2020-12-18 08:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize configures and starts the telemetry server.
|
2020-12-18 15:04:42 +00:00
|
|
|
func (c Conf) Initialize(configDir string) error {
|
|
|
|
var err error
|
2020-12-18 08:01:19 +00:00
|
|
|
logger.Debug(logSender, "", "initializing telemetry server with config %+v", c)
|
2020-12-18 15:04:42 +00:00
|
|
|
authUserFile := getConfigPath(c.AuthUserFile, configDir)
|
|
|
|
httpAuth, err = common.NewBasicAuthProvider(authUserFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certificateFile := getConfigPath(c.CertificateFile, configDir)
|
|
|
|
certificateKeyFile := getConfigPath(c.CertificateKeyFile, configDir)
|
|
|
|
initializeRouter(c.EnableProfiler)
|
2020-12-18 08:01:19 +00:00
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%d", c.BindAddress, c.BindPort),
|
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
WriteTimeout: 60 * time.Second,
|
|
|
|
IdleTimeout: 120 * time.Second,
|
2020-12-18 08:47:22 +00:00
|
|
|
MaxHeaderBytes: 1 << 14, // 16KB
|
2020-12-18 08:01:19 +00:00
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
if certificateFile != "" && certificateKeyFile != "" {
|
|
|
|
certMgr, err = common.NewCertManager(certificateFile, certificateKeyFile, logSender)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config := &tls.Config{
|
|
|
|
GetCertificate: certMgr.GetCertificateFunc(),
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
httpServer.TLSConfig = config
|
|
|
|
return httpServer.ListenAndServeTLS("", "")
|
|
|
|
}
|
2020-12-18 08:01:19 +00:00
|
|
|
return httpServer.ListenAndServe()
|
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
|
|
|
|
// ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
|
|
|
|
func ReloadTLSCertificate() error {
|
|
|
|
if certMgr != nil {
|
|
|
|
return certMgr.LoadCertificate(logSender)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigPath(name, configDir string) string {
|
|
|
|
if !utils.IsFileInputValid(name) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if name != "" && !filepath.IsAbs(name) {
|
|
|
|
return filepath.Join(configDir, name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|