-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to using modular checks per resource type
- Loading branch information
1 parent
0bbbe01
commit e52ac62
Showing
7 changed files
with
459 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,33 @@ | ||
name: "CodeQL Advanced" | ||
name: CodeQL | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
types: | ||
- opened | ||
- synchronize | ||
- reopened | ||
- labeled | ||
- unlabeled | ||
schedule: | ||
- cron: '20 19 * * 6' | ||
|
||
jobs: | ||
analyze: | ||
name: Analyze (${{ matrix.language }}) | ||
codeql: | ||
name: Run codeql | ||
runs-on: ubuntu-latest | ||
permissions: | ||
# required for all workflows | ||
security-events: write | ||
|
||
# required to fetch internal or private CodeQL packs | ||
packages: read | ||
|
||
# only required for workflows in private repositories | ||
actions: read | ||
contents: read | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- language: go | ||
build-mode: autobuild | ||
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' | ||
# Use `c-cpp` to analyze code written in C, C++ or both | ||
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both | ||
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both | ||
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, | ||
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. | ||
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how | ||
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages | ||
language: | ||
- go | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
# Initializes the CodeQL tools for scanning. | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v3 | ||
with: | ||
languages: ${{ matrix.language }} | ||
build-mode: ${{ matrix.build-mode }} | ||
# 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. | ||
|
||
# For more 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-extended,security-and-quality | ||
|
||
# If the analyze step fails for one of the languages you are analyzing with | ||
# "We were unable to automatically build your code", modify the matrix above | ||
# to set the build mode to "manual" for that language. Then modify this step | ||
# to build your code. | ||
# ℹ️ 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: matrix.build-mode == 'manual' | ||
shell: bash | ||
run: | | ||
echo 'If you are using a "manual" build mode for one or more of the' \ | ||
'languages you are analyzing, replace this with the commands to build' \ | ||
'your code, for example:' | ||
echo ' make bootstrap' | ||
echo ' make release' | ||
exit 1 | ||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v3 | ||
with: | ||
category: "/language:${{matrix.language}}" | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: go.mod | ||
- uses: github/codeql-action/init@v3 | ||
with: | ||
languages: '${{ matrix.language }}' | ||
- uses: github/codeql-action/autobuild@v3 | ||
- uses: github/codeql-action/analyze@v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package main | ||
|
||
import ( | ||
"slices" | ||
) | ||
|
||
type CheckResult struct { | ||
ok bool | ||
errors []ResultError | ||
} | ||
|
||
func changeIsRequestedByOwner( | ||
resourceChange ResourceChange, | ||
requester *StateResource, | ||
_ []*StateResource, | ||
plan *Plan, | ||
) CheckResult { | ||
checkResult := CheckResult{ok: true, errors: []ResultError{}} | ||
|
||
// If the owner is defined but it's a new group it's in the state post-apply so we have to use config to check it | ||
if resourceChange.Change.AfterUnknown.OwnerUserGroupID { | ||
if !isUserGroupMemberInConfig(resourceChange, requester, plan) { | ||
checkResult.ok = false | ||
checkResult.errors = append(checkResult.errors, | ||
newRequestError(resourceChange.Address, resourceChange.Change.After.Tag), | ||
) | ||
} | ||
|
||
// There is an error in validating topic owner so return the errors immediately | ||
return checkResult | ||
} | ||
|
||
// When the resource is created, the requester must be a member of the owner group after the change | ||
if slices.Contains(resourceChange.Change.Actions, "create") { | ||
checkResult.errors = append(checkResult.errors, | ||
validateRequesterFromState(resourceChange.Address, resourceChange.Change.After, requester, plan)...) | ||
} | ||
// When the resource is updated, the requester must be a member of the owner group before and after the change | ||
if slices.Contains(resourceChange.Change.Actions, "update") { | ||
checkResult.errors = append(checkResult.errors, | ||
validateRequesterFromState(resourceChange.Address, resourceChange.Change.Before, requester, plan)...) | ||
checkResult.errors = append(checkResult.errors, | ||
validateRequesterFromState(resourceChange.Address, resourceChange.Change.After, requester, plan)...) | ||
} | ||
// When the resource is deleted, the requester must be a member of the owner group before the change | ||
if slices.Contains(resourceChange.Change.Actions, "delete") { | ||
checkResult.errors = append(checkResult.errors, | ||
validateRequesterFromState(resourceChange.Address, resourceChange.Change.Before, requester, plan)...) | ||
} | ||
|
||
if len(checkResult.errors) > 0 { | ||
checkResult.ok = false | ||
} | ||
return checkResult | ||
} | ||
|
||
func changeIsApprovedByOwner( | ||
resourceChange ResourceChange, | ||
_ *StateResource, | ||
approvers []*StateResource, | ||
plan *Plan, | ||
) CheckResult { | ||
checkResult := CheckResult{ok: true, errors: []ResultError{}} | ||
|
||
// If the owner is defined but it's a new group it's in the state post-apply so we have to use config to check it | ||
if resourceChange.Change.AfterUnknown.OwnerUserGroupID { | ||
foundApprover := false | ||
for _, approver := range approvers { | ||
if isUserGroupMemberInConfig(resourceChange, approver, plan) { | ||
foundApprover = true // one known approver is enough | ||
} | ||
} | ||
|
||
if !foundApprover { | ||
checkResult.ok = false | ||
checkResult.errors = append(checkResult.errors, | ||
newApproveError(resourceChange.Address, resourceChange.Change.After.Tag), | ||
) | ||
|
||
// There is an error in validating topic owner so return the errors immediately | ||
return checkResult | ||
} | ||
} | ||
|
||
// When the resource is created, the approvers must be a member of the owner group after the change | ||
if slices.Contains(resourceChange.Change.Actions, "create") { | ||
checkResult.errors = append( | ||
checkResult.errors, | ||
validateApproversFromState(resourceChange.Address, resourceChange.Change.After, approvers, plan)..., | ||
) | ||
} | ||
|
||
if slices.Contains(resourceChange.Change.Actions, "update") { | ||
// updating owner requires approvals from both old and the new owner | ||
// in other cases checking Change.After would be redundant | ||
checkResult.errors = append( | ||
checkResult.errors, | ||
validateApproversFromState(resourceChange.Address, resourceChange.Change.Before, approvers, plan)..., | ||
) | ||
checkResult.errors = append( | ||
checkResult.errors, | ||
validateApproversFromState(resourceChange.Address, resourceChange.Change.After, approvers, plan)..., | ||
) | ||
} | ||
|
||
// When the resource is deleted, the approvers must be a member of the owner group before the change | ||
if slices.Contains(resourceChange.Change.Actions, "delete") { | ||
checkResult.errors = append( | ||
checkResult.errors, | ||
validateApproversFromState(resourceChange.Address, resourceChange.Change.Before, approvers, plan)..., | ||
) | ||
} | ||
|
||
if len(checkResult.errors) > 0 { | ||
checkResult.ok = false | ||
} | ||
return checkResult | ||
} | ||
|
||
func validateApproversFromState( | ||
address string, | ||
resource *ChangeResource, | ||
approvers []*StateResource, | ||
plan *Plan, | ||
) []ResultError { | ||
resultErrors := []ResultError{} | ||
|
||
// if the resource in state is missing or doesn't have an owner, return immediately | ||
if resource == nil { | ||
return resultErrors | ||
} | ||
if resource.OwnerUserGroupID == nil { | ||
return resultErrors | ||
} | ||
if *resource.OwnerUserGroupID == "" { | ||
return resultErrors | ||
} | ||
|
||
// At least one approver is required | ||
for _, approver := range approvers { | ||
if isUserGroupMemberInState(resource, approver, plan) { | ||
// found a member, short circuit the function | ||
return resultErrors | ||
} | ||
} | ||
|
||
// did not find a member, add an approve error | ||
resultErrors = append(resultErrors, newApproveError(address, resource.Tag)) | ||
return resultErrors | ||
} | ||
|
||
func validateRequesterFromState( | ||
address string, | ||
resource *ChangeResource, | ||
requester *StateResource, | ||
plan *Plan, | ||
) []ResultError { | ||
resultErrors := []ResultError{} | ||
|
||
// if the resource in state is missing or doesn't have an owner, return immediately | ||
if resource == nil { | ||
return resultErrors | ||
} | ||
if resource.OwnerUserGroupID == nil { | ||
return resultErrors | ||
} | ||
if *resource.OwnerUserGroupID == "" { | ||
return resultErrors | ||
} | ||
|
||
// Requester is required | ||
if requester == nil || !isUserGroupMemberInState(resource, requester, plan) { | ||
resultErrors = append(resultErrors, newRequestError(address, resource.Tag)) | ||
return resultErrors | ||
} | ||
|
||
// did not find any member, add an approve error | ||
return resultErrors | ||
} | ||
|
||
func newRequestError(address string, tag []Tag) ResultError { | ||
return ResultError{ | ||
Error: "requesting user is not a member of the owner group", | ||
Address: address, | ||
Tags: tag, | ||
} | ||
} | ||
|
||
func newApproveError(address string, tag []Tag) ResultError { | ||
return ResultError{ | ||
Error: "approval is required from a member of the owner group", | ||
Address: address, | ||
Tags: tag, | ||
} | ||
} |
Oops, something went wrong.