Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Enable users to integrate external checks via blank-imports #3809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checks/all_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GetAllWithExperimental() checker.CheckNameToFnMap {
return getAll(true /*overrideExperimental*/)
}

func registerCheck(name string, fn checker.CheckFn, supportedRequestTypes []checker.RequestType) error {
func RegisterCheck(name string, fn checker.CheckFn, supportedRequestTypes []checker.RequestType) error {
if name == "" {
return errInternalNameCannotBeEmpty
}
Expand Down
8 changes: 4 additions & 4 deletions checks/all_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/ossf/scorecard/v4/checker"
)

func Test_registerCheck(t *testing.T) {
func Test_RegisterCheck(t *testing.T) {
t.Parallel()
type args struct {
fn checker.CheckFn
Expand All @@ -33,7 +33,7 @@ func Test_registerCheck(t *testing.T) {
wanterr bool
}{
{
name: "registerCheck",
name: "RegisterCheck",
args: args{
name: "test",
fn: func(x *checker.CheckRequest) checker.CheckResult { return checker.CheckResult{} },
Expand All @@ -60,8 +60,8 @@ func Test_registerCheck(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := registerCheck(tt.args.name, tt.args.fn, nil /*supportedRequestTypes*/); (err != nil) != tt.wanterr {
t.Errorf("registerCheck() error = %v, wantErr %v", err, tt.wanterr)
if err := RegisterCheck(tt.args.name, tt.args.fn, nil /*supportedRequestTypes*/); (err != nil) != tt.wanterr {
t.Errorf("RegisterCheck() error = %v, wantErr %v", err, tt.wanterr)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion checks/binary_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
checker.CommitBased,
checker.FileBased,
}
if err := registerCheck(CheckBinaryArtifacts, BinaryArtifacts, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckBinaryArtifacts, BinaryArtifacts, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CheckBranchProtection = "Branch-Protection"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckBranchProtection, BranchProtection, nil); err != nil {
if err := RegisterCheck(CheckBranchProtection, BranchProtection, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/ci_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
supportedRequestTypes := []checker.RequestType{
checker.CommitBased,
}
if err := registerCheck(CheckCITests, CITests, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckCITests, CITests, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/cii_best_practices.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckCIIBestPractices = "CII-Best-Practices"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckCIIBestPractices, CIIBestPractices, nil); err != nil {
if err := RegisterCheck(CheckCIIBestPractices, CIIBestPractices, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/code_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
supportedRequestTypes := []checker.RequestType{
checker.CommitBased,
}
if err := registerCheck(CheckCodeReview, CodeReview, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckCodeReview, CodeReview, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckContributors = "Contributors"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckContributors, Contributors, nil); err != nil {
if err := RegisterCheck(CheckContributors, Contributors, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/dangerous_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
checker.FileBased,
checker.CommitBased,
}
if err := registerCheck(CheckDangerousWorkflow, DangerousWorkflow, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckDangerousWorkflow, DangerousWorkflow, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/dependency_update_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
supportedRequestTypes := []checker.RequestType{
checker.FileBased,
}
if err := registerCheck(CheckDependencyUpdateTool, DependencyUpdateTool, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckDependencyUpdateTool, DependencyUpdateTool, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/fuzzing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckFuzzing = "Fuzzing"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckFuzzing, Fuzzing, nil); err != nil {
if err := RegisterCheck(CheckFuzzing, Fuzzing, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
supportedRequestTypes := []checker.RequestType{
checker.CommitBased,
}
if err := registerCheck(CheckLicense, License, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckLicense, License, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/maintained.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckMaintained = "Maintained"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckMaintained, Maintained, nil); err != nil {
if err := RegisterCheck(CheckMaintained, Maintained, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const CheckPackaging = "Packaging"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckPackaging, Packaging, nil); err != nil {
if err := RegisterCheck(CheckPackaging, Packaging, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
checker.FileBased,
checker.CommitBased,
}
if err := registerCheck(CheckTokenPermissions, TokenPermissions, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckTokenPermissions, TokenPermissions, supportedRequestTypes); err != nil {
// This should never happen.
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/pinned_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
checker.FileBased,
checker.CommitBased,
}
if err := registerCheck(CheckPinnedDependencies, PinningDependencies, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckPinnedDependencies, PinningDependencies, supportedRequestTypes); err != nil {
// This should never happen.
panic(err)
}
Expand Down
66 changes: 66 additions & 0 deletions checks/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 OpenSSF 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 checks

import (
"io/ioutil"
"log"
"os"
"plugin"
"strings"
"sync"
)

const CheckPluginLocationEnvironmentVariable = "SCORECARD_DYNAMIC_CHECKS"

var pluginMutex sync.Mutex

//nolint:gochecknoinits
func init() {
pluginsDir, ok := os.LookupEnv(CheckPluginLocationEnvironmentVariable)
if !ok {
return
}

files, err := ioutil.ReadDir(pluginsDir)
if err != nil {
log.Fatal(err)
}

// Avoid `recursive call during initialization - linker skew` via mutex
pluginMutex.Lock()
defer pluginMutex.Unlock()

for _, file := range files {
if !strings.HasSuffix(file.Name(), ".so") {
continue
}

p, err := plugin.Open(file.Name())
if err != nil {
log.Fatal(err)
}

registerCheck, err := p.Lookup("RegisterCheck")
if err != nil {
log.Fatal(err)
}

err = registerCheck.(func() error)()
if err != nil {
log.Fatal(err)
}
}
}
2 changes: 1 addition & 1 deletion checks/sast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckSAST = "SAST"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckSAST, SAST, nil); err != nil {
if err := RegisterCheck(CheckSAST, SAST, nil); err != nil {
// This should never happen.
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
supportedRequestTypes := []checker.RequestType{
checker.CommitBased,
}
if err := registerCheck(CheckSecurityPolicy, SecurityPolicy, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckSecurityPolicy, SecurityPolicy, supportedRequestTypes); err != nil {
// This should never happen.
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/signed_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CheckSignedReleases = "Signed-Releases"

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckSignedReleases, SignedReleases, nil); err != nil {
if err := RegisterCheck(CheckSignedReleases, SignedReleases, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
checker.CommitBased,
checker.FileBased,
}
if err := registerCheck(CheckVulnerabilities, Vulnerabilities, supportedRequestTypes); err != nil {
if err := RegisterCheck(CheckVulnerabilities, Vulnerabilities, supportedRequestTypes); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

//nolint:gochecknoinits
func init() {
if err := registerCheck(CheckWebHooks, WebHooks, nil); err != nil {
if err := RegisterCheck(CheckWebHooks, WebHooks, nil); err != nil {
// this should never happen
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/write.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The steps to writing a check are as follows:
const CheckMyCheckName string = "My-Check"

func init() {
registerCheck(CheckMyCheckName, EntryPointMyCheck)
RegisterCheck(CheckMyCheckName, EntryPointMyCheck)
}
```

Expand Down
10 changes: 9 additions & 1 deletion docs/checks/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/ossf/scorecard/v4/docs/checks/internal"
"github.com/ossf/scorecard/v4/docs/checks/plugin"
sce "github.com/ossf/scorecard/v4/errors"
)

Expand All @@ -32,6 +33,7 @@ const docURL = "https://github.com/ossf/scorecard/blob/%s/docs/checks.md"
// contains checks' documentation.
type DocImpl struct {
internaldoc internal.Doc
plugindoc plugin.Doc
}

// Read loads the checks' documentation.
Expand All @@ -42,7 +44,13 @@ func Read() (Doc, error) {
return &d, fmt.Errorf("internal.ReadDoc: %w", e)
}

d := DocImpl{internaldoc: m}
p, e := plugin.ReadDoc()
if e != nil {
d := DocImpl{}
return &d, fmt.Errorf("plugin.ReadDoc: %w", e)
}

d := DocImpl{internaldoc: m, plugindoc: p}
return &d, nil
}

Expand Down
Loading