This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathhttpapi.go
More file actions
540 lines (466 loc) · 18 KB
/
httpapi.go
File metadata and controls
540 lines (466 loc) · 18 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package githubapp
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
authcheck "github.com/sourcegraph/sourcegraph/internal/auth"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/encryption"
"github.com/sourcegraph/sourcegraph/internal/encryption/keyring"
"github.com/sourcegraph/sourcegraph/internal/extsvc"
"github.com/sourcegraph/sourcegraph/internal/extsvc/github"
ghtypes "github.com/sourcegraph/sourcegraph/internal/github_apps/types"
"github.com/sourcegraph/sourcegraph/internal/httpcli"
"github.com/sourcegraph/sourcegraph/internal/rcache"
"github.com/sourcegraph/sourcegraph/internal/redispool"
"github.com/sourcegraph/sourcegraph/internal/types"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
const cacheTTLSeconds = 60 * 60 // 1 hour
type gitHubAppServer struct {
cache *rcache.Cache
db database.DB
}
func (srv *gitHubAppServer) siteAdminMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 🚨 SECURITY: only site admins can create GitHub apps
if err := authcheck.CheckCurrentUserIsSiteAdmin(r.Context(), srv.db); err != nil {
if errors.Is(err, authcheck.ErrMustBeSiteAdmin) {
http.Error(w, "User must be site admin", http.StatusForbidden)
return
}
if errors.Is(err, authcheck.ErrNotAuthenticated) {
http.Error(w, "User must be authenticated", http.StatusUnauthorized)
return
}
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
next.ServeHTTP(w, r)
})
}
func (srv *gitHubAppServer) registerRoutes(router *mux.Router) {
router.Path("/state").Methods("GET").HandlerFunc(srv.stateHandler)
router.Path("/new-app-state").Methods("GET").HandlerFunc(srv.newAppStateHandler)
router.Path("/redirect").Methods("GET").HandlerFunc(srv.redirectHandler)
router.Path("/setup").Methods("GET").HandlerFunc(srv.setupHandler)
router.Use(srv.siteAdminMiddleware)
}
// SetupGitHubAppRoutes registers the routes for the GitHub App setup API.
func SetupGitHubAppRoutes(router *mux.Router, db database.DB) {
ghAppState := rcache.NewWithTTL(redispool.Cache, "github_app_state", cacheTTLSeconds)
appServer := &gitHubAppServer{
cache: ghAppState,
db: db,
}
appServer.registerRoutes(router)
}
// setupGitHubAppRoutesWithCache is the same as SetupGitHubAppRoutes but allows to pass a cache.
// Useful for testing.
func setupGitHubAppRoutesWithCache(router *mux.Router, db database.DB, cache *rcache.Cache) {
appServer := &gitHubAppServer{
cache: cache,
db: db,
}
appServer.registerRoutes(router)
}
// randomState returns a random sha256 hash that can be used as a state parameter. It is only
// exported for testing purposes.
func randomState() (string, error) {
data := make([]byte, 128)
if _, err := io.ReadFull(rand.Reader, data); err != nil {
return "", err
}
h := sha256.New()
h.Write(data)
return hex.EncodeToString(h.Sum(nil)), nil
}
type GitHubAppResponse struct {
AppID int `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
HtmlURL string `json:"html_url"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
PEM string `json:"pem"`
WebhookSecret string `json:"webhook_secret"`
Permissions map[string]string `json:"permissions"`
Events []string `json:"events"`
}
type gitHubAppStateDetails struct {
WebhookUUID string `json:"webhookUUID,omitempty"`
Domain string `json:"domain"`
AppID int `json:"app_id,omitempty"`
BaseURL string `json:"base_url,omitempty"`
Kind string `json:"kind,omitempty"`
UserID int32 `json:"user_id,omitempty"`
}
func (srv *gitHubAppServer) stateHandler(w http.ResponseWriter, r *http.Request) {
s, err := randomState()
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when generating state parameter: %s", err.Error()), http.StatusInternalServerError)
return
}
gqlID := r.URL.Query().Get("id")
domain := r.URL.Query().Get("domain")
baseURL := r.URL.Query().Get("baseURL")
kind := r.URL.Query().Get("kind")
if gqlID == "" {
// we marshal an empty `gitHubAppStateDetails` struct because we want the values saved in the cache
// to always conform to the same structure.
stateDetails, err := json.Marshal(gitHubAppStateDetails{})
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when marshalling state: %s", err.Error()), http.StatusInternalServerError)
return
}
srv.cache.Set(s, stateDetails)
_, _ = w.Write([]byte(s))
return
}
id64, err := UnmarshalGitHubAppID(graphql.ID(gqlID))
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while unmarshalling App ID: %s", err.Error()), http.StatusBadRequest)
return
}
stateDetails, err := json.Marshal(gitHubAppStateDetails{
AppID: int(id64),
Domain: domain,
BaseURL: baseURL,
Kind: kind,
})
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when marshalling state: %s", err.Error()), http.StatusInternalServerError)
return
}
srv.cache.Set(s, stateDetails)
_, _ = w.Write([]byte(s))
}
func unmarshalUserID(id graphql.ID) (userID int32, err error) {
err = relay.UnmarshalSpec(id, &userID)
return
}
func (srv *gitHubAppServer) newAppStateHandler(w http.ResponseWriter, r *http.Request) {
webhookURN := r.URL.Query().Get("webhookURN")
appName := r.URL.Query().Get("appName")
domain := r.URL.Query().Get("domain")
baseURL := r.URL.Query().Get("baseURL")
kind := r.URL.Query().Get("kind")
marshalledUserID := r.URL.Query().Get("userID")
var userID int32
if marshalledUserID != "" {
uid, err := unmarshalUserID(graphql.ID(marshalledUserID))
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while unmarshalling user ID: %s", err.Error()), http.StatusBadRequest)
return
}
userID = uid
}
var webhookUUID string
if webhookURN != "" {
ws := backend.NewWebhookService(srv.db, keyring.Default())
hook, err := ws.CreateWebhook(r.Context(), appName, extsvc.KindGitHub, webhookURN, nil)
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while setting up webhook endpoint: %s", err.Error()), http.StatusInternalServerError)
return
}
webhookUUID = hook.UUID.String()
}
s, err := randomState()
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when generating state parameter: %s", err.Error()), http.StatusInternalServerError)
return
}
stateDetails, err := json.Marshal(gitHubAppStateDetails{
WebhookUUID: webhookUUID,
Domain: domain,
BaseURL: baseURL,
Kind: kind,
UserID: userID,
})
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when marshalling state: %s", err.Error()), http.StatusInternalServerError)
return
}
srv.cache.Set(s, stateDetails)
resp := struct {
State string `json:"state"`
WebhookUUID string `json:"webhookUUID,omitempty"`
}{
State: s,
WebhookUUID: webhookUUID,
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while writing response: %s", err.Error()), http.StatusInternalServerError)
}
}
func (srv *gitHubAppServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
state := query.Get("state")
code := query.Get("code")
if state == "" || code == "" {
http.Error(w, "Bad request, code and state query params must be present", http.StatusBadRequest)
return
}
// Check that the length of state is the expected length
if len(state) != 64 {
http.Error(w, "Bad request, state query param is wrong length", http.StatusBadRequest)
return
}
stateValue, ok := srv.cache.Get(state)
if !ok {
http.Error(w, "Bad request, state query param does not match", http.StatusBadRequest)
return
}
var stateDetails gitHubAppStateDetails
err := json.Unmarshal(stateValue, &stateDetails)
if err != nil {
http.Error(w, "Bad request, invalid state", http.StatusBadRequest)
return
}
srv.cache.Delete(state)
webhookUUID, err := uuid.Parse(stateDetails.WebhookUUID)
if err != nil {
http.Error(w, fmt.Sprintf("Bad request, could not parse webhook UUID: %v", err), http.StatusBadRequest)
return
}
baseURL, err := url.Parse(stateDetails.BaseURL)
if err != nil {
http.Error(w, fmt.Sprintf("Bad request, could not parse baseURL from state: %v, error: %v", stateDetails.BaseURL, err), http.StatusInternalServerError)
return
}
apiURL, _ := github.APIRoot(baseURL)
u, err := url.JoinPath(apiURL.String(), "/app-manifests", code, "conversions")
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when building manifest endpoint URL: %v", err), http.StatusInternalServerError)
return
}
domain, err := parseDomain(&stateDetails.Domain)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to parse domain: %v", err), http.StatusBadRequest)
return
}
kind, err := parseKind(&stateDetails.Kind)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to parse kind: %v", err), http.StatusBadRequest)
return
}
if kind == nil {
http.Error(w, "invalid kind provided", http.StatusBadRequest)
return
}
app, err := createGitHubApp(u, *domain, httpcli.UncachedExternalClient, *kind, stateDetails.UserID)
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while converting github app: %s", err.Error()), http.StatusInternalServerError)
return
}
id, err := srv.db.GitHubApps().Create(r.Context(), app)
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error while storing github app in DB: %s", err.Error()), http.StatusInternalServerError)
return
}
webhookDB := srv.db.Webhooks(keyring.Default().WebhookKey)
hook, err := webhookDB.GetByUUID(r.Context(), webhookUUID)
if err != nil {
http.Error(w, fmt.Sprintf("Error while fetching webhook: %s", err.Error()), http.StatusInternalServerError)
return
}
hook.Secret = encryption.NewUnencrypted(app.WebhookSecret)
hook.Name = app.Name
if _, err := webhookDB.Update(r.Context(), hook); err != nil {
http.Error(w, fmt.Sprintf("Error while updating webhook secret: %s", err.Error()), http.StatusInternalServerError)
return
}
state, err = randomState()
if err != nil {
http.Error(w, fmt.Sprintf("Unexpected error when creating state param: %s", err.Error()), http.StatusInternalServerError)
return
}
newStateDetails, err := json.Marshal(gitHubAppStateDetails{
Domain: stateDetails.Domain,
AppID: id,
Kind: stateDetails.Kind,
BaseURL: stateDetails.BaseURL,
UserID: stateDetails.UserID,
})
if err != nil {
http.Error(w, fmt.Sprintf("unexpected error when marshalling state: %s", err.Error()), http.StatusInternalServerError)
return
}
srv.cache.Set(state, newStateDetails)
// The installations page often takes a few seconds to become available after the
// app is first created, so we sleep for a bit to give it time to load. The exact
// length of time to sleep was determined empirically.
time.Sleep(3 * time.Second)
redirectURL, err := url.JoinPath(app.AppURL, "installations/new")
if err != nil {
// if there is an error, try to redirect to app url, which should show Install button as well
redirectURL = app.AppURL
}
http.Redirect(w, r, redirectURL+fmt.Sprintf("?state=%s", state), http.StatusSeeOther)
}
func (srv *gitHubAppServer) setupHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
state := query.Get("state")
instID := query.Get("installation_id")
if state == "" || instID == "" {
// If neither state nor installation ID is set, we redirect to the GitHub Apps page.
// This can happen when someone installs the App directly from GitHub, instead of
// following the link from within Sourcegraph.
http.Redirect(w, r, "/site-admin/github-apps", http.StatusFound)
return
}
// Check that the length of state is the expected length
if len(state) != 64 {
http.Error(w, "Bad request, state query param is wrong length", http.StatusBadRequest)
return
}
setupInfo, ok := srv.cache.Get(state)
if !ok {
redirectURL := generateRedirectURL(gitHubAppStateDetails{}, nil, nil, errors.New("Bad request, state query param does not match"))
http.Redirect(w, r, redirectURL, http.StatusFound)
return
}
var stateDetails gitHubAppStateDetails
err := json.Unmarshal(setupInfo, &stateDetails)
if err != nil {
redirectURL := generateRedirectURL(gitHubAppStateDetails{}, nil, nil, errors.New("Bad request, invalid state"))
http.Redirect(w, r, redirectURL, http.StatusFound)
return
}
// Wait until we've validated the type of state before deleting it from the cache.
srv.cache.Delete(state)
kind, err := parseKind(&stateDetails.Kind)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to parse kind: %v", err), http.StatusBadRequest)
return
}
if kind == nil {
http.Error(w, "invalid kind provided", http.StatusBadRequest)
return
}
installationID, err := strconv.Atoi(instID)
if err != nil {
redirectURL := generateRedirectURL(stateDetails, nil, nil, errors.New("Bad request, could not parse installation ID"))
http.Redirect(w, r, redirectURL, http.StatusFound)
return
}
action := query.Get("setup_action")
if action == "install" {
// The actual installation is handled via an incoming webhook in cmd/frontend/webhooks/webhooks.go
ctx := r.Context()
app, err := srv.db.GitHubApps().GetByID(ctx, stateDetails.AppID)
if err != nil {
redirectURL := generateRedirectURL(stateDetails, &installationID, nil, errors.Newf("Unexpected error while fetching GitHub App from DB: %s", err.Error()))
http.Redirect(w, r, redirectURL, http.StatusFound)
return
}
redirectURL := generateRedirectURL(stateDetails, &installationID, &app.Name, nil)
http.Redirect(w, r, redirectURL, http.StatusFound)
return
} else {
http.Error(w, fmt.Sprintf("Bad request; unsupported setup action: %s", action), http.StatusBadRequest)
return
}
}
func generateRedirectURL(stateDetails gitHubAppStateDetails, installationID *int, appName *string, creationErr error) string {
// If we got an error but didn't even get far enough to parse a domain for the new
// GitHub App, we still want to route the user back to somewhere useful, so we send
// them back to the main site admin GitHub Apps page.
if stateDetails.Domain == "" && creationErr != nil {
return fmt.Sprintf("/site-admin/github-apps?kind=%s&success=false&error=%s", stateDetails.Kind, url.QueryEscape(creationErr.Error()))
}
parsedDomain, err := parseDomain(&stateDetails.Domain)
if err != nil {
return fmt.Sprintf("/site-admin/github-apps?kind=%s&success=false&error=%s", stateDetails.Kind, url.QueryEscape(fmt.Sprintf("invalid domain: %s", stateDetails.Domain)))
}
if parsedDomain == nil {
return fmt.Sprintf("/site-admin/github-apps?kind=%s&success=false&error=%s", stateDetails.Kind, "unable to parse domain")
}
kind, err := parseKind(&stateDetails.Kind)
if err != nil {
return fmt.Sprintf("/site-admin/github-apps?success=false&kind=%s&error=%s", stateDetails.Kind, url.QueryEscape(err.Error()))
}
if kind == nil {
return fmt.Sprintf("/site-admin/github-apps?kind=%s&success=false&error=%s", stateDetails.Kind, "unable to parse kind")
}
switch *parsedDomain {
case types.ReposGitHubAppDomain:
ghAppPageUrl := "/site-admin/github-apps"
if creationErr != nil {
return fmt.Sprintf("%s?success=false&error=%s", ghAppPageUrl, url.QueryEscape(creationErr.Error()))
}
if installationID == nil || stateDetails.AppID == 0 {
return fmt.Sprintf("%s?success=false&error=%s", ghAppPageUrl, url.QueryEscape("missing installation ID or app ID"))
}
return fmt.Sprintf("%s/%s?installation_id=%d", ghAppPageUrl, MarshalGitHubAppID(int64(stateDetails.AppID)), *installationID)
case types.BatchesGitHubAppDomain:
ghAppPageUrl := "/site-admin/batch-changes"
if *kind == ghtypes.UserCredentialGitHubAppKind {
ghAppPageUrl = "/user/settings/batch-changes"
}
if creationErr != nil {
return fmt.Sprintf("%s?kind=%s&success=false&error=%s", ghAppPageUrl, *kind, url.QueryEscape(creationErr.Error()))
}
// This shouldn't really happen unless we also had an error, but we handle it just
// in case
if appName == nil {
return fmt.Sprintf("%s?kind=%s&success=true", ghAppPageUrl, *kind)
}
return fmt.Sprintf("%s?kind=%s&success=true&app_name=%s", ghAppPageUrl, *kind, *appName)
default:
return fmt.Sprintf("/site-admin/github-apps?kind=%s&success=false&error=%s", *kind, url.QueryEscape(fmt.Sprintf("unsupported github apps domain: %v", parsedDomain)))
}
}
var MockCreateGitHubApp func(conversionURL string, domain types.GitHubAppDomain) (*ghtypes.GitHubApp, error)
func createGitHubApp(conversionURL string, domain types.GitHubAppDomain, httpClient *http.Client, kind ghtypes.GitHubAppKind, userID int32) (*ghtypes.GitHubApp, error) {
if MockCreateGitHubApp != nil {
return MockCreateGitHubApp(conversionURL, domain)
}
r, err := http.NewRequest(http.MethodPost, conversionURL, http.NoBody)
if err != nil {
return nil, err
}
resp, err := httpClient.Do(r)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.Newf("expected 201 statusCode, got: %d", resp.StatusCode)
}
defer resp.Body.Close()
var response GitHubAppResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
htmlURL, err := url.Parse(response.HtmlURL)
if err != nil {
return nil, err
}
return &ghtypes.GitHubApp{
AppID: response.AppID,
Name: response.Name,
Slug: response.Slug,
ClientID: response.ClientID,
ClientSecret: response.ClientSecret,
WebhookSecret: response.WebhookSecret,
PrivateKey: response.PEM,
BaseURL: htmlURL.Scheme + "://" + htmlURL.Host,
AppURL: htmlURL.String(),
Domain: domain,
Kind: kind,
Logo: fmt.Sprintf("%s://%s/identicons/app/app/%s", htmlURL.Scheme, htmlURL.Host, response.Slug),
CreatedByUserId: int(userID),
}, nil
}