2020-04-13 11:59:05 +02:00
|
|
|
package promauth
|
|
|
|
|
|
|
|
import (
|
2020-05-03 11:41:13 +02:00
|
|
|
"bytes"
|
2021-05-22 15:20:18 +02:00
|
|
|
"context"
|
2020-04-13 11:59:05 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2022-04-22 22:50:51 +02:00
|
|
|
"net/http"
|
2021-05-22 16:59:23 +02:00
|
|
|
"net/url"
|
2022-04-22 23:16:34 +02:00
|
|
|
"strings"
|
2021-05-14 19:00:05 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2021-12-02 23:08:42 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2022-09-26 16:35:45 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
2022-06-22 19:38:43 +02:00
|
|
|
"github.com/VictoriaMetrics/fasthttp"
|
2022-06-21 19:23:30 +02:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2021-05-22 15:20:18 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/clientcredentials"
|
2020-04-13 11:59:05 +02:00
|
|
|
)
|
|
|
|
|
2021-11-05 13:41:14 +01:00
|
|
|
// Secret represents a string secret such as password or auth token.
|
|
|
|
//
|
|
|
|
// It is marshaled to "<secret>" string in yaml.
|
|
|
|
//
|
|
|
|
// This is needed for hiding secret strings in /config page output.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1764
|
|
|
|
type Secret struct {
|
2022-05-06 23:02:54 +02:00
|
|
|
S string
|
2021-11-05 13:41:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSecret returns new secret for s.
|
|
|
|
func NewSecret(s string) *Secret {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Secret{
|
2022-05-06 23:02:54 +02:00
|
|
|
S: s,
|
2021-11-05 13:41:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements yaml.Marshaler interface.
|
|
|
|
//
|
|
|
|
// It substitutes the secret with "<secret>" string.
|
|
|
|
func (s *Secret) MarshalYAML() (interface{}, error) {
|
|
|
|
return "<secret>", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
|
|
|
func (s *Secret) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var secret string
|
|
|
|
if err := f(&secret); err != nil {
|
|
|
|
return fmt.Errorf("cannot parse secret: %w", err)
|
|
|
|
}
|
2022-05-06 23:02:54 +02:00
|
|
|
s.S = secret
|
2021-11-05 13:41:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the secret in plaintext.
|
|
|
|
func (s *Secret) String() string {
|
|
|
|
if s == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-05-06 23:02:54 +02:00
|
|
|
return s.S
|
2021-11-05 13:41:14 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:05 +02:00
|
|
|
// TLSConfig represents TLS config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#tls_config
|
|
|
|
type TLSConfig struct {
|
2022-06-01 20:34:00 +02:00
|
|
|
CA []byte `yaml:"ca,omitempty"`
|
2020-11-13 15:17:03 +01:00
|
|
|
CAFile string `yaml:"ca_file,omitempty"`
|
2022-06-01 20:34:00 +02:00
|
|
|
Cert []byte `yaml:"cert,omitempty"`
|
2020-11-13 15:17:03 +01:00
|
|
|
CertFile string `yaml:"cert_file,omitempty"`
|
2022-06-01 20:34:00 +02:00
|
|
|
Key []byte `yaml:"key,omitempty"`
|
2020-11-13 15:17:03 +01:00
|
|
|
KeyFile string `yaml:"key_file,omitempty"`
|
|
|
|
ServerName string `yaml:"server_name,omitempty"`
|
|
|
|
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
|
2022-04-22 23:16:34 +02:00
|
|
|
MinVersion string `yaml:"min_version,omitempty"`
|
2022-09-26 16:35:45 +02:00
|
|
|
// Do not define MaxVersion field (max_version), since this has no sense from security PoV.
|
|
|
|
// This can only result in lower security level if improperly set.
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
// String returns human-readable representation of tc
|
|
|
|
func (tc *TLSConfig) String() string {
|
|
|
|
if tc == nil {
|
2022-04-22 23:23:19 +02:00
|
|
|
return ""
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
caHash := xxhash.Sum64(tc.CA)
|
|
|
|
certHash := xxhash.Sum64(tc.Cert)
|
|
|
|
keyHash := xxhash.Sum64(tc.Key)
|
2022-06-04 00:01:13 +02:00
|
|
|
return fmt.Sprintf("hash(ca)=%d, ca_file=%q, hash(cert)=%d, cert_file=%q, hash(key)=%d, key_file=%q, server_name=%q, insecure_skip_verify=%v, min_version=%q",
|
2022-07-04 13:27:48 +02:00
|
|
|
caHash, tc.CAFile, certHash, tc.CertFile, keyHash, tc.KeyFile, tc.ServerName, tc.InsecureSkipVerify, tc.MinVersion)
|
2022-04-22 23:23:19 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 20:17:43 +02:00
|
|
|
// Authorization represents generic authorization config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/
|
|
|
|
type Authorization struct {
|
2021-11-05 13:41:14 +01:00
|
|
|
Type string `yaml:"type,omitempty"`
|
|
|
|
Credentials *Secret `yaml:"credentials,omitempty"`
|
|
|
|
CredentialsFile string `yaml:"credentials_file,omitempty"`
|
2021-04-02 20:17:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:05 +02:00
|
|
|
// BasicAuthConfig represents basic auth config.
|
|
|
|
type BasicAuthConfig struct {
|
2021-11-05 13:41:14 +01:00
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password *Secret `yaml:"password,omitempty"`
|
|
|
|
PasswordFile string `yaml:"password_file,omitempty"`
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 20:17:43 +02:00
|
|
|
// HTTPClientConfig represents http client config.
|
|
|
|
type HTTPClientConfig struct {
|
|
|
|
Authorization *Authorization `yaml:"authorization,omitempty"`
|
|
|
|
BasicAuth *BasicAuthConfig `yaml:"basic_auth,omitempty"`
|
2021-11-05 13:41:14 +01:00
|
|
|
BearerToken *Secret `yaml:"bearer_token,omitempty"`
|
2021-04-02 20:17:43 +02:00
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
2021-05-22 15:20:18 +02:00
|
|
|
OAuth2 *OAuth2Config `yaml:"oauth2,omitempty"`
|
2021-04-02 20:17:43 +02:00
|
|
|
TLSConfig *TLSConfig `yaml:"tls_config,omitempty"`
|
2022-06-22 19:38:43 +02:00
|
|
|
|
|
|
|
// Headers contains optional HTTP headers, which must be sent in the request to the server
|
|
|
|
Headers []string `yaml:"headers,omitempty"`
|
2023-05-26 09:39:45 +02:00
|
|
|
|
|
|
|
// FollowRedirects specifies whether the client should follow HTTP 3xx redirects.
|
|
|
|
FollowRedirects *bool `yaml:"follow_redirects,omitempty"`
|
2023-06-05 15:56:49 +02:00
|
|
|
|
2023-07-07 00:59:56 +02:00
|
|
|
// Do not support enable_http2 option because of the following reasons:
|
|
|
|
//
|
|
|
|
// - http2 is used very rarely comparing to http for Prometheus metrics exposition and service discovery
|
|
|
|
// - http2 is much harder to debug than http
|
|
|
|
// - http2 has very bad security record because of its complexity - see https://portswigger.net/research/http2
|
|
|
|
//
|
|
|
|
// VictoriaMetrics components are compiled with nethttpomithttp2 tag because of these issues.
|
|
|
|
//
|
|
|
|
// EnableHTTP2 bool
|
2021-04-02 20:17:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 23:40:08 +02:00
|
|
|
// ProxyClientConfig represents proxy client config.
|
|
|
|
type ProxyClientConfig struct {
|
|
|
|
Authorization *Authorization `yaml:"proxy_authorization,omitempty"`
|
|
|
|
BasicAuth *BasicAuthConfig `yaml:"proxy_basic_auth,omitempty"`
|
2021-11-05 13:41:14 +01:00
|
|
|
BearerToken *Secret `yaml:"proxy_bearer_token,omitempty"`
|
2021-04-03 23:40:08 +02:00
|
|
|
BearerTokenFile string `yaml:"proxy_bearer_token_file,omitempty"`
|
2022-06-22 19:38:43 +02:00
|
|
|
OAuth2 *OAuth2Config `yaml:"proxy_oauth2,omitempty"`
|
2021-04-03 23:40:08 +02:00
|
|
|
TLSConfig *TLSConfig `yaml:"proxy_tls_config,omitempty"`
|
2022-06-22 19:38:43 +02:00
|
|
|
|
|
|
|
// Headers contains optional HTTP headers, which must be sent in the request to the proxy
|
|
|
|
Headers []string `yaml:"proxy_headers,omitempty"`
|
2021-04-03 23:40:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 15:20:18 +02:00
|
|
|
// OAuth2Config represent OAuth2 configuration
|
|
|
|
type OAuth2Config struct {
|
2021-05-22 16:59:23 +02:00
|
|
|
ClientID string `yaml:"client_id"`
|
2021-11-05 13:41:14 +01:00
|
|
|
ClientSecret *Secret `yaml:"client_secret,omitempty"`
|
2021-11-05 11:53:12 +01:00
|
|
|
ClientSecretFile string `yaml:"client_secret_file,omitempty"`
|
|
|
|
Scopes []string `yaml:"scopes,omitempty"`
|
2021-05-22 16:59:23 +02:00
|
|
|
TokenURL string `yaml:"token_url"`
|
2021-11-05 11:53:12 +01:00
|
|
|
EndpointParams map[string]string `yaml:"endpoint_params,omitempty"`
|
2022-04-22 22:50:51 +02:00
|
|
|
TLSConfig *TLSConfig `yaml:"tls_config,omitempty"`
|
2022-04-22 23:00:44 +02:00
|
|
|
ProxyURL string `yaml:"proxy_url,omitempty"`
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 16:59:23 +02:00
|
|
|
// String returns string representation of o.
|
|
|
|
func (o *OAuth2Config) String() string {
|
2022-04-22 23:23:19 +02:00
|
|
|
return fmt.Sprintf("clientID=%q, clientSecret=%q, clientSecretFile=%q, Scopes=%q, tokenURL=%q, endpointParams=%q, tlsConfig={%s}, proxyURL=%q",
|
|
|
|
o.ClientID, o.ClientSecret, o.ClientSecretFile, o.Scopes, o.TokenURL, o.EndpointParams, o.TLSConfig.String(), o.ProxyURL)
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OAuth2Config) validate() error {
|
2021-05-22 16:59:23 +02:00
|
|
|
if o.ClientID == "" {
|
|
|
|
return fmt.Errorf("client_id cannot be empty")
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
2021-11-05 13:41:14 +01:00
|
|
|
if o.ClientSecret == nil && o.ClientSecretFile == "" {
|
2021-05-22 15:20:18 +02:00
|
|
|
return fmt.Errorf("ClientSecret or ClientSecretFile must be set")
|
|
|
|
}
|
2021-11-05 13:41:14 +01:00
|
|
|
if o.ClientSecret != nil && o.ClientSecretFile != "" {
|
2021-05-22 16:59:23 +02:00
|
|
|
return fmt.Errorf("ClientSecret and ClientSecretFile cannot be set simultaneously")
|
|
|
|
}
|
|
|
|
if o.TokenURL == "" {
|
|
|
|
return fmt.Errorf("token_url cannot be empty")
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:59:23 +02:00
|
|
|
type oauth2ConfigInternal struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
cfg *clientcredentials.Config
|
|
|
|
clientSecretFile string
|
2022-04-22 22:50:51 +02:00
|
|
|
ctx context.Context
|
2021-05-22 16:59:23 +02:00
|
|
|
tokenSource oauth2.TokenSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOAuth2ConfigInternal(baseDir string, o *OAuth2Config) (*oauth2ConfigInternal, error) {
|
|
|
|
oi := &oauth2ConfigInternal{
|
|
|
|
cfg: &clientcredentials.Config{
|
|
|
|
ClientID: o.ClientID,
|
2021-11-05 13:41:14 +01:00
|
|
|
ClientSecret: o.ClientSecret.String(),
|
2021-05-22 16:59:23 +02:00
|
|
|
TokenURL: o.TokenURL,
|
|
|
|
Scopes: o.Scopes,
|
|
|
|
EndpointParams: urlValuesFromMap(o.EndpointParams),
|
|
|
|
},
|
|
|
|
}
|
2021-05-22 15:20:18 +02:00
|
|
|
if o.ClientSecretFile != "" {
|
2021-12-02 23:08:42 +01:00
|
|
|
oi.clientSecretFile = fs.GetFilepath(baseDir, o.ClientSecretFile)
|
2021-05-22 16:59:23 +02:00
|
|
|
secret, err := readPasswordFromFile(oi.clientSecretFile)
|
2021-05-22 15:20:18 +02:00
|
|
|
if err != nil {
|
2021-05-22 16:59:23 +02:00
|
|
|
return nil, fmt.Errorf("cannot read OAuth2 secret from %q: %w", oi.clientSecretFile, err)
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
2021-05-22 16:59:23 +02:00
|
|
|
oi.cfg.ClientSecret = secret
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
opts := &Options{
|
|
|
|
BaseDir: baseDir,
|
|
|
|
TLSConfig: o.TLSConfig,
|
|
|
|
}
|
|
|
|
ac, err := opts.NewConfig()
|
2022-04-22 22:50:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot initialize TLS config for OAuth2: %w", err)
|
|
|
|
}
|
2022-04-22 23:00:44 +02:00
|
|
|
tlsCfg := ac.NewTLSConfig()
|
|
|
|
var proxyURLFunc func(*http.Request) (*url.URL, error)
|
|
|
|
if o.ProxyURL != "" {
|
|
|
|
u, err := url.Parse(o.ProxyURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse proxy_url=%q: %w", o.ProxyURL, err)
|
|
|
|
}
|
|
|
|
proxyURLFunc = http.ProxyURL(u)
|
|
|
|
}
|
2022-04-22 22:50:51 +02:00
|
|
|
c := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2022-04-22 23:00:44 +02:00
|
|
|
TLSClientConfig: tlsCfg,
|
|
|
|
Proxy: proxyURLFunc,
|
2022-04-22 22:50:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
oi.ctx = context.WithValue(context.Background(), oauth2.HTTPClient, c)
|
|
|
|
oi.tokenSource = oi.cfg.TokenSource(oi.ctx)
|
2021-05-22 16:59:23 +02:00
|
|
|
return oi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func urlValuesFromMap(m map[string]string) url.Values {
|
|
|
|
result := make(url.Values, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
result[k] = []string{v}
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
2021-05-22 16:59:23 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oi *oauth2ConfigInternal) getTokenSource() (oauth2.TokenSource, error) {
|
|
|
|
oi.mu.Lock()
|
|
|
|
defer oi.mu.Unlock()
|
|
|
|
|
|
|
|
if oi.clientSecretFile == "" {
|
|
|
|
return oi.tokenSource, nil
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
2021-05-22 16:59:23 +02:00
|
|
|
newSecret, err := readPasswordFromFile(oi.clientSecretFile)
|
2021-05-22 15:20:18 +02:00
|
|
|
if err != nil {
|
2021-05-22 16:59:23 +02:00
|
|
|
return nil, fmt.Errorf("cannot read OAuth2 secret from %q: %w", oi.clientSecretFile, err)
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
2021-05-22 16:59:23 +02:00
|
|
|
if newSecret == oi.cfg.ClientSecret {
|
|
|
|
return oi.tokenSource, nil
|
|
|
|
}
|
|
|
|
oi.cfg.ClientSecret = newSecret
|
2022-04-22 22:50:51 +02:00
|
|
|
oi.tokenSource = oi.cfg.TokenSource(oi.ctx)
|
2021-05-22 16:59:23 +02:00
|
|
|
return oi.tokenSource, nil
|
2021-05-22 15:20:18 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:05 +02:00
|
|
|
// Config is auth config.
|
|
|
|
type Config struct {
|
|
|
|
// Optional TLS config
|
|
|
|
TLSRootCA *x509.CertPool
|
|
|
|
TLSServerName string
|
|
|
|
TLSInsecureSkipVerify bool
|
2022-04-22 23:16:34 +02:00
|
|
|
TLSMinVersion uint16
|
2021-05-14 19:00:05 +02:00
|
|
|
|
2021-07-01 14:27:44 +02:00
|
|
|
getTLSCert func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
|
|
|
|
tlsCertDigest string
|
|
|
|
|
2023-10-17 11:58:19 +02:00
|
|
|
getAuthHeader func() (string, error)
|
2021-05-14 19:00:05 +02:00
|
|
|
authHeaderLock sync.Mutex
|
|
|
|
authHeader string
|
|
|
|
authHeaderDeadline uint64
|
|
|
|
|
2022-06-22 19:38:43 +02:00
|
|
|
headers []keyValue
|
|
|
|
|
2021-05-14 19:00:05 +02:00
|
|
|
authDigest string
|
|
|
|
}
|
|
|
|
|
2022-06-22 19:38:43 +02:00
|
|
|
type keyValue struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseHeaders(headers []string) ([]keyValue, error) {
|
|
|
|
if len(headers) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
kvs := make([]keyValue, len(headers))
|
|
|
|
for i, h := range headers {
|
|
|
|
n := strings.IndexByte(h, ':')
|
|
|
|
if n < 0 {
|
|
|
|
return nil, fmt.Errorf(`missing ':' in header %q; expecting "key: value" format`, h)
|
|
|
|
}
|
|
|
|
kv := &kvs[i]
|
|
|
|
kv.key = strings.TrimSpace(h[:n])
|
|
|
|
kv.value = strings.TrimSpace(h[n+1:])
|
|
|
|
}
|
|
|
|
return kvs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadersNoAuthString returns string representation of ac headers
|
|
|
|
func (ac *Config) HeadersNoAuthString() string {
|
|
|
|
if len(ac.headers) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
a := make([]string, len(ac.headers))
|
|
|
|
for i, h := range ac.headers {
|
|
|
|
a[i] = h.key + ": " + h.value + "\r\n"
|
|
|
|
}
|
|
|
|
return strings.Join(a, "")
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:59:55 +02:00
|
|
|
// SetHeaders sets the configured ac headers to req.
|
2023-10-17 11:58:19 +02:00
|
|
|
func (ac *Config) SetHeaders(req *http.Request, setAuthHeader bool) error {
|
2022-06-22 19:38:43 +02:00
|
|
|
reqHeaders := req.Header
|
|
|
|
for _, h := range ac.headers {
|
|
|
|
reqHeaders.Set(h.key, h.value)
|
|
|
|
}
|
|
|
|
if setAuthHeader {
|
2023-10-17 11:58:19 +02:00
|
|
|
ah, err := ac.GetAuthHeader()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set request auth header: %w", err)
|
|
|
|
}
|
|
|
|
if ah != "" {
|
2022-06-22 19:38:43 +02:00
|
|
|
reqHeaders.Set("Authorization", ah)
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return nil
|
2022-06-22 19:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFasthttpHeaders sets the configured ac headers to req.
|
2023-10-17 11:58:19 +02:00
|
|
|
func (ac *Config) SetFasthttpHeaders(req *fasthttp.Request, setAuthHeader bool) error {
|
2022-06-22 19:38:43 +02:00
|
|
|
reqHeaders := &req.Header
|
|
|
|
for _, h := range ac.headers {
|
|
|
|
reqHeaders.Set(h.key, h.value)
|
|
|
|
}
|
|
|
|
if setAuthHeader {
|
2023-10-17 11:58:19 +02:00
|
|
|
ah, err := ac.GetAuthHeader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ah != "" {
|
2022-06-22 19:38:43 +02:00
|
|
|
reqHeaders.Set("Authorization", ah)
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return nil
|
2022-06-22 19:38:43 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:00:05 +02:00
|
|
|
// GetAuthHeader returns optional `Authorization: ...` http header.
|
2023-10-17 11:58:19 +02:00
|
|
|
func (ac *Config) GetAuthHeader() (string, error) {
|
2021-05-14 19:00:05 +02:00
|
|
|
f := ac.getAuthHeader
|
|
|
|
if f == nil {
|
2023-10-17 11:58:19 +02:00
|
|
|
return "", nil
|
2021-05-14 19:00:05 +02:00
|
|
|
}
|
|
|
|
ac.authHeaderLock.Lock()
|
|
|
|
defer ac.authHeaderLock.Unlock()
|
|
|
|
if fasttime.UnixTimestamp() > ac.authHeaderDeadline {
|
2023-10-17 11:58:19 +02:00
|
|
|
var err error
|
|
|
|
if ac.authHeader, err = f(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-14 19:00:05 +02:00
|
|
|
// Cache the authHeader for a second.
|
|
|
|
ac.authHeaderDeadline = fasttime.UnixTimestamp() + 1
|
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return ac.authHeader, nil
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:00:05 +02:00
|
|
|
// String returns human-readable representation for ac.
|
2022-06-04 00:01:13 +02:00
|
|
|
//
|
|
|
|
// It is also used for comparing Config objects for equality. If two Config
|
|
|
|
// objects have the same string representation, then they are considered equal.
|
2020-05-03 11:41:13 +02:00
|
|
|
func (ac *Config) String() string {
|
2022-06-22 19:38:43 +02:00
|
|
|
return fmt.Sprintf("AuthDigest=%s, Headers=%s, TLSRootCA=%s, TLSCertificate=%s, TLSServerName=%s, TLSInsecureSkipVerify=%v, TLSMinVersion=%d",
|
|
|
|
ac.authDigest, ac.headers, ac.tlsRootCAString(), ac.tlsCertDigest, ac.TLSServerName, ac.TLSInsecureSkipVerify, ac.TLSMinVersion)
|
2020-05-03 11:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *Config) tlsRootCAString() string {
|
|
|
|
if ac.TLSRootCA == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
data := ac.TLSRootCA.Subjects()
|
|
|
|
return string(bytes.Join(data, []byte("\n")))
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:05 +02:00
|
|
|
// NewTLSConfig returns new TLS config for the given ac.
|
|
|
|
func (ac *Config) NewTLSConfig() *tls.Config {
|
|
|
|
tlsCfg := &tls.Config{
|
|
|
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
|
|
|
}
|
2021-03-09 17:54:09 +01:00
|
|
|
if ac == nil {
|
|
|
|
return tlsCfg
|
|
|
|
}
|
2021-07-02 12:20:15 +02:00
|
|
|
if ac.getTLSCert != nil {
|
|
|
|
var certLock sync.Mutex
|
|
|
|
var cert *tls.Certificate
|
|
|
|
var certDeadline uint64
|
|
|
|
tlsCfg.GetClientCertificate = func(cri *tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
// Cache the certificate for up to a second in order to save CPU time
|
|
|
|
// on certificate parsing when TLS connection are frequently re-established.
|
|
|
|
certLock.Lock()
|
|
|
|
defer certLock.Unlock()
|
|
|
|
if fasttime.UnixTimestamp() > certDeadline {
|
|
|
|
c, err := ac.getTLSCert(cri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cert = c
|
|
|
|
certDeadline = fasttime.UnixTimestamp() + 1
|
|
|
|
}
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 17:54:09 +01:00
|
|
|
tlsCfg.RootCAs = ac.TLSRootCA
|
2020-04-13 11:59:05 +02:00
|
|
|
tlsCfg.ServerName = ac.TLSServerName
|
|
|
|
tlsCfg.InsecureSkipVerify = ac.TLSInsecureSkipVerify
|
2022-04-22 23:16:34 +02:00
|
|
|
tlsCfg.MinVersion = ac.TLSMinVersion
|
2022-09-26 16:35:45 +02:00
|
|
|
// Do not set tlsCfg.MaxVersion, since this has no sense from security PoV.
|
|
|
|
// This can only result in lower security level if improperly set.
|
2020-04-13 11:59:05 +02:00
|
|
|
return tlsCfg
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:17:43 +02:00
|
|
|
// NewConfig creates auth config for the given hcc.
|
|
|
|
func (hcc *HTTPClientConfig) NewConfig(baseDir string) (*Config, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
opts := &Options{
|
|
|
|
BaseDir: baseDir,
|
|
|
|
Authorization: hcc.Authorization,
|
|
|
|
BasicAuth: hcc.BasicAuth,
|
|
|
|
BearerToken: hcc.BearerToken.String(),
|
|
|
|
BearerTokenFile: hcc.BearerTokenFile,
|
|
|
|
OAuth2: hcc.OAuth2,
|
|
|
|
TLSConfig: hcc.TLSConfig,
|
|
|
|
Headers: hcc.Headers,
|
|
|
|
}
|
|
|
|
return opts.NewConfig()
|
2021-04-02 20:17:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 23:40:08 +02:00
|
|
|
// NewConfig creates auth config for the given pcc.
|
|
|
|
func (pcc *ProxyClientConfig) NewConfig(baseDir string) (*Config, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
opts := &Options{
|
|
|
|
BaseDir: baseDir,
|
|
|
|
Authorization: pcc.Authorization,
|
|
|
|
BasicAuth: pcc.BasicAuth,
|
|
|
|
BearerToken: pcc.BearerToken.String(),
|
|
|
|
BearerTokenFile: pcc.BearerTokenFile,
|
|
|
|
OAuth2: pcc.OAuth2,
|
|
|
|
TLSConfig: pcc.TLSConfig,
|
|
|
|
Headers: pcc.Headers,
|
|
|
|
}
|
|
|
|
return opts.NewConfig()
|
2022-06-22 19:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig creates auth config for the given ba.
|
|
|
|
func (ba *BasicAuthConfig) NewConfig(baseDir string) (*Config, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
opts := &Options{
|
|
|
|
BaseDir: baseDir,
|
|
|
|
BasicAuth: ba,
|
|
|
|
}
|
|
|
|
return opts.NewConfig()
|
2022-04-22 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
// Options contain options, which must be passed to NewConfig.
|
|
|
|
type Options struct {
|
|
|
|
// BaseDir is an optional path to a base directory for resolving
|
|
|
|
// relative filepaths in various config options.
|
|
|
|
//
|
|
|
|
// It is set to the current directory by default.
|
|
|
|
BaseDir string
|
|
|
|
|
|
|
|
// Authorization contains optional Authorization.
|
|
|
|
Authorization *Authorization
|
|
|
|
|
|
|
|
// BasicAuth contains optional BasicAuthConfig.
|
|
|
|
BasicAuth *BasicAuthConfig
|
|
|
|
|
|
|
|
// BearerToken contains optional bearer token.
|
|
|
|
BearerToken string
|
|
|
|
|
|
|
|
// BearerTokenFile contains optional path to a file with bearer token.
|
|
|
|
BearerTokenFile string
|
|
|
|
|
|
|
|
// OAuth2 contains optional OAuth2Config.
|
|
|
|
OAuth2 *OAuth2Config
|
|
|
|
|
|
|
|
// TLSconfig contains optional TLSConfig.
|
|
|
|
TLSConfig *TLSConfig
|
|
|
|
|
|
|
|
// Headers contains optional http request headers in the form 'Foo: bar'.
|
|
|
|
Headers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig creates auth config from the given opts.
|
|
|
|
func (opts *Options) NewConfig() (*Config, error) {
|
|
|
|
baseDir := opts.BaseDir
|
|
|
|
if baseDir == "" {
|
|
|
|
baseDir = "."
|
|
|
|
}
|
|
|
|
var actx authContext
|
|
|
|
if opts.Authorization != nil {
|
|
|
|
if err := actx.initFromAuthorization(baseDir, opts.Authorization); err != nil {
|
|
|
|
return nil, err
|
2021-04-02 20:17:43 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if opts.BasicAuth != nil {
|
|
|
|
if actx.getAuthHeader != nil {
|
2021-04-02 20:17:43 +02:00
|
|
|
return nil, fmt.Errorf("cannot use both `authorization` and `basic_auth`")
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if err := actx.initFromBasicAuthConfig(baseDir, opts.BasicAuth); err != nil {
|
|
|
|
return nil, err
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if opts.BearerTokenFile != "" {
|
|
|
|
if actx.getAuthHeader != nil {
|
2021-04-02 20:17:43 +02:00
|
|
|
return nil, fmt.Errorf("cannot simultaneously use `authorization`, `basic_auth` and `bearer_token_file`")
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if opts.BearerToken != "" {
|
|
|
|
return nil, fmt.Errorf("both `bearer_token`=%q and `bearer_token_file`=%q are set", opts.BearerToken, opts.BearerTokenFile)
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if err := actx.initFromBearerTokenFile(baseDir, opts.BearerTokenFile); err != nil {
|
|
|
|
return nil, err
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if opts.BearerToken != "" {
|
|
|
|
if actx.getAuthHeader != nil {
|
2021-04-02 20:17:43 +02:00
|
|
|
return nil, fmt.Errorf("cannot simultaneously use `authorization`, `basic_auth` and `bearer_token`")
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if err := actx.initFromBearerToken(opts.BearerToken); err != nil {
|
|
|
|
return nil, err
|
2021-05-14 19:00:05 +02:00
|
|
|
}
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if opts.OAuth2 != nil {
|
|
|
|
if actx.getAuthHeader != nil {
|
2021-05-22 15:20:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot simultaneously use `authorization`, `basic_auth, `bearer_token` and `ouath2`")
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
if err := actx.initFromOAuth2Config(baseDir, opts.OAuth2); err != nil {
|
2021-05-22 15:20:18 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
var tctx tlsContext
|
|
|
|
if opts.TLSConfig != nil {
|
|
|
|
if err := tctx.initFromTLSConfig(baseDir, opts.TLSConfig); err != nil {
|
|
|
|
return nil, err
|
2022-04-22 23:16:34 +02:00
|
|
|
}
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
headers, err := parseHeaders(opts.Headers)
|
2022-06-22 19:38:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-13 11:59:05 +02:00
|
|
|
ac := &Config{
|
2022-07-04 13:27:48 +02:00
|
|
|
TLSRootCA: tctx.rootCA,
|
|
|
|
TLSServerName: tctx.serverName,
|
|
|
|
TLSInsecureSkipVerify: tctx.insecureSkipVerify,
|
|
|
|
TLSMinVersion: tctx.minVersion,
|
2021-05-14 19:00:05 +02:00
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
getTLSCert: tctx.getTLSCert,
|
|
|
|
tlsCertDigest: tctx.tlsCertDigest,
|
2021-07-01 14:27:44 +02:00
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
getAuthHeader: actx.getAuthHeader,
|
|
|
|
headers: headers,
|
|
|
|
authDigest: actx.authDigest,
|
2020-04-13 11:59:05 +02:00
|
|
|
}
|
|
|
|
return ac, nil
|
|
|
|
}
|
2022-04-22 23:16:34 +02:00
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
type authContext struct {
|
|
|
|
// getAuthHeader must return <value> for 'Authorization: <value>' http request header
|
2023-10-17 11:58:19 +02:00
|
|
|
getAuthHeader func() (string, error)
|
2022-07-04 13:27:48 +02:00
|
|
|
|
|
|
|
// authDigest must contain the digest for the used authorization
|
|
|
|
// The digest must be changed whenever the original config changes.
|
|
|
|
authDigest string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actx *authContext) initFromAuthorization(baseDir string, az *Authorization) error {
|
|
|
|
azType := "Bearer"
|
|
|
|
if az.Type != "" {
|
|
|
|
azType = az.Type
|
|
|
|
}
|
|
|
|
if az.CredentialsFile == "" {
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
|
|
|
return azType + " " + az.Credentials.String(), nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("custom(type=%q, creds=%q)", az.Type, az.Credentials)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if az.Credentials != nil {
|
|
|
|
return fmt.Errorf("both `credentials`=%q and `credentials_file`=%q are set", az.Credentials, az.CredentialsFile)
|
|
|
|
}
|
|
|
|
filePath := fs.GetFilepath(baseDir, az.CredentialsFile)
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
token, err := readPasswordFromFile(filePath)
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot read credentials from `credentials_file`=%q: %w", az.CredentialsFile, err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return azType + " " + token, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("custom(type=%q, credsFile=%q)", az.Type, filePath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actx *authContext) initFromBasicAuthConfig(baseDir string, ba *BasicAuthConfig) error {
|
|
|
|
if ba.Username == "" {
|
|
|
|
return fmt.Errorf("missing `username` in `basic_auth` section")
|
|
|
|
}
|
|
|
|
if ba.PasswordFile == "" {
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
// See https://en.wikipedia.org/wiki/Basic_access_authentication
|
|
|
|
token := ba.Username + ":" + ba.Password.String()
|
|
|
|
token64 := base64.StdEncoding.EncodeToString([]byte(token))
|
2023-10-17 11:58:19 +02:00
|
|
|
return "Basic " + token64, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("basic(username=%q, password=%q)", ba.Username, ba.Password)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ba.Password != nil {
|
|
|
|
return fmt.Errorf("both `password`=%q and `password_file`=%q are set in `basic_auth` section", ba.Password, ba.PasswordFile)
|
|
|
|
}
|
|
|
|
filePath := fs.GetFilepath(baseDir, ba.PasswordFile)
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
password, err := readPasswordFromFile(filePath)
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot read password from `password_file`=%q set in `basic_auth` section: %w", ba.PasswordFile, err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
// See https://en.wikipedia.org/wiki/Basic_access_authentication
|
|
|
|
token := ba.Username + ":" + password
|
|
|
|
token64 := base64.StdEncoding.EncodeToString([]byte(token))
|
2023-10-17 11:58:19 +02:00
|
|
|
return "Basic " + token64, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("basic(username=%q, passwordFile=%q)", ba.Username, filePath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actx *authContext) initFromBearerTokenFile(baseDir string, bearerTokenFile string) error {
|
|
|
|
filePath := fs.GetFilepath(baseDir, bearerTokenFile)
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
2022-07-04 13:27:48 +02:00
|
|
|
token, err := readPasswordFromFile(filePath)
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot read bearer token from `bearer_token_file`=%q: %w", bearerTokenFile, err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return "Bearer " + token, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("bearer(tokenFile=%q)", filePath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actx *authContext) initFromBearerToken(bearerToken string) error {
|
2023-10-17 11:58:19 +02:00
|
|
|
actx.getAuthHeader = func() (string, error) {
|
|
|
|
return "Bearer " + bearerToken, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("bearer(token=%q)", bearerToken)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actx *authContext) initFromOAuth2Config(baseDir string, o *OAuth2Config) error {
|
2023-10-17 11:58:19 +02:00
|
|
|
if err := o.validate(); err != nil {
|
2022-07-04 13:27:48 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
|
|
|
|
actx.getAuthHeader = func() (string, error) {
|
|
|
|
oi, err := newOAuth2ConfigInternal(baseDir, o)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-04 13:27:48 +02:00
|
|
|
ts, err := oi.getTokenSource()
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot get OAuth2 tokenSource: %w", err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
t, err := ts.Token()
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot get OAuth2 token: %w", err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
2023-10-17 11:58:19 +02:00
|
|
|
return t.Type() + " " + t.AccessToken, nil
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
|
|
|
actx.authDigest = fmt.Sprintf("oauth2(%s)", o.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type tlsContext struct {
|
2023-10-17 11:58:19 +02:00
|
|
|
getTLSCert func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
|
|
|
|
tlsCertDigest string
|
|
|
|
|
2022-07-04 13:27:48 +02:00
|
|
|
rootCA *x509.CertPool
|
|
|
|
serverName string
|
|
|
|
insecureSkipVerify bool
|
|
|
|
minVersion uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tctx *tlsContext) initFromTLSConfig(baseDir string, tc *TLSConfig) error {
|
|
|
|
tctx.serverName = tc.ServerName
|
|
|
|
tctx.insecureSkipVerify = tc.InsecureSkipVerify
|
|
|
|
if len(tc.Key) != 0 || len(tc.Cert) != 0 {
|
|
|
|
cert, err := tls.X509KeyPair(tc.Cert, tc.Key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot load TLS certificate from the provided `cert` and `key` values: %w", err)
|
|
|
|
}
|
|
|
|
tctx.getTLSCert = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
h := xxhash.Sum64(tc.Key) ^ xxhash.Sum64(tc.Cert)
|
|
|
|
tctx.tlsCertDigest = fmt.Sprintf("digest(key+cert)=%d", h)
|
|
|
|
} else if tc.CertFile != "" || tc.KeyFile != "" {
|
|
|
|
tctx.getTLSCert = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
// Re-read TLS certificate from disk. This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1420
|
|
|
|
certPath := fs.GetFilepath(baseDir, tc.CertFile)
|
|
|
|
keyPath := fs.GetFilepath(baseDir, tc.KeyFile)
|
|
|
|
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot load TLS certificate from `cert_file`=%q, `key_file`=%q: %w", tc.CertFile, tc.KeyFile, err)
|
|
|
|
}
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
// Check whether the configured TLS cert can be loaded.
|
|
|
|
if _, err := tctx.getTLSCert(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tctx.tlsCertDigest = fmt.Sprintf("certFile=%q, keyFile=%q", tc.CertFile, tc.KeyFile)
|
|
|
|
}
|
|
|
|
if len(tc.CA) != 0 {
|
|
|
|
tctx.rootCA = x509.NewCertPool()
|
|
|
|
if !tctx.rootCA.AppendCertsFromPEM(tc.CA) {
|
|
|
|
return fmt.Errorf("cannot parse data from `ca` value")
|
|
|
|
}
|
|
|
|
} else if tc.CAFile != "" {
|
|
|
|
path := fs.GetFilepath(baseDir, tc.CAFile)
|
|
|
|
data, err := fs.ReadFileOrHTTP(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot read `ca_file` %q: %w", tc.CAFile, err)
|
|
|
|
}
|
|
|
|
tctx.rootCA = x509.NewCertPool()
|
|
|
|
if !tctx.rootCA.AppendCertsFromPEM(data) {
|
|
|
|
return fmt.Errorf("cannot parse data from `ca_file` %q", tc.CAFile)
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 16:35:45 +02:00
|
|
|
v, err := netutil.ParseTLSVersion(tc.MinVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse `min_version`: %w", err)
|
2022-07-04 13:27:48 +02:00
|
|
|
}
|
2022-09-26 16:35:45 +02:00
|
|
|
tctx.minVersion = v
|
2022-07-04 13:27:48 +02:00
|
|
|
return nil
|
|
|
|
}
|