2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2020 OpenSSF Scorecard Authors
|
2020-10-26 23:22:13 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
2021-05-21 09:47:49 +03:00
|
|
|
"context"
|
2021-08-03 01:38:42 +03:00
|
|
|
"errors"
|
2020-10-13 18:35:55 +03:00
|
|
|
"fmt"
|
2021-05-21 09:47:49 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
opencensusstats "go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/tag"
|
2021-05-24 06:51:52 +03:00
|
|
|
|
2022-01-12 22:49:01 +03:00
|
|
|
sce "github.com/ossf/scorecard/v4/errors"
|
|
|
|
"github.com/ossf/scorecard/v4/stats"
|
2020-10-09 17:47:59 +03:00
|
|
|
)
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
const checkRetries = 3
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// Runner runs a check with retries.
|
2021-04-10 15:26:56 +03:00
|
|
|
type Runner struct {
|
2021-05-21 09:47:49 +03:00
|
|
|
CheckName string
|
|
|
|
Repo string
|
2022-02-08 03:49:49 +03:00
|
|
|
CheckRequest CheckRequest
|
2020-10-13 18:35:55 +03:00
|
|
|
}
|
|
|
|
|
2022-02-27 05:09:21 +03:00
|
|
|
// NewRunner creates a new instance of `Runner`.
|
|
|
|
func NewRunner(checkName, repo string, checkReq *CheckRequest) *Runner {
|
|
|
|
return &Runner{
|
|
|
|
CheckName: checkName,
|
|
|
|
Repo: repo,
|
|
|
|
CheckRequest: *checkReq,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCheckName sets the check name.
|
|
|
|
func (r *Runner) SetCheckName(check string) {
|
|
|
|
r.CheckName = check
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRepo sets the repository.
|
|
|
|
func (r *Runner) SetRepo(repo string) {
|
|
|
|
r.Repo = repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCheckRequest sets the check request.
|
|
|
|
func (r *Runner) SetCheckRequest(checkReq *CheckRequest) {
|
|
|
|
r.CheckRequest = *checkReq
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// CheckFn defined for convenience.
|
2021-04-19 22:18:34 +03:00
|
|
|
type CheckFn func(*CheckRequest) CheckResult
|
2021-04-10 15:26:56 +03:00
|
|
|
|
2022-02-08 03:49:49 +03:00
|
|
|
// Check defines a Scorecard check fn and its supported request types.
|
|
|
|
type Check struct {
|
|
|
|
Fn CheckFn
|
|
|
|
SupportedRequestTypes []RequestType
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// CheckNameToFnMap defined here for convenience.
|
2022-02-08 03:49:49 +03:00
|
|
|
type CheckNameToFnMap map[string]Check
|
2021-04-10 15:26:56 +03:00
|
|
|
|
2021-07-20 21:36:35 +03:00
|
|
|
func logStats(ctx context.Context, startTime time.Time, result *CheckResult) error {
|
2021-06-10 00:18:40 +03:00
|
|
|
runTimeInSecs := time.Now().Unix() - startTime.Unix()
|
|
|
|
opencensusstats.Record(ctx, stats.CheckRuntimeInSec.M(runTimeInSecs))
|
2021-06-22 01:38:50 +03:00
|
|
|
|
|
|
|
if result.Error != nil {
|
2022-04-23 02:22:43 +03:00
|
|
|
ctx, err := tag.New(ctx, tag.Upsert(stats.ErrorName, sce.GetName(result.Error)))
|
2021-06-22 01:38:50 +03:00
|
|
|
if err != nil {
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("tag.New: %v", err))
|
2021-06-22 01:38:50 +03:00
|
|
|
}
|
|
|
|
opencensusstats.Record(ctx, stats.CheckErrors.M(1))
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-10 00:18:40 +03:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// Run runs a given check.
|
2022-02-08 03:49:49 +03:00
|
|
|
func (r *Runner) Run(ctx context.Context, c Check) CheckResult {
|
|
|
|
// Sanity check.
|
|
|
|
unsupported := ListUnsupported(r.CheckRequest.RequiredTypes, c.SupportedRequestTypes)
|
|
|
|
if len(unsupported) != 0 {
|
|
|
|
return CreateRuntimeErrorResult(r.CheckName,
|
|
|
|
sce.WithMessage(sce.ErrorUnsupportedCheck,
|
|
|
|
fmt.Sprintf("requiredType: %s not supported by check %s", fmt.Sprint(unsupported), r.CheckName)))
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:34:57 +03:00
|
|
|
ctx, err := tag.New(ctx, tag.Upsert(stats.CheckName, r.CheckName))
|
2021-05-21 09:47:49 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-22 01:38:50 +03:00
|
|
|
startTime := time.Now()
|
2021-05-21 09:47:49 +03:00
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
var res CheckResult
|
2022-02-27 05:09:21 +03:00
|
|
|
l := NewLogger()
|
2021-04-10 15:26:56 +03:00
|
|
|
for retriesRemaining := checkRetries; retriesRemaining > 0; retriesRemaining-- {
|
|
|
|
checkRequest := r.CheckRequest
|
2021-05-22 08:04:04 +03:00
|
|
|
checkRequest.Ctx = ctx
|
2022-02-27 05:09:21 +03:00
|
|
|
checkRequest.Dlogger = l
|
2022-02-08 03:49:49 +03:00
|
|
|
res = c.Fn(&checkRequest)
|
2022-04-23 02:22:43 +03:00
|
|
|
if res.Error != nil && errors.Is(res.Error, sce.ErrRepoUnreachable) {
|
2022-02-12 02:48:58 +03:00
|
|
|
checkRequest.Dlogger.Warn(&LogMessage{
|
2022-04-23 02:22:43 +03:00
|
|
|
Text: fmt.Sprintf("%v", res.Error),
|
2022-02-12 02:48:58 +03:00
|
|
|
})
|
2020-10-13 18:35:55 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-08-17 02:26:19 +03:00
|
|
|
|
2021-12-15 00:10:24 +03:00
|
|
|
// Set details.
|
2022-02-12 02:48:58 +03:00
|
|
|
// TODO(#1393): Remove.
|
2022-05-12 22:58:12 +03:00
|
|
|
res.Details = l.Flush()
|
2021-12-15 00:10:24 +03:00
|
|
|
|
2021-07-20 21:36:35 +03:00
|
|
|
if err := logStats(ctx, startTime, &res); err != nil {
|
2021-06-22 01:38:50 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-13 18:35:55 +03:00
|
|
|
return res
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|