-
Notifications
You must be signed in to change notification settings - Fork 420
/
github.go
647 lines (551 loc) · 18.8 KB
/
github.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
Copyright 2019 The Tekton Authors
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 github
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
gh "github.com/google/go-github/v31/github"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
"github.com/tektoncd/triggers/pkg/interceptors"
"golang.org/x/oauth2"
"google.golang.org/grpc/codes"
"gopkg.in/yaml.v2"
)
var _ triggersv1.InterceptorInterface = (*InterceptorImpl)(nil)
var acceptedEventTypes = []string{"pull_request", "push"}
type testURLKey string
const (
changedFilesExtensionsKey = "changed_files"
testURL testURLKey = "TESTURL"
OKToTestCommentRegexp = `(^|\n)\/ok-to-test(\r\n|\r|\n|$)`
)
// In a pull request, these are the only two events that should trigger a PipelineRun/TaskRun
var ownersEventTypes = []string{"pull_request", "issue_comment"}
// ErrInvalidContentType is returned when the content-type is not a JSON body.
var ErrInvalidContentType = errors.New("form parameter encoding not supported, please change the hook to send JSON payloads")
type InterceptorImpl struct {
SecretGetter interceptors.SecretGetter
}
type payloadDetails struct {
PrNumber int
Owner string
Repository string
ChangedFiles string
}
type OwnersPayloadDetails struct {
PrNumber int
Sender string
Owner string
Repository string
IssueCommentBody string
}
type OwnersConfig struct {
Approvers []string `json:"approvers,omitempty"`
Reviewers []string `json:"reviewers,omitempty"`
}
func NewInterceptor(sg interceptors.SecretGetter) *InterceptorImpl {
return &InterceptorImpl{
SecretGetter: sg,
}
}
// InterceptorParams provides a webhook to intercept and pre-process events
type InterceptorParams struct {
SecretRef *triggersv1.SecretRef `json:"secretRef,omitempty"`
// +listType=atomic
EventTypes []string `json:"eventTypes,omitempty"`
AddChangedFiles AddChangedFiles `json:"addChangedFiles,omitempty"`
GithubOwners Owners `json:"githubOwners,omitempty"`
}
type CheckType string
const (
// Set the checkType to orgMembers to allow org members to submit or comment on PR to proceed
OrgMembers CheckType = "orgMembers"
// Set the checkType to repoMembers to allow repo members to submit or comment on PR to proceed
RepoMembers CheckType = "repoMembers"
// Set the checkType to all if both repo members or org members can submit or comment on PR to proceed
All CheckType = "all"
// Set the checkType to none if neither of repo members or org members can not submit or comment on PR to proceed
None CheckType = "none"
)
type Owners struct {
Enabled bool `json:"enabled,omitempty"`
// This param/variable is required for private repos or when checkType is set to orgMembers or repoMembers or all
PersonalAccessToken *triggersv1.SecretRef `json:"personalAccessToken,omitempty"`
// Set the value to one of the supported values (orgMembers, repoMembers, both, none)
CheckType CheckType `json:"checkType,omitempty"`
}
type AddChangedFiles struct {
Enabled bool `json:"enabled,omitempty"`
PersonalAccessToken *triggersv1.SecretRef `json:"personalAccessToken,omitempty"`
}
func (w *InterceptorImpl) Process(ctx context.Context, r *triggersv1.InterceptorRequest) *triggersv1.InterceptorResponse {
headers := interceptors.Canonical(r.Header)
if v := headers.Get("Content-Type"); v == "application/x-www-form-urlencoded" {
return interceptors.Fail(codes.InvalidArgument, ErrInvalidContentType.Error())
}
p := InterceptorParams{}
if err := interceptors.UnmarshalParams(r.InterceptorParams, &p); err != nil {
return interceptors.Failf(codes.InvalidArgument, "failed to parse interceptor params: %v", err)
}
actualEvent := headers.Get("X-GitHub-Event")
// Check if the event type is in the allow-list
if p.EventTypes != nil {
isAllowed := false
for _, allowedEvent := range p.EventTypes {
if actualEvent == allowedEvent {
isAllowed = true
break
}
}
if !isAllowed {
return interceptors.Failf(codes.FailedPrecondition, "event type %s is not allowed", actualEvent)
}
}
// Next validate secrets
if p.SecretRef != nil {
// Check the secret to see if it is empty
if p.SecretRef.SecretKey == "" {
return interceptors.Fail(codes.FailedPrecondition, "github interceptor secretRef.secretKey is empty")
}
header := headers.Get("X-Hub-Signature-256")
if header == "" {
header = headers.Get("X-Hub-Signature")
}
if header == "" {
return interceptors.Fail(codes.FailedPrecondition, "Must set X-Hub-Signature-256 or X-Hub-Signature header")
}
if r.Context == nil {
return interceptors.Failf(codes.InvalidArgument, "no request context passed")
}
ns, _ := triggersv1.ParseTriggerID(r.Context.TriggerID)
secretToken, err := w.SecretGetter.Get(ctx, ns, p.SecretRef)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error getting secret: %v", err)
}
if err := gh.ValidateSignature(header, []byte(r.Body), secretToken); err != nil {
return interceptors.Fail(codes.FailedPrecondition, err.Error())
}
}
if p.AddChangedFiles.Enabled {
shouldAddChangedFiles := false
for _, allowedEvent := range acceptedEventTypes {
if actualEvent == allowedEvent {
shouldAddChangedFiles = true
break
}
}
if !shouldAddChangedFiles {
return &triggersv1.InterceptorResponse{
Continue: true,
}
}
if r.Context == nil {
return interceptors.Failf(codes.InvalidArgument, "no request context passed")
}
secretToken, err := w.getGithubTokenSecret(ctx, r, p)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error getting secret: %v", err)
}
payload, err := parseBodyForChangedFiles(r.Body, actualEvent)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error parsing body: %v", err)
}
var changedFiles string
if actualEvent == "pull_request" {
changedFiles, err = getChangedFilesFromPr(ctx, payload, headers.Get("X-Github-Enterprise-Host"), secretToken)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error getting changed files: %v", err)
}
} else {
changedFiles = payload.ChangedFiles
}
return &triggersv1.InterceptorResponse{
Extensions: map[string]interface{}{
changedFilesExtensionsKey: changedFiles,
},
Continue: true,
}
}
// For event types pull_request, issue_comment check github owners approval is required
// User can specify both event type or any one of them
if p.GithubOwners.Enabled {
ownerCheckAllowed := false
for _, allowedEvent := range ownersEventTypes {
if actualEvent == allowedEvent {
ownerCheckAllowed = true
break
}
}
if !ownerCheckAllowed {
return &triggersv1.InterceptorResponse{
Continue: true,
}
}
ghToken, err := w.getPersonalAccessTokenSecret(ctx, r, p)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error getting github token: %v", err)
}
if ghToken == "" && (p.GithubOwners.CheckType != "none") {
return interceptors.Fail(codes.FailedPrecondition, "checkType is set to check org or repo members but no personalAccessToken was supplied")
}
// The X-Github-Enterprise-Host header only exists when the webhook comes from a github enterprise
// server and is left empty for regular hosted Github
enterpriseBaseURL := headers.Get("X-Github-Enterprise-Host")
client, err := makeClient(ctx, enterpriseBaseURL, ghToken)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error making client: %v", err)
}
payload, err := parseBodyForOwners(r.Body, actualEvent)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error parsing body: %v", err)
}
allowed, err := checkOwnershipAndMembership(ctx, payload, p, client)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error checking owner verification: %v", err)
}
if allowed && actualEvent == "pull_request" {
return &triggersv1.InterceptorResponse{
Continue: true,
}
}
commentAllowed, err := okToTestFromAnOwner(ctx, payload, p, client)
if err != nil {
return interceptors.Failf(codes.FailedPrecondition, "error checking comments for verification: %v", err)
}
if !commentAllowed {
return interceptors.Fail(codes.FailedPrecondition, "owners check requirements not met")
}
}
return &triggersv1.InterceptorResponse{
Continue: true,
}
}
func (w *InterceptorImpl) getGithubTokenSecret(ctx context.Context, r *triggersv1.InterceptorRequest, p InterceptorParams) (string, error) {
if p.AddChangedFiles.PersonalAccessToken == nil {
return "", nil
}
if p.AddChangedFiles.PersonalAccessToken.SecretKey == "" {
return "", fmt.Errorf("github interceptor githubToken.secretKey is empty")
}
ns, _ := triggersv1.ParseTriggerID(r.Context.TriggerID)
secretToken, err := w.SecretGetter.Get(ctx, ns, p.AddChangedFiles.PersonalAccessToken)
if err != nil {
return "", err
}
return string(secretToken), nil
}
func parseBodyForChangedFiles(body string, eventType string) (payloadDetails, error) {
results := payloadDetails{}
if body == "" {
return results, fmt.Errorf("body is empty")
}
var jsonMap map[string]interface{}
err := json.Unmarshal([]byte(body), &jsonMap)
if err != nil {
return results, err
}
var prNum int
_, ok := jsonMap["number"]
if ok {
prNum = int(jsonMap["number"].(float64))
} else {
if eventType == "pull_request" {
return results, fmt.Errorf("pull_request body missing 'number' field")
}
prNum = -1
}
repoSection, ok := jsonMap["repository"].(map[string]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'repository' field")
}
fullName, ok := repoSection["full_name"].(string)
if !ok {
return results, fmt.Errorf("payload body missing 'repository.full_name' field")
}
changedFiles := []string{}
commitsSection, ok := jsonMap["commits"].([]interface{})
if ok {
for _, commit := range commitsSection {
addedFiles, ok := commit.(map[string]interface{})["added"].([]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'commits.*.added' field")
}
modifiedFiles, ok := commit.(map[string]interface{})["modified"].([]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'commits.*.modified' field")
}
removedFiles, ok := commit.(map[string]interface{})["removed"].([]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'commits.*.removed' field")
}
for _, fileName := range addedFiles {
changedFiles = append(changedFiles, fmt.Sprintf("%v", fileName))
}
for _, fileName := range modifiedFiles {
changedFiles = append(changedFiles, fmt.Sprintf("%v", fileName))
}
for _, fileName := range removedFiles {
changedFiles = append(changedFiles, fmt.Sprintf("%v", fileName))
}
}
}
results = payloadDetails{
PrNumber: prNum,
Owner: strings.Split(fullName, "/")[0],
Repository: strings.Split(fullName, "/")[1],
ChangedFiles: strings.Join(changedFiles, ","),
}
return results, nil
}
func getChangedFilesFromPr(ctx context.Context, payload payloadDetails, enterpriseBaseURL string, token string) (string, error) {
changedFiles := []string{}
client, err := makeClient(ctx, enterpriseBaseURL, token)
if err != nil {
return "", err
}
opt := &gh.ListOptions{PerPage: 100}
for {
files, resp, err := client.PullRequests.ListFiles(ctx, payload.Owner, payload.Repository, payload.PrNumber, opt)
if err != nil {
return "", err
}
for _, file := range files {
changedFiles = append(changedFiles, *file.Filename)
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return strings.Join(changedFiles, ","), nil
}
func makeClient(ctx context.Context, enterpriseBaseURL string, token string) (*gh.Client, error) {
var httpClient *http.Client
var client *gh.Client
var err error
if token != "" {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient = oauth2.NewClient(ctx, tokenSource)
} else {
httpClient = nil
}
testingURL := ""
if ctx.Value(testURL) != nil {
testingURL = fmt.Sprintf("%v", ctx.Value(testURL))
}
if enterpriseBaseURL != "" || testingURL != "" {
enterpriseBaseURL = "https://" + enterpriseBaseURL
if testingURL != "" {
enterpriseBaseURL = testingURL
}
client, err = gh.NewEnterpriseClient(enterpriseBaseURL, enterpriseBaseURL, httpClient)
if err != nil {
return client, err
}
} else {
client = gh.NewClient(httpClient)
}
return client, nil
}
func (w *InterceptorImpl) getPersonalAccessTokenSecret(ctx context.Context, r *triggersv1.InterceptorRequest, p InterceptorParams) (string, error) {
if p.GithubOwners.PersonalAccessToken == nil {
return "", nil
}
if p.GithubOwners.PersonalAccessToken.SecretKey == "" {
return "", fmt.Errorf("github interceptor personalAccessToken.secretKey is empty")
}
if r.Context == nil {
return "", fmt.Errorf("no request context passed")
}
ns, _ := triggersv1.ParseTriggerID(r.Context.TriggerID)
secretToken, err := w.SecretGetter.Get(ctx, ns, p.GithubOwners.PersonalAccessToken)
if err != nil {
return "", err
}
return string(secretToken), nil
}
func okToTestFromAnOwner(ctx context.Context, payload OwnersPayloadDetails, p InterceptorParams, client *gh.Client) (bool, error) {
if MatchRegexp(OKToTestCommentRegexp, payload.IssueCommentBody) {
allowed, err := checkOwnershipAndMembership(ctx, payload, p, client)
if err != nil {
return false, err
}
if allowed {
return true, nil
}
}
return false, nil
}
func checkOwnershipAndMembership(ctx context.Context, payload OwnersPayloadDetails, p InterceptorParams, client *gh.Client) (bool, error) {
if p.GithubOwners.CheckType == "orgMembers" || p.GithubOwners.CheckType == "all" {
isUserMemberOrg, err := checkSenderOrgMembership(ctx, payload, client)
if err != nil {
return false, err
}
if isUserMemberOrg {
return true, nil
}
}
if p.GithubOwners.CheckType == "repoMembers" || p.GithubOwners.CheckType == "all" {
checkSenderRepoMembership, err := checkSenderRepoMembership(ctx, payload, client)
if err != nil {
return false, err
}
if checkSenderRepoMembership {
return true, nil
}
}
ownerContent, err := getContentFromOwners(ctx, "OWNERS", payload, client)
if err != nil {
if strings.Contains(err.Error(), "404") {
// no owner file, skipping
return false, nil
}
return false, err
}
return userInOwnerFile(ownerContent, payload.Sender)
}
func checkSenderOrgMembership(ctx context.Context, payload OwnersPayloadDetails, client *gh.Client) (bool, error) {
users, resp, err := client.Organizations.ListMembers(ctx, payload.Owner, &gh.ListMembersOptions{
PublicOnly: true, // we can't list private member in a org
})
if resp != nil && resp.Response.StatusCode == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err
}
for _, user := range users {
login := *user.Login
if login == payload.Sender {
return true, nil
}
}
return false, nil
}
func checkSenderRepoMembership(ctx context.Context, payload OwnersPayloadDetails, client *gh.Client) (bool, error) {
users, _, err := client.Repositories.ListCollaborators(ctx, payload.Owner, payload.Repository, &gh.ListCollaboratorsOptions{})
if err != nil {
return false, err
}
for _, user := range users {
login := *user.Login
if login == payload.Sender {
return true, nil
}
}
return false, nil
}
func getContentFromOwners(ctx context.Context, path string, payload OwnersPayloadDetails, client *gh.Client) (string, error) {
fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, payload.Owner, payload.Repository, path, &gh.RepositoryContentGetOptions{})
if err != nil {
return "", err
}
if directoryContent != nil {
return "", fmt.Errorf("referenced file inside the Github Repository %s is a directory", path)
}
fileData, err := fileContent.GetContent()
if err != nil {
return "", err
}
return fileData, nil
}
func userInOwnerFile(ownerContent, sender string) (bool, error) {
oc := OwnersConfig{}
err := yaml.Unmarshal([]byte(ownerContent), &oc)
if err != nil {
return false, err
}
for _, owner := range append(oc.Approvers, oc.Reviewers...) {
if strings.EqualFold(owner, sender) {
return true, nil
}
}
return false, nil
}
func MatchRegexp(reg, comment string) bool {
re := regexp.MustCompile(reg)
return string(re.Find([]byte(comment))) != ""
}
func parseBodyForOwners(body string, eventType string) (OwnersPayloadDetails, error) {
results := OwnersPayloadDetails{}
if body == "" {
return results, fmt.Errorf("payload body is empty")
}
var jsonMap map[string]interface{}
err := json.Unmarshal([]byte(body), &jsonMap)
if err != nil {
return results, err
}
var prNum int
if eventType == "pull_request" {
_, ok := jsonMap["number"]
if !ok {
return results, fmt.Errorf("pull_request body missing 'number' field")
}
prNum = int(jsonMap["number"].(float64))
} else {
prNum = -1
}
var issueCommentBody string
if eventType == "issue_comment" {
issueSection, ok := jsonMap["issue"].(map[string]interface{})
if !ok {
return results, fmt.Errorf("issue_comment body missing 'issue' section")
}
_, ok = issueSection["number"]
if !ok {
return results, fmt.Errorf("'number' field missing in the issue section of issue_comment body")
}
prNum = int(issueSection["number"].(float64))
issueCommentBodySection, ok := jsonMap["comment"].(map[string]interface{})
if !ok {
return results, fmt.Errorf("issue_comment body missing 'comment' section")
}
_, ok = issueCommentBodySection["body"]
if !ok {
return results, fmt.Errorf("'body' field missing in the comment section of issue_comment body")
}
issueCommentBody = issueCommentBodySection["body"].(string)
} else {
issueCommentBody = ""
}
repoSection, ok := jsonMap["repository"].(map[string]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'repository' field")
}
fullName, ok := repoSection["full_name"].(string)
if !ok {
return results, fmt.Errorf("payload body missing 'repository.full_name' field")
}
senderSection, ok := jsonMap["sender"].(map[string]interface{})
if !ok {
return results, fmt.Errorf("payload body missing 'sender' field")
}
prSender, _ := senderSection["login"].(string)
results = OwnersPayloadDetails{
PrNumber: prNum,
Sender: prSender,
Owner: strings.Split(fullName, "/")[0],
Repository: strings.Split(fullName, "/")[1],
IssueCommentBody: issueCommentBody,
}
return results, nil
}