-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
apps.go
448 lines (386 loc) · 15.2 KB
/
apps.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Copyright 2020 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package web implements web proxy handler that provides
// web interface to view and connect to teleport nodes
package web
import (
"context"
"net/http"
"sort"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
apiclient "github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/httplib"
"github.com/gravitational/teleport/lib/reversetunnel"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/web/app"
"github.com/gravitational/teleport/lib/web/ui"
)
// clusterAppsGet returns a list of applications in a form the UI can present.
func (h *Handler) clusterAppsGet(w http.ResponseWriter, r *http.Request, p httprouter.Params, sctx *SessionContext, site reversetunnel.RemoteSite) (interface{}, error) {
identity, err := sctx.GetIdentity()
if err != nil {
return nil, trace.Wrap(err)
}
// Get a list of application servers and their proxied apps.
clt, err := sctx.GetUserClient(r.Context(), site)
if err != nil {
return nil, trace.Wrap(err)
}
req, err := convertListResourcesRequest(r, types.KindAppOrSAMLIdPServiceProvider)
if err != nil {
return nil, trace.Wrap(err)
}
page, err := apiclient.GetResourcePage[types.AppServerOrSAMLIdPServiceProvider](r.Context(), clt, req)
if err != nil {
// If the error returned is due to types.KindAppOrSAMLIdPServiceProvider being unsupported, then fallback to attempting to just fetch types.AppServers.
// This is for backwards compatibility with leaf clusters that don't support this new type yet.
// DELETE IN 15.0
if trace.IsNotImplemented(err) {
req, err = convertListResourcesRequest(r, types.KindAppServer)
if err != nil {
return nil, trace.Wrap(err)
}
appServerPage, err := apiclient.GetResourcePage[types.AppServer](r.Context(), clt, req)
if err != nil {
return nil, trace.Wrap(err)
}
// Convert the ResourcePage returned containing AppServers to a ResourcePage containing AppServerOrSAMLIdPServiceProviders.
page = appServerOrSPPageFromAppServerPage(appServerPage)
} else {
return nil, trace.Wrap(err)
}
}
userGroups, err := apiclient.GetAllResources[types.UserGroup](r.Context(), clt, &proto.ListResourcesRequest{
ResourceType: types.KindUserGroup,
Namespace: apidefaults.Namespace,
UseSearchAsRoles: true,
})
if err != nil {
h.log.Debugf("Unable to fetch user groups while listing applications, unable to display associated user groups: %v", err)
}
userGroupLookup := make(map[string]types.UserGroup, len(userGroups))
for _, userGroup := range userGroups {
userGroupLookup[userGroup.GetName()] = userGroup
}
var appsAndSPs types.AppServersOrSAMLIdPServiceProviders
appsToUserGroups := map[string]types.UserGroups{}
for _, appOrSP := range page.Resources {
appsAndSPs = append(appsAndSPs, appOrSP)
if appOrSP.IsAppServer() {
app := appOrSP.GetAppServer().GetApp()
ugs := types.UserGroups{}
for _, userGroupName := range app.GetUserGroups() {
userGroup := userGroupLookup[userGroupName]
if userGroup == nil {
h.log.Debugf("Unable to find user group %s when creating user groups, skipping", userGroupName)
continue
}
ugs = append(ugs, userGroup)
}
sort.Sort(ugs)
appsToUserGroups[app.GetName()] = ugs
}
}
return listResourcesGetResponse{
Items: ui.MakeApps(ui.MakeAppsConfig{
LocalClusterName: h.auth.clusterName,
LocalProxyDNSName: h.proxyDNSName(),
AppClusterName: site.GetName(),
Identity: identity,
AppsToUserGroups: appsToUserGroups,
AppServersAndSAMLIdPServiceProviders: appsAndSPs,
}),
StartKey: page.NextKey,
TotalCount: page.Total,
}, nil
}
type GetAppFQDNRequest resolveAppParams
type GetAppFQDNResponse struct {
// FQDN is application FQDN.
FQDN string `json:"fqdn"`
}
type CreateAppSessionRequest resolveAppParams
type CreateAppSessionResponse struct {
// CookieValue is the application session cookie value.
CookieValue string `json:"cookie_value"`
// SubjectCookieValue is the application session subject cookie token.
SubjectCookieValue string `json:"subject_cookie_value"`
// FQDN is application FQDN.
FQDN string `json:"fqdn"`
}
// getAppFQDN resolves the input params to a known application and returns
// its valid FQDN.
//
// GET /v1/webapi/apps/:fqdnHint/:clusterName/:publicAddr
func (h *Handler) getAppFQDN(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *SessionContext) (interface{}, error) {
req := GetAppFQDNRequest{
FQDNHint: p.ByName("fqdnHint"),
ClusterName: p.ByName("clusterName"),
PublicAddr: p.ByName("publicAddr"),
}
// Get an auth client connected with the user's identity.
authClient, err := ctx.GetClient()
if err != nil {
return nil, trace.Wrap(err)
}
// Get a reverse tunnel proxy aware of the user's permissions.
proxy, err := h.ProxyWithRoles(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
// Use the information the caller provided to attempt to resolve to an
// application running within either the root or leaf cluster.
result, err := h.resolveApp(r.Context(), authClient, proxy, resolveAppParams(req))
if err != nil {
return nil, trace.Wrap(err, "unable to resolve FQDN: %v", req.FQDNHint)
}
return &GetAppFQDNResponse{
FQDN: result.FQDN,
}, nil
}
// createAppSession creates a new application session.
//
// POST /v1/webapi/sessions/app
func (h *Handler) createAppSession(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *SessionContext) (interface{}, error) {
var req resolveAppParams
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}
// Get an auth client connected with the user's identity.
authClient, err := ctx.GetClient()
if err != nil {
return nil, trace.Wrap(err)
}
// Get a reverse tunnel proxy aware of the user's permissions.
proxy, err := h.ProxyWithRoles(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
// Use the information the caller provided to attempt to resolve to an
// application running within either the root or leaf cluster.
result, err := h.resolveApp(r.Context(), authClient, proxy, req)
if err != nil {
return nil, trace.Wrap(err, "unable to resolve FQDN: %v", req.FQDNHint)
}
h.log.Debugf("Creating application web session for %v in %v.", result.App.GetPublicAddr(), result.ClusterName)
// Ensuring proxy can handle the connection is only done when the request is
// coming from the WebUI.
if h.healthCheckAppServer != nil && !app.HasClientCert(r) {
h.log.Debugf("Ensuring proxy can handle requests requests for application %q.", result.App.GetName())
err := h.healthCheckAppServer(r.Context(), result.App.GetPublicAddr(), result.ClusterName)
if err != nil {
return nil, trace.ConnectionProblem(err, "Unable to serve application requests. Please try again. If the issue persists, verify if the Application Services are connected to Teleport.")
}
}
// Create an application web session.
//
// Application sessions should not last longer than the parent session.TTL
// will be derived from the identity which has the same expiration as the
// parent web session.
//
// PublicAddr and ClusterName will get encoded within the certificate and
// used for request routing.
ws, err := authClient.CreateAppSession(r.Context(), types.CreateAppSessionRequest{
Username: ctx.GetUser(),
PublicAddr: result.App.GetPublicAddr(),
ClusterName: result.ClusterName,
AWSRoleARN: req.AWSRole,
})
if err != nil {
return nil, trace.Wrap(err)
}
// Block and wait a few seconds for the session that was created to show up
// in the cache. If this request is not blocked here, it can get stuck in a
// racy session creation loop.
err = h.waitForAppSession(r.Context(), ws.GetName(), ctx.GetUser())
if err != nil {
return nil, trace.Wrap(err)
}
// Extract the identity of the user.
certificate, err := tlsca.ParseCertificatePEM(ws.GetTLSCert())
if err != nil {
return nil, trace.Wrap(err)
}
identity, err := tlsca.FromSubject(certificate.Subject, certificate.NotAfter)
if err != nil {
return nil, trace.Wrap(err)
}
userMetadata := identity.GetUserMetadata()
userMetadata.User = ws.GetUser()
userMetadata.AWSRoleARN = req.AWSRole
// Now that the certificate has been issued, emit a "new session created"
// for all events associated with this certificate.
appSessionStartEvent := &apievents.AppSessionStart{
Metadata: apievents.Metadata{
Type: events.AppSessionStartEvent,
Code: events.AppSessionStartCode,
ClusterName: identity.RouteToApp.ClusterName,
},
ServerMetadata: apievents.ServerMetadata{
ServerID: h.cfg.HostUUID,
ServerNamespace: apidefaults.Namespace,
},
SessionMetadata: apievents.SessionMetadata{
SessionID: identity.RouteToApp.SessionID,
WithMFA: identity.MFAVerified,
},
UserMetadata: userMetadata,
ConnectionMetadata: apievents.ConnectionMetadata{
RemoteAddr: r.RemoteAddr,
},
PublicAddr: identity.RouteToApp.PublicAddr,
AppMetadata: apievents.AppMetadata{
AppURI: result.App.GetURI(),
AppPublicAddr: result.App.GetPublicAddr(),
AppName: result.App.GetName(),
},
}
if err := h.cfg.Emitter.EmitAuditEvent(h.cfg.Context, appSessionStartEvent); err != nil {
return nil, trace.Wrap(err)
}
return &CreateAppSessionResponse{
CookieValue: ws.GetName(),
SubjectCookieValue: ws.GetBearerToken(),
FQDN: result.FQDN,
}, nil
}
// waitForAppSession will block until the requested application session shows up in the
// cache or a timeout occurs.
func (h *Handler) waitForAppSession(ctx context.Context, sessionID, user string) error {
return auth.WaitForAppSession(ctx, sessionID, user, h.cfg.AccessPoint)
}
type resolveAppParams struct {
// FQDNHint indicates (tentatively) the fully qualified domain name of the application.
FQDNHint string `json:"fqdn"`
// PublicAddr is the public address of the application.
PublicAddr string `json:"public_addr"`
// ClusterName is the cluster within which this application is running.
ClusterName string `json:"cluster_name"`
// AWSRole is the AWS role ARN when accessing AWS management console.
AWSRole string `json:"arn,omitempty"`
}
type resolveAppResult struct {
// ServerID is the ID of the server this application is running on.
ServerID string
// FQDN is the best effort FQDN resolved for this application.
FQDN string
// ClusterName is the name of the cluster within which the application
// is running.
ClusterName string
// App is the requested application.
App types.Application
}
func (h *Handler) resolveApp(ctx context.Context, clt app.Getter, proxy reversetunnel.Tunnel, params resolveAppParams) (*resolveAppResult, error) {
var (
server types.AppServer
appClusterName string
err error
)
// If the request contains a public address and cluster name (for example, if it came
// from the application launcher in the Web UI) then directly exactly resolve the
// application that the caller is requesting. If it does not, do best effort FQDN resolution.
switch {
case params.PublicAddr != "" && params.ClusterName != "":
server, appClusterName, err = h.resolveDirect(ctx, proxy, params.PublicAddr, params.ClusterName)
case params.FQDNHint != "":
server, appClusterName, err = h.resolveFQDN(ctx, clt, proxy, params.FQDNHint)
default:
err = trace.BadParameter("no inputs to resolve application")
}
if err != nil {
return nil, trace.Wrap(err)
}
fqdn := ui.AssembleAppFQDN(h.auth.clusterName, h.proxyDNSName(), appClusterName, server.GetApp())
return &resolveAppResult{
ServerID: server.GetName(),
FQDN: fqdn,
ClusterName: appClusterName,
App: server.GetApp(),
}, nil
}
// resolveDirect takes a public address and cluster name and exactly resolves
// the application and the server on which it is running.
func (h *Handler) resolveDirect(ctx context.Context, proxy reversetunnel.Tunnel, publicAddr string, clusterName string) (types.AppServer, string, error) {
clusterClient, err := proxy.GetSite(clusterName)
if err != nil {
return nil, "", trace.Wrap(err)
}
authClient, err := clusterClient.GetClient()
if err != nil {
return nil, "", trace.Wrap(err)
}
servers, err := app.Match(ctx, authClient, app.MatchPublicAddr(publicAddr))
if err != nil {
return nil, "", trace.Wrap(err)
}
if len(servers) == 0 {
return nil, "", trace.NotFound("failed to match applications with public addr %s", publicAddr)
}
return servers[0], clusterName, nil
}
// resolveFQDN makes a best effort attempt to resolve FQDN to an application
// running within a root or leaf cluster.
func (h *Handler) resolveFQDN(ctx context.Context, clt app.Getter, proxy reversetunnel.Tunnel, fqdn string) (types.AppServer, string, error) {
return app.ResolveFQDN(ctx, clt, proxy, h.proxyDNSNames(), fqdn)
}
// proxyDNSName is a DNS name the HTTP proxy is available at, where
// the local cluster name is used as a best-effort fallback.
func (h *Handler) proxyDNSName() string {
dnsNames := h.proxyDNSNames()
if len(dnsNames) == 0 {
return h.auth.clusterName
}
return dnsNames[0]
}
// proxyDNSNames returns DNS names the HTTP proxy is available at, the local
// cluster name is used as a best-effort fallback.
func (h *Handler) proxyDNSNames() (dnsNames []string) {
for _, addr := range h.cfg.ProxyPublicAddrs {
dnsName, err := utils.DNSName(addr.String())
if err != nil {
continue
}
dnsNames = append(dnsNames, dnsName)
}
if len(dnsNames) == 0 {
return []string{h.auth.clusterName}
}
return dnsNames
}
// appServerOrSPPageFromAppServerPage converts a ResourcePage containing AppServers to a ResourcePage containing AppServerOrSAMLIdPServiceProviders.
// DELETE IN 15.0
func appServerOrSPPageFromAppServerPage(appServerPage apiclient.ResourcePage[types.AppServer]) apiclient.ResourcePage[types.AppServerOrSAMLIdPServiceProvider] {
resources := make([]types.AppServerOrSAMLIdPServiceProvider, len(appServerPage.Resources))
for i, appServer := range appServerPage.Resources {
// Create AppServerOrSAMLIdPServiceProvider object from appServer.
appServerOrSP := &types.AppServerOrSAMLIdPServiceProviderV1{
Resource: &types.AppServerOrSAMLIdPServiceProviderV1_AppServer{
AppServer: appServer.(*types.AppServerV3),
},
}
resources[i] = appServerOrSP
}
return apiclient.ResourcePage[types.AppServerOrSAMLIdPServiceProvider]{
Resources: resources,
Total: appServerPage.Total,
NextKey: appServerPage.NextKey,
}
}