Skip to content
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

feat(queries): implementation of regal for linting rego files #7295

Open
wants to merge 87 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
80c1f31
initial implementation of regal for linting rego
ArturRibeiro-CX Nov 8, 2024
fd6622e
fix linting problems and add more configurations
ArturRibeiro-CX Nov 8, 2024
3b6b852
remove config from validate rego and fix linting on libraries
ArturRibeiro-CX Nov 8, 2024
bc27f73
fix opa-fmt lint issues
ArturRibeiro-CX Nov 8, 2024
6a8b9fb
add some configurations to ignore and fix use-some-for-output-vars
ArturRibeiro-CX Nov 8, 2024
9e80aeb
import future.keywords.in as needed
ArturRibeiro-CX Nov 8, 2024
6cc3d26
add future.keywords.in import
ArturRibeiro-CX Nov 8, 2024
ea4c140
add missing future.keywords.in import
ArturRibeiro-CX Nov 8, 2024
09ac2a0
update regal config file and fix linting
ArturRibeiro-CX Nov 8, 2024
9593ddc
add missing import
ArturRibeiro-CX Nov 8, 2024
8ff3303
rename regal file
ArturRibeiro-CX Nov 9, 2024
dc8f2ba
change regal file
ArturRibeiro-CX Nov 9, 2024
bce04c4
change config file location and prefer some in iteration lint issue
ArturRibeiro-CX Nov 9, 2024
67b6b17
fix prefer-some-in-iteration
ArturRibeiro-CX Nov 9, 2024
3de2b00
add missing import
ArturRibeiro-CX Nov 9, 2024
e4c890f
fix linting elb without secure protocol
ArturRibeiro-CX Nov 9, 2024
b67a331
fix errors
ArturRibeiro-CX Nov 9, 2024
0a5be6e
fix issues in some and k8s unit tests
ArturRibeiro-CX Nov 9, 2024
da79611
fix missing import
ArturRibeiro-CX Nov 9, 2024
72c8517
fix missing import
ArturRibeiro-CX Nov 9, 2024
f0154d7
fix typo
ArturRibeiro-CX Nov 9, 2024
2b316d2
fix typo
ArturRibeiro-CX Nov 9, 2024
817f66e
fix more some lint issues and unit tests
ArturRibeiro-CX Nov 9, 2024
22a3272
add missing import
ArturRibeiro-CX Nov 9, 2024
9b52592
fix fmt and unit tests
ArturRibeiro-CX Nov 9, 2024
662c4d5
fix unit tests
ArturRibeiro-CX Nov 9, 2024
31f3003
fix unit tests
ArturRibeiro-CX Nov 9, 2024
60ec7f2
fix non-raw-regex-pattern and some issues in linting
ArturRibeiro-CX Nov 10, 2024
3169a54
fix non-raw-regex-pattern and some issues in linting
ArturRibeiro-CX Nov 10, 2024
bc5f3dc
fix missing import
ArturRibeiro-CX Nov 10, 2024
8fa67a6
fix use-in-operator and some..in lint issues
ArturRibeiro-CX Nov 10, 2024
2af6f1a
fix opa-fmt and fix config file with proper links to documentation
ArturRibeiro-CX Nov 10, 2024
c85432a
fix no-whitespace-comment and some .. in lint issues
ArturRibeiro-CX Nov 11, 2024
eee4e82
fix missing import
ArturRibeiro-CX Nov 11, 2024
1936e62
fix missing import
ArturRibeiro-CX Nov 11, 2024
5fe8468
test without prefer-some-in-iteration
ArturRibeiro-CX Nov 11, 2024
53972b9
fix more linting problems
ArturRibeiro-CX Nov 11, 2024
37edee3
fix missing import
ArturRibeiro-CX Nov 11, 2024
6d2d354
fix unit tests and some more rego linting problems
ArturRibeiro-CX Nov 15, 2024
6385e6c
some more lint issues fixed and unit tests
ArturRibeiro-CX Nov 16, 2024
f644972
fix unit tests
ArturRibeiro-CX Nov 16, 2024
85ce7a1
fix unit tests and lint issue
ArturRibeiro-CX Nov 16, 2024
778b4af
fix lint problems and test k in x approach
ArturRibeiro-CX Nov 16, 2024
f7cbc81
fix unit test which was failing due to remadiation level
ArturRibeiro-CX Nov 17, 2024
a41c34b
fix opa fmt
ArturRibeiro-CX Nov 17, 2024
858bea3
fix custom-has-key-construct lint problem and add rules to config file
ArturRibeiro-CX Nov 17, 2024
eacc29a
fix lint problems with using some in instead of iterations
ArturRibeiro-CX Nov 17, 2024
e0337e1
fix typo
ArturRibeiro-CX Nov 17, 2024
9c80299
import missing package
ArturRibeiro-CX Nov 17, 2024
63d50a0
fix typo
ArturRibeiro-CX Nov 17, 2024
606f72a
fix typos
ArturRibeiro-CX Nov 17, 2024
631148e
fix unit tests and refactor order of expected results
ArturRibeiro-CX Nov 17, 2024
ba24b25
more some .. in changes in policies
ArturRibeiro-CX Nov 17, 2024
90046ad
fix missing import
ArturRibeiro-CX Nov 17, 2024
7342d64
fix missing import
ArturRibeiro-CX Nov 17, 2024
86e0858
fix unit tests
ArturRibeiro-CX Nov 17, 2024
04a2444
fix unit tests
ArturRibeiro-CX Nov 18, 2024
0dede59
update dockerfile images and fix some..in linting problems
ArturRibeiro-CX Nov 19, 2024
122e446
fix missing import
ArturRibeiro-CX Nov 19, 2024
4e445f4
fix typo
ArturRibeiro-CX Nov 19, 2024
aec1ee4
fix integration tests and fix more some..in lint issues
ArturRibeiro-CX Nov 20, 2024
92356b9
fix some..in lint issues
ArturRibeiro-CX Nov 20, 2024
c6ebfb1
fix typo
ArturRibeiro-CX Nov 20, 2024
119406f
fix typo
ArturRibeiro-CX Nov 20, 2024
4d0a3b7
fix unit tests and more some..in lint issues
ArturRibeiro-CX Nov 20, 2024
c38ef57
test bug on rds_db_instance_publicly_accessible and fix lint problems
ArturRibeiro-CX Nov 23, 2024
d9686e8
fix some..in lint issues in k8s, openapi, pulumi, serverlessFW
ArturRibeiro-CX Nov 23, 2024
cbf896b
update param.in to param["in"]
ArturRibeiro-CX Nov 23, 2024
b64571b
update p.in to p["in"]
ArturRibeiro-CX Nov 23, 2024
4d74f00
update value.in to value["in"]
ArturRibeiro-CX Nov 23, 2024
a488d44
update params.in to params["in"]
ArturRibeiro-CX Nov 23, 2024
cd34ee3
update paramOne.in to paramOne["in"]
ArturRibeiro-CX Nov 23, 2024
13f1914
update parameter.in to parameter["in"]
ArturRibeiro-CX Nov 23, 2024
61febba
revert some..in due to errors in integration tests
ArturRibeiro-CX Nov 23, 2024
d1c26f2
fix some..in lint issues
ArturRibeiro-CX Nov 24, 2024
1fe3896
fix missing import
ArturRibeiro-CX Nov 24, 2024
b24cbc9
fix unit tests error
ArturRibeiro-CX Nov 24, 2024
656ad4f
fix some..in lint issues
ArturRibeiro-CX Nov 24, 2024
befcf45
fix unit tests and more some..in lint issues
ArturRibeiro-CX Nov 24, 2024
9b42138
fix some..in lint issues
ArturRibeiro-CX Nov 24, 2024
215ff33
fix unit tests
ArturRibeiro-CX Nov 24, 2024
2449df3
fix some..in lint issue
ArturRibeiro-CX Nov 24, 2024
b76a57a
fix some..in lint issues with input.document iteration
ArturRibeiro-CX Dec 1, 2024
9c56d73
fix some..in lint issues
ArturRibeiro-CX Dec 1, 2024
fa5253f
fix unit tests
ArturRibeiro-CX Dec 1, 2024
42d6f83
fix unit tests and opa fmt lint issue
ArturRibeiro-CX Dec 1, 2024
d111acd
update config file with proper lint levels
ArturRibeiro-CX Dec 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 21 additions & 0 deletions .github/workflows/validate-rego.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: validate-rego

