forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
165 lines (146 loc) · 4.51 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package metrics
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/pprof"
"strings"
"github.com/cockroachdb/cmux"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authorization/authorizer"
)
type Listener struct {
Addr string
TLSConfig *tls.Config
Username string
Password string
Authenticator authenticator.Request
Authorizer authorizer.Authorizer
Record authorizer.AttributesRecord
Checks []healthz.HealthzChecker
}
func (l Listener) handler() http.Handler {
mux := http.NewServeMux()
healthz.InstallHandler(mux, l.Checks...)
if l.Authenticator != nil {
protected := http.NewServeMux()
protected.HandleFunc("/debug/pprof/", pprof.Index)
protected.HandleFunc("/debug/pprof/profile", pprof.Profile)
protected.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
protected.Handle("/metrics", prometheus.Handler())
mux.Handle("/", l.authorizeHandler(protected))
}
return mux
}
func (l Listener) authorizeHandler(protected http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if len(l.Username) > 0 || len(l.Password) > 0 {
if u, p, ok := req.BasicAuth(); ok {
if u == l.Username && p == l.Password {
protected.ServeHTTP(w, req)
} else {
http.Error(w, fmt.Sprintf("Unauthorized"), http.StatusUnauthorized)
}
return
}
}
user, ok, err := l.Authenticator.AuthenticateRequest(req)
if !ok || err != nil {
// older routers will not have permission to check token access review, so treat this
// as an authorization denied if so
if !ok || errors.IsUnauthorized(err) {
glog.V(5).Infof("Unable to authenticate: %v", err)
http.Error(w, "Unable to authenticate due to an error", http.StatusUnauthorized)
} else {
glog.V(3).Infof("Unable to authenticate: %v", err)
http.Error(w, "Unable to authenticate due to an error", http.StatusInternalServerError)
}
return
}
scopedRecord := l.Record
switch req.Method {
case "POST":
scopedRecord.Verb = "create"
case "GET", "HEAD":
scopedRecord.Verb = "get"
case "PUT":
scopedRecord.Verb = "update"
case "PATCH":
scopedRecord.Verb = "patch"
case "DELETE":
scopedRecord.Verb = "delete"
default:
scopedRecord.Verb = ""
}
switch {
case req.URL.Path == "/metrics":
scopedRecord.Subresource = "metrics"
case strings.HasPrefix(req.URL.Path, "/debug/"):
scopedRecord.Subresource = "debug"
}
scopedRecord.User = user
authorized, reason, err := l.Authorizer.Authorize(scopedRecord)
if err != nil {
glog.V(3).Infof("Unable to authorize: %v", err)
http.Error(w, "Unable to authorize the user due to an error", http.StatusInternalServerError)
return
}
if authorized != authorizer.DecisionAllow {
glog.V(5).Infof("Unable to authorize: %v", err)
http.Error(w, fmt.Sprintf("Forbidden: %s", reason), http.StatusForbidden)
return
}
protected.ServeHTTP(w, req)
})
}
// Listen starts a server for health, metrics, and profiling on the provided listen port.
// It will terminate the process if the server fails. Metrics and profiling are only exposed
// if username and password are provided and the user's input matches.
func (l Listener) Listen() {
handler := l.handler()
tcpl, err := net.Listen("tcp", l.Addr)
if err != nil {
glog.Fatal(err)
}
// if a TLS connection was requested, set up a connection mux that will send TLS requests to
// the TLS server but send HTTP requests to the HTTP server. Preserves the ability for HTTP
// health checks to call HTTP on the router while still allowing TLS certs and end to end
// metrics protection.
m := cmux.New(tcpl)
// match HTTP first
httpl := m.Match(cmux.HTTP1Fast())
go func() {
s := &http.Server{
Handler: handler,
}
if err := s.Serve(httpl); err != cmux.ErrListenerClosed {
glog.Fatal(err)
}
}()
// match TLS if configured
if l.TLSConfig != nil {
glog.Infof("Router health and metrics port listening at %s on HTTP and HTTPS", l.Addr)
tlsl := m.Match(cmux.Any())
tlsl = tls.NewListener(tlsl, l.TLSConfig)
go func() {
s := &http.Server{
Handler: handler,
}
if err := s.Serve(tlsl); err != cmux.ErrListenerClosed {
glog.Fatal(err)
}
}()
} else {
glog.Infof("Router health and metrics port listening at %s", l.Addr)
}
go func() {
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
glog.Fatal(err)
}
}()
}