Skip to content

Commit d666fc7

Browse files
Add support for SAST (CodeQL) workflow (#1668)
1 parent 5be58e7 commit d666fc7

File tree

4 files changed

+256
-0
lines changed

4 files changed

+256
-0
lines changed

remediation/workflow/addworkflow.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package workflow
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
"sort"
9+
"strings"
10+
)
11+
12+
const CodeQLWorkflowFileName = "codeql.yml"
13+
14+
type WorkflowParameters struct {
15+
LanguagesToAdd []string
16+
DefaultBranch string
17+
}
18+
19+
func getTemplate(file string) (string, error) {
20+
templatePath := os.Getenv("WORKFLOW_TEMPLATES")
21+
22+
if templatePath == "" {
23+
templatePath = "../../workflow-templates"
24+
}
25+
26+
template, err := ioutil.ReadFile(path.Join(templatePath, file))
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
return string(template), nil
32+
}
33+
34+
func AddWorkflow(name string, workflowParameters WorkflowParameters) (string, error) {
35+
if name == "codeql" {
36+
codeqlWorkflow, err := getTemplate(CodeQLWorkflowFileName)
37+
if err != nil {
38+
return "", err
39+
}
40+
41+
sort.Strings(workflowParameters.LanguagesToAdd)
42+
codeqlWorkflow = strings.ReplaceAll(codeqlWorkflow, "$default-branch", fmt.Sprintf(`"%s"`, workflowParameters.DefaultBranch))
43+
codeqlWorkflow = strings.ReplaceAll(codeqlWorkflow, "$detected-codeql-languages", strings.Join(workflowParameters.LanguagesToAdd, ", "))
44+
codeqlWorkflow = strings.ReplaceAll(codeqlWorkflow, "$cron-weekly", fmt.Sprintf(`"%s"`, "0 0 * * 1")) // Note: Runs every monday at 12:00 AM
45+
46+
return codeqlWorkflow, nil
47+
} else {
48+
return "", fmt.Errorf("match for %s Workflow name not found", name)
49+
}
50+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package workflow
2+
3+
import (
4+
"io/ioutil"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func Test_AddWorkflow(t *testing.T) {
10+
tests := []struct {
11+
workflowName string
12+
workflowParameters WorkflowParameters
13+
expectedError bool
14+
expectedOutputFile string
15+
}{
16+
{
17+
workflowName: "codeql",
18+
workflowParameters: WorkflowParameters{
19+
LanguagesToAdd: []string{"cpp", "go", "java"},
20+
DefaultBranch: "main",
21+
},
22+
expectedError: false,
23+
expectedOutputFile: "../../testfiles/expected-codeql.yml",
24+
},
25+
{
26+
workflowName: "xyz",
27+
workflowParameters: WorkflowParameters{
28+
LanguagesToAdd: []string{"cpp"},
29+
DefaultBranch: "main",
30+
},
31+
expectedError: true,
32+
expectedOutputFile: "",
33+
},
34+
}
35+
36+
for _, test := range tests {
37+
output, err := AddWorkflow(test.workflowName, test.workflowParameters)
38+
if err != nil {
39+
if !test.expectedError {
40+
t.Errorf("Error adding Workflow: %v", err)
41+
}
42+
continue
43+
}
44+
expectedOutput, err := ioutil.ReadFile(test.expectedOutputFile)
45+
if err != nil {
46+
t.Errorf("Error in reading file: %v", err)
47+
}
48+
49+
if !reflect.DeepEqual(output, string(expectedOutput)) {
50+
t.Errorf("test failed %s did not match expected output\n%s", test.workflowName, output)
51+
}
52+
}
53+
54+
}

testfiles/expected-codeql.yml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: ["main"]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: ["main"]
20+
schedule:
21+
- cron: "0 0 * * 1"
22+
23+
permissions: # added using https://github.com/step-security/secure-workflows
24+
contents: read
25+
26+
jobs:
27+
analyze:
28+
name: Analyze
29+
runs-on: ubuntu-latest
30+
permissions:
31+
actions: read
32+
contents: read
33+
security-events: write
34+
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
language: [cpp, go, java]
39+
# CodeQL supports [ $supported-codeql-languages ]
40+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
41+
42+
steps:
43+
- name: Checkout repository
44+
uses: actions/checkout@v3
45+
46+
# Initializes the CodeQL tools for scanning.
47+
- name: Initialize CodeQL
48+
uses: github/codeql-action/init@v2
49+
with:
50+
languages: ${{ matrix.language }}
51+
# If you wish to specify custom queries, you can do so here or in a config file.
52+
# By default, queries listed here will override any specified in a config file.
53+
# Prefix the list here with "+" to use these queries and those in the config file.
54+
55+
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
56+
# queries: security-extended,security-and-quality
57+
58+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
59+
# If this step fails, then you should remove it and run the build manually (see below)
60+
- name: Autobuild
61+
uses: github/codeql-action/autobuild@v2
62+
63+
# ℹ️ Command-line programs to run using the OS shell.
64+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
65+
66+
# If the Autobuild fails above, remove it and uncomment the following three lines.
67+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
68+
69+
# - run: |
70+
# echo "Run, Build Application using script"
71+
# ./location_of_script_within_repo/buildscript.sh
72+
73+
- name: Perform CodeQL Analysis
74+
uses: github/codeql-action/analyze@v2
75+
with:
76+
category: "/language:${{matrix.language}}"

workflow-templates/codeql.yml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [$default-branch]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [$default-branch]
20+
schedule:
21+
- cron: $cron-weekly
22+
23+
permissions: # added using https://github.com/step-security/secure-workflows
24+
contents: read
25+
26+
jobs:
27+
analyze:
28+
name: Analyze
29+
runs-on: ubuntu-latest
30+
permissions:
31+
actions: read
32+
contents: read
33+
security-events: write
34+
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
language: [$detected-codeql-languages]
39+
# CodeQL supports [ $supported-codeql-languages ]
40+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
41+
42+
steps:
43+
- name: Checkout repository
44+
uses: actions/checkout@v3
45+
46+
# Initializes the CodeQL tools for scanning.
47+
- name: Initialize CodeQL
48+
uses: github/codeql-action/init@v2
49+
with:
50+
languages: ${{ matrix.language }}
51+
# If you wish to specify custom queries, you can do so here or in a config file.
52+
# By default, queries listed here will override any specified in a config file.
53+
# Prefix the list here with "+" to use these queries and those in the config file.
54+
55+
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
56+
# queries: security-extended,security-and-quality
57+
58+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
59+
# If this step fails, then you should remove it and run the build manually (see below)
60+
- name: Autobuild
61+
uses: github/codeql-action/autobuild@v2
62+
63+
# ℹ️ Command-line programs to run using the OS shell.
64+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
65+
66+
# If the Autobuild fails above, remove it and uncomment the following three lines.
67+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
68+
69+
# - run: |
70+
# echo "Run, Build Application using script"
71+
# ./location_of_script_within_repo/buildscript.sh
72+
73+
- name: Perform CodeQL Analysis
74+
uses: github/codeql-action/analyze@v2
75+
with:
76+
category: "/language:${{matrix.language}}"

0 commit comments

Comments
 (0)