Skip to content

[FEATURE] Added template for Dependency-review and Scorecards #1755

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

Merged
merged 3 commits into from
Jan 4, 2023
Merged
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
27 changes: 25 additions & 2 deletions remediation/workflow/addworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
18 changes: 16 additions & 2 deletions remediation/workflow/addworkflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions testfiles/addworkflow/expected-dependency-review.yml
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions testfiles/addworkflow/expected-scorecards.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions workflow-templates/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions workflow-templates/scorecards.yml
Original file line number Diff line number Diff line change
@@ -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