/
scorecard.go
337 lines (292 loc) · 10 KB
/
scorecard.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
// Copyright 2022 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 scorecard implements the generic Security Scorecards policy
package scorecard
import (
"context"
"fmt"
"net/http"
"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/ossf/allstar/pkg/scorecard"
"github.com/ossf/scorecard/v4/checker"
"github.com/ossf/scorecard/v4/checks"
"github.com/google/go-github/v59/github"
"github.com/rs/zerolog/log"
)
const configFile = "scorecard.yaml"
const polName = "Security Scorecards"
// OrgConfig is the org-level config definition for this 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"`
// Checks is a list of check names to run from Security Scorecards. These
// must match the name that the check uses in it's call to
// "registerCheck". See the check code for each name:
// https://github.com/ossf/scorecard/tree/main/checks For example, the name
// for the Signed Releases check is "Signed-Releases".
Checks []string `json:"checks"`
// Threshold is the score threshold that checks must meet to pass the
// policy. If all checks score equal or above the threshold, the Allstar
// policy will pass. The default is checker.MaxResultScore:
// https://pkg.go.dev/github.com/ossf/scorecard/v4@v4.4.0/checker#pkg-constants
Threshold int `json:"threshold"`
}
// RepoConfig is the repo-level config for this 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"`
// Checks overrides the same setting in org-level, only if present.
Checks *[]string `json:"checks"`
// Threshold overrides the same setting in org-level, only if present.
Threshold *int `json:"threshold"`
}
type mergedConfig struct {
Action string
Checks []string
Threshold int
}
type details struct {
// Findings key is the check name, and value are logs from Scorecards.
Findings map[string][]string
}
var configFetchConfig func(context.Context, *github.Client, string, string, string, config.ConfigLevel, interface{}) error
var configIsEnabled func(context.Context, config.OrgOptConfig, config.RepoOptConfig, config.RepoOptConfig, *github.Client, string, string) (bool, error)
var scorecardGet func(context.Context, string, http.RoundTripper) (*scorecard.ScClient, error)
var checksAllChecks checker.CheckNameToFnMap
func init() {
configFetchConfig = config.FetchConfig
configIsEnabled = config.IsEnabled
checksAllChecks = checks.GetAll()
scorecardGet = scorecard.Get
}
// Scorecard is the Security Scorecard policy object, implements
// policydef.Policy.
type Scorecard bool
// NewScorecard returns a new Scorecard policy.
func NewScorecard() policydef.Policy {
var b Scorecard
return b
}
// Name returns the name of this policy, implementing policydef.Policy.Name()
func (b Scorecard) Name() string {
return polName
}
// Check whether this policy is enabled or not
func (b Scorecard) 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)
}
// Check performs the policy check for this policy based on the
// configuration stored in the org/repo, implementing policydef.Policy.Check()
func (b Scorecard) Check(ctx context.Context, 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
}
log.Info().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Bool("enabled", enabled).
Msg("Check repo enabled")
mc := mergeConfig(oc, orc, rc, repo)
fullName := fmt.Sprintf("%s/%s", owner, repo)
tr := c.Client().Transport
scc, err := scorecardGet(ctx, fullName, tr)
if err != nil {
return nil, err
}
var notify string
pass := true
f := make(map[string][]string)
for _, n := range mc.Checks {
if n == checks.CheckVulnerabilities {
// FIXME Rolling back support for Vulns, needs more testing.
continue
}
l := checker.NewLogger()
cr := &checker.CheckRequest{
Ctx: ctx,
RepoClient: scc.ScRepoClient,
Repo: scc.ScRepo,
Dlogger: l,
}
check, ok := checksAllChecks[n]
if !ok {
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Str("check", n).
Msg("Unknown scorecard check specified.")
break
}
res := check.Fn(cr)
if res.Error != nil {
// We are not sure that all checks are safe to run inside Allstar, some
// might error, and we don't want to abort a whole org enforcement loop
// for an expected error. Just log the error and move on. If there are
// any results from a previous check, those can be returned, so just
// break from the loop here.
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Str("check", n).
Err(res.Error).
Msg("Scorecard check errored.")
break
}
logs := convertLogs(l.Flush())
if len(logs) > 0 {
f[n] = logs
}
if res.Score < mc.Threshold && res.Score != checker.InconclusiveResultScore {
pass = false
if notify == "" {
notify = `Project is out of compliance with Security Scorecards policy
**Rule Description**
This is a generic passthrough policy that runs the configured checks from Security Scorecards. Please see the [Security Scorecards Documentation](https://github.com/ossf/scorecard/blob/main/docs/checks.md) for more information on each check.
The score was %v, and the passing threshold is %v.
`
notify = fmt.Sprintf(notify, res.Score, mc.Threshold)
}
if len(logs) > 10 {
notify += fmt.Sprintf(
"**First 10 Results from policy: %v : %v**\n\n%v"+
"- Run a Scorecards scan to see full list.\n\n",
res.Name, res.Reason, listJoin(logs[:10]))
} else {
notify += fmt.Sprintf("**Results from policy: %v : %v**\n\n%v\n",
res.Name, res.Reason, listJoin(logs))
}
}
}
return &policydef.Result{
Enabled: enabled,
Pass: pass,
NotifyText: notify,
Details: details{
Findings: f,
},
}, nil
}
func listJoin(list []string) string {
var s string
for _, l := range list {
s += fmt.Sprintf("- %v\n", l)
}
return s
}
func convertLogs(logs []checker.CheckDetail) []string {
var s []string
for _, l := range logs {
if l.Msg.Finding != nil {
if l.Msg.Finding.Location == nil || l.Msg.Finding.Location.Snippet == nil || l.Msg.Finding.Location.LineStart == nil {
s = append(s, fmt.Sprintf("%v", l.Msg.Finding.Message))
} else {
s = append(s, fmt.Sprintf("%v[%v]:%v", *l.Msg.Finding.Location.Snippet, *l.Msg.Finding.Location.LineStart, l.Msg.Finding.Message))
}
} else {
s = append(s, fmt.Sprintf("%v[%v]:%v", l.Msg.Path, l.Msg.Offset, l.Msg.Text))
}
}
return s
}
// Fix implementing policydef.Policy.Fix(). Scorecard checks will not have a Fix option.
func (b Scorecard) 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 (b Scorecard) 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",
Threshold: checker.MaxResultScore,
}
if err := configFetchConfig(ctx, c, owner, "", configFile, config.OrgLevel, oc); err != nil {
log.Error().
Str("org", owner).
Str("repo", repo).
Bool("orgLevel", true).
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).
Bool("orgLevel", false).
Str("area", polName).
Str("file", configFile).
Err(err).
Msg("Unexpected config error, using defaults.")
}
return oc, orc, rc
}
func mergeConfig(oc *OrgConfig, orc, rc *RepoConfig, repo string) *mergedConfig {
mc := &mergedConfig{
Action: oc.Action,
Checks: oc.Checks,
Threshold: oc.Threshold,
}
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.Checks != nil {
mc.Checks = *rc.Checks
}
if rc.Threshold != nil {
mc.Threshold = *rc.Threshold
}
return mc
}