@@ -12,6 +12,7 @@ import (
12
12
"net/http"
13
13
"os"
14
14
"path/filepath"
15
+ "strings"
15
16
"time"
16
17
17
18
"github.com/prometheus/client_golang/prometheus"
@@ -128,14 +129,35 @@ type asyncResult struct {
128
129
}
129
130
130
131
func createHttpServer () * http.Server {
132
+ auth := authHandler {downstream : promhttp .Handler ()}
131
133
handler := http .NewServeMux ()
132
- handler .Handle ("/metrics" , promhttp . Handler () )
134
+ handler .Handle ("/metrics" , & auth )
133
135
server := & http.Server {
134
136
Handler : handler ,
135
137
}
136
138
return server
137
139
}
138
140
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
+
139
161
func shutdownHttpServer (parentCtx context.Context , svr * http.Server ) {
140
162
ctx , cancel := context .WithTimeout (parentCtx , 5 * time .Second )
141
163
defer cancel ()
0 commit comments