scorecard/checks/signed_tags.go

89 lines
2.4 KiB
Go
Raw Normal View History

// 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 (
"errors"
"github.com/shurcooL/githubv4"
2021-05-24 06:51:52 +03:00
"github.com/ossf/scorecard/checker"
2020-10-09 17:47:59 +03:00
)
const (
// CheckSignedTags is the registered name for SignedTags.
CheckSignedTags = "Signed-Tags"
tagLookBack = 5
)
// ErrorNoTags indicates no tags were found for this repo.
var ErrorNoTags = errors.New("no tags found")
//nolint:gochecknoinits
2020-10-09 17:47:59 +03:00
func init() {
registerCheck(CheckSignedTags, SignedTags)
2020-10-09 17:47:59 +03:00
}
func SignedTags(c *checker.CheckRequest) checker.CheckResult {
type ref struct {
Name githubv4.String
Target struct {
Oid githubv4.String
}
}
var query struct {
Repository struct {
Refs struct {
Nodes []ref
} `graphql:"refs(refPrefix: \"refs/tags/\", last: $count)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(c.Owner),
"name": githubv4.String(c.Repo),
"count": githubv4.Int(tagLookBack),
}
if err := c.GraphClient.Query(c.Ctx, &query, variables); err != nil {
return checker.MakeRetryResult(CheckSignedTags, err)
2020-10-09 17:47:59 +03:00
}
totalTags := 0
2020-10-09 17:47:59 +03:00
totalSigned := 0
for _, t := range query.Repository.Refs.Nodes {
sha := string(t.Target.Oid)
totalTags++
gt, _, err := c.Client.Git.GetTag(c.Ctx, c.Owner, c.Repo, sha)
2020-10-09 17:47:59 +03:00
if err != nil {
2021-02-12 22:25:13 +03:00
c.Logf("!! unable to find the annotated commit: %s", sha)
Fixes - verifiedtag checks The reason the tags aren't working for certain repositories is that because the Lightweight Tags vs Annotated Tags >Basically, lightweight tags are just pointers to specific commits. No further information is saved; on the other hand, annotated tags are regular objects, which have an author and a date and can be referred because they have their own SHA key. https://api.github.com/repos/ossf/scorecard/git/refs/tags ``` [ { "ref": "refs/tags/v1.0.0", "node_id": "MDM6UmVmMzAyNjcwNzk3OnJlZnMvdGFncy92MS4wLjA=", "url": "https://api.github.com/repos/ossf/scorecard/git/refs/tags/v1.0.0", "object": { "sha": "87997ffb5724cb479223a08a2890c60b0ea4bfbd", "type": "commit", "url": "https://api.github.com/repos/ossf/scorecard/git/commits/87997ffb5724cb479223a08a2890c60b0ea4bfbd" } }, { "ref": "refs/tags/v1.1.0", "node_id": "MDM6UmVmMzAyNjcwNzk3OnJlZnMvdGFncy92MS4xLjA=", "url": "https://api.github.com/repos/ossf/scorecard/git/refs/tags/v1.1.0", "object": { "sha": "f2c633854602cf0c8f33164a169fb0a8454bee01", "type": "tag", "url": "https://api.github.com/repos/ossf/scorecard/git/tags/f2c633854602cf0c8f33164a169fb0a8454bee01" } } ] ``` Annotated tags https://api.github.com/repos/kubernetes/kubernetes/git/refs/tags ``` [ { "ref": "refs/tags/v0.2", "node_id": "MDM6UmVmMjA1ODA0OTg6cmVmcy90YWdzL3YwLjI=", "url": "https://api.github.com/repos/kubernetes/kubernetes/git/refs/tags/v0.2", "object": { "sha": "64dbf9ae21dd0deb485f88b79b96eb35ca855138", "type": "tag", "url": "https://api.github.com/repos/kubernetes/kubernetes/git/tags/64dbf9ae21dd0deb485f88b79b96eb35ca855138" } } ] ``` The look for the tag fails because of there isn't a tag object but only a commit object. https://api.github.com/repos/ossf/scorecard/git/commits/87997ffb5724cb479223a08a2890c60b0ea4bfbd fixes #107
2021-02-12 20:51:11 +03:00
continue
2020-10-09 17:47:59 +03:00
}
if gt.GetVerification().GetVerified() {
c.Logf("verified tag found: %s, commit: %s", t.Name, sha)
2020-10-09 17:47:59 +03:00
totalSigned++
} else {
c.Logf("!! unverified tag found: %s, commit: %s, reason: %s", t.Name, sha, gt.GetVerification().GetReason())
2020-10-09 17:47:59 +03:00
}
}
if totalTags == 0 {
c.Logf("no tags found")
return checker.MakeInconclusiveResult(CheckSignedTags, ErrorNoTags)
}
c.Logf("found %d out of %d verified tags", totalSigned, totalTags)
return checker.MakeProportionalResult(CheckSignedTags, totalSigned, totalTags, 0.8)
2020-10-09 17:47:59 +03:00
}