2020-04-26 21:29:09 +00:00
|
|
|
package httpclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-02-13 13:41:37 +00:00
|
|
|
"fmt"
|
2020-04-26 21:29:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2021-02-12 20:42:49 +00:00
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
)
|
|
|
|
|
2021-02-13 13:41:37 +00:00
|
|
|
// TLSKeyPair defines the paths for a TLS key pair
|
|
|
|
type TLSKeyPair struct {
|
|
|
|
Cert string `json:"cert" mapstructure:"cert"`
|
|
|
|
Key string `json:"key" mapstructure:"key"`
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
// Config defines the configuration for HTTP clients.
|
|
|
|
// HTTP clients are used for executing hooks such as the ones used for
|
|
|
|
// custom actions, external authentication and pre-login user modifications
|
|
|
|
type Config struct {
|
2021-02-12 20:42:49 +00:00
|
|
|
// Timeout specifies a time limit, in seconds, for a request
|
2020-04-26 21:29:09 +00:00
|
|
|
Timeout int64 `json:"timeout" mapstructure:"timeout"`
|
2021-02-12 20:42:49 +00:00
|
|
|
// RetryWaitMin defines the minimum waiting time between attempts in seconds
|
|
|
|
RetryWaitMin int `json:"retry_wait_min" mapstructure:"retry_wait_min"`
|
|
|
|
// RetryWaitMax defines the minimum waiting time between attempts in seconds
|
|
|
|
RetryWaitMax int `json:"retry_wait_max" mapstructure:"retry_wait_max"`
|
|
|
|
// RetryMax defines the maximum number of attempts
|
|
|
|
RetryMax int `json:"retry_max" mapstructure:"retry_max"`
|
2020-04-26 21:29:09 +00:00
|
|
|
// CACertificates defines extra CA certificates to trust.
|
|
|
|
// The paths can be absolute or relative to the config dir.
|
|
|
|
// Adding trusted CA certificates is a convenient way to use self-signed
|
|
|
|
// certificates without defeating the purpose of using TLS
|
2020-05-03 09:37:50 +00:00
|
|
|
CACertificates []string `json:"ca_certificates" mapstructure:"ca_certificates"`
|
2021-02-13 13:41:37 +00:00
|
|
|
// Certificates defines the certificates to use for mutual TLS
|
|
|
|
Certificates []TLSKeyPair `json:"certificates" mapstructure:"certificates"`
|
2020-05-03 09:37:50 +00:00
|
|
|
// if enabled the HTTP client accepts any TLS certificate presented by
|
|
|
|
// the server and any host name in that certificate.
|
|
|
|
// In this mode, TLS is susceptible to man-in-the-middle attacks.
|
|
|
|
// This should be used only for testing.
|
|
|
|
SkipTLSVerify bool `json:"skip_tls_verify" mapstructure:"skip_tls_verify"`
|
2020-04-26 21:29:09 +00:00
|
|
|
customTransport *http.Transport
|
2021-02-12 20:42:49 +00:00
|
|
|
tlsConfig *tls.Config
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const logSender = "httpclient"
|
|
|
|
|
|
|
|
var httpConfig Config
|
|
|
|
|
|
|
|
// Initialize configures HTTP clients
|
2021-02-13 13:41:37 +00:00
|
|
|
func (c *Config) Initialize(configDir string) error {
|
|
|
|
rootCAs, err := c.loadCACerts(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-26 21:29:09 +00:00
|
|
|
customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
if customTransport.TLSClientConfig != nil {
|
|
|
|
customTransport.TLSClientConfig.RootCAs = rootCAs
|
|
|
|
} else {
|
|
|
|
customTransport.TLSClientConfig = &tls.Config{
|
2021-02-13 13:41:37 +00:00
|
|
|
RootCAs: rootCAs,
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 09:37:50 +00:00
|
|
|
customTransport.TLSClientConfig.InsecureSkipVerify = c.SkipTLSVerify
|
2021-02-13 13:41:37 +00:00
|
|
|
c.customTransport = customTransport
|
|
|
|
c.tlsConfig = customTransport.TLSClientConfig
|
|
|
|
|
|
|
|
err = c.loadCertificates(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
httpConfig = *c
|
|
|
|
return nil
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadCACerts returns system cert pools and try to add the configured
|
|
|
|
// CA certificates to it
|
2021-02-13 13:41:37 +00:00
|
|
|
func (c *Config) loadCACerts(configDir string) (*x509.CertPool, error) {
|
2020-04-26 21:29:09 +00:00
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
rootCAs = x509.NewCertPool()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ca := range c.CACertificates {
|
|
|
|
if !utils.IsFileInputValid(ca) {
|
2021-02-13 13:41:37 +00:00
|
|
|
return nil, fmt.Errorf("unable to load invalid CA certificate: %#v", ca)
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
if !filepath.IsAbs(ca) {
|
|
|
|
ca = filepath.Join(configDir, ca)
|
|
|
|
}
|
|
|
|
certs, err := ioutil.ReadFile(ca)
|
|
|
|
if err != nil {
|
2021-02-13 13:41:37 +00:00
|
|
|
return nil, fmt.Errorf("unable to load CA certificate: %v", err)
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
if rootCAs.AppendCertsFromPEM(certs) {
|
|
|
|
logger.Debug(logSender, "", "CA certificate %#v added to the trusted certificates", ca)
|
|
|
|
} else {
|
2021-02-13 13:41:37 +00:00
|
|
|
return nil, fmt.Errorf("unable to add CA certificate %#v to the trusted cetificates", ca)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rootCAs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) loadCertificates(configDir string) error {
|
|
|
|
if len(c.Certificates) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyPair := range c.Certificates {
|
|
|
|
cert := keyPair.Cert
|
|
|
|
key := keyPair.Key
|
|
|
|
if !utils.IsFileInputValid(cert) {
|
|
|
|
return fmt.Errorf("unable to load invalid certificate: %#v", cert)
|
|
|
|
}
|
|
|
|
if !utils.IsFileInputValid(key) {
|
|
|
|
return fmt.Errorf("unable to load invalid key: %#v", key)
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(cert) {
|
|
|
|
cert = filepath.Join(configDir, cert)
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(key) {
|
|
|
|
key = filepath.Join(configDir, key)
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.LoadX509KeyPair(cert, key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load key pair %#v, %#v: %v", cert, key, err)
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
2021-02-13 13:41:37 +00:00
|
|
|
logger.Debug(logSender, "", "client certificate %#v and key %#v successfully loaded", cert, key)
|
|
|
|
c.tlsConfig.Certificates = append(c.tlsConfig.Certificates, tlsCert)
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
2021-02-13 13:41:37 +00:00
|
|
|
return nil
|
2020-04-26 21:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHTTPClient returns an HTTP client with the configured parameters
|
|
|
|
func GetHTTPClient() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Timeout: time.Duration(httpConfig.Timeout) * time.Second,
|
|
|
|
Transport: httpConfig.customTransport,
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 20:42:49 +00:00
|
|
|
|
|
|
|
// GetRetraybleHTTPClient returns an HTTP client that retry a request on error.
|
|
|
|
// It uses the configured retry parameters
|
|
|
|
func GetRetraybleHTTPClient() *retryablehttp.Client {
|
|
|
|
client := retryablehttp.NewClient()
|
|
|
|
client.HTTPClient.Timeout = time.Duration(httpConfig.Timeout) * time.Second
|
|
|
|
client.HTTPClient.Transport.(*http.Transport).TLSClientConfig = httpConfig.tlsConfig
|
|
|
|
client.Logger = &logger.LeveledLogger{Sender: "RetryableHTTPClient"}
|
|
|
|
client.RetryWaitMin = time.Duration(httpConfig.RetryWaitMin) * time.Second
|
|
|
|
client.RetryWaitMax = time.Duration(httpConfig.RetryWaitMax) * time.Second
|
|
|
|
client.RetryMax = httpConfig.RetryMax
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|