Skip to content

Commit 1c95237

Browse files
Only run allowed checks in different modes (#1579)
Co-authored-by: Azeem Shaikh <azeems@google.com>
1 parent eac2aec commit 1c95237

25 files changed

+116
-305
lines changed

checker/check_request.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,34 @@ type CheckRequest struct {
3030
Repo clients.Repo
3131
VulnerabilitiesClient clients.VulnerabilitiesClient
3232
// UPGRADEv6: return raw results instead of scores.
33-
RawResults *RawResults
33+
RawResults *RawResults
34+
RequiredTypes []RequestType
35+
}
36+
37+
// RequestType identifies special requirements/attributes that need to be supported by checks.
38+
type RequestType int
39+
40+
const (
41+
// FileBased request types require checks to run solely on file-content.
42+
FileBased RequestType = iota
43+
)
44+
45+
// ListUnsupported returns []RequestType not in `supported` and are `required`.
46+
func ListUnsupported(required, supported []RequestType) []RequestType {
47+
var ret []RequestType
48+
for _, t := range required {
49+
if !contains(supported, t) {
50+
ret = append(ret, t)
51+
}
52+
}
53+
return ret
54+
}
55+
56+
func contains(in []RequestType, exists RequestType) bool {
57+
for _, r := range in {
58+
if r == exists {
59+
return true
60+
}
61+
}
62+
return false
3463
}

checker/check_runner.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,22 @@ const checkRetries = 3
3131

3232
// Runner runs a check with retries.
3333
type Runner struct {
34-
CheckRequest CheckRequest
3534
CheckName string
3635
Repo string
36+
CheckRequest CheckRequest
3737
}
3838

3939
// CheckFn defined for convenience.
4040
type CheckFn func(*CheckRequest) CheckResult
4141

42+
// Check defines a Scorecard check fn and its supported request types.
43+
type Check struct {
44+
Fn CheckFn
45+
SupportedRequestTypes []RequestType
46+
}
47+
4248
// CheckNameToFnMap defined here for convenience.
43-
type CheckNameToFnMap map[string]CheckFn
49+
type CheckNameToFnMap map[string]Check
4450

4551
func logStats(ctx context.Context, startTime time.Time, result *CheckResult) error {
4652
runTimeInSecs := time.Now().Unix() - startTime.Unix()
@@ -57,7 +63,15 @@ func logStats(ctx context.Context, startTime time.Time, result *CheckResult) err
5763
}
5864

5965
// Run runs a given check.
60-
func (r *Runner) Run(ctx context.Context, f CheckFn) CheckResult {
66+
func (r *Runner) Run(ctx context.Context, c Check) CheckResult {
67+
// Sanity check.
68+
unsupported := ListUnsupported(r.CheckRequest.RequiredTypes, c.SupportedRequestTypes)
69+
if len(unsupported) != 0 {
70+
return CreateRuntimeErrorResult(r.CheckName,
71+
sce.WithMessage(sce.ErrorUnsupportedCheck,
72+
fmt.Sprintf("requiredType: %s not supported by check %s", fmt.Sprint(unsupported), r.CheckName)))
73+
}
74+
6175
ctx, err := tag.New(ctx, tag.Upsert(stats.CheckName, r.CheckName))
6276
if err != nil {
6377
panic(err)
@@ -71,7 +85,7 @@ func (r *Runner) Run(ctx context.Context, f CheckFn) CheckResult {
7185
checkRequest.Ctx = ctx
7286
l = logger{}
7387
checkRequest.Dlogger = &l
74-
res = f(&checkRequest)
88+
res = c.Fn(&checkRequest)
7589
if res.Error2 != nil && errors.Is(res.Error2, sce.ErrRepoUnreachable) {
7690
checkRequest.Dlogger.Warn("%v", res.Error2)
7791
continue

checks/all_checks.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ import (
2222
// AllChecks is the list of all security checks that will be run.
2323
var AllChecks = checker.CheckNameToFnMap{}
2424

25-
func registerCheck(name string, fn checker.CheckFn) error {
25+
func registerCheck(name string, fn checker.CheckFn, supportedRequestTypes []checker.RequestType) error {
2626
if name == "" {
2727
return errInternalNameCannotBeEmpty
2828
}
2929
if fn == nil {
3030
return errInternalCheckFuncCannotBeNil
3131
}
32-
AllChecks[name] = fn
32+
AllChecks[name] = checker.Check{
33+
Fn: fn,
34+
SupportedRequestTypes: supportedRequestTypes,
35+
}
3336
return nil
3437
}

checks/all_checks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Test_registerCheck(t *testing.T) {
6262
tt := tt
6363
t.Run(tt.name, func(t *testing.T) {
6464
t.Parallel()
65-
if err := registerCheck(tt.args.name, tt.args.fn); (err != nil) != tt.wanterr {
65+
if err := registerCheck(tt.args.name, tt.args.fn, nil /*supportedRequestTypes*/); (err != nil) != tt.wanterr {
6666
t.Errorf("registerCheck() error = %v, wantErr %v", err, tt.wanterr)
6767
}
6868
})

checks/binary_artifact.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const CheckBinaryArtifacts string = "Binary-Artifacts"
2626

2727
//nolint
2828
func init() {
29-
if err := registerCheck(CheckBinaryArtifacts, BinaryArtifacts); err != nil {
29+
var supportedRequestTypes = []checker.RequestType{
30+
checker.FileBased,
31+
}
32+
if err := registerCheck(CheckBinaryArtifacts, BinaryArtifacts, supportedRequestTypes); err != nil {
3033
// this should never happen
3134
panic(err)
3235
}

checks/branch_protection.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import (
2121
sce "github.com/ossf/scorecard/v4/errors"
2222
)
2323

24-
const (
25-
// CheckBranchProtection is the exported name for Branch-Protected check.
26-
CheckBranchProtection = "Branch-Protection"
27-
)
24+
// CheckBranchProtection is the exported name for Branch-Protected check.
25+
const CheckBranchProtection = "Branch-Protection"
2826

2927
//nolint:gochecknoinits
3028
func init() {
31-
if err := registerCheck(CheckBranchProtection, BranchProtection); err != nil {
29+
if err := registerCheck(CheckBranchProtection, BranchProtection, nil); err != nil {
3230
// this should never happen
3331
panic(err)
3432
}

checks/ci_tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131

3232
//nolint:gochecknoinits
3333
func init() {
34-
if err := registerCheck(CheckCITests, CITests); err != nil {
34+
if err := registerCheck(CheckCITests, CITests, nil); err != nil {
3535
// this should never happen
3636
panic(err)
3737
}

checks/cii_best_practices.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232

3333
//nolint:gochecknoinits
3434
func init() {
35-
if err := registerCheck(CheckCIIBestPractices, CIIBestPractices); err != nil {
35+
if err := registerCheck(CheckCIIBestPractices, CIIBestPractices, nil); err != nil {
3636
// this should never happen
3737
panic(err)
3838
}

checks/code_review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const CheckCodeReview = "Code-Review"
2626

2727
//nolint:gochecknoinits
2828
func init() {
29-
if err := registerCheck(CheckCodeReview, CodeReview); err != nil {
29+
if err := registerCheck(CheckCodeReview, CodeReview, nil); err != nil {
3030
// this should never happen
3131
panic(err)
3232
}

checks/contributors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131

3232
//nolint:gochecknoinits
3333
func init() {
34-
if err := registerCheck(CheckContributors, Contributors); err != nil {
34+
if err := registerCheck(CheckContributors, Contributors, nil); err != nil {
3535
// this should never happen
3636
panic(err)
3737
}

0 commit comments

Comments
 (0)