on:
pull_request:
paths:
- "assets/**/*.rego"

jobs:
lint-rego:
name: Run Regal Linter on Rego Files
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Setup Regal
uses: StyraInc/[email protected]
with:
version: v0.11.0
- name: Run Regal Linter
run: regal lint --format=github assets --config-file=assets/.regal/config.yml
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM checkmarx/go:1.23.1-r0@sha256:61d8f083c9781614cad318dc8a0b35fb2b9c7f88226829f4a5bdc00117c47cc2 AS build_env
FROM checkmarx/go:1.23.3-r1@sha256:aab4acc0cb9d689cbc17c5dbdaa58a993779d82e6f9061962b6722270d518f6f AS build_env

# Copy the source from the current directory to the Working Directory inside the container
WORKDIR /app
Expand Down Expand Up @@ -29,7 +29,7 @@ RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
# Runtime image
# Ignore no User Cmd since KICS container is stopped afer scan
# kics-scan ignore-line
FROM checkmarx/git@sha256:a4253a0291cec1dab3c4e85ccfd0c49d8c6d4a52e34c9159a59cb6a5fc7b4432
FROM checkmarx/git@sha256:125b4c8f86e647f00e2a26e2a38da10528e325aea3af9306ac3d125d441e92c7

ENV TERM xterm-256color

