diff --git a/src/cli.py b/src/cli.py index c946aa0..0b22eb0 100644 --- a/src/cli.py +++ b/src/cli.py @@ -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`)?", ) @@ -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, @@ -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, diff --git a/src/ghas_cli/utils/repositories.py b/src/ghas_cli/utils/repositories.py index 43926e9..95766f7 100644 --- a/src/ghas_cli/utils/repositories.py +++ b/src/ghas_cli/utils/repositories.py @@ -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, @@ -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}", @@ -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", diff --git a/templates/codeql-analysis-default.yml b/templates/codeql-analysis-default.yml new file mode 100644 index 0000000..6341fee --- /dev/null +++ b/templates/codeql-analysis-default.yml @@ -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 diff --git a/templates/codeql-analysis-javascript.yml b/templates/codeql-analysis-javascript.yml index e69de29..eb1bfbc 100644 --- a/templates/codeql-analysis-javascript.yml +++ b/templates/codeql-analysis-javascript.yml @@ -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 diff --git a/templates/codeql-analysis-python.yml b/templates/codeql-analysis-python.yml index cff74c1..3c9fe1f 100644 --- a/templates/codeql-analysis-python.yml +++ b/templates/codeql-analysis-python.yml @@ -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 diff --git a/templates/codeql-analysis-ruby.yml b/templates/codeql-analysis-ruby.yml index cff74c1..be6fb4b 100644 --- a/templates/codeql-analysis-ruby.yml +++ b/templates/codeql-analysis-ruby.yml @@ -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 diff --git a/templates/codeql-config-default.yml b/templates/codeql-config-default.yml new file mode 100644 index 0000000..7f46aa8 --- /dev/null +++ b/templates/codeql-config-default.yml @@ -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/*" diff --git a/templates/codeql-config-javascript.yml b/templates/codeql-config-javascript.yml new file mode 100644 index 0000000..7f46aa8 --- /dev/null +++ b/templates/codeql-config-javascript.yml @@ -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/*" diff --git a/templates/codeql-config-python.yml b/templates/codeql-config-python.yml new file mode 100644 index 0000000..7f46aa8 --- /dev/null +++ b/templates/codeql-config-python.yml @@ -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/*" diff --git a/templates/codeql-config-ruby.yml b/templates/codeql-config-ruby.yml new file mode 100644 index 0000000..7f46aa8 --- /dev/null +++ b/templates/codeql-config-ruby.yml @@ -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/*"