Skip to content

Commit 3eb98c6

Browse files
hongkailiuopenshift-cherrypick-robot
authored andcommitted
CVO protects /metrics with authorization
1 parent 94c0e70 commit 3eb98c6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/cvo/metrics.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"path/filepath"
15+
"strings"
1516
"time"
1617

1718
"github.com/prometheus/client_golang/prometheus"
@@ -128,14 +129,35 @@ type asyncResult struct {
128129
}
129130

130131
func createHttpServer() *http.Server {
132+
auth := authHandler{downstream: promhttp.Handler()}
131133
handler := http.NewServeMux()
132-
handler.Handle("/metrics", promhttp.Handler())
134+
handler.Handle("/metrics", &auth)
133135
server := &http.Server{
134136
Handler: handler,
135137
}
136138
return server
137139
}
138140

141+
type authHandler struct {
142+
downstream http.Handler
143+
}
144+
145+
func (a *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
146+
authHeader := r.Header.Get("Authorization")
147+
if authHeader == "" {
148+
http.Error(w, "failed to get the Authorization header", http.StatusUnauthorized)
149+
return
150+
}
151+
token := strings.TrimPrefix(authHeader, "Bearer ")
152+
if token == authHeader {
153+
http.Error(w, "failed to get the Bearer token", http.StatusUnauthorized)
154+
return
155+
}
156+
157+
// TODO use the token
158+
a.downstream.ServeHTTP(w, r)
159+
}
160+
139161
func shutdownHttpServer(parentCtx context.Context, svr *http.Server) {
140162
ctx, cancel := context.WithTimeout(parentCtx, 5*time.Second)
141163
defer cancel()

0 commit comments

Comments
 (0)