Expand Down
66 changes: 66 additions & 0 deletions assets/.regal/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
rules:
bugs:
not-equals-in-loop:
# https://docs.styra.com/regal/rules/bugs/not-equals-in-loop
level: ignore
rule-shadows-builtin:
# https://docs.styra.com/regal/rules/bugs/rule-shadows-builtin
level: warn
idiomatic:
no-defined-entrypoint:
# https://docs.styra.com/regal/rules/idiomatic/no-defined-entrypoint
# No single entrypoint for this project
level: ignore
# temporary
non-raw-regex-pattern:
# https://docs.styra.com/regal/rules/idiomatic/non-raw-regex-pattern
level: warn
use-in-operator:
# https://docs.styra.com/regal/rules/idiomatic/use-in-operator
level: warn
use-some-for-output-vars:
# https://docs.styra.com/regal/rules/idiomatic/use-some-for-output-vars
# These would be good to address, but would require a concentrated effort
level: ignore
custom-has-key-construct:
# https://docs.styra.com/regal/rules/idiomatic/custom-has-key-construct
level: warn
equals-pattern-matching:
# https://docs.styra.com/regal/rules/idiomatic/equals-pattern-matching
level: warn
style:
avoid-get-and-list-prefix:
# https://docs.styra.com/regal/rules/style/avoid-get-and-list-prefix
level: ignore
external-reference:
# https://docs.styra.com/regal/rules/style/external-reference
level: ignore
file-length:
# https://docs.styra.com/regal/rules/style/file-length
level: ignore
line-length:
# https://docs.styra.com/regal/rules/style/line-length
level: ignore
no-whitespace-comment:
# https://docs.styra.com/regal/rules/style/no-whitespace-comment
level: warn
opa-fmt:
# https://docs.styra.com/regal/rules/style/opa-fmt
level: warn
prefer-some-in-iteration:
# https://docs.styra.com/regal/rules/style/prefer-some-in-iteration
# 10000+ violations fixed but way more to go
level: ignore
prefer-snake-case:
# https://docs.styra.com/regal/rules/style/prefer-snake-case
level: ignore
rule-length:
# https://docs.styra.com/regal/rules/style/rule-length
level: ignore
todo-comment:
# https://docs.styra.com/regal/rules/style/todo-comment
# only one TODO comment in the codebase to fix this issue
level: ignore
use-assignment-operator:
# https://docs.styra.com/regal/rules/style/use-assignment-operator
level: ignore
30 changes: 14 additions & 16 deletions assets/libraries/ansible.rego
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package generic.ansible

import future.keywords.in

# Global variable with all tasks in input
tasks := TasksPerDocument

# Builds an object that stores all tasks for each document id
TasksPerDocument[id] = result {
document := input.document[i]
some document in input.document
id := document.id
result := getTasks(document)
}
Expand Down Expand Up @@ -33,14 +35,12 @@ getTasksFromBlocks(playbook) = result {
not task.block
validPath(path)
]
} else = [playbook] {
true
}
} else = [playbook]

# Validates the path of a nested element inside a block task to assure it's a task
validPath(path) {
count(path) > 1
validGroup(path[minus(count(path), 2)])
validGroup(path[count(path) - 2])
}

# Identifies a block task
Expand Down Expand Up @@ -96,9 +96,7 @@ allowsPort(allowed, port) {
high >= portNumber
} else {
allowed.ports[_] == port
} else = false {
true
}
} else = false

