-
Notifications
You must be signed in to change notification settings - Fork 83
/
acl.go
325 lines (291 loc) · 10.2 KB
/
acl.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
package github
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/v56/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/acl"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/policy"
)
// CheckPolicyAllowing check that policy is allowing the event to be processed
// we check the membership of the team allowed
// if the team is not found we explicitly disallow the policy, user have to correct the setting.
func (v *Provider) CheckPolicyAllowing(ctx context.Context, event *info.Event, allowedTeams []string) (bool, string) {
for _, team := range allowedTeams {
// TODO: caching
opt := github.ListOptions{PerPage: v.paginedNumber}
for {
members, resp, err := v.Client.Teams.ListTeamMembersBySlug(ctx, event.Organization, team, &github.TeamListTeamMembersOptions{ListOptions: opt})
if resp.StatusCode == http.StatusNotFound {
// we explicitly disallow the policy when the team is not found
// maybe we should ignore it instead? i'd rather keep this explicit
// and conservative since being security related.
return false, fmt.Sprintf("team: %s is not found on the organization: %s", team, event.Organization)
}
if err != nil {
// probably a 500 or another api error, no need to try again and again with other teams
return false, fmt.Sprintf("error while getting team membership for user: %s in team: %s, error: %s", event.Sender, team, err.Error())
}
for _, member := range members {
if member.GetLogin() == event.Sender {
return true, fmt.Sprintf("allowing user: %s as a member of the team: %s", event.Sender, team)
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
return false, fmt.Sprintf("user: %s is not a member of any of the allowed teams: %v", event.Sender, allowedTeams)
}
// IsAllowedOwnersFile gets the owner file from main branch and check if we have
// explicitly allowed the user in there.
func (v *Provider) IsAllowedOwnersFile(ctx context.Context, event *info.Event) (bool, error) {
ownerContent, err := v.getFileFromDefaultBranch(ctx, "OWNERS", event)
if err != nil {
if strings.Contains(err.Error(), "cannot find") {
// no owner file, skipping
return false, nil
}
return false, err
}
return acl.UserInOwnerFile(ownerContent, event.Sender)
}
func (v *Provider) IsAllowed(ctx context.Context, event *info.Event) (bool, error) {
aclPolicy := policy.Policy{
Repository: v.repo,
EventEmitter: v.eventEmitter,
Event: event,
VCX: v,
Logger: v.Logger,
}
// Try to detect a policy rule allowing this
tType, _ := detectTriggerTypeFromPayload("", event.Event)
policyAllowed, policyReason := aclPolicy.IsAllowed(ctx, tType)
switch policyAllowed {
case policy.ResultAllowed:
return true, nil
case policy.ResultDisallowed:
return false, nil
case policy.ResultNotSet: // this is to make golangci-lint happy
}
// Check all the ACL rules
allowed, err := v.aclCheckAll(ctx, event)
if err != nil {
return false, err
}
if allowed {
return true, nil
}
// Try to parse the comment from an owner who has issues a /ok-to-test
ownerAllowed, err := v.aclAllowedOkToTestFromAnOwner(ctx, event)
if err != nil {
return false, err
}
if ownerAllowed {
return true, nil
}
// error with the policy reason if it was set
if policyReason != "" {
return false, fmt.Errorf(policyReason)
}
// finally silently return false if no rules allowed this
return false, nil
}
// allowedOkToTestFromAnOwner Go over comments in a pull request and check
// if there is a /ok-to-test in there running an aclCheck again on the comment
// Sender if she is an OWNER and then allow it to run CI.
// TODO: pull out the github logic from there in an agnostic way.
func (v *Provider) aclAllowedOkToTestFromAnOwner(ctx context.Context, event *info.Event) (bool, error) {
revent := info.NewEvent()
event.DeepCopyInto(revent)
revent.EventType = ""
revent.TriggerTarget = ""
if revent.Event == nil {
return false, nil
}
switch event := revent.Event.(type) {
case *github.IssueCommentEvent:
// if we don't need to check old comments, then on issue comment we
// need to check if comment have /ok-to-test and is from allowed user
if !v.Run.Info.Pac.RememberOKToTest {
return v.aclAllowedOkToTestCurrentComment(ctx, revent, event.Comment.GetID())
}
revent.URL = event.Issue.GetPullRequestLinks().GetHTMLURL()
case *github.PullRequestEvent:
// if we don't need to check old comments, then on push event we don't need
// to check anything for the non-allowed user
if !v.Run.Info.Pac.RememberOKToTest {
return false, nil
}
revent.URL = event.GetPullRequest().GetHTMLURL()
default:
return false, nil
}
comments, err := v.GetStringPullRequestComment(ctx, revent, acl.OKToTestCommentRegexp)
if err != nil {
return false, err
}
for _, comment := range comments {
revent.Sender = comment.User.GetLogin()
allowed, err := v.aclCheckAll(ctx, revent)
if err != nil {
return false, err
}
if allowed {
return true, nil
}
}
return false, nil
}
// aclAllowedOkToTestCurrentEvent only check if this is issue comment event
// have /ok-to-test regex and sender is allowed.
func (v *Provider) aclAllowedOkToTestCurrentComment(ctx context.Context, revent *info.Event, id int64) (bool, error) {
comment, _, err := v.Client.Issues.GetComment(ctx, revent.Organization, revent.Repository, id)
if err != nil {
return false, err
}
if acl.MatchRegexp(acl.OKToTestCommentRegexp, comment.GetBody()) {
revent.Sender = comment.User.GetLogin()
allowed, err := v.aclCheckAll(ctx, revent)
if err != nil {
return false, err
}
if allowed {
return true, nil
}
}
return false, nil
}
// aclCheck check if we are allowed to run the pipeline on that PR.
func (v *Provider) aclCheckAll(ctx context.Context, rev *info.Event) (bool, error) {
// if the sender own the repo, then allow it to run
if rev.Organization == rev.Sender {
return true, nil
}
// If the user who has submitted the PR is not a owner or public member or Collaborator or not there in OWNERS file
// but has permission to push to branches then allow the CI to be run.
// This can only happen with GithubApp and Bots.
// Ex: dependabot, bots
if rev.PullRequestNumber != 0 {
isSameCloneURL, err := v.checkPullRequestForSameURL(ctx, rev)
if err != nil {
return false, err
}
if isSameCloneURL {
return true, nil
}
}
// If the user who has submitted the pr is a owner on the repo then allows
// the CI to be run.
isUserMemberRepo, err := v.checkSenderOrgMembership(ctx, rev)
if err != nil {
return false, err
}
if isUserMemberRepo {
return true, nil
}
checkSenderRepoMembership, err := v.checkSenderRepoMembership(ctx, rev)
if err != nil {
return false, err
}
if checkSenderRepoMembership {
return true, nil
}
// If we have a prow OWNERS file in the defaultBranch (ie: master) then
// parse it in approvers and reviewers field and check if sender is in there.
return v.IsAllowedOwnersFile(ctx, rev)
}
// checkPullRequestForSameURL checks If PullRequests are for same clone URL and different branches
// means if the user has access to create a branch in the repository without forking or having any permissions then PAC should allow to run CI.
//
// ex: dependabot, *[bot] etc...
func (v *Provider) checkPullRequestForSameURL(ctx context.Context, runevent *info.Event) (bool, error) {
pr, resp, err := v.Client.PullRequests.Get(ctx, runevent.Organization, runevent.Repository, runevent.PullRequestNumber)
if err != nil {
return false, err
}
if resp != nil && resp.StatusCode == http.StatusNotFound {
return false, nil
}
if pr.GetHead().GetRepo().GetCloneURL() == pr.GetBase().GetRepo().GetCloneURL() && pr.GetHead().GetRef() != pr.GetBase().GetRef() {
return true, nil
}
return false, nil
}
// checkSenderOrgMembership Get sender user's organization. We can
// only get the one that the user sets as public 🤷.
func (v *Provider) checkSenderOrgMembership(ctx context.Context, runevent *info.Event) (bool, error) {
opt := &github.ListMembersOptions{
ListOptions: github.ListOptions{PerPage: v.paginedNumber},
}
for {
users, resp, err := v.Client.Organizations.ListMembers(ctx, runevent.Organization, opt)
// If we are 404 it means we are checking a repo owner and not a org so let's bail out with grace
if resp != nil && resp.StatusCode == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err
}
for _, v := range users {
if v.GetLogin() == runevent.Sender {
return true, nil
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return false, nil
}
// checkSenderRepoMembership check if user is allowed to run CI.
func (v *Provider) checkSenderRepoMembership(ctx context.Context, runevent *info.Event) (bool, error) {
isCollab, _, err := v.Client.Repositories.IsCollaborator(ctx,
runevent.Organization,
runevent.Repository,
runevent.Sender)
return isCollab, err
}
// getFileFromDefaultBranch will get a file directly from the Default BaseBranch as
// configured in runinfo which is directly set in webhook by Github.
func (v *Provider) getFileFromDefaultBranch(ctx context.Context, path string, runevent *info.Event) (string, error) {
tektonyaml, err := v.GetFileInsideRepo(ctx, runevent, path, runevent.DefaultBranch)
if err != nil {
return "", fmt.Errorf("cannot find %s inside the %s branch: %w", path, runevent.DefaultBranch, err)
}
return tektonyaml, err
}
// GetStringPullRequestComment return the comment if we find a regexp in one of
// the comments text of a pull request.
func (v *Provider) GetStringPullRequestComment(ctx context.Context, runevent *info.Event, reg string) ([]*github.IssueComment, error) {
var ret []*github.IssueComment
prNumber, err := convertPullRequestURLtoNumber(runevent.URL)
if err != nil {
return nil, err
}
opt := &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{PerPage: v.paginedNumber},
}
for {
comments, resp, err := v.Client.Issues.ListComments(ctx, runevent.Organization, runevent.Repository,
prNumber, opt)
if err != nil {
return nil, err
}
for _, v := range comments {
if acl.MatchRegexp(reg, v.GetBody()) {
ret = append(ret, v)
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return ret, nil
}