Skip to content

Commit 065a59a

Browse files
author
Julien Pivotto
committed
Use upstream parameter follow_redirects
Signed-off-by: Julien Pivotto <[email protected]>
1 parent 054784b commit 065a59a

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
This release is built with go 1.16.4, which contains a [bugfix](https://github.com/golang/go/issues/45712)
44
that can cause an untrusted target to make Blackbox Exporter crash.
55

6+
In the HTTP probe, `no_follow_redirect` has been changed to `follow_redirect`.
7+
This release accepts both, with a precedence to the `no_follow_redirect` parameter.
8+
In the next release, `no_follow_redirect` will be removed.
9+
10+
* [CHANGE] HTTP proble: no_follow_redirect has been renamed to follow_redirect.
611
* [FEATURE] Add support for decompression of HTTP responses. #764
712
* [FEATURE] Enable TLS and basic authentication. #784
813
* [FEATURE] HTTP probe: *experimental* OAuth2 support.

CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The other placeholders are specified separately.
6262
[ compression: <string> | default = "" ]
6363

6464
# Whether or not the probe will follow any redirects.
65-
[ no_follow_redirects: <boolean> | default = false ]
65+
[ follow_redirects: <boolean> | default = true ]
6666

6767
# Probe fails if SSL is present.
6868
[ fail_if_ssl: <boolean> | default = false ]

config/config.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828

2929
yaml "gopkg.in/yaml.v3"
3030

31+
"github.com/go-kit/kit/log"
32+
"github.com/go-kit/kit/log/level"
3133
"github.com/miekg/dns"
3234
"github.com/prometheus/client_golang/prometheus"
3335
"github.com/prometheus/common/config"
@@ -57,6 +59,7 @@ var (
5759
// DefaultHTTPProbe set default value for HTTPProbe
5860
DefaultHTTPProbe = HTTPProbe{
5961
IPProtocolFallback: true,
62+
HTTPClientConfig: config.DefaultHTTPClientConfig,
6063
}
6164

6265
// DefaultTCPProbe set default value for TCPProbe
@@ -89,7 +92,7 @@ type SafeConfig struct {
8992
C *Config
9093
}
9194

92-
func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
95+
func (sc *SafeConfig) ReloadConfig(confFile string, logger log.Logger) (err error) {
9396
var c = &Config{}
9497
defer func() {
9598
if err != nil {
@@ -112,6 +115,17 @@ func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
112115
return fmt.Errorf("error parsing config file: %s", err)
113116
}
114117

118+
for name, module := range c.Modules {
119+
if module.HTTP.NoFollowRedirects != nil {
120+
// Hide the old flag from the /config page.
121+
module.HTTP.NoFollowRedirects = nil
122+
c.Modules[name] = module
123+
if logger != nil {
124+
level.Warn(logger).Log("msg", "no_follow_redirects is deprecated and will be removed in the next release. It is replaced by follow_redirects.", "module", name)
125+
}
126+
}
127+
}
128+
115129
sc.Lock()
116130
sc.C = c
117131
sc.Unlock()
@@ -181,7 +195,7 @@ type HTTPProbe struct {
181195
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
182196
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
183197
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
184-
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
198+
NoFollowRedirects *bool `yaml:"no_follow_redirects,omitempty"`
185199
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
186200
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
187201
Method string `yaml:"method,omitempty"`
@@ -277,6 +291,10 @@ func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
277291
return err
278292
}
279293

294+
if s.NoFollowRedirects != nil {
295+
s.HTTPClientConfig.FollowRedirects = !*s.NoFollowRedirects
296+
}
297+
280298
for key, value := range s.Headers {
281299
switch strings.Title(key) {
282300
case "Accept-Encoding":

config/config_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) {
2525
C: &Config{},
2626
}
2727

28-
err := sc.ReloadConfig("testdata/blackbox-good.yml")
28+
err := sc.ReloadConfig("testdata/blackbox-good.yml", nil)
2929
if err != nil {
3030
t.Errorf("Error loading config %v: %v", "blackbox.yml", err)
3131
}
@@ -90,7 +90,7 @@ func TestLoadBadConfigs(t *testing.T) {
9090
}
9191
for _, test := range tests {
9292
t.Run(test.input, func(t *testing.T) {
93-
got := sc.ReloadConfig(test.input)
93+
got := sc.ReloadConfig(test.input, nil)
9494
if got == nil || got.Error() != test.want {
9595
t.Fatalf("ReloadConfig(%q) = %v; want %q", test.input, got, test.want)
9696
}
@@ -103,7 +103,7 @@ func TestHideConfigSecrets(t *testing.T) {
103103
C: &Config{},
104104
}
105105

106-
err := sc.ReloadConfig("testdata/blackbox-good.yml")
106+
err := sc.ReloadConfig("testdata/blackbox-good.yml", nil)
107107
if err != nil {
108108
t.Errorf("Error loading config %v: %v", "testdata/blackbox-good.yml", err)
109109
}

main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func run() int {
224224
level.Info(logger).Log("msg", "Starting blackbox_exporter", "version", version.Info())
225225
level.Info(logger).Log("build_context", version.BuildContext())
226226

227-
if err := sc.ReloadConfig(*configFile); err != nil {
227+
if err := sc.ReloadConfig(*configFile, logger); err != nil {
228228
level.Error(logger).Log("msg", "Error loading config", "err", err)
229229
return 1
230230
}
@@ -265,13 +265,13 @@ func run() int {
265265
for {
266266
select {
267267
case <-hup:
268-
if err := sc.ReloadConfig(*configFile); err != nil {
268+
if err := sc.ReloadConfig(*configFile, logger); err != nil {
269269
level.Error(logger).Log("msg", "Error reloading config", "err", err)
270270
continue
271271
}
272272
level.Info(logger).Log("msg", "Reloaded config file")
273273
case rc := <-reloadCh:
274-
if err := sc.ReloadConfig(*configFile); err != nil {
274+
if err := sc.ReloadConfig(*configFile, logger); err != nil {
275275
level.Error(logger).Log("msg", "Error reloading config", "err", err)
276276
rc <- err
277277
} else {

prober/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
373373
client.CheckRedirect = func(r *http.Request, via []*http.Request) error {
374374
level.Info(logger).Log("msg", "Received redirect", "location", r.Response.Header.Get("Location"))
375375
redirects = len(via)
376-
if redirects > 10 || httpConfig.NoFollowRedirects {
376+
if redirects > 10 || !httpConfig.HTTPClientConfig.FollowRedirects {
377377
level.Info(logger).Log("msg", "Not following redirect")
378378
return errors.New("don't follow redirects")
379379
}

prober/http_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func TestRedirectFollowed(t *testing.T) {
526526
registry := prometheus.NewRegistry()
527527
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
528528
defer cancel()
529-
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true}}, registry, log.NewNopLogger())
529+
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, HTTPClientConfig: pconfig.DefaultHTTPClientConfig}}, registry, log.NewNopLogger())
530530
body := recorder.Body.String()
531531
if !result {
532532
t.Fatalf("Redirect test failed unexpectedly, got %s", body)
@@ -554,7 +554,7 @@ func TestRedirectNotFollowed(t *testing.T) {
554554
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
555555
defer cancel()
556556
result := ProbeHTTP(testCTX, ts.URL,
557-
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, NoFollowRedirects: true, ValidStatusCodes: []int{302}}}, registry, log.NewNopLogger())
557+
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, HTTPClientConfig: pconfig.HTTPClientConfig{FollowRedirects: false}, ValidStatusCodes: []int{302}}}, registry, log.NewNopLogger())
558558
body := recorder.Body.String()
559559
if !result {
560560
t.Fatalf("Redirect test failed unexpectedly, got %s", body)
@@ -601,7 +601,7 @@ func TestRedirectionLimit(t *testing.T) {
601601
result := ProbeHTTP(
602602
testCTX,
603603
ts.URL,
604-
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true}},
604+
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, HTTPClientConfig: pconfig.DefaultHTTPClientConfig}},
605605
registry,
606606
log.NewNopLogger())
607607
if result {
@@ -1136,7 +1136,7 @@ func TestRedirectToTLSHostWorks(t *testing.T) {
11361136
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
11371137
defer cancel()
11381138
result := ProbeHTTP(testCTX, ts.URL,
1139-
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true}}, registry, log.NewNopLogger())
1139+
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, HTTPClientConfig: pconfig.DefaultHTTPClientConfig}}, registry, log.NewNopLogger())
11401140
if !result {
11411141
t.Fatalf("Redirect test failed unexpectedly")
11421142
}
@@ -1209,7 +1209,7 @@ func TestCookieJar(t *testing.T) {
12091209
registry := prometheus.NewRegistry()
12101210
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
12111211
defer cancel()
1212-
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true}}, registry, log.NewNopLogger())
1212+
result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, HTTPClientConfig: pconfig.DefaultHTTPClientConfig}}, registry, log.NewNopLogger())
12131213
body := recorder.Body.String()
12141214
if !result {
12151215
t.Fatalf("Redirect test failed unexpectedly, got %s", body)

0 commit comments

Comments
 (0)