Skip to content

Commit 36f54f3

Browse files
committed
Add consul tls
Signed-off-by: SungJin1212 <[email protected]>
1 parent 24efa2b commit 36f54f3

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [ENHANCEMENT] Distributor: Add new `cortex_reduced_resolution_histogram_samples_total` metric to track the number of histogram samples which resolution was reduced. #6182
3333
* [ENHANCEMENT] StoreGateway: Implement metadata API limit in queryable. #6195
3434
* [ENHANCEMENT] Ingester: Add matchers to ingester LabelNames() and LabelNamesStream() RPC. #6209
35+
* [ENHANCEMENT] KV: Add TLS configs to consul. #6374
3536
* [ENHANCEMENT] Ingester/Store Gateway Clients: Introduce an experimental HealthCheck handler to quickly fail requests directed to unhealthy targets. #6225 #6257
3637
* [ENHANCEMENT] Upgrade build image and Go version to 1.23.2. #6261 #6262
3738
* [ENHANCEMENT] Ingester: Introduce a new experimental feature for caching expanded postings on the ingester. #6296

docs/configuration/config-file-reference.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -2445,7 +2445,7 @@ The `consul_config` configures the consul client. The supported CLI flags `<pref
24452445
# CLI flag: -<prefix>.consul.acl-token
24462446
[acl_token: <string> | default = ""]
24472447
2448-
# HTTP timeout when talking to Consul
2448+
# HTTP timeout when talking to Consul.
24492449
# CLI flag: -<prefix>.consul.client-timeout
24502450
[http_client_timeout: <duration> | default = 20s]
24512451
@@ -2461,6 +2461,33 @@ The `consul_config` configures the consul client. The supported CLI flags `<pref
24612461
# Burst size used in rate limit. Values less than 1 are treated as 1.
24622462
# CLI flag: -<prefix>.consul.watch-burst-size
24632463
[watch_burst_size: <int> | default = 1]
2464+
2465+
# Enable TLS.
2466+
# CLI flag: -<prefix>.consul.tls-enabled
2467+
[tls_enabled: <boolean> | default = false]
2468+
2469+
# Path to the client certificate file, which will be used for authenticating
2470+
# with the server. Also requires the key path to be configured.
2471+
# CLI flag: -<prefix>.consul.tls-cert-path
2472+
[tls_cert_path: <string> | default = ""]
2473+
2474+
# Path to the key file for the client certificate. Also requires the client
2475+
# certificate to be configured.
2476+
# CLI flag: -<prefix>.consul.tls-key-path
2477+
[tls_key_path: <string> | default = ""]
2478+
2479+
# Path to the CA certificates file to validate server certificate against. If
2480+
# not set, the host's root CA certificates are used.
2481+
# CLI flag: -<prefix>.consul.tls-ca-path
2482+
[tls_ca_path: <string> | default = ""]
2483+
2484+
# Override the expected name on the server certificate.
2485+
# CLI flag: -<prefix>.consul.tls-server-name
2486+
[tls_server_name: <string> | default = ""]
2487+
2488+
# Skip validating server certificate.
2489+
# CLI flag: -<prefix>.consul.tls-insecure-skip-verify
2490+
[tls_insecure_skip_verify: <boolean> | default = false]
24642491
```
24652492

24662493
### `distributor_config`

pkg/ring/kv/consul/client.go

+47-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/cortexproject/cortex/pkg/ring/kv/codec"
2121
"github.com/cortexproject/cortex/pkg/util/backoff"
2222
"github.com/cortexproject/cortex/pkg/util/flagext"
23+
cortextls "github.com/cortexproject/cortex/pkg/util/tls"
2324
)
2425

2526
const (
@@ -40,12 +41,14 @@ var (
4041

4142
// Config to create a ConsulClient
4243
type Config struct {
43-
Host string `yaml:"host"`
44-
ACLToken flagext.Secret `yaml:"acl_token"`
45-
HTTPClientTimeout time.Duration `yaml:"http_client_timeout"`
46-
ConsistentReads bool `yaml:"consistent_reads"`
47-
WatchKeyRateLimit float64 `yaml:"watch_rate_limit"` // Zero disables rate limit
48-
WatchKeyBurstSize int `yaml:"watch_burst_size"` // Burst when doing rate-limit, defaults to 1
44+
Host string `yaml:"host"`
45+
ACLToken flagext.Secret `yaml:"acl_token"`
46+
HTTPClientTimeout time.Duration `yaml:"http_client_timeout"`
47+
ConsistentReads bool `yaml:"consistent_reads"`
48+
WatchKeyRateLimit float64 `yaml:"watch_rate_limit"` // Zero disables rate limit
49+
WatchKeyBurstSize int `yaml:"watch_burst_size"` // Burst when doing rate-limit, defaults to 1
50+
EnableTLS bool `yaml:"tls_enabled"`
51+
TLS cortextls.ClientConfig `yaml:",inline"`
4952

5053
// Used in tests only.
5154
MaxCasRetries int `yaml:"-"`
@@ -74,24 +77,53 @@ type Client struct {
7477
func (cfg *Config) RegisterFlags(f *flag.FlagSet, prefix string) {
7578
f.StringVar(&cfg.Host, prefix+"consul.hostname", "localhost:8500", "Hostname and port of Consul.")
7679
f.Var(&cfg.ACLToken, prefix+"consul.acl-token", "ACL Token used to interact with Consul.")
77-
f.DurationVar(&cfg.HTTPClientTimeout, prefix+"consul.client-timeout", 2*longPollDuration, "HTTP timeout when talking to Consul")
80+
f.DurationVar(&cfg.HTTPClientTimeout, prefix+"consul.client-timeout", 2*longPollDuration, "HTTP timeout when talking to Consul.")
7881
f.BoolVar(&cfg.ConsistentReads, prefix+"consul.consistent-reads", false, "Enable consistent reads to Consul.")
7982
f.Float64Var(&cfg.WatchKeyRateLimit, prefix+"consul.watch-rate-limit", 1, "Rate limit when watching key or prefix in Consul, in requests per second. 0 disables the rate limit.")
8083
f.IntVar(&cfg.WatchKeyBurstSize, prefix+"consul.watch-burst-size", 1, "Burst size used in rate limit. Values less than 1 are treated as 1.")
84+
f.BoolVar(&cfg.EnableTLS, prefix+"consul.tls-enabled", false, "Enable TLS.")
85+
cfg.TLS.RegisterFlagsWithPrefix(prefix+"consul", f)
86+
}
87+
88+
func (cfg *Config) GetTLS() *consul.TLSConfig {
89+
return &consul.TLSConfig{
90+
Address: cfg.TLS.ServerName,
91+
CertFile: cfg.TLS.CertPath,
92+
KeyFile: cfg.TLS.KeyPath,
93+
CAFile: cfg.TLS.CAPath,
94+
InsecureSkipVerify: cfg.TLS.InsecureSkipVerify,
95+
}
8196
}
8297

8398
// NewClient returns a new Client.
8499
func NewClient(cfg Config, codec codec.Codec, logger log.Logger, registerer prometheus.Registerer) (*Client, error) {
85-
client, err := consul.NewClient(&consul.Config{
100+
scheme := "http"
101+
transport := cleanhttp.DefaultPooledTransport()
102+
103+
config := &consul.Config{
86104
Address: cfg.Host,
87105
Token: cfg.ACLToken.Value,
88-
Scheme: "http",
89-
HttpClient: &http.Client{
90-
Transport: cleanhttp.DefaultPooledTransport(),
91-
// See https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
92-
Timeout: cfg.HTTPClientTimeout,
93-
},
94-
})
106+
}
107+
108+
if cfg.EnableTLS {
109+
tlsConfig := cfg.GetTLS()
110+
tlsClientConfig, err := consul.SetupTLSConfig(tlsConfig)
111+
if err != nil {
112+
return nil, err
113+
}
114+
transport.TLSClientConfig = tlsClientConfig
115+
scheme = "https"
116+
config.TLSConfig = *tlsConfig
117+
}
118+
119+
config.Scheme = scheme
120+
config.HttpClient = &http.Client{
121+
Transport: transport,
122+
// See https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
123+
Timeout: cfg.HTTPClientTimeout,
124+
}
125+
126+
client, err := consul.NewClient(config)
95127
if err != nil {
96128
return nil, err
97129
}

0 commit comments

Comments
 (0)