Skip to content

Commit

Permalink
Update scorecard interface to use scorecard.Run()
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Mendoza <[email protected]>
  • Loading branch information
jeffmendoza authored and justaugustus committed Oct 18, 2024
1 parent 6507a6c commit 9f5fb01
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 51 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/bombsimon/logrusr/v2 v2.0.1 // indirect
github.com/caarlos0/env/v6 v6.10.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
Expand Down Expand Up @@ -89,6 +91,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jedib0t/go-pretty/v6 v6.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand All @@ -99,6 +102,7 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/buildkit v0.15.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/owenrumney/go-sarif/v2 v2.3.2 // indirect
Expand All @@ -115,6 +119,8 @@ require (
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spdx/gordf v0.0.0-20221230105357-b735bd5aac89 // indirect
github.com/spdx/tools-golang v0.5.5 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
Expand Down Expand Up @@ -146,7 +152,9 @@ require (
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
mvdan.cc/sh/v3 v3.8.0 // indirect
sigs.k8s.io/release-utils v0.8.3 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,22 @@ github.com/bradleyfalzon/ghinstallation/v2 v2.11.0/go.mod h1:0LWKQwOHewXO/1acI6T
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/caarlos0/env/v6 v6.10.0 h1:lA7sxiGArZ2KkiqpOQNf8ERBRWI+v8MWIH+eGjSN22I=
github.com/caarlos0/env/v6 v6.10.0/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/containerd/stargz-snapshotter/estargz v0.15.1 h1:eXJjw9RbkLFgioVaTG+G/ZW/0kEe2oEKCdS/ZxIyoCU=
github.com/containerd/stargz-snapshotter/estargz v0.15.1/go.mod h1:gr2RNwukQ/S9Nv33Lt6UC7xEx58C+LHRdoqbEKjz1Kk=
github.com/containerd/typeurl/v2 v2.1.1 h1:3Q4Pt7i8nYwy2KmQWIw2+1hTvwTE/6w9FqcttATPO/4=
github.com/containerd/typeurl/v2 v2.1.1/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
Expand Down Expand Up @@ -247,6 +252,8 @@ github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISH
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465 h1:KwWnWVWCNtNq/ewIX7HIKnELmEx2nDP42yskD/pi7QE=
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
Expand Down Expand Up @@ -278,12 +285,15 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/moby/buildkit v0.15.0 h1:vnZLThPr9JU6SvItctKoa6NfgPZ8oUApg/TCOaa/SVs=
github.com/moby/buildkit v0.15.0/go.mod h1:oN9S+8I7wF26vrqn9NuAF6dFSyGTfXvtiu9o1NlnnH4=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
Expand Down Expand Up @@ -321,6 +331,7 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/shurcooL/githubv4 v0.0.0-20210725200734-83ba7b4c9228 h1:N5B+JgvM/DVYIxreItPJMM3yWrNO/GB2q4nESrtBisM=
Expand All @@ -338,6 +349,10 @@ github.com/spdx/gordf v0.0.0-20221230105357-b735bd5aac89 h1:dArkMwZ7Mf2JiU8Ofdmq
github.com/spdx/gordf v0.0.0-20221230105357-b735bd5aac89/go.mod h1:uKWaldnbMnjsSAXRurWqqrdyZen1R7kxl8TkmWk2OyM=
github.com/spdx/tools-golang v0.5.5 h1:61c0KLfAcNqAjlg6UNMdkwpMernhw3zVRwDZ2x9XOmk=
github.com/spdx/tools-golang v0.5.5/go.mod h1:MVIsXx8ZZzaRWNQpUDhC4Dud34edUYJYecciXgrw5vE=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down Expand Up @@ -586,5 +601,7 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
mvdan.cc/sh/v3 v3.8.0 h1:ZxuJipLZwr/HLbASonmXtcvvC9HXY9d2lXZHnKGjFc8=
mvdan.cc/sh/v3 v3.8.0/go.mod h1:w04623xkgBVo7/IUK89E0g8hBykgEpN0vgOj3RJr6MY=
sigs.k8s.io/release-utils v0.8.3 h1:KtOtA4qDmzJyeQ2zkDsFVI25+NViwms/o5eL2NftFdA=
sigs.k8s.io/release-utils v0.8.3/go.mod h1:fp82Fma06OXBhEJ+GUJKqvcplDBomruK1R/1fWJnsrQ=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
54 changes: 41 additions & 13 deletions pkg/policies/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"fmt"
"path/filepath"

"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/ossf/allstar/pkg/scorecard"
"github.com/google/go-github/v59/github"
"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/checks"

"github.com/google/go-github/v59/github"
sc "github.com/ossf/scorecard/v5/pkg/scorecard"
"github.com/rs/zerolog/log"

"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/ossf/allstar/pkg/scorecard"
)

const configFile = "binary_artifacts.yaml"
Expand Down Expand Up @@ -124,15 +125,42 @@ func (b Binary) Check(ctx context.Context, c *github.Client, owner,
return nil, err
}

l := checker.NewLogger()
cr := &checker.CheckRequest{
Ctx: ctx,
RepoClient: scc.ScRepoClient,
Repo: scc.ScRepo,
Dlogger: l,
allRes, err := sc.Run(ctx, scc.ScRepo,
sc.WithRepoClient(scc.ScRepoClient),
sc.WithChecks([]string{checks.CheckBinaryArtifacts}),
)
if err != nil {
msg := "Error while running checks.BinaryArtifacts"
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Err(err).
Msg(msg)
return &policydef.Result{
Enabled: enabled,
Pass: true,
NotifyText: fmt.Sprintf("%s: %v", msg, err),
Details: details{},
}, nil
}
if len(allRes.Checks) != 1 {
msg := "Error while running checks.BinaryArtifacts : did not get expected checks"
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Int("chk_len", len(allRes.Checks)).
Msg(msg)
return &policydef.Result{
Enabled: enabled,
Pass: true,
NotifyText: msg,
Details: details{},
}, nil
}
res := allRes.Checks[0]

res := checks.BinaryArtifacts(cr)
if res.Error != nil {
msg := "Error while running checks.BinaryArtifacts"
log.Warn().
Expand All @@ -149,7 +177,7 @@ func (b Binary) Check(ctx context.Context, c *github.Client, owner,
}, nil
}

logs := convertAndFilterLogs(l.Flush(), mc)
logs := convertAndFilterLogs(res.Details, mc)

// We assume every log is a finding and do filtering on the Allstar side
pass := len(logs) == 0
Expand Down
58 changes: 37 additions & 21 deletions pkg/policies/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
"fmt"
"net/http"

"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/ossf/allstar/pkg/scorecard"
"github.com/google/go-github/v59/github"
"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/checks"

"github.com/google/go-github/v59/github"
"github.com/ossf/scorecard/v5/clients"
sc "github.com/ossf/scorecard/v5/pkg/scorecard"
"github.com/rs/zerolog/log"

"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/policydef"
"github.com/ossf/allstar/pkg/scorecard"
)

const configFile = "scorecard.yaml"
Expand Down Expand Up @@ -86,14 +88,15 @@ type details struct {
var configFetchConfig func(context.Context, *github.Client, string, string, string, config.ConfigLevel, interface{}) error
var configIsEnabled func(context.Context, config.OrgOptConfig, config.RepoOptConfig, config.RepoOptConfig, *github.Client, string, string) (bool, error)
var scorecardGet func(context.Context, string, http.RoundTripper) (*scorecard.ScClient, error)

var checksAllChecks checker.CheckNameToFnMap
var scRun func(context.Context, clients.Repo, ...sc.Option) (sc.Result, error)

func init() {
configFetchConfig = config.FetchConfig
configIsEnabled = config.IsEnabled
checksAllChecks = checks.GetAll()
scorecardGet = scorecard.Get
scRun = sc.Run
}

// Scorecard is the Security Scorecard policy object, implements
Expand Down Expand Up @@ -149,31 +152,44 @@ func (b Scorecard) Check(ctx context.Context, c *github.Client, owner,

for _, n := range mc.Checks {

if n == checks.CheckVulnerabilities {
// FIXME Rolling back support for Vulns, needs more testing.
continue
_, ok := checksAllChecks[n]
if !ok {
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Str("check", n).
Msg("Unknown scorecard check specified.")
break
}

l := checker.NewLogger()
cr := &checker.CheckRequest{
Ctx: ctx,
RepoClient: scc.ScRepoClient,
Repo: scc.ScRepo,
Dlogger: l,
// Run each check seperately for now.
allRes, err := scRun(ctx, scc.ScRepo,
sc.WithRepoClient(scc.ScRepoClient),
sc.WithChecks([]string{n}),
)
if err != nil {
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Str("check", n).
Err(err).
Msg("Scorecard check errored.")
break
}

check, ok := checksAllChecks[n]
if !ok {
if len(allRes.Checks) != 1 {
log.Warn().
Str("org", owner).
Str("repo", repo).
Str("area", polName).
Str("check", n).
Msg("Unknown scorecard check specified.")
Int("chk_len", len(allRes.Checks)).
Msg("Scorecard did not return expected checks.")
break
}
res := allRes.Checks[0]

res := check.Fn(cr)
if res.Error != nil {
// We are not sure that all checks are safe to run inside Allstar, some
// might error, and we don't want to abort a whole org enforcement loop
Expand All @@ -190,7 +206,7 @@ func (b Scorecard) Check(ctx context.Context, c *github.Client, owner,
break
}

logs := convertLogs(l.Flush())
logs := convertLogs(res.Details)
if len(logs) > 0 {
f[n] = logs
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/policies/scorecard/scorecard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/ossf/allstar/pkg/config"
"github.com/ossf/allstar/pkg/scorecard"
"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/clients"
sc "github.com/ossf/scorecard/v5/pkg/scorecard"
)

func TestConfigPrecedence(t *testing.T) {
Expand Down Expand Up @@ -196,10 +198,11 @@ func TestCheck(t *testing.T) {
return &scorecard.ScClient{}, nil
}
checksAllChecks = checker.CheckNameToFnMap{}
checksAllChecks["test"] = checker.Check{
Fn: func(cr *checker.CheckRequest) checker.CheckResult {
return test.Result
},
checksAllChecks["test"] = checker.Check{}
scRun = func(context.Context, clients.Repo, ...sc.Option) (sc.Result, error) {
return sc.Result{
Checks: []checker.CheckResult{test.Result},
}, nil
}
s := NewScorecard()
res, err := s.Check(context.Background(), github.NewClient(nil), "", "")
Expand Down
Loading

0 comments on commit 9f5fb01

Please sign in to comment.