/
config.go
132 lines (118 loc) · 4.66 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
*
* Copyright 2022 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package promquery
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"time"
prom_api "github.com/prometheus/client_golang/api"
prom_v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/sapcc/go-bits/osext"
)
// Config contains the set of configuration parameters that are required for
// establishing a connection to a Prometheus instance's API.
type Config struct {
// Required: Main URL of Prometheus instance.
ServerURL string `yaml:"url"`
// Optional: To check validity of TLS server certificate.
ServerCACertificatePath string `yaml:"ca_cert"`
// Optional: TLS client certificate to present while connecting.
ClientCertificatePath string `yaml:"cert"`
// Required if ClientCertificatePath is given: Private key for TLS client certificate.
ClientCertificateKeyPath string `yaml:"key"`
// Cache for repeated calls to Connect().
cachedConnection prom_v1.API `yaml:"-"`
}
// ConfigFromEnv fills a Config object from the following environment variables:
//
// ${envPrefix}_URL - required
// ${envPrefix}_CACERT - optional
// ${envPrefix}_CERT - optional
// ${envPrefix}_KEY - optional (required if client cert is given)
//
// This function exits through logg.Fatal() if a required value is missing.
func ConfigFromEnv(envPrefix string) Config {
cfg := Config{
ServerURL: osext.MustGetenv(envPrefix + "_URL"),
ServerCACertificatePath: os.Getenv(envPrefix + "_CACERT"),
ClientCertificatePath: os.Getenv(envPrefix + "_CERT"),
}
if cfg.ClientCertificatePath != "" {
cfg.ClientCertificateKeyPath = osext.MustGetenv(envPrefix + "_KEY")
}
return cfg
}
// Connect sets up a Prometheus client from the given Config.
func (cfg Config) Connect() (Client, error) {
if cfg.cachedConnection != nil {
return Client{cfg.cachedConnection}, nil
}
if cfg.ServerURL == "" {
return Client{}, errors.New("cannot connect to Prometheus: missing server URL")
}
if cfg.ClientCertificatePath == "" && cfg.ClientCertificateKeyPath != "" {
return Client{}, fmt.Errorf("cannot connect to Prometheus at %s: private key given, but no client certificate given", cfg.ServerURL)
}
if cfg.ClientCertificatePath != "" && cfg.ClientCertificateKeyPath == "" {
return Client{}, fmt.Errorf("cannot connect to Prometheus at %s: client certificate given, but no private key given", cfg.ServerURL)
}
// same configuration as prom_api.DefaultRoundTripper (but we cannot just clone it because it contains a Mutex)
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
}
transport.TLSClientConfig = &tls.Config{} //nolint:gosec // used for a client which defaults to TLS version 1.2
if cfg.ClientCertificatePath != "" {
clientCert, err := tls.LoadX509KeyPair(cfg.ClientCertificatePath, cfg.ClientCertificateKeyPath)
if err != nil {
return Client{}, fmt.Errorf("cannot load client certificate from %s and %s: %w",
cfg.ClientCertificatePath, cfg.ClientCertificateKeyPath, err)
}
transport.TLSClientConfig.Certificates = []tls.Certificate{clientCert}
}
if cfg.ServerCACertificatePath != "" {
serverCACert, err := os.ReadFile(cfg.ServerCACertificatePath)
if err != nil {
return Client{}, fmt.Errorf("cannot load CA certificate from %s: %w",
cfg.ServerCACertificatePath, err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(serverCACert)
transport.TLSClientConfig.RootCAs = certPool
}
promCfg := prom_api.Config{
Address: cfg.ServerURL,
RoundTripper: transport,
}
client, err := prom_api.NewClient(promCfg)
if err != nil {
return Client{}, fmt.Errorf("cannot connect to Prometheus at %s: %w", cfg.ServerURL, err)
}
cfg.cachedConnection = prom_v1.NewAPI(client) // speed up future calls to Connect()
return Client{cfg.cachedConnection}, nil
}