Skip to content

Commit 8de147f

Browse files
leopoldjuergenGitHub Enterprise
authored andcommitted
Merge pull request #33 from ZaaS/mingxia/only_GET_allowed
Disable http methods other than GET
2 parents 7b952c0 + 294d494 commit 8de147f

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/spectrum-virtualize-exporter
2-
/temp.yaml
2+
/temp.yaml
3+
/.vscode

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* [CHANGE] Disable http methods other than GET
2+
13
## 0.8.0 / 2019-07-30
24
* [BUGFIX] Fix formulas of volume usage.
35
* [FEATURE] Enhance error logging and response examples

collector/collector.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ func (c *SVCCollector) collectForHost(host utils.Targets, ch chan<- prometheus.M
138138
requestErrorCount++
139139
success = 0
140140
return
141-
142141
}
143142
authTokenCache.Store(host.IpAddress, authtoken)
144143
result, _ := authTokenCache.Load(host.IpAddress)

main.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ func main() {
5656
http.Handle(*metricsPath, newHandler(!*disableExporterMetrics))
5757

5858
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
59-
w.Write([]byte(`<html>
59+
if r.Method == "GET" {
60+
w.Write([]byte(`<html>
6061
<head><title>Spectrum Virtualize exporter</title></head>
6162
<body>
6263
<h1>Spectrum Virtualize exporter</h1>
6364
<p><a href='` + *metricsPath + `'>Metrics</a></p>
6465
</body>
6566
</html>`))
67+
} else {
68+
http.Error(w, "403 Forbidden", 403)
69+
}
6670
})
6771
// http.Handle(*metricsPath, prometheus.Handler()) // Normal metrics endpoint for Spectrum Virtualize exporter itself.
6872

@@ -76,14 +80,13 @@ func targetsForRequest(r *http.Request) ([]utils.Targets, error) {
7680
if reqTarget == "" {
7781
return cfg.Targets, nil
7882
}
79-
8083
for _, t := range cfg.Targets {
8184
if t.IpAddress == reqTarget {
8285
return []utils.Targets{t}, nil
8386
}
8487
}
8588

86-
return nil, fmt.Errorf("the target '%s' os not defined in the configuration file", reqTarget)
89+
return nil, fmt.Errorf("The target '%s' os not defined in the configuration file", reqTarget)
8790
}
8891

8992
func newHandler(includeExporterMetrics bool) *handler {
@@ -104,21 +107,24 @@ func newHandler(includeExporterMetrics bool) *handler {
104107

105108
// ServeHTTP implements http.Handler.
106109
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
107-
108-
targets, err := targetsForRequest(r)
109-
if err != nil {
110-
http.Error(w, err.Error(), 400)
111-
}
112-
113-
handler, err := h.innerHandler(targets...)
114-
if err != nil {
115-
log.Warnln("Couldn't create metrics handler:", err)
116-
w.WriteHeader(http.StatusBadRequest)
117-
w.Write([]byte(fmt.Sprintf("Couldn't create metrics handler: %s", err)))
118-
return
110+
if r.Method == "GET" {
111+
targets, err := targetsForRequest(r)
112+
if err != nil {
113+
http.Error(w, err.Error(), 400)
114+
return
115+
} else {
116+
handler, err := h.innerHandler(targets...)
117+
if err != nil {
118+
log.Warnln("Couldn't create metrics handler:", err)
119+
w.WriteHeader(http.StatusBadRequest)
120+
w.Write([]byte(fmt.Sprintf("Couldn't create metrics handler: %s", err)))
121+
return
122+
}
123+
handler.ServeHTTP(w, r)
124+
}
125+
} else {
126+
http.Error(w, "403 Forbidden", 403)
119127
}
120-
handler.ServeHTTP(w, r)
121-
122128
}
123129

124130
func (h *handler) innerHandler(targets ...utils.Targets) (http.Handler, error) {

0 commit comments

Comments
 (0)