|
| 1 | +// Copyright 2022 Security Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dependencydiff |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "path" |
| 21 | + |
| 22 | + "github.com/ossf/scorecard/v4/checker" |
| 23 | + "github.com/ossf/scorecard/v4/checks" |
| 24 | + "github.com/ossf/scorecard/v4/clients" |
| 25 | + "github.com/ossf/scorecard/v4/clients/githubrepo" |
| 26 | + sce "github.com/ossf/scorecard/v4/errors" |
| 27 | + "github.com/ossf/scorecard/v4/log" |
| 28 | + "github.com/ossf/scorecard/v4/pkg" |
| 29 | + "github.com/ossf/scorecard/v4/policy" |
| 30 | +) |
| 31 | + |
| 32 | +// Depdiff is the exported name for dependency-diff. |
| 33 | +const Depdiff = "Dependency-diff" |
| 34 | + |
| 35 | +type dependencydiffContext struct { |
| 36 | + logger *log.Logger |
| 37 | + ownerName, repoName, baseSHA, headSHA string |
| 38 | + ctx context.Context |
| 39 | + ghRepo clients.Repo |
| 40 | + ghRepoClient clients.RepoClient |
| 41 | + ossFuzzClient clients.RepoClient |
| 42 | + vulnsClient clients.VulnerabilitiesClient |
| 43 | + ciiClient clients.CIIBestPracticesClient |
| 44 | + changeTypesToCheck map[pkg.ChangeType]bool |
| 45 | + checkNamesToRun []string |
| 46 | + dependencydiffs []dependency |
| 47 | + results []pkg.DependencyCheckResult |
| 48 | +} |
| 49 | + |
| 50 | +// GetDependencyDiffResults gets dependency changes between two given code commits BASE and HEAD |
| 51 | +// along with the Scorecard check results of the dependencies, and returns a slice of DependencyCheckResult. |
| 52 | +// TO use this API, an access token must be set following https://github.com/ossf/scorecard#authentication. |
| 53 | +func GetDependencyDiffResults( |
| 54 | + ctx context.Context, ownerName, repoName, baseSHA, headSHA string, scorecardChecksNames []string, |
| 55 | + changeTypesToCheck map[pkg.ChangeType]bool) ([]pkg.DependencyCheckResult, error) { |
| 56 | + // Fetch the raw dependency diffs. |
| 57 | + dCtx := dependencydiffContext{ |
| 58 | + logger: log.NewLogger(log.InfoLevel), |
| 59 | + ownerName: ownerName, |
| 60 | + repoName: repoName, |
| 61 | + baseSHA: baseSHA, |
| 62 | + headSHA: headSHA, |
| 63 | + ctx: ctx, |
| 64 | + changeTypesToCheck: changeTypesToCheck, |
| 65 | + checkNamesToRun: scorecardChecksNames, |
| 66 | + } |
| 67 | + err := fetchRawDependencyDiffData(&dCtx) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("error in fetchRawDependencyDiffData: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Initialize the repo and client(s) corresponding to the checks to run. |
| 73 | + err = initRepoAndClientByChecks(&dCtx) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("error in initRepoAndClientByChecks: %w", err) |
| 76 | + } |
| 77 | + err = getScorecardCheckResults(&dCtx) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("error getting scorecard check results: %w", err) |
| 80 | + } |
| 81 | + return dCtx.results, nil |
| 82 | +} |
| 83 | + |
| 84 | +func initRepoAndClientByChecks(dCtx *dependencydiffContext) error { |
| 85 | + repo, repoClient, ossFuzzClient, ciiClient, vulnsClient, err := checker.GetClients( |
| 86 | + dCtx.ctx, path.Join(dCtx.ownerName, dCtx.repoName), "", dCtx.logger, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("error creating the github repo: %w", err) |
| 90 | + } |
| 91 | + // If the caller doesn't specify the checks to run, run all checks and return all the clients. |
| 92 | + if dCtx.checkNamesToRun == nil || len(dCtx.checkNamesToRun) == 0 { |
| 93 | + dCtx.ghRepo, dCtx.ghRepoClient, dCtx.ossFuzzClient, dCtx.ciiClient, dCtx.vulnsClient = |
| 94 | + repo, repoClient, ossFuzzClient, ciiClient, vulnsClient |
| 95 | + return nil |
| 96 | + } |
| 97 | + dCtx.ghRepo = repo |
| 98 | + dCtx.ghRepoClient = githubrepo.CreateGithubRepoClient(dCtx.ctx, dCtx.logger) |
| 99 | + for _, cn := range dCtx.checkNamesToRun { |
| 100 | + switch cn { |
| 101 | + case checks.CheckFuzzing: |
| 102 | + dCtx.ossFuzzClient = ossFuzzClient |
| 103 | + case checks.CheckCIIBestPractices: |
| 104 | + dCtx.ciiClient = ciiClient |
| 105 | + case checks.CheckVulnerabilities: |
| 106 | + dCtx.vulnsClient = vulnsClient |
| 107 | + } |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func getScorecardCheckResults(dCtx *dependencydiffContext) error { |
| 113 | + // Initialize the checks to run from the caller's input. |
| 114 | + checksToRun, err := policy.GetEnabled(nil, dCtx.checkNamesToRun, nil) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("error init scorecard checks: %w", err) |
| 117 | + } |
| 118 | + for _, d := range dCtx.dependencydiffs { |
| 119 | + depCheckResult := pkg.DependencyCheckResult{ |
| 120 | + PackageURL: d.PackageURL, |
| 121 | + SourceRepository: d.SourceRepository, |
| 122 | + ChangeType: d.ChangeType, |
| 123 | + ManifestPath: d.ManifestPath, |
| 124 | + Ecosystem: d.Ecosystem, |
| 125 | + Version: d.Version, |
| 126 | + Name: d.Name, |
| 127 | + } |
| 128 | + // For now we skip those without source repo urls. |
| 129 | + // TODO (#2063): use the BigQuery dataset to supplement null source repo URLs to fetch the Scorecard results for them. |
| 130 | + if d.SourceRepository != nil && *d.SourceRepository != "" { |
| 131 | + if d.ChangeType != nil && (dCtx.changeTypesToCheck[*d.ChangeType] || dCtx.changeTypesToCheck == nil) { |
| 132 | + // Run scorecard on those types of dependencies that the caller would like to check. |
| 133 | + // If the input map changeTypesToCheck is empty, by default, we run checks for all valid types. |
| 134 | + // TODO (#2064): use the Scorecare REST API to retrieve the Scorecard result statelessly. |
| 135 | + scorecardResult, err := pkg.RunScorecards( |
| 136 | + dCtx.ctx, |
| 137 | + dCtx.ghRepo, |
| 138 | + // TODO (#2065): In future versions, ideally, this should be |
| 139 | + // the commitSHA corresponding to d.Version instead of HEAD. |
| 140 | + clients.HeadSHA, |
| 141 | + checksToRun, |
| 142 | + dCtx.ghRepoClient, |
| 143 | + dCtx.ossFuzzClient, |
| 144 | + dCtx.ciiClient, |
| 145 | + dCtx.vulnsClient, |
| 146 | + ) |
| 147 | + // If the run fails, we leave the current dependency scorecard result empty and record the error |
| 148 | + // rather than letting the entire API return nil since we still expect results for other dependencies. |
| 149 | + if err != nil { |
| 150 | + depCheckResult.ScorecardResultsWithError.Error = sce.WithMessage(sce.ErrScorecardInternal, |
| 151 | + fmt.Sprintf("error running the scorecard checks: %v", err)) |
| 152 | + } else { // Otherwise, we record the scorecard check results for this dependency. |
| 153 | + depCheckResult.ScorecardResultsWithError.ScorecardResults = &scorecardResult |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + dCtx.results = append(dCtx.results, depCheckResult) |
| 158 | + } |
| 159 | + return nil |
| 160 | +} |
0 commit comments