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-11-07 00:28:26 +03:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
const (
|
2021-05-28 00:54:34 +03:00
|
|
|
// CheckSignedTags is the registered name for SignedTags.
|
|
|
|
CheckSignedTags = "Signed-Tags"
|
|
|
|
tagLookBack = 5
|
2021-04-10 15:26:56 +03:00
|
|
|
)
|
2020-10-16 19:55:25 +03:00
|
|
|
|
2021-05-24 21:34:33 +03:00
|
|
|
// ErrorNoTags indicates no tags were found for this repo.
|
|
|
|
var ErrorNoTags = errors.New("no tags found")
|
2021-05-22 22:36:47 +03:00
|
|
|
|
2021-05-22 20:19:52 +03:00
|
|
|
//nolint:gochecknoinits
|
2020-10-09 17:47:59 +03:00
|
|
|
func init() {
|
2021-05-28 00:54:34 +03:00
|
|
|
registerCheck(CheckSignedTags, SignedTags)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
func SignedTags(c *checker.CheckRequest) checker.CheckResult {
|
2020-11-07 00:28:26 +03:00
|
|
|
type ref struct {
|
|
|
|
Name githubv4.String
|
|
|
|
Target struct {
|
|
|
|
Oid githubv4.String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var query struct {
|
|
|
|
Repository struct {
|
|
|
|
Refs struct {
|
|
|
|
Nodes []ref
|
2020-11-10 05:13:59 +03:00
|
|
|
} `graphql:"refs(refPrefix: \"refs/tags/\", last: $count)"`
|
2020-11-07 00:28:26 +03:00
|
|
|
} `graphql:"repository(owner: $owner, name: $name)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
variables := map[string]interface{}{
|
|
|
|
"owner": githubv4.String(c.Owner),
|
|
|
|
"name": githubv4.String(c.Repo),
|
2020-11-10 05:13:59 +03:00
|
|
|
"count": githubv4.Int(tagLookBack),
|
2020-11-07 00:28:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.GraphClient.Query(c.Ctx, &query, variables); err != nil {
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeRetryResult(CheckSignedTags, err)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|
2020-12-21 01:10:05 +03:00
|
|
|
totalTags := 0
|
2020-10-09 17:47:59 +03:00
|
|
|
totalSigned := 0
|
2020-11-07 00:28:26 +03:00
|
|
|
for _, t := range query.Repository.Refs.Nodes {
|
|
|
|
sha := string(t.Target.Oid)
|
2020-12-21 01:10:05 +03:00
|
|
|
totalTags++
|
2020-11-07 00:28:26 +03:00
|
|
|
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() {
|
2020-12-20 23:28:45 +03:00
|
|
|
c.Logf("verified tag found: %s, commit: %s", t.Name, sha)
|
2020-10-09 17:47:59 +03:00
|
|
|
totalSigned++
|
2020-12-20 23:28:45 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 01:10:05 +03:00
|
|
|
if totalTags == 0 {
|
|
|
|
c.Logf("no tags found")
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeInconclusiveResult(CheckSignedTags, ErrorNoTags)
|
2020-12-20 23:28:45 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 01:10:05 +03:00
|
|
|
c.Logf("found %d out of %d verified tags", totalSigned, totalTags)
|
2021-05-28 00:54:34 +03:00
|
|
|
return checker.MakeProportionalResult(CheckSignedTags, totalSigned, totalTags, 0.8)
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|