Skip to content

Commit

Permalink
Merge pull request #1 from elastic/infra_changes
Browse files Browse the repository at this point in the history
general changes
  • Loading branch information
oren-zohar authored Nov 11, 2021
2 parents 2650c5e + e1532a5 commit b9db385
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 125 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data.json
output.json
data.yaml
input.json
13 changes: 0 additions & 13 deletions compliance/cis.rego

This file was deleted.

11 changes: 11 additions & 0 deletions compliance/cis_k8s.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package compliance.cis_k8s

import data.compliance.cis.rules

default_tags := ["CIS", "CIS v1.6.0", "Kubernetes"]

findings[finding] {
some rule_id
data.activated_rules.cis_k8s[rule_id]
finding = rules[rule_id].finding
}
17 changes: 17 additions & 0 deletions compliance/lib/common.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package compliance.lib.common

# set the rule result
calculate_result(evaluation) = "passed" {
evaluation
} else = "violation"

file_ownership_match(uid, gid, requierd_uid, requierd_gid) {
uid == requierd_uid
gid == requierd_gid
} else = false

# todo: compare performance of regex alternatives
file_permission_match(filemode, user, group, other) {
pattern = sprintf("0?[0-%d][0-%d][0-%d]", [user, group, other])
regex.match(pattern, filemode)
} else = false
30 changes: 30 additions & 0 deletions compliance/lib/data_adapter.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package compliance.lib.data_adapter

is_osquery {
input.osquery
}

is_file {
is_osquery
input.osquery.filename
}

filename = file_name {
is_file
file_name = input.osquery.filename
}

filemode = file_mode {
is_file
file_mode = input.osquery.mode
}

owner_user_id = uid {
is_file
uid = input.osquery.uid
}

owner_group_id = gid {
is_file
gid = input.osquery.gid
}
21 changes: 0 additions & 21 deletions compliance/lib/osquery.rego

This file was deleted.

9 changes: 9 additions & 0 deletions compliance/lib/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lib.test

rule_pass(finding) {
finding.evaluation == "passed"
}

rule_violation(finding) {
finding.evaluation == "violation"
}
17 changes: 0 additions & 17 deletions compliance/rules/cis_1_1_1/cis_1_1_1.rego

This file was deleted.

20 changes: 20 additions & 0 deletions compliance/rules/cis_1_1_1/rule.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package compliance.cis.rules.cis_1_1_1

import data.compliance.lib.data_adapter
import data.compliance.lib.common
import data.compliance.cis_k8s

# Ensure that the API server pod specification file permissions are set to 644 or more restrictive
finding = result {
data_adapter.filename == "kube-apiserver.yaml"
filemode := data_adapter.filemode
rule_evaluation := common.file_permission_match(filemode, 6, 4, 4)

# set result
result := {
"evaluation" : common.calculate_result(rule_evaluation),
"evidence" : { "filemode" : filemode },
"rule_name" : "Ensure that the API server pod specification file permissions are set to 644 or more restrictive",
"tags" : array.concat(cis_k8s.default_tags, ["CIS 1.1.1"])
}
}
21 changes: 21 additions & 0 deletions compliance/rules/cis_1_1_1/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package compliance.cis.rules.cis_1_1_1

import data.lib.test

test_violation {
test.rule_violation(finding) with input as rule_input("0700")
}

test_pass {
test.rule_pass(finding) with input as rule_input("0644")
}

rule_input(filemode) = {
"osquery": {
"mode": filemode,
"path": "/hostfs/etc/kubernetes/manifests/kube-apiserver.yaml",
"uid": "root",
"filename": "kube-apiserver.yaml",
"gid": "root"
}
}
22 changes: 22 additions & 0 deletions compliance/rules/cis_1_1_2/rule.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package compliance.cis.rules.cis_1_1_2

import data.compliance.lib.data_adapter
import data.compliance.lib.common
import data.compliance.cis_k8s


# Ensure that the API server pod specification file ownership is set to root:root
finding = result {
data_adapter.filename == "kube-apiserver.yaml"
uid = data_adapter.owner_user_id
gid = data_adapter.owner_group_id
rule_evaluation := common.file_ownership_match(uid, gid, "root", "root")

# set result
result := {
"evaluation" : common.calculate_result(rule_evaluation),
"evidence" : {"uid" : uid, "gid" : gid},
"rule_name" : "Ensure that the API server pod specification file ownership is set to root:root",
"tags" : array.concat(cis_k8s.default_tags, ["CIS 1.1.2"])
}
}
23 changes: 23 additions & 0 deletions compliance/rules/cis_1_1_2/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package compliance.cis.rules.cis_1_1_2

import data.lib.test

test_violation {
test.rule_violation(finding) with input as rule_input("root", "user")
test.rule_violation(finding) with input as rule_input("user", "root")
test.rule_violation(finding) with input as rule_input("user", "user")
}

test_pass {
test.rule_pass(finding) with input as rule_input("root", "root")
}

rule_input(uid, gid) = {
"osquery": {
"mode": "0644",
"path": "/hostfs/etc/kubernetes/manifests/kube-apiserver.yaml",
"uid": uid,
"filename": "kube-apiserver.yaml",
"gid": gid
}
}
5 changes: 0 additions & 5 deletions data.json

This file was deleted.

65 changes: 0 additions & 65 deletions input.json

This file was deleted.

6 changes: 3 additions & 3 deletions main.rego
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import data.compliance.cis
import data.compliance.cis_k8s

# input is a resource
# data is configuration
# data is policy/configuration
# output is findings

resource = input
findings = cis.findings
findings = cis_k8s.findings

0 comments on commit b9db385

Please sign in to comment.