# Checks if a given port is included in a network rule
isPortInRule(rule, portNumber) {
Expand All @@ -112,7 +110,7 @@ isPortInRule(rule, portNumber) {
}

isPortInRule(rule, portNumber) {
rule.ports[_] == portNumber
portNumber in rule.ports
}

isPortInRule(rule, portNumber) {
Expand Down Expand Up @@ -149,11 +147,11 @@ isEntireNetwork(cidr) {
}

installer_modules := [
"community.general.apk", "ansible.builtin.apt", "ansible.builtin.apt", "community.general.bundler", "ansible.builtin.dnf", "community.general.easy_install",
"community.general.gem", "community.general.homebrew", "community.general.jenkins_plugin", "community.general.npm", "community.general.openbsd_pkg",
"ansible.builtin.package", "ansible.builtin.package", "community.general.pear", "community.general.pacman", "ansible.builtin.pip", "community.general.pkg5",
"community.general.pkgutil", "community.general.pkgutil", "community.general.portage", "community.general.slackpkg", "community.general.sorcery",
"community.general.swdepot", "win_chocolatey", "community.general.yarn", "ansible.builtin.yum", "community.general.zypper", "apk", "apt", "bower", "bundler",
"dnf", "easy_install", "gem", "homebrew", "jenkins_plugin", "npm", "openbsd_package", "openbsd_pkg", "package", "pacman", "pear", "pip", "pkg5", "pkgutil",
"community.general.apk", "ansible.builtin.apt", "ansible.builtin.apt", "community.general.bundler", "ansible.builtin.dnf", "community.general.easy_install",
"community.general.gem", "community.general.homebrew", "community.general.jenkins_plugin", "community.general.npm", "community.general.openbsd_pkg",
"ansible.builtin.package", "ansible.builtin.package", "community.general.pear", "community.general.pacman", "ansible.builtin.pip", "community.general.pkg5",
"community.general.pkgutil", "community.general.pkgutil", "community.general.portage", "community.general.slackpkg", "community.general.sorcery",
"community.general.swdepot", "win_chocolatey", "community.general.yarn", "ansible.builtin.yum", "community.general.zypper", "apk", "apt", "bower", "bundler",
"dnf", "easy_install", "gem", "homebrew", "jenkins_plugin", "npm", "openbsd_package", "openbsd_pkg", "package", "pacman", "pear", "pip", "pkg5", "pkgutil",
"portage", "slackpkg", "sorcery", "swdepot", "win_chocolatey", "yarn", "yum", "zypper",
]
]
35 changes: 16 additions & 19 deletions assets/libraries/azureresourcemanager.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package generic.azureresourcemanager

# gets the network security group properties for two types of resource ('Microsoft.Network/networkSecurityGroups' and 'Microsoft.Network/networkSecurityGroups/securityRules')
get_sg_info(value) = typeInfo {
value.type == "Microsoft.Network/networkSecurityGroups/securityRules"
value.type == "Microsoft.Network/networkSecurityGroups/securityRules"
typeInfo := {
"type": value.type,
"properties": value.properties,
"type": value.type,
"properties": value.properties,
"path": "resources.type={{Microsoft.Network/networkSecurityGroups/securityRules}}.properties",
"sl": ["properties"]
}
"sl": ["properties"],
}
} else = typeInfo {
value.type == "securityRules"
typeInfo := {
"type": value.type,
"properties": value.properties,
"type": value.type,
"properties": value.properties,
"path": "resources.type={{securityRules}}.properties",
"sl": ["properties"]
"sl": ["properties"],
}
}

Expand Down Expand Up @@ -58,14 +58,12 @@ get_children(doc, parent, path) = childArr {
childArr := array.concat(resourceArr, outerArr)
}

get_outer_children(doc, nameParent) = outerArr {
outerArr := [x |
[path, value] := walk(doc)
startswith(value.name, nameParent)
value.name != nameParent
x := {"value": value, "path": path}
]
}
get_outer_children(doc, nameParent) := [x |
[path, value] := walk(doc)
startswith(value.name, nameParent)
value.name != nameParent
x := {"value": value, "path": path}
]

getDefaultValueFromParametersIfPresent(doc, valueToCheck) = [value, propertyType] {
parameterName := isParameterReference(valueToCheck)
Expand All @@ -84,11 +82,10 @@ isParameterReference(valueToCheck) = parameterName {
parameterName := trim_right(trim_left(trim_left(valueToCheck, "[parameters"), "('"), "')]")
}


isDisabledOrUndefined(doc, resource, parametersPath){
isDisabledOrUndefined(doc, resource, parametersPath) {
object.get(resource, split(parametersPath, "."), "not defined") == "not defined"
} else {
value := object.get(resource, split(parametersPath, "."),"")
value := object.get(resource, split(parametersPath, "."), "")
[check, _] := getDefaultValueFromParametersIfPresent(doc, value)
check == false
}
1 change: 0 additions & 1 deletion assets/libraries/cicd.rego
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package generic.cicd

7 changes: 4 additions & 3 deletions assets/libraries/cloudformation.rego
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ udpPortsMap = {
}

# Get content of the resource(s) based on the type
getResourcesByType(resources, type) = list {
list = [resource | resources[i].Type == type; resource := resources[i]]
}
getResourcesByType(resources, type) = [resource |
resources[i].Type == type
resource := resources[i]
]

getBucketName(resource) = name {
name := resource.Properties.Bucket
Expand Down
Loading
Loading