Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions glitch/rego/queries/design/design_avoid_comments.rego
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package glitch

import rego.v1

import data.glitch_lib

get_first_line(elements) = line {
count(elements) > 0
line = { elements[0].line }
get_first_line(elements) := line if {
count(elements) > 0
line = {elements[0].line}
}
get_first_line(elements) = set() {
count(elements) == 0

get_first_line(elements) := set() if {
count(elements) == 0
}

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
count(parent.comments) > 0
count(parent.comments) > 0

lines := { parent.line | parent.line > 0 }
| get_first_line(parent.atomic_units)
| get_first_line(parent.statements)
| get_first_line(parent.unit_blocks)
lines := (({parent.line | parent.line > 0} | get_first_line(parent.atomic_units)) | get_first_line(parent.statements)) | get_first_line(parent.unit_blocks)

count(lines) > 0
line := min(lines)
count(lines) > 0
line := min(lines)

comment := parent.comments[_]
comment.line >= line
comment := parent.comments[_]
comment.line >= line

result := {{
result := {
"type": "design_avoid_comments",
"element": comment,
"path": parent.path,
"description": "Avoid comments - Comments may lead to bad code or be used as a way to justify bad code."
}}
"description": "Avoid comments - Comments may lead to bad code or be used as a way to justify bad code.",
}
}
26 changes: 14 additions & 12 deletions glitch/rego/queries/design/design_imperative_abstraction.rego
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package glitch

import rego.v1

import data.glitch_lib

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""

resources := count(glitch_lib.all_atomic_units(parent))
executions := count({au |
au := glitch_lib.all_atomic_units(parent)[_]
au.type == data.design.exec_atomic_units[_]
})
resources := count(glitch_lib.all_atomic_units(parent))
executions := count({au |
au := glitch_lib.all_atomic_units(parent)[_]
au.type == data.design.exec_atomic_units[_]
})

executions > 2
(executions / resources) > 0.2
executions > 2
executions / resources > 0.2

result := {{
result := {
"type": "design_imperative_abstraction",
"element": parent,
"path": parent.path,
"description": "Imperative abstraction - The presence of imperative statements defies the purpose of IaC declarative languages."
}}
"description": "Imperative abstraction - The presence of imperative statements defies the purpose of IaC declarative languages.",
}
}
34 changes: 18 additions & 16 deletions glitch/rego/queries/design/design_long_resource.rego
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package glitch

import rego.v1

import data.glitch_lib

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
node.type == data.design.exec_atomic_units[_]
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
node.type == data.design.exec_atomic_units[_]

lines := [
line |
attr := node.attributes[_]
line := split(attr.code, "\n")[_]
not regex.match("^\\s*$", line)
]
lines := [
line |
attr := node.attributes[_]
line := split(attr.code, "\n")[_]
not regex.match("^\\s*$", line)
]

count(lines) > 7
count(lines) > 7

result := {{
result := {
"type": "design_long_resource",
"element": node,
"path": parent.path,
"description": "Long Resource - Long resources may decrease the readability and maintainability of the code."
}}
"description": "Long Resource - Long resources may decrease the readability and maintainability of the code.",
}
}
106 changes: 54 additions & 52 deletions glitch/rego/queries/design/design_misplaced_attribute.rego
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
package glitch

import rego.v1

import data.glitch_lib

# Second option for Puppet was not tested in real code due to lack of test data

chef_priority(attr) = index {
attr == "source"
index = 1
} else = index {
attr == "owner"
index = 2
} else = index {
attr == "group"
index = 2
} else = index {
attr == "mode"
index = 3
} else = index {
attr == "action"
index = 4
chef_priority(attr) := index if {
attr == "source"
index = 1
} else := index if {
attr == "owner"
index = 2
} else := index if {
attr == "group"
index = 2
} else := index if {
attr == "mode"
index = 3
} else := index if {
attr == "action"
index = 4
}

# Chef
Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".rb")
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".rb")

atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]

attr_names := ["source", "owner", "group", "mode", "action"]
order := [
chef_priority(attr) |
attr := node.attributes[_].name
attr = attr_names[_]
]
attr_names := ["source", "owner", "group", "mode", "action"]
order := [
chef_priority(attr) |
attr := node.attributes[_].name
attr = attr_names[_]
]

order != sort(order)
order != sort(order)

result := {{
result := {{
"type": "design_misplaced_attribute",
"element": node,
"path": parent.path,
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order."
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order.",
}}
}

# Puppet
Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".pp")
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".pp")
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]

some n
n > 0
node.attributes[n].name == "ensure"
some n
n > 0
node.attributes[n].name == "ensure"

result := {{
result := {{
"type": "design_misplaced_attribute",
"element": node,
"path": parent.path,
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order."
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order.",
}}
}

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".pp")
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
endswith(parent.name, ".pp")

some i
i >= 0
i < count(parent.attributes) - 1
parent.attributes[i].value != {}
some i
i >= 0
i < count(parent.attributes) - 1
parent.attributes[i].value != {}

result := {{
result := {
"type": "design_misplaced_attribute",
"element": parent,
"path": parent.path,
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order."
}}
"description": "Misplaced attribute - The developers should try to follow the languages' style guides. These style guides define the expected attribute order.",
}
}
40 changes: 22 additions & 18 deletions glitch/rego/queries/design/design_multifaceted_abstraction.rego
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package glitch

import rego.v1

import data.glitch_lib

checker(node) {
regex.match("(&&|;|\\|)", node.name)
checker(node) if {
regex.match("(&&|;|\\|)", node.name)
}
checker(node) {
attr := node.attributes[_]
regex.match("(&&|;|\\|)", attr.value.value)

checker(node) if {
attr := node.attributes[_]
regex.match("(&&|;|\\|)", attr.value.value)
}
checker(node) {
attr := node.attributes[_]
regex.match("(&&|;|\\|)", attr.value.code)

checker(node) if {
attr := node.attributes[_]
regex.match("(&&|;|\\|)", attr.value.code)
}

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
node.type == data.design.exec_atomic_units[_]
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
atomic_units := glitch_lib.all_atomic_units(parent)
node := atomic_units[_]
node.type == data.design.exec_atomic_units[_]

checker(node)
checker(node)

result := {{
result := {
"type": "design_multifaceted_abstraction",
"element": node,
"path": parent.path,
"description": "Multifaceted Abstraction - Each block should only specify the properties of a single piece of software."
}}
"description": "Multifaceted Abstraction - Each block should only specify the properties of a single piece of software.",
}
}
24 changes: 13 additions & 11 deletions glitch/rego/queries/design/implementation_too_many_variables.rego
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package glitch

import rego.v1

import data.glitch_lib

ALLOWED_TYPES = ["unkown", "script", "tasks"]
ALLOWED_TYPES := ["unkown", "script", "tasks"]

Glitch_Analysis[result] {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
type := ALLOWED_TYPES[_]
parent.type = type
Glitch_Analysis contains result if {
parent := glitch_lib._gather_parent_unit_blocks[_]
parent.path != ""
type := ALLOWED_TYPES[_]
parent.type = type

vars := glitch_lib.count_nodes_with_irtype(parent, "Variable")
vars := glitch_lib.count_nodes_with_irtype(parent, "Variable")

(vars / parent.lines) > 0.3
vars / parent.lines > 0.3

result := {{
result := {
"type": "implementation_too_many_variables",
"element": parent,
"path": parent.path,
"description": "Too many variables - The existence of too many variables in a single IaC script may reveal that the script is being used for too many purposes."
}}
"description": "Too many variables - The existence of too many variables in a single IaC script may reveal that the script is being used for too many purposes.",
}
}
Loading