2020-10-26 23:22:13 +03:00
|
|
|
// 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.
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
2021-05-17 23:03:39 +03:00
|
|
|
"errors"
|
2020-10-09 17:47:59 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/go-github/v32/github"
|
2021-05-24 06:51:52 +03:00
|
|
|
|
2020-10-27 22:23:48 +03:00
|
|
|
"github.com/ossf/scorecard/checker"
|
2020-10-09 17:47:59 +03:00
|
|
|
)
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
const (
|
|
|
|
signedReleasesStr = "Signed-Releases"
|
|
|
|
releaseLookBackDays = 5
|
|
|
|
)
|
2020-10-16 23:09:20 +03:00
|
|
|
|
2021-05-24 21:34:33 +03:00
|
|
|
// ErrorNoReleases indicates no releases were found for this repo.
|
2021-05-22 22:36:47 +03:00
|
|
|
var ErrorNoReleases = errors.New("no releases found")
|
|
|
|
|
2021-05-22 20:19:52 +03:00
|
|
|
//nolint:gochecknoinits
|
2020-10-09 17:47:59 +03:00
|
|
|
func init() {
|
2021-04-10 15:26:56 +03:00
|
|
|
registerCheck(signedReleasesStr, SignedReleases)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func SignedReleases(c *checker.CheckRequest) checker.CheckResult {
|
2020-10-09 17:47:59 +03:00
|
|
|
releases, _, err := c.Client.Repositories.ListReleases(c.Ctx, c.Owner, c.Repo, &github.ListOptions{})
|
|
|
|
if err != nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeRetryResult(signedReleasesStr, err)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:32:16 +03:00
|
|
|
artifactExtensions := []string{".asc", ".minisig", ".sig"}
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
totalReleases := 0
|
|
|
|
totalSigned := 0
|
|
|
|
for _, r := range releases {
|
|
|
|
assets, _, err := c.Client.Repositories.ListReleaseAssets(c.Ctx, c.Owner, c.Repo, r.GetID(), &github.ListOptions{})
|
|
|
|
if err != nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeRetryResult(signedReleasesStr, err)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
2020-10-16 23:09:20 +03:00
|
|
|
if len(assets) == 0 {
|
2020-10-09 17:47:59 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-12-24 00:20:14 +03:00
|
|
|
c.Logf("release found: %s", r.GetTagName())
|
2020-10-09 17:47:59 +03:00
|
|
|
totalReleases++
|
|
|
|
signed := false
|
|
|
|
for _, asset := range assets {
|
2020-12-20 23:32:16 +03:00
|
|
|
for _, suffix := range artifactExtensions {
|
2020-10-09 17:47:59 +03:00
|
|
|
if strings.HasSuffix(asset.GetName(), suffix) {
|
2020-11-24 17:50:52 +03:00
|
|
|
c.Logf("signed release artifact found: %s, url: %s", asset.GetName(), asset.GetURL())
|
2020-10-09 17:47:59 +03:00
|
|
|
signed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if signed {
|
|
|
|
totalSigned++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 23:32:16 +03:00
|
|
|
if !signed {
|
2020-12-24 00:20:14 +03:00
|
|
|
c.Logf("!! release %s has no signed artifacts", r.GetTagName())
|
2020-12-20 23:32:16 +03:00
|
|
|
}
|
2021-04-10 15:26:56 +03:00
|
|
|
if totalReleases > releaseLookBackDays {
|
2020-10-16 23:09:20 +03:00
|
|
|
break
|
|
|
|
}
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if totalReleases == 0 {
|
2020-12-20 23:32:16 +03:00
|
|
|
c.Logf("no releases found")
|
2021-05-22 22:36:47 +03:00
|
|
|
return checker.MakeInconclusiveResult(signedReleasesStr, ErrorNoReleases)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
2020-12-20 23:32:16 +03:00
|
|
|
|
2020-12-21 01:10:05 +03:00
|
|
|
c.Logf("found signed artifacts for %d out of %d releases", totalSigned, totalReleases)
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeProportionalResult(signedReleasesStr, totalSigned, totalReleases, 0.8)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|