Skip to content

Commit

Permalink
Merge pull request #22 from Malwarebytes/codeql
Browse files Browse the repository at this point in the history
Improve mass-deploy process
  • Loading branch information
jboursier-mwb authored Sep 28, 2022
2 parents f09dba3 + 1f4d806 commit 5fb4ba4
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def mass_cli() -> None:
@mass_cli.command("deploy")
@click.option(
"-a",
"--actions",
"--actions_enable",
type=click.BOOL,
prompt="Enable GH Actions (to `selected`)?",
)
Expand Down Expand Up @@ -572,7 +572,7 @@ def mass_cli() -> None:
)
@click.option("-o", "--organization", prompt="Organization name", type=str)
def mass_deploy(
actions: bool,
actions_enable: bool,
secretscanner: bool,
pushprotection: bool,
dependabot: bool,
Expand All @@ -596,20 +596,25 @@ def mass_deploy(
template_codeql = f.read()

print(
f"Enabling Actions ({actions}), Secret Scanner ({secretscanner}), Push Protection ({pushprotection}), Dependabot ({dependabot}), CodeQL ({codeql}) to {len(repos_list)} repositories.",
end="",
f"Enabling Actions ({actions_enable}), Secret Scanner ({secretscanner}), Push Protection ({pushprotection}), Dependabot ({dependabot}), CodeQL ({codeql}) to {len(repos_list)} repositories."
)

for repo in repos_list:

repo = repo.rstrip("\n")
issue_secretscanner_res = None
issue_pushprotection_res = None
issue_dependabot_res = None
issue_codeql_res = None
actions_res = None
secretscanner_res = None
pushprotection_res = None
dependabot_res = None
codeql_res = None

print("{repo}....", end="")
print(f"{repo}....", end="")

if actions:
if actions_enable:
actions_res = actions.set_permissions(
repository_name=repo,
organization=organization,
Expand Down
29 changes: 29 additions & 0 deletions src/ghas_cli/utils/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ def load_codeql_base64_template(language: str, default_branch: str = "main") ->
)


def load_codeql_config_base64_template(language: str) -> tuple:
language = language.lower()
try:
with open(f"./templates/codeql-config-{language.lower()}.yml", "r") as f:
template = f.read()
except Exception as e:
with open(f"./templates/codeql-config-default.yml", "r") as f:
template = f.read()
return language, str(base64.b64encode(template.encode(encoding="utf-8")), "utf-8")


def create_codeql_pr(
organization: str,
token: str,
Expand Down Expand Up @@ -439,6 +450,8 @@ def create_codeql_pr(
)

for language in languages:

# Workflow config
lang, template = load_codeql_base64_template(language, default_branch)
payload = {
"message": f"Enable CodeQL analysis for {language}",
Expand All @@ -454,6 +467,22 @@ def create_codeql_pr(
if commit_resp.status_code != 201:
return False

# CodeQL config file
lang, template = load_codeql_config_base64_template(language)
payload = {
"message": f"Enable CodeQL config file for {language}",
"content": template,
"branch": target_branch,
}

commit_resp = requests.put(
url=f"https://api.github.com/repos/{organization}/{repository}/contents/.github/workflows/codeql-config-{lang}.yml",
headers=headers,
json=payload,
)
if commit_resp.status_code != 201:
return False

# Create PR
payload = {
"title": "Security Code Scanning - configuration files",
Expand Down
69 changes: 69 additions & 0 deletions templates/codeql-analysis-default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# Reach out on Slack at '#github-appsec-security' to get help.

name: "CodeQL"

on:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]
schedule:
- cron: "36 17 * * 3"
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["ruby"]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: ./.github/codeql/codeql-config-default.yml
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# 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
# queries: security-and-quality

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
#- name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
69 changes: 69 additions & 0 deletions templates/codeql-analysis-javascript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# Reach out on Slack at '#github-appsec-security' to get help.

name: "CodeQL"

on:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]
schedule:
- cron: "36 17 * * 3"
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["javascript"]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: ./.github/codeql/codeql-config-javascript.yml
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# 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
# queries: security-and-quality

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
#- name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
70 changes: 69 additions & 1 deletion templates/codeql-analysis-python.yml
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
# Python CodeQL
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# Reach out on Slack at '#github-appsec-security' to get help.

name: "CodeQL"

on:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]
schedule:
- cron: "36 17 * * 3"
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["python"]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: ./.github/codeql/codeql-config-python.yml
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# 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
# queries: security-and-quality

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
#- name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
70 changes: 69 additions & 1 deletion templates/codeql-analysis-ruby.yml
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
# Python CodeQL
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# Reach out on Slack at '#github-appsec-security' to get help.

name: "CodeQL"

on:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]
schedule:
- cron: "36 17 * * 3"
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["ruby"]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: ./.github/codeql/codeql-config-ruby.yml
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# 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
# queries: security-and-quality

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
#- name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
10 changes: 10 additions & 0 deletions templates/codeql-config-default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Configuration file for CodeQL.
# You can tune the CodeQL analysis (excluding paths from being scanned for instance)
#
# See https://codeql.github.com/docs/codeql-cli/specifying-command-options-in-a-codeql-configuration-file/
# Reach out on Slack at '#github-appsec-security' to get help.

paths-ignore:
- "**/*__tests__*"
- "*vendor"
- "docs/*"
Loading

0 comments on commit 5fb4ba4

Please sign in to comment.