-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.go
269 lines (208 loc) · 6.05 KB
/
audit.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
package cmd
import (
"context"
"errors"
"fmt"
"os"
"golang.org/x/exp/slices"
"golang.org/x/oauth2"
"github.com/google/go-github/v50/github"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
branchFl string
auditCmd = &cobra.Command{
Use: "audit",
Short: "Validates that 1 or more repos meet a set of policy",
RunE: func(cmd *cobra.Command, args []string) error {
var results auditResults
repoFile, _, err := loadRepositoriesFile(repositoriesFileFl)
if err != nil {
return err
}
policy, _, err := loadPolicyFile(policyFileFl)
if err != nil {
return err
}
ghToken := viper.GetString("GH_TOKEN")
if ghToken == "" {
return errors.New("GitHub credentials were not found. Please run `warden configure`.")
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
group, err := repoFile.Group(groupFl)
if err != nil {
return err
}
repos, err := WardenRepos(group.GetRepositories(childrenFl))
if err != nil {
return err
}
for _, repo := range repos {
var currentBranch string
repoResp, _, _ := client.Repositories.Get(context.Background(), repo.Owner, repo.Name)
if repoResp.GetArchived() != policy.Archived {
continue
}
// check if we're working with the default branch or a specific one
if branchFl != "" {
currentBranch = branchFl
} else {
currentBranch = repoResp.GetDefaultBranch()
}
if repoResp.GetDefaultBranch() != policy.DefaultBranch {
results.add(
repo,
RESULT_ERROR,
ERR_BRANCH_DEFAULT,
policy.DefaultBranch,
repoResp.GetDefaultBranch(),
)
}
// if license is to be checked...
if policy.License != nil && policy.License.Scope == repoResp.GetVisibility() || policy.License.Scope == "all" {
if repoResp.GetLicense().GetKey() == "" {
results.add(
repo,
RESULT_ERROR,
ERR_LICENSE_MISSING,
)
} else if !slices.Contains(policy.License.Names, repoResp.GetLicense().GetKey()) {
results.add(
repo,
RESULT_ERROR,
ERR_LICENSE_DIFFERENT,
policy.License.Names,
repoResp.GetLicense().GetKey(),
)
}
}
// if label checks are to happen
if len(policy.Labels) > 0 {
labels, _, err := client.Issues.ListLabels(context.Background(), repo.Owner, repo.Name, nil)
if err != nil {
return err
}
if policy.LabelStrategy == "available" || policy.LabelStrategy == "" {
// for each labal we're checking for
for _, label := range policy.Labels {
found := false
for _, iLabel := range labels {
if label == iLabel.GetName() {
found = true
}
}
if !found {
results.add(
repo,
RESULT_ERROR,
ERR_LABEL_MISSING,
label,
)
}
}
} else if policy.LabelStrategy == "only" {
// for each labal we're checking for
for _, iLabel := range labels {
found := ""
for _, label := range policy.Labels {
if label == iLabel.GetName() {
found = label
}
}
if found != "" {
results.add(
repo,
RESULT_ERROR,
ERR_LABEL_EXTRA,
found,
)
}
}
} else {
return errors.New("The labelStrategy of " + policy.LabelStrategy + " isn't valid.")
}
}
// if access permissions are to be checked...
if len(policy.Access) > 0 {
teams, resp, err := client.Repositories.ListTeams(context.Background(), repo.Owner, repo.Name, nil)
if resp.StatusCode == 404 {
// considering this repo worked for other audits but not this, this likely
// means we don't have admin access in order to check teams
results.add(repo, RESULT_WARNING, fmt.Sprintf("Couldn't pull teams. There's a visibility issue here."))
// skip the rest
policy.Access = nil
} else if err != nil {
return err
}
for _, accessPolicy := range policy.Access {
results.merge(auditAccessPolicy(accessPolicy, repo, teams))
}
}
// if codeowners are to to be checked...
if len(policy.Codeowners) > 0 {
for _, coPolicy := range policy.Codeowners {
results.merge(auditCodeownersPolicy(coPolicy, repo, client, currentBranch))
}
}
}
fmt.Printf(
`======================================================================
Warden Audit Results
errors: %d warnings: %d repos: %d group: %s
======================================================================
`,
len(results.ByType(RESULT_ERROR)),
len(results.ByType(RESULT_WARNING)),
len(repos),
groupFl,
)
if len(results) > 0 {
var curRepo string
for _, result := range results {
// print repo URL whenever we move to the next one
if curRepo != result.repository.ToHTTPS() {
curRepo = result.repository.ToHTTPS()
fmt.Fprintf(os.Stderr, "%s:\n", curRepo)
}
switch result.resultType {
case RESULT_ERROR:
fmt.Fprintf(os.Stderr, " \033[31mx\033[0m %s\n", result)
case RESULT_WARNING:
fmt.Printf(" \033[33mo\033[0m %s\n", result)
}
}
fmt.Println("") // intentional
}
if len(results.ByType(RESULT_ERROR)) > 0 {
return fmt.Errorf("The audit failed. Above are the policy failures, by repository.\n")
}
fmt.Println("The audit completed successfully.")
return nil
},
}
)
func init() {
AddChildrenFlag(auditCmd)
AddGroupFlag(auditCmd)
AddPolicyFileFlag(auditCmd)
AddRepositoriesFileFlag(auditCmd)
auditCmd.PersistentFlags().StringVar(&branchFl, "branch", "", "git branch to audit (for applicable polcies")
rootCmd.AddCommand(auditCmd)
}
func tagsMatched(policyTags, repoTags []string) bool {
// if policy doesn't specify tags, then all repos are allowed
if len(policyTags) == 0 {
return true
}
for _, tag := range policyTags {
if slices.Contains(repoTags, tag) {
return true
}
}
return false
}