mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-19 04:57:14 +03:00
83e9f52501
Co-authored-by: Azeem Shaikh <azeems@google.com>
229 lines
6.1 KiB
Go
229 lines
6.1 KiB
Go
// Copyright 2020 Security Scorecard 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 checker includes structs and functions used for running a check.
|
|
package checker
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
// UPGRADEv2: to remove.
|
|
const (
|
|
MaxResultConfidence = 10
|
|
HalfResultConfidence = 5
|
|
MinResultConfidence = 0
|
|
)
|
|
|
|
// UPGRADEv2: to remove.
|
|
const migrationThresholdPassValue = 8
|
|
|
|
// ErrorDemoninatorZero indicates the denominator for a proportional result is 0.
|
|
// UPGRADEv2: to remove.
|
|
var ErrorDemoninatorZero = errors.New("internal error: denominator is 0")
|
|
|
|
// DetailType defines types of details.
|
|
type DetailType int
|
|
|
|
const (
|
|
// DetailInfo is info-level log.
|
|
DetailInfo DetailType = iota
|
|
// DetailWarn is warn log.
|
|
DetailWarn
|
|
// DetailDebug is debug log.
|
|
DetailDebug
|
|
)
|
|
|
|
// CheckDetail contains information for each detail.
|
|
//nolint:govet
|
|
type CheckDetail struct {
|
|
Type DetailType // Any of DetailWarn, DetailInfo, DetailDebug.
|
|
Msg string // A short string explaining why the details was recorded/logged..
|
|
}
|
|
|
|
// DetailLogger logs map to CheckDetail struct.
|
|
type DetailLogger interface {
|
|
Info(desc string, args ...interface{})
|
|
Warn(desc string, args ...interface{})
|
|
Debug(desc string, args ...interface{})
|
|
}
|
|
|
|
//nolint
|
|
const (
|
|
MaxResultScore = 10
|
|
MinResultScore = 0
|
|
InconclusiveResultScore = -1
|
|
)
|
|
|
|
// nolint
|
|
// CheckResult captures result from a check run.
|
|
type CheckResult struct {
|
|
// Old structure
|
|
Error error `json:"-"`
|
|
Name string
|
|
Details []string
|
|
Confidence int
|
|
Pass bool
|
|
ShouldRetry bool `json:"-"`
|
|
|
|
// UPGRADEv2: New structure. Omitting unchanged Name field
|
|
// for simplicity.
|
|
Version int `json:"-"` // Default value of 0 indicates old structure.
|
|
Error2 error `json:"-"` // Runtime error indicate a filure to run the check.
|
|
Details2 []CheckDetail `json:"-"` // Details of tests and sub-checks
|
|
Score int `json:"-"` // {[-1,0...10], -1 = Inconclusive}
|
|
Reason string `json:"-"` // A sentence describing the check result (score, etc)
|
|
}
|
|
|
|
// CreateProportionalScore creates a proportional score.
|
|
func CreateProportionalScore(success, total int) int {
|
|
if total == 0 {
|
|
return 0
|
|
}
|
|
|
|
return int(math.Min(float64(MaxResultScore*success/total), float64(MaxResultScore)))
|
|
}
|
|
|
|
// AggregateScores adds up all scores
|
|
// and normalizes the result.
|
|
// Each score contributes equally.
|
|
func AggregateScores(scores ...int) int {
|
|
n := float64(len(scores))
|
|
r := 0
|
|
for _, s := range scores {
|
|
r += s
|
|
}
|
|
return int(math.Floor(float64(r) / n))
|
|
}
|
|
|
|
// AggregateScoresWithWeight adds up all scores
|
|
// and normalizes the result.
|
|
func AggregateScoresWithWeight(scores map[int]int) int {
|
|
r := 0
|
|
ws := 0
|
|
for s, w := range scores {
|
|
r += s * w
|
|
ws += w
|
|
}
|
|
return int(math.Floor(float64(r) / float64(ws)))
|
|
}
|
|
|
|
// NormalizeReason - placeholder function if we want to update range of scores.
|
|
func NormalizeReason(reason string, score int) string {
|
|
return fmt.Sprintf("%v -- score normalized to %d", reason, score)
|
|
}
|
|
|
|
// CreateResultWithScore is used when
|
|
// the check runs without runtime errors and we want to assign a
|
|
// specific score.
|
|
func CreateResultWithScore(name, reason string, score int) CheckResult {
|
|
pass := true
|
|
if score < migrationThresholdPassValue {
|
|
pass = false
|
|
}
|
|
return CheckResult{
|
|
Name: name,
|
|
// Old structure.
|
|
Error: nil,
|
|
Confidence: MaxResultScore,
|
|
Pass: pass,
|
|
ShouldRetry: false,
|
|
// New structure.
|
|
//nolint
|
|
Version: 2,
|
|
Error2: nil,
|
|
Score: score,
|
|
Reason: reason,
|
|
}
|
|
}
|
|
|
|
// CreateProportionalScoreResult is used when
|
|
// the check runs without runtime errors and we assign a
|
|
// proportional score. This may be used if a check contains
|
|
// multiple tests and we want to assign a score proportional
|
|
// the the number of tests that succeeded.
|
|
func CreateProportionalScoreResult(name, reason string, b, t int) CheckResult {
|
|
pass := true
|
|
score := CreateProportionalScore(b, t)
|
|
if score < migrationThresholdPassValue {
|
|
pass = false
|
|
}
|
|
return CheckResult{
|
|
Name: name,
|
|
// Old structure.
|
|
Error: nil,
|
|
Confidence: MaxResultConfidence,
|
|
Pass: pass,
|
|
ShouldRetry: false,
|
|
// New structure.
|
|
//nolint
|
|
Version: 2,
|
|
Error2: nil,
|
|
Score: score,
|
|
Reason: NormalizeReason(reason, score),
|
|
}
|
|
}
|
|
|
|
// CreateMaxScoreResult is used when
|
|
// the check runs without runtime errors and we can assign a
|
|
// maximum score to the result.
|
|
func CreateMaxScoreResult(name, reason string) CheckResult {
|
|
return CreateResultWithScore(name, reason, MaxResultScore)
|
|
}
|
|
|
|
// CreateMinScoreResult is used when
|
|
// the check runs without runtime errors and we can assign a
|
|
// minimum score to the result.
|
|
func CreateMinScoreResult(name, reason string) CheckResult {
|
|
return CreateResultWithScore(name, reason, MinResultScore)
|
|
}
|
|
|
|
// CreateInconclusiveResult is used when
|
|
// the check runs without runtime errors, but we don't
|
|
// have enough evidence to set a score.
|
|
func CreateInconclusiveResult(name, reason string) CheckResult {
|
|
return CheckResult{
|
|
Name: name,
|
|
// Old structure.
|
|
Confidence: 0,
|
|
Pass: false,
|
|
ShouldRetry: false,
|
|
// New structure.
|
|
//nolint
|
|
Version: 2,
|
|
Score: InconclusiveResultScore,
|
|
Reason: reason,
|
|
}
|
|
}
|
|
|
|
// CreateRuntimeErrorResult is used when the check fails to run because of a runtime error.
|
|
func CreateRuntimeErrorResult(name string, e error) CheckResult {
|
|
return CheckResult{
|
|
Name: name,
|
|
// Old structure.
|
|
Error: e,
|
|
Confidence: 0,
|
|
Pass: false,
|
|
ShouldRetry: false,
|
|
// New structure.
|
|
//nolint
|
|
Version: 2,
|
|
Error2: e,
|
|
Score: InconclusiveResultScore,
|
|
Reason: e.Error(), // Note: message already accessible by caller thru `Error`.
|
|
}
|
|
}
|