-
Notifications
You must be signed in to change notification settings - Fork 124
/
outside.go
415 lines (355 loc) · 12.1 KB
/
outside.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
// Copyright 2021 Allstar 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 Outside implements the Outside Collaborators security policy.
package outside
import (
"context"
"fmt"
"github.com/gobwas/glob"
"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/google/go-github/v59/github"
"github.com/rs/zerolog/log"
)
const configFile = "outside.yaml"
const polName = "Outside Collaborators"
const accessText = "Found %v outside collaborators with %v access.\n"
const accessExp = `This policy requires users with this access to be members of the organisation. That way you can easily audit who has access to your repo, and if an account is compromised it can quickly be denied access to organization resources. To fix this you should either remove the user from repository-based access, or add them to the organization.
* Remove the user from the repository-based access. From the main page of the repository, go to Settings -> Manage Access.
(For more information, see https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-user-account/managing-access-to-your-personal-repositories/removing-a-collaborator-from-a-personal-repository)
OR
* Invite the user to join your organisation. Click your profile photo and choose “Your Organization” → choose the org name → “People” → “Invite Member.” (For more information, see https://docs.github.com/en/organizations/managing-membership-in-your-organization/inviting-users-to-join-your-organization)
If you don't see the Settings tab you probably don't have administrative access. Reach out to the administrators of the organisation to fix this issue.
OR
* Exempt the user by adding an exemption to your organization-level Outside Collaborators configuration file.
`
// OrgConfig is the org-level config definition for Outside Collaborators
// security policy.
type OrgConfig struct {
// OptConfig is the standard org-level opt in/out config, RepoOverride
// applies to all config.
OptConfig config.OrgOptConfig `json:"optConfig"`
// Action defines which action to take, default log, other: issue...
Action string `json:"action"`
// PushAllowed defined if outside collaborators are allowed to have push
// access, default true.
PushAllowed bool `json:"pushAllowed"`
// AdminAllowed defined if outside collaborators are allowed to have admin
// access, default false.
AdminAllowed bool `json:"adminAllowed"`
// Exemptions is a list of user-repo-access pairings to exempt.
// Exemptions are only defined at the org level because they should be made
// obvious to org security managers.
Exemptions []*OutsideExemption `json:"exemptions"`
}
// RepoConfig is the repo-level config for Outside Collaborators security
// policy.
type RepoConfig struct {
// OptConfig is the standard repo-level opt in/out config.
OptConfig config.RepoOptConfig `json:"optConfig"`
// Action overrides the same setting in org-level, only if present.
Action *string `json:"action"`
// PushAllowed overrides the same setting in org-level, only if present.
PushAllowed *bool `json:"pushAllowed"`
// AdminAllowed overrides the same setting in org-level, only if present.
AdminAllowed *bool `json:"adminAllowed"`
}
type mergedConfig struct {
Action string
PushAllowed bool
AdminAllowed bool
Exemptions []*OutsideExemption
}
type globCache map[string]glob.Glob
// OutsideExemption is an exemption entry for the Outside Collaborators policy.
type OutsideExemption struct {
// User is a GitHub username
User string `json:"user"`
// Repo is a GitHub repo name
Repo string `json:"repo"`
// Push allows push permission
Push bool `json:"push"`
// Admin allows admin permission
Admin bool `json:"admin"`
}
type details struct {
OutsidePushCount int
OutsidePushers []string
OutsideAdminCount int
OutsideAdmins []string
OwnerCount int
DirectOrgAdmins []string
TeamAdmins []string
}
var configFetchConfig func(context.Context, *github.Client, string, string, string, config.ConfigLevel, interface{}) error
var configIsEnabled func(ctx context.Context, o config.OrgOptConfig, orc, r config.RepoOptConfig, c *github.Client, owner, repo string) (bool, error)
func init() {
configFetchConfig = config.FetchConfig
configIsEnabled = config.IsEnabled
}
// Outside is the Outside Collaborators policy object, implements policydef.Policy.
type Outside bool
// NewOutside returns a new Outside Collaborators policy.
func NewOutside() policydef.Policy {
var o Outside
return o
}
// Name returns the name of this policy, implementing policydef.Policy.Name()
func (o Outside) Name() string {
return polName
}
type repositories interface {
ListCollaborators(context.Context, string, string,
*github.ListCollaboratorsOptions) ([]*github.User, *github.Response, error)
ListTeams(context.Context, string, string, *github.ListOptions) (
[]*github.Team, *github.Response, error)
}
// Check performs the policy check for Outside Collaborators based on the
// configuration stored in the org/repo, implementing policydef.Policy.Check()
func (o Outside) Check(ctx context.Context, c *github.Client, owner,
repo string) (*policydef.Result, error) {
return check(ctx, c.Repositories, c, owner, repo)
}
// Check whether this policy is enabled or not
func (o Outside) IsEnabled(ctx context.Context, c *github.Client, owner, repo string) (bool, error) {
oc, orc, rc := getConfig(ctx, c, owner, repo)
return configIsEnabled(ctx, oc.OptConfig, orc.OptConfig, rc.OptConfig, c, owner, repo)
}
func check(ctx context.Context, rep repositories, c *github.Client, owner,
repo string) (*policydef.Result, error) {
oc, orc, rc := getConfig(ctx, c, owner, repo)
enabled, err := configIsEnabled(ctx, oc.OptConfig, orc.OptConfig, rc.OptConfig, c, owner, repo)
if err != nil {
return nil, err
}
mc := mergeConfig(oc, orc, rc, repo)
gc := globCache{}
var d details
outAdmins, err := getUsers(ctx, rep, owner, repo, "admin", "outside", mc.Exemptions, gc)
if err != nil {
return nil, err
}
outPushers, err := getUsers(ctx, rep, owner, repo, "push", "outside", mc.Exemptions, gc)
if err != nil {
return nil, err
}
d.OutsideAdminCount = len(outAdmins)
d.OutsideAdmins = outAdmins
d.OutsidePushCount = len(outPushers)
d.OutsidePushers = outPushers
directAdmins, err := getUsers(ctx, rep, owner, repo, "admin", "direct", mc.Exemptions, gc)
if err != nil {
return nil, err
}
var directOrgAdmins []string
for _, a := range directAdmins {
if !in(a, outAdmins) {
directOrgAdmins = append(directOrgAdmins, a)
}
}
d.OwnerCount = d.OwnerCount + len(directOrgAdmins)
d.DirectOrgAdmins = directOrgAdmins
opt := &github.ListOptions{
PerPage: 100,
}
var teams []*github.Team
for {
ts, resp, err := rep.ListTeams(ctx, owner, repo, opt)
if err != nil {
return nil, err
}
teams = append(teams, ts...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
var teamAdmins []string
for _, t := range teams {
if t.GetPermissions()["admin"] {
teamAdmins = append(teamAdmins, t.GetSlug())
}
}
d.OwnerCount = d.OwnerCount + len(teamAdmins)
d.TeamAdmins = teamAdmins
rv := &policydef.Result{
Enabled: enabled,
Pass: true,
Details: d,
}
exp := false
if d.OutsidePushCount > 0 && !mc.PushAllowed {
rv.Pass = false
rv.NotifyText = rv.NotifyText +
fmt.Sprintf(accessText, d.OutsidePushCount, "push")
exp = true
}
if d.OutsideAdminCount > 0 && !mc.AdminAllowed {
rv.Pass = false
rv.NotifyText = rv.NotifyText +
fmt.Sprintf(accessText, d.OutsideAdminCount, "admin")
exp = true
}
if exp {
rv.NotifyText = rv.NotifyText + accessExp
}
return rv, nil
}
func in(name string, list []string) bool {
for _, v := range list {
if v == name {
return true
}
}
return false
}
func getUsers(ctx context.Context, r repositories, owner, repo, perm,
aff string, exemptions []*OutsideExemption, gc globCache) ([]string, error) {
opt := &github.ListCollaboratorsOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
Affiliation: aff,
}
var users []*github.User
for {
us, resp, err := r.ListCollaborators(ctx, owner, repo, opt)
if err != nil {
return nil, err
}
users = append(users, us...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
var rv []string
for _, u := range users {
if u.GetPermissions()[perm] {
if !isExempt(repo, u.GetLogin(), perm, exemptions, gc) {
rv = append(rv, u.GetLogin())
}
}
}
return rv, nil
}
func isExempt(repo, user, access string, ee []*OutsideExemption, gc globCache) bool {
for _, e := range ee {
if !(((e.Push || e.Admin) && access == "push") || (e.Admin && access == "admin")) {
continue
}
g, err := gc.compileGlob(e.Repo)
if err != nil {
log.Warn().
Str("repo", repo).
Str("glob", e.Repo).
Err(err).
Msg("Unexpected error compiling the glob.")
} else if g.Match(repo) && e.User == user {
return true
}
}
return false
}
// Fix implementing policydef.Policy.Fix(). Currently not supported. Plan
// to support this TODO.
func (o Outside) Fix(ctx context.Context, c *github.Client, owner, repo string) error {
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Msg("Action fix is configured, but not implemented.")
return nil
}
// GetAction returns the configured action from this policy's
// configuration stored in the org-level repo, default log. Implementing
// policydef.Policy.GetAction()
func (o Outside) GetAction(ctx context.Context, c *github.Client, owner, repo string) string {
oc, orc, rc := getConfig(ctx, c, owner, repo)
mc := mergeConfig(oc, orc, rc, repo)
return mc.Action
}
func getConfig(ctx context.Context, c *github.Client, owner, repo string) (*OrgConfig, *RepoConfig, *RepoConfig) {
oc := &OrgConfig{ // Fill out non-zero defaults
Action: "log",
PushAllowed: true,
}
if err := configFetchConfig(ctx, c, owner, "", configFile, config.OrgLevel, oc); err != nil {
log.Error().
Str("org", owner).
Str("repo", repo).
Str("configLevel", "orgLevel").
Str("area", polName).
Str("file", configFile).
Err(err).
Msg("Unexpected config error, using defaults.")
}
orc := &RepoConfig{}
if err := configFetchConfig(ctx, c, owner, repo, configFile, config.OrgRepoLevel, orc); err != nil {
log.Error().
Str("org", owner).
Str("repo", repo).
Str("configLevel", "orgRepoLevel").
Str("area", polName).
Str("file", configFile).
Err(err).
Msg("Unexpected config error, using defaults.")
}
rc := &RepoConfig{}
if err := configFetchConfig(ctx, c, owner, repo, configFile, config.RepoLevel, rc); err != nil {
log.Error().
Str("org", owner).
Str("repo", repo).
Str("configLevel", "repoLevel").
Str("area", polName).
Str("file", configFile).
Err(err).
Msg("Unexpected config error, using defaults.")
}
return oc, orc, rc
}
func mergeConfig(oc *OrgConfig, orc *RepoConfig, rc *RepoConfig, repo string) *mergedConfig {
mc := &mergedConfig{
Action: oc.Action,
PushAllowed: oc.PushAllowed,
AdminAllowed: oc.AdminAllowed,
Exemptions: oc.Exemptions,
}
mc = mergeInRepoConfig(mc, orc, repo)
if !oc.OptConfig.DisableRepoOverride {
mc = mergeInRepoConfig(mc, rc, repo)
}
return mc
}
func mergeInRepoConfig(mc *mergedConfig, rc *RepoConfig, repo string) *mergedConfig {
if rc.Action != nil {
mc.Action = *rc.Action
}
if rc.PushAllowed != nil {
mc.PushAllowed = *rc.PushAllowed
}
if rc.AdminAllowed != nil {
mc.AdminAllowed = *rc.AdminAllowed
}
return mc
}
// compileGlob returns cached glob if present, otherwise attempts glob.Compile.
func (g globCache) compileGlob(s string) (glob.Glob, error) {
if glob, ok := g[s]; ok {
return glob, nil
}
c, err := glob.Compile(s)
if err != nil {
return nil, err
}
g[s] = c
return c, nil
}