diff --git a/remediation/workflow/addworkflow.go b/remediation/workflow/addworkflow.go index 593de38d0..57ecd72a5 100644 --- a/remediation/workflow/addworkflow.go +++ b/remediation/workflow/addworkflow.go @@ -9,7 +9,14 @@ import ( "strings" ) -const CodeQLWorkflowFileName = "codeql.yml" +const ( + CodeQLWorkflowFileName = "codeql.yml" + DependencyReviewFileName = "dependency-review.yml" + ScorecardFileName = "scorecards.yml" + CodeQL = "CodeQL" + DependencyReview = "Dependency-review" + Scorecard = "Scorecard" +) type WorkflowParameters struct { LanguagesToAdd []string @@ -32,7 +39,7 @@ func getTemplate(file string) (string, error) { } func AddWorkflow(name string, workflowParameters WorkflowParameters) (string, error) { - if name == "codeql" { + if name == CodeQL { codeqlWorkflow, err := getTemplate(CodeQLWorkflowFileName) if err != nil { return "", err @@ -44,6 +51,22 @@ func AddWorkflow(name string, workflowParameters WorkflowParameters) (string, er codeqlWorkflow = strings.ReplaceAll(codeqlWorkflow, "$cron-weekly", fmt.Sprintf(`"%s"`, "0 0 * * 1")) // Note: Runs every monday at 12:00 AM return codeqlWorkflow, nil + + } else if name == DependencyReview { + dependencyReviewWorkflow, err := getTemplate(DependencyReviewFileName) + if err != nil { + return "", err + } + return dependencyReviewWorkflow, nil + + } else if name == Scorecard { + scorecardsWorkflow, err := getTemplate(ScorecardFileName) + if err != nil { + return "", err + } + scorecardsWorkflow = strings.ReplaceAll(scorecardsWorkflow, "$default-branch", fmt.Sprintf(`"%s"`, workflowParameters.DefaultBranch)) + return scorecardsWorkflow, nil + } else { return "", fmt.Errorf("match for %s Workflow name not found", name) } diff --git a/remediation/workflow/addworkflow_test.go b/remediation/workflow/addworkflow_test.go index 5d8c8ad29..34d195efb 100644 --- a/remediation/workflow/addworkflow_test.go +++ b/remediation/workflow/addworkflow_test.go @@ -14,13 +14,13 @@ func Test_AddWorkflow(t *testing.T) { expectedOutputFile string }{ { - workflowName: "codeql", + workflowName: "CodeQL", workflowParameters: WorkflowParameters{ LanguagesToAdd: []string{"cpp", "go", "java"}, DefaultBranch: "main", }, expectedError: false, - expectedOutputFile: "../../testfiles/expected-codeql.yml", + expectedOutputFile: "../../testfiles/addworkflow/expected-codeql.yml", }, { workflowName: "xyz", @@ -31,6 +31,20 @@ func Test_AddWorkflow(t *testing.T) { expectedError: true, expectedOutputFile: "", }, + { + workflowName: "Dependency-review", + workflowParameters: WorkflowParameters{}, + expectedError: false, + expectedOutputFile: "../../testfiles/addworkflow/expected-dependency-review.yml", + }, + { + workflowName: "Scorecard", + workflowParameters: WorkflowParameters{ + DefaultBranch: "main", + }, + expectedError: false, + expectedOutputFile: "../../testfiles/addworkflow/expected-scorecards.yml", + }, } for _, test := range tests { diff --git a/testfiles/expected-codeql.yml b/testfiles/addworkflow/expected-codeql.yml similarity index 100% rename from testfiles/expected-codeql.yml rename to testfiles/addworkflow/expected-codeql.yml diff --git a/testfiles/addworkflow/expected-dependency-review.yml b/testfiles/addworkflow/expected-dependency-review.yml new file mode 100644 index 000000000..fe461b424 --- /dev/null +++ b/testfiles/addworkflow/expected-dependency-review.yml @@ -0,0 +1,20 @@ +# Dependency Review Action +# +# This Action will scan dependency manifest files that change as part of a Pull Request, surfacing known-vulnerable versions of the packages declared or updated in the PR. Once installed, if the workflow run is marked as required, PRs introducing known-vulnerable packages will be blocked from merging. +# +# Source repository: https://github.com/actions/dependency-review-action +# Public documentation: https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-dependency-review#dependency-review-enforcement +name: 'Dependency Review' +on: [pull_request] + +permissions: + contents: read + +jobs: + dependency-review: + runs-on: ubuntu-latest + steps: + - name: 'Checkout Repository' + uses: actions/checkout@v3 + - name: 'Dependency Review' + uses: actions/dependency-review-action@v2 diff --git a/testfiles/addworkflow/expected-scorecards.yml b/testfiles/addworkflow/expected-scorecards.yml new file mode 100644 index 000000000..714309940 --- /dev/null +++ b/testfiles/addworkflow/expected-scorecards.yml @@ -0,0 +1,71 @@ +# This workflow uses actions that are not certified by GitHub. They are provided +# by a third-party and are governed by separate terms of service, privacy +# policy, and support documentation. + +name: Scorecards supply-chain security +on: + # For Branch-Protection check. Only the default branch is supported. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection + branch_protection_rule: + # To guarantee Maintained check is occasionally updated. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained + schedule: + - cron: '20 7 * * 2' + push: + branches: ["main"] + +# Declare default permissions as read only. +permissions: read-all + +jobs: + analysis: + name: Scorecards analysis + runs-on: ubuntu-latest + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + # Needed to publish results and get a badge (see publish_results below). + id-token: write + contents: read + actions: read + + steps: + - name: "Checkout code" + uses: actions/checkout@v3 + with: + persist-credentials: false + + - name: "Run analysis" + uses: ossf/scorecard-action@v2 + with: + results_file: results.sarif + results_format: sarif + # (Optional) "write" PAT token. Uncomment the `repo_token` line below if: + # - you want to enable the Branch-Protection check on a *public* repository, or + # - you are installing Scorecards on a *private* repository + # To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat. + # repo_token: ${{ secrets.SCORECARD_TOKEN }} + + # Public repositories: + # - Publish results to OpenSSF REST API for easy access by consumers + # - Allows the repository to include the Scorecard badge. + # - See https://github.com/ossf/scorecard-action#publishing-results. + # For private repositories: + # - `publish_results` will always be set to `false`, regardless + # of the value entered here. + publish_results: true + + # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF + # format to the repository Actions tab. + - name: "Upload artifact" + uses: actions/upload-artifact@v3 + with: + name: SARIF file + path: results.sarif + retention-days: 5 + + # Upload the results to GitHub's code scanning dashboard. + - name: "Upload to code-scanning" + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: results.sarif diff --git a/workflow-templates/dependency-review.yml b/workflow-templates/dependency-review.yml new file mode 100644 index 000000000..fe461b424 --- /dev/null +++ b/workflow-templates/dependency-review.yml @@ -0,0 +1,20 @@ +# Dependency Review Action +# +# This Action will scan dependency manifest files that change as part of a Pull Request, surfacing known-vulnerable versions of the packages declared or updated in the PR. Once installed, if the workflow run is marked as required, PRs introducing known-vulnerable packages will be blocked from merging. +# +# Source repository: https://github.com/actions/dependency-review-action +# Public documentation: https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-dependency-review#dependency-review-enforcement +name: 'Dependency Review' +on: [pull_request] + +permissions: + contents: read + +jobs: + dependency-review: + runs-on: ubuntu-latest + steps: + - name: 'Checkout Repository' + uses: actions/checkout@v3 + - name: 'Dependency Review' + uses: actions/dependency-review-action@v2 diff --git a/workflow-templates/scorecards.yml b/workflow-templates/scorecards.yml new file mode 100644 index 000000000..01f9e907b --- /dev/null +++ b/workflow-templates/scorecards.yml @@ -0,0 +1,71 @@ +# This workflow uses actions that are not certified by GitHub. They are provided +# by a third-party and are governed by separate terms of service, privacy +# policy, and support documentation. + +name: Scorecards supply-chain security +on: + # For Branch-Protection check. Only the default branch is supported. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection + branch_protection_rule: + # To guarantee Maintained check is occasionally updated. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained + schedule: + - cron: '20 7 * * 2' + push: + branches: [$default-branch] + +# Declare default permissions as read only. +permissions: read-all + +jobs: + analysis: + name: Scorecards analysis + runs-on: ubuntu-latest + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + # Needed to publish results and get a badge (see publish_results below). + id-token: write + contents: read + actions: read + + steps: + - name: "Checkout code" + uses: actions/checkout@v3 + with: + persist-credentials: false + + - name: "Run analysis" + uses: ossf/scorecard-action@v2 + with: + results_file: results.sarif + results_format: sarif + # (Optional) "write" PAT token. Uncomment the `repo_token` line below if: + # - you want to enable the Branch-Protection check on a *public* repository, or + # - you are installing Scorecards on a *private* repository + # To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat. + # repo_token: ${{ secrets.SCORECARD_TOKEN }} + + # Public repositories: + # - Publish results to OpenSSF REST API for easy access by consumers + # - Allows the repository to include the Scorecard badge. + # - See https://github.com/ossf/scorecard-action#publishing-results. + # For private repositories: + # - `publish_results` will always be set to `false`, regardless + # of the value entered here. + publish_results: true + + # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF + # format to the repository Actions tab. + - name: "Upload artifact" + uses: actions/upload-artifact@v3 + with: + name: SARIF file + path: results.sarif + retention-days: 5 + + # Upload the results to GitHub's code scanning dashboard. + - name: "Upload to code-scanning" + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: results.sarif