-
Notifications
You must be signed in to change notification settings - Fork 567
/
dex_web.go
279 lines (248 loc) · 9.22 KB
/
dex_web.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package server
import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/pachyderm/pachyderm/v2/src/identity"
"github.com/pachyderm/pachyderm/v2/src/internal/dbutil"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
dex_server "github.com/dexidp/dex/server"
dex_storage "github.com/dexidp/dex/storage"
)
var (
dexRequestCountMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "pachyderm",
Subsystem: "auth_dex",
Name: "http_requests_total",
Help: "Count of http requests handled by Dex, by response status code and HTTP method",
}, []string{"code", "method"})
dexRequestsInFlightMetric = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "pachyderm",
Subsystem: "auth_dex",
Name: "http_requests_in_flight",
Help: "Number of requests currently being handled by Dex",
})
dexRequestsDurationMetric = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "pachyderm",
Subsystem: "auth_dex",
Name: "http_requests_duration_seconds",
Help: "Histogram of time spent processing Dex requests, by response status code and HTTP method",
Buckets: []float64{0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10, 30, 60, 300, 600},
}, []string{"code", "method"})
dexApprovalErrorCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "pachyderm",
Subsystem: "auth_dex",
Name: "approval_errors_total",
Help: "Count of HTTP requests to /approval that ended in error",
})
dexStartupErrorCountMetric = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "pachyderm",
Subsystem: "auth_dex",
Name: "startup_errors_total",
Help: "Count of HTTP requests that were rejected because the server can't start",
})
)
// webDir is the path to find the static assets for the web server.
// This is always /dex-assets in the docker image, but it can be overridden for testing
var webDir = "/dex-assets"
// dexWeb wraps a Dex web server and hot reloads it when the
// issuer is reconfigured.
type dexWeb struct {
sync.RWMutex
env Env
// Rather than restart the server on every request, we cache it
// along with the config and set of connectors. If either of these
// change we restart the server because Dex doesn't support
// reconfiguring them on the fly.
currentConfig *identity.IdentityServerConfig
currentConnectors *identity.ListIDPConnectorsResponse
server *dex_server.Server
serverCancel context.CancelFunc
storageProvider dex_storage.Storage
apiServer identity.APIServer
assetsDir string
addr string
}
func newDexWeb(env Env, apiServer identity.APIServer, options ...IdentityServerOption) *dexWeb {
web := &dexWeb{
env: env,
storageProvider: env.DexStorage,
apiServer: apiServer,
addr: dexHTTPPort,
assetsDir: webDir,
}
for _, opt := range options {
opt(web)
}
return web
}
// stopWebServer must be called while holding the write mutex
func (w *dexWeb) stopWebServer() {
ctx := pctx.Child(w.env.BackgroundContext, "dex")
log.Info(ctx, "stopping identity web server")
// Stop the background jobs for the existing server
if w.serverCancel != nil {
w.serverCancel()
}
w.server = nil
}
// serverNeedsRestart returns true if the server hasn't started yet, or if the config or set of
// connectors has changed. Must be called while holding a read lock on `w`.
func (w *dexWeb) serverNeedsRestart(config *identity.IdentityServerConfig, connectors *identity.ListIDPConnectorsResponse) bool {
return w.server == nil ||
w.currentConfig == nil ||
w.currentConnectors == nil ||
!proto.Equal(config, w.currentConfig) ||
!proto.Equal(connectors, w.currentConnectors)
}
// startWebServer starts a new web server with the appropriate configuration and connectors.
func (w *dexWeb) startWebServer(config *identity.IdentityServerConfig, connectors *identity.ListIDPConnectorsResponse) (*dex_server.Server, error) {
ctx := pctx.Child(w.env.BackgroundContext, "dex")
w.Lock()
defer w.Unlock()
// If the config and connectors have already been updated while we were blocked,
// don't restart the server again.
if !w.serverNeedsRestart(config, connectors) {
return w.server, nil
}
w.stopWebServer()
log.Info(ctx, "starting identity web server")
storage := w.storageProvider
// If no connectors are configured, add a static placeholder which directs the user
// to configure a connector
if len(connectors.Connectors) == 0 {
log.Info(ctx, "no idp connectors configured, using placeholder")
dex_server.ConnectorsConfig["placeholder"] = func() dex_server.ConnectorConfig { return new(placeholderConfig) }
storage = dex_storage.WithStaticConnectors(storage, []dex_storage.Connector{
{
ID: "placeholder",
Type: "placeholder",
Name: "No IDPs Configured",
Config: []byte(""),
},
})
}
var err error
idTokenExpiry := 24 * time.Hour
if config.IdTokenExpiry != "" {
idTokenExpiry, err = time.ParseDuration(config.IdTokenExpiry)
if err != nil {
return nil, errors.EnsureStack(err)
}
}
var refreshTokenPolicy *dex_server.RefreshTokenPolicy
if config.RotationTokenExpiry != "" {
refreshTokenPolicy, err = dex_server.NewRefreshTokenPolicy(log.NewLogrus(pctx.Child(ctx, "NewRefreshTokenPolicy")), false, "", config.RotationTokenExpiry, "")
if err != nil {
return nil, errors.EnsureStack(err)
}
}
serverConfig := dex_server.Config{
Storage: storage,
Issuer: config.Issuer,
IDTokensValidFor: idTokenExpiry,
SkipApprovalScreen: true,
Web: dex_server.WebConfig{
Issuer: "Pachyderm",
LogoURL: "/theme/logo.svg",
Theme: "pachyderm",
Dir: w.assetsDir,
},
RefreshTokenPolicy: refreshTokenPolicy,
Logger: log.NewLogrus(ctx),
}
ctx, w.serverCancel = pctx.WithCancel(ctx)
w.server, err = dex_server.NewServer(ctx, serverConfig)
if err != nil {
return nil, errors.EnsureStack(err)
}
w.currentConfig = config
w.currentConnectors = connectors
return w.server, nil
}
// getServer either returns a cached web server, or starts a new one
// if the config or set of connectors has changed
func (w *dexWeb) getServer(ctx context.Context) (*dex_server.Server, error) {
var server *dex_server.Server
config, err := w.apiServer.GetIdentityServerConfig(ctx, &identity.GetIdentityServerConfigRequest{})
if err != nil {
return nil, errors.EnsureStack(err)
}
connectors, err := w.apiServer.ListIDPConnectors(ctx, &identity.ListIDPConnectorsRequest{})
if err != nil {
return nil, errors.EnsureStack(err)
}
// Get a read lock to check if the server needs a restart
w.RLock()
if w.serverNeedsRestart(config.Config, connectors) {
// If the server needs to restart, unlock and acquire the write lock
w.RUnlock()
return w.startWebServer(config.Config, connectors)
}
server = w.server
w.RUnlock()
return server, nil
}
// interceptApproval handles the `/approval` route which is called after a user has
// authenticated to the IDP but before they're redirected back to the OIDC server
func (w *dexWeb) interceptApproval(server *dex_server.Server) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
authReq, err := w.storageProvider.GetAuthRequest(r.FormValue("req"))
if err != nil {
dexApprovalErrorCountMetric.Inc()
log.Error(ctx, "failed to get auth request", zap.Error(err))
rw.WriteHeader(http.StatusInternalServerError)
return
}
if !authReq.LoggedIn {
dexApprovalErrorCountMetric.Inc()
log.Error(ctx, "auth request does not have an identity for approval")
rw.WriteHeader(http.StatusInternalServerError)
return
}
if err := dbutil.WithTx(r.Context(), w.env.DB, func(tx *pachsql.Tx) error {
err := addUserInTx(r.Context(), tx, authReq.Claims.Email)
return errors.Wrapf(err, "unable to record user identity for login")
}); err != nil {
dexApprovalErrorCountMetric.Inc()
rw.WriteHeader(http.StatusInternalServerError)
log.Error(ctx, "error while adding user in tx", zap.Error(err))
return
}
server.ServeHTTP(rw, r)
}
}
// ServeHTTP proxies requests to the Dex server, if it's configured.
func (w *dexWeb) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
server, err := w.getServer(r.Context())
if server == nil {
dexStartupErrorCountMetric.Inc()
log.Error(r.Context(), "unable to start Dex server", zap.Error(err))
http.Error(rw, "unable to start Dex server, check logs", http.StatusInternalServerError)
return
}
mux := http.NewServeMux()
mux.HandleFunc("/approval", w.interceptApproval(server))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
fmt.Fprintf(w, "200 OK")
return
}
server.ServeHTTP(w, r)
})
instrumented := promhttp.InstrumentHandlerInFlight(dexRequestsInFlightMetric,
promhttp.InstrumentHandlerDuration(dexRequestsDurationMetric,
promhttp.InstrumentHandlerCounter(dexRequestCountMetric, mux)))
instrumented.ServeHTTP(rw, r)
}