forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_protection.go
503 lines (458 loc) · 19.2 KB
/
branch_protection.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
/*
Copyright 2018 The Kubernetes 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 config
import (
"errors"
"fmt"
"strings"
"github.com/sirupsen/logrus"
apiequality "k8s.io/apimachinery/pkg/api/equality"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
)
// Policy for the config/org/repo/branch.
// When merging policies, a nil value results in inheriting the parent policy.
type Policy struct {
// Unmanaged makes us not manage the branchprotection.
// Careful: Contrary to all other settings, this can _not_ be overridden
// on a lower level and is always inherited.
Unmanaged *bool `json:"unmanaged,omitempty"`
// Protect overrides whether branch protection is enabled if set.
Protect *bool `json:"protect,omitempty"`
// RequiredStatusChecks configures github contexts
RequiredStatusChecks *ContextPolicy `json:"required_status_checks,omitempty"`
// Admins overrides whether protections apply to admins if set.
Admins *bool `json:"enforce_admins,omitempty"`
// Restrictions limits who can merge
Restrictions *Restrictions `json:"restrictions,omitempty"`
// RequiredPullRequestReviews specifies github approval/review criteria.
RequiredPullRequestReviews *ReviewPolicy `json:"required_pull_request_reviews,omitempty"`
// RequiredLinearHistory enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch.
RequiredLinearHistory *bool `json:"required_linear_history,omitempty"`
// AllowForcePushes permits force pushes to the protected branch by anyone with write access to the repository.
AllowForcePushes *bool `json:"allow_force_pushes,omitempty"`
// AllowDeletions allows deletion of the protected branch by anyone with write access to the repository.
AllowDeletions *bool `json:"allow_deletions,omitempty"`
// Exclude specifies a set of regular expressions which identify branches
// that should be excluded from the protection policy
Exclude []string `json:"exclude,omitempty"`
}
func (p Policy) defined() bool {
return p.Protect != nil || p.RequiredStatusChecks != nil || p.Admins != nil || p.Restrictions != nil || p.RequiredPullRequestReviews != nil ||
p.RequiredLinearHistory != nil || p.AllowForcePushes != nil || p.AllowDeletions != nil
}
// ContextPolicy configures required github contexts.
// When merging policies, contexts are appended to context list from parent.
// Strict determines whether merging to the branch invalidates existing contexts.
type ContextPolicy struct {
// Contexts appends required contexts that must be green to merge
Contexts []string `json:"contexts,omitempty"`
// Strict overrides whether new commits in the base branch require updating the PR if set
Strict *bool `json:"strict,omitempty"`
}
// ReviewPolicy specifies github approval/review criteria.
// Any nil values inherit the policy from the parent, otherwise bool/ints are overridden.
// Non-empty lists are appended to parent lists.
type ReviewPolicy struct {
// Restrictions appends users/teams that are allowed to merge
DismissalRestrictions *Restrictions `json:"dismissal_restrictions,omitempty"`
// DismissStale overrides whether new commits automatically dismiss old reviews if set
DismissStale *bool `json:"dismiss_stale_reviews,omitempty"`
// RequireOwners overrides whether CODEOWNERS must approve PRs if set
RequireOwners *bool `json:"require_code_owner_reviews,omitempty"`
// Approvals overrides the number of approvals required if set (set to 0 to disable)
Approvals *int `json:"required_approving_review_count,omitempty"`
}
// Restrictions limits who can merge
// Users and Teams items are appended to parent lists.
type Restrictions struct {
Users []string `json:"users,omitempty"`
Teams []string `json:"teams,omitempty"`
}
// selectInt returns the child if set, else parent
func selectInt(parent, child *int) *int {
if child != nil {
return child
}
return parent
}
// selectBool returns the child argument if set, otherwise the parent
func selectBool(parent, child *bool) *bool {
if child != nil {
return child
}
return parent
}
// unionStrings merges the parent and child items together
func unionStrings(parent, child []string) []string {
if child == nil {
return parent
}
if parent == nil {
return child
}
s := sets.NewString(parent...)
s.Insert(child...)
return s.List()
}
func mergeContextPolicy(parent, child *ContextPolicy) *ContextPolicy {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &ContextPolicy{
Contexts: unionStrings(parent.Contexts, child.Contexts),
Strict: selectBool(parent.Strict, child.Strict),
}
}
func mergeReviewPolicy(parent, child *ReviewPolicy) *ReviewPolicy {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &ReviewPolicy{
DismissalRestrictions: mergeRestrictions(parent.DismissalRestrictions, child.DismissalRestrictions),
DismissStale: selectBool(parent.DismissStale, child.DismissStale),
RequireOwners: selectBool(parent.RequireOwners, child.RequireOwners),
Approvals: selectInt(parent.Approvals, child.Approvals),
}
}
func mergeRestrictions(parent, child *Restrictions) *Restrictions {
if child == nil {
return parent
}
if parent == nil {
return child
}
return &Restrictions{
Users: unionStrings(parent.Users, child.Users),
Teams: unionStrings(parent.Teams, child.Teams),
}
}
// Apply returns a policy that merges the child into the parent
func (p Policy) Apply(child Policy) Policy {
return Policy{
Unmanaged: selectBool(p.Unmanaged, child.Unmanaged),
Protect: selectBool(p.Protect, child.Protect),
RequiredStatusChecks: mergeContextPolicy(p.RequiredStatusChecks, child.RequiredStatusChecks),
Admins: selectBool(p.Admins, child.Admins),
RequiredLinearHistory: selectBool(p.RequiredLinearHistory, child.RequiredLinearHistory),
AllowForcePushes: selectBool(p.AllowForcePushes, child.AllowForcePushes),
AllowDeletions: selectBool(p.AllowDeletions, child.AllowDeletions),
Restrictions: mergeRestrictions(p.Restrictions, child.Restrictions),
RequiredPullRequestReviews: mergeReviewPolicy(p.RequiredPullRequestReviews, child.RequiredPullRequestReviews),
Exclude: unionStrings(p.Exclude, child.Exclude),
}
}
// BranchProtection specifies the global branch protection policy
type BranchProtection struct {
Policy `json:",inline"`
// ProtectTested determines if branch protection rules are set for all repos
// that Prow has registered jobs for, regardless of if those repos are in the
// branch protection config.
ProtectTested *bool `json:"protect-tested-repos,omitempty"`
// Orgs holds branch protection options for orgs by name
Orgs map[string]Org `json:"orgs,omitempty"`
// AllowDisabledPolicies allows a child to disable all protection even if the
// branch has inherited protection options from a parent.
AllowDisabledPolicies *bool `json:"allow_disabled_policies,omitempty"`
// AllowDisabledJobPolicies allows a branch to choose to opt out of branch protection
// even if Prow has registered required jobs for that branch.
AllowDisabledJobPolicies *bool `json:"allow_disabled_job_policies,omitempty"`
// ProtectReposWithOptionalJobs will make the Branchprotector manage required status
// contexts on repositories that only have optional jobs (default: false)
ProtectReposWithOptionalJobs *bool `json:"protect_repos_with_optional_jobs,omitempty"`
}
func isPolicySet(p Policy) bool {
return !apiequality.Semantic.DeepEqual(p, Policy{})
}
func (bp *BranchProtection) merge(additional *BranchProtection) error {
var errs []error
if isPolicySet(bp.Policy) && isPolicySet(additional.Policy) {
errs = append(errs, errors.New("both brachprotection configs set a top-level policy"))
} else if isPolicySet(additional.Policy) {
bp.Policy = additional.Policy
}
if bp.ProtectTested != nil && additional.ProtectTested != nil {
errs = append(errs, errors.New("both branchprotection configs set protect-tested-repos"))
} else if additional.ProtectTested != nil {
bp.ProtectTested = additional.ProtectTested
}
if bp.AllowDisabledPolicies != nil && additional.AllowDisabledPolicies != nil {
errs = append(errs, errors.New("both branchprotection configs set allow_disabled_policies"))
} else if additional.AllowDisabledPolicies != nil {
bp.AllowDisabledPolicies = additional.AllowDisabledPolicies
}
if bp.AllowDisabledJobPolicies != nil && additional.AllowDisabledJobPolicies != nil {
errs = append(errs, errors.New("both branchprotection configs set allow_disabled_job_policies"))
} else if additional.AllowDisabledJobPolicies != nil {
bp.AllowDisabledJobPolicies = additional.AllowDisabledJobPolicies
}
if bp.ProtectReposWithOptionalJobs != nil && additional.ProtectReposWithOptionalJobs != nil {
errs = append(errs, errors.New("both branchprotection configs set protect_repos_with_optional_jobs"))
} else if additional.ProtectReposWithOptionalJobs != nil {
bp.ProtectReposWithOptionalJobs = additional.ProtectReposWithOptionalJobs
}
for org := range additional.Orgs {
if bp.Orgs == nil {
bp.Orgs = map[string]Org{}
}
if isPolicySet(bp.Orgs[org].Policy) && isPolicySet(additional.Orgs[org].Policy) {
errs = append(errs, fmt.Errorf("both branchprotection configs define a policy for org %s", org))
} else if _, ok := additional.Orgs[org]; ok && !isPolicySet(bp.Orgs[org].Policy) {
orgSettings := bp.Orgs[org]
orgSettings.Policy = additional.Orgs[org].Policy
bp.Orgs[org] = orgSettings
}
for repo := range additional.Orgs[org].Repos {
if bp.Orgs[org].Repos == nil {
orgSettings := bp.Orgs[org]
orgSettings.Repos = map[string]Repo{}
bp.Orgs[org] = orgSettings
}
if isPolicySet(bp.Orgs[org].Repos[repo].Policy) && isPolicySet(additional.Orgs[org].Repos[repo].Policy) {
errs = append(errs, fmt.Errorf("both branchprotection configs define a policy for repo %s/%s", org, repo))
} else if _, ok := additional.Orgs[org].Repos[repo]; ok && !isPolicySet(bp.Orgs[org].Repos[repo].Policy) {
repoSettings := bp.Orgs[org].Repos[repo]
repoSettings.Policy = additional.Orgs[org].Repos[repo].Policy
bp.Orgs[org].Repos[repo] = repoSettings
}
for branch := range additional.Orgs[org].Repos[repo].Branches {
if bp.Orgs[org].Repos[repo].Branches == nil {
branchSettings := bp.Orgs[org].Repos[repo]
branchSettings.Branches = map[string]Branch{}
bp.Orgs[org].Repos[repo] = branchSettings
}
if isPolicySet(bp.Orgs[org].Repos[repo].Branches[branch].Policy) && isPolicySet(additional.Orgs[org].Repos[repo].Branches[branch].Policy) {
errs = append(errs, fmt.Errorf("both branchprotection configs define a policy for branch %s in repo %s/%s", branch, org, repo))
} else if _, ok := additional.Orgs[org].Repos[repo].Branches[branch]; ok && !isPolicySet(bp.Orgs[org].Repos[repo].Branches[branch].Policy) {
branchSettings := bp.Orgs[org].Repos[repo].Branches[branch]
branchSettings.Policy = additional.Orgs[org].Repos[repo].Branches[branch].Policy
bp.Orgs[org].Repos[repo].Branches[branch] = branchSettings
}
}
}
}
return utilerrors.NewAggregate(errs)
}
// GetOrg returns the org config after merging in any global policies.
func (bp BranchProtection) GetOrg(name string) *Org {
o, ok := bp.Orgs[name]
if ok {
o.Policy = bp.Apply(o.Policy)
} else {
o.Policy = bp.Policy
}
return &o
}
// Org holds the default protection policy for an entire org, as well as any repo overrides.
type Org struct {
Policy `json:",inline"`
Repos map[string]Repo `json:"repos,omitempty"`
}
// GetRepo returns the repo config after merging in any org policies.
func (o Org) GetRepo(name string) *Repo {
r, ok := o.Repos[name]
if ok {
r.Policy = o.Apply(r.Policy)
} else {
r.Policy = o.Policy
}
return &r
}
// Repo holds protection policy overrides for all branches in a repo, as well as specific branch overrides.
type Repo struct {
Policy `json:",inline"`
Branches map[string]Branch `json:"branches,omitempty"`
}
// GetBranch returns the branch config after merging in any repo policies.
func (r Repo) GetBranch(name string) (*Branch, error) {
b, ok := r.Branches[name]
if ok {
b.Policy = r.Apply(b.Policy)
if b.Protect == nil {
return nil, errors.New("defined branch policies must set protect")
}
} else {
b.Policy = r.Policy
}
return &b, nil
}
// Branch holds protection policy overrides for a particular branch.
type Branch struct {
Policy `json:",inline"`
}
// GetBranchProtection returns the policy for a given branch.
//
// Handles merging any policies defined at repo/org/global levels into the branch policy.
func (c *Config) GetBranchProtection(org, repo, branch string, presubmits []Presubmit) (*Policy, error) {
if _, present := c.BranchProtection.Orgs[org]; !present {
return nil, nil // only consider branches in configured orgs
}
b, err := c.BranchProtection.GetOrg(org).GetRepo(repo).GetBranch(branch)
if err != nil {
return nil, err
}
return c.GetPolicy(org, repo, branch, *b, presubmits)
}
// GetPolicy returns the protection policy for the branch, after merging in presubmits.
func (c *Config) GetPolicy(org, repo, branch string, b Branch, presubmits []Presubmit) (*Policy, error) {
policy := b.Policy
// Automatically require contexts from prow which must always be present
if prowContexts, requiredIfPresentContexts, optionalContexts := BranchRequirements(branch, presubmits); c.shouldManageRequiredStatusCheck(prowContexts, requiredIfPresentContexts, optionalContexts) {
// Error if protection is disabled
if policy.Protect != nil && !*policy.Protect {
if c.BranchProtection.AllowDisabledJobPolicies != nil && *c.BranchProtection.AllowDisabledJobPolicies {
return nil, nil
}
return nil, fmt.Errorf("required prow jobs require branch protection")
}
ps := Policy{
RequiredStatusChecks: &ContextPolicy{
Contexts: prowContexts,
},
}
// Require protection by default if ProtectTested is true
if c.BranchProtection.ProtectTested != nil && *c.BranchProtection.ProtectTested {
yes := true
ps.Protect = &yes
}
policy = policy.Apply(ps)
}
if policy.Protect != nil && !*policy.Protect {
// Ensure that protection is false => no protection settings
var old *bool
old, policy.Protect = policy.Protect, old
if policy.defined() && !boolValFromPtr(c.BranchProtection.AllowDisabledPolicies) {
return nil, fmt.Errorf("%s/%s=%s defines a policy, which requires protect: true", org, repo, branch)
}
policy.Protect = old
}
if !policy.defined() {
return nil, nil
}
return &policy, nil
}
func (c *Config) shouldManageRequiredStatusCheck(requiredContexts, requiredIfPresentContexts, optionalContexts []string) bool {
if len(requiredContexts) > 0 {
return true
}
if c.BranchProtection.ProtectReposWithOptionalJobs == nil || !*c.BranchProtection.ProtectReposWithOptionalJobs {
return false
}
return len(requiredIfPresentContexts) > 0 || len(optionalContexts) > 0
}
func isUnprotected(policy Policy, allowDisabledPolicies bool, hasRequiredContexts bool, allowDisabledJobPolicies bool) bool {
if policy.Protect != nil && !*policy.Protect {
if hasRequiredContexts && allowDisabledJobPolicies {
return true
}
if allowDisabledPolicies {
policy.Protect = nil
if policy.defined() {
return true
}
}
}
return false
}
func (c *Config) reposWithDisabledPolicy() []string {
repoWarns := sets.NewString()
for orgName, org := range c.BranchProtection.Orgs {
for repoName := range org.Repos {
repoPolicy := c.BranchProtection.GetOrg(orgName).GetRepo(repoName)
if isUnprotected(repoPolicy.Policy, boolValFromPtr(c.BranchProtection.AllowDisabledPolicies), false, false) {
repoWarns.Insert(fmt.Sprintf("%s/%s", orgName, repoName))
}
}
}
return repoWarns.List()
}
// boolValFromPtr returns the bool value from a bool pointer.
// Nil counts as false. We need the boolpointers to be able
// to differentiate unset from false in the serialization.
func boolValFromPtr(b *bool) bool {
return b != nil && *b
}
// unprotectedBranches returns the set of names of branches
// which have protection flag set to false, but have either:
// a. a protection policy, or
// b. a required context
func (c *Config) unprotectedBranches(presubmits map[string][]Presubmit) []string {
branchWarns := sets.NewString()
for orgName, org := range c.BranchProtection.Orgs {
for repoName, repo := range org.Repos {
branches := sets.NewString()
for branchName := range repo.Branches {
b, err := c.BranchProtection.GetOrg(orgName).GetRepo(repoName).GetBranch(branchName)
if err != nil {
continue
}
policy, err := c.GetPolicy(orgName, repoName, branchName, *b, []Presubmit{})
if err != nil || policy == nil {
continue
}
requiredContexts, _, _ := BranchRequirements(branchName, presubmits[orgName+"/"+repoName])
if isUnprotected(*policy, boolValFromPtr(c.BranchProtection.AllowDisabledPolicies), len(requiredContexts) > 0, boolValFromPtr(c.BranchProtection.AllowDisabledJobPolicies)) {
branches.Insert(branchName)
}
}
if branches.Len() > 0 {
branchWarns.Insert(fmt.Sprintf("%s/%s=%s", orgName, repoName, strings.Join(branches.List(), ",")))
}
}
}
return branchWarns.List()
}
// BranchProtectionWarnings logs two sets of warnings:
// - The list of repos with unprotected branches,
// - The list of repos with disabled policies, i.e. Protect set to false,
// because any branches not explicitly specified in the configuration will be unprotected.
func (c *Config) BranchProtectionWarnings(logger *logrus.Entry, presubmits map[string][]Presubmit) {
if warnings := c.reposWithDisabledPolicy(); len(warnings) > 0 {
logger.WithField("repos", strings.Join(warnings, ",")).Debug("The following repos define a policy, but have protect: false")
}
if warnings := c.unprotectedBranches(presubmits); len(warnings) > 0 {
logger.WithField("repos", strings.Join(warnings, ",")).Debug("The following repos define a policy or require context(s), but have one or more branches with protect: false")
}
}
// BranchRequirements partitions status contexts for a given org, repo branch into three buckets:
// - contexts that are always required to be present
// - contexts that are required, _if_ present
// - contexts that are always optional
func BranchRequirements(branch string, jobs []Presubmit) ([]string, []string, []string) {
var required, requiredIfPresent, optional []string
for _, j := range jobs {
if !j.CouldRun(branch) {
continue
}
if j.ContextRequired() {
if j.TriggersConditionally() {
// jobs that trigger conditionally cannot be
// required as their status may not exist on PRs
requiredIfPresent = append(requiredIfPresent, j.Context)
} else {
// jobs that produce required contexts and will
// always run should be required at all times
required = append(required, j.Context)
}
} else {
optional = append(optional, j.Context)
}
}
return required, requiredIfPresent, optional
}