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

[New Rule] Api server first automated rule (1.2.2) #11

Merged
merged 12 commits into from
Nov 22, 2021
26 changes: 26 additions & 0 deletions compliance/cis_k8s/rules/cis_1_2_2/rule.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package compliance.cis_k8s.rules.cis_1_2_2

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

# Ensure that the --basic-auth-file argument is not set (Automated)
finding = result {
command_args := data_adapter.command_args
rule_evaluation := common.array_contains(command_args, "--basic-auth-file") == false

# set result
result := {
"evaluation": common.calculate_result(rule_evaluation),
"evidence": {"command_args": command_args},
}
}

metadata = {
"name": "Ensure that the --basic-auth-file argument is not set",
"description": "Basic authentication uses plaintext credentials for authentication. Currently, the basic authentication credentials last indefinitely, and the password cannot be changed without restarting the API server. The basic authentication is currently supported for convenience. Hence, basic authentication should not be used.",
"impact": "You will have to configure and use alternate authentication mechanisms such as tokens and certificates. Username and password for basic authentication could no longer be used.",
"tags": array.concat(cis_k8s.default_tags, ["CIS 1.2.2", "API Server"]),
"benchmark": cis_k8s.benchmark_name,
"remediation": "Follow the documentation and configure alternate mechanisms for authentication. Then, edit the API server pod specification file /etc/kubernetes/manifests/kube-apiserver.yaml on the master node and remove the --basic-auth-file=<filename> parameter.",
}
18 changes: 18 additions & 0 deletions compliance/cis_k8s/rules/cis_1_2_2/test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package compliance.cis_k8s.rules.cis_1_2_2

import data.cis_k8s.test_data
import data.lib.test

test_violation {
test.assert_fail(finding) with input as rule_input("api_server", "--basic-auth-file")
}

test_pass {
test.assert_pass(finding) with input as rule_input("api_server", "")
}

test_not_evaluated {
not finding with input as rule_input("some_process", "")
}

rule_input(process_type, argument) = test_data.api_server_input(process_type, [argument])
6 changes: 6 additions & 0 deletions compliance/cis_k8s/test_data.rego
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ filesystem_input(filename, mode, uid, gid) = {
"uid": uid,
"gid": gid,
}

# Recivies an array of arguments representing the API Server command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't mind - change the file name to test_utils.rego same goes with package
(leave that refactor to the backlog)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it in a separate PR after all other rules are merged

api_server_input(process_type, arguments) = {
"type": process_type,
"command": concat(" ", array.concat(["kube-apiserver"], arguments)),
}
29 changes: 29 additions & 0 deletions compliance/lib/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ file_permission_match(filemode, user, group, other) {
true
}

array_contains(array, key) {
contains(array[_], key)
} else = false {
true
}

# gets argument's value
get_arg_value(arguments, key) = value {
contains(arguments[i], key)
argument := arguments[i]
[_, value] := split(argument, "=")
}

# checks if argument contains value (argument format is csv)
arg_values_contains(arguments, key, value) {
argument := get_arg_value(arguments, key)
values := split(argument, ",")
value = values[_]
} else = false {
true
}

# checks if a argument is set to greater value then minimum
arg_at_least(arguments, key, minimum) {
value := get_arg_value(arguments, key)
to_number(value) >= minimum
} else = false {
true

# check if file is in path
file_in_path(path, file_path) {
closed_path := concat("", [file_path, "/"]) # make sure last dir name is closed by "/"
Expand Down
10 changes: 10 additions & 0 deletions compliance/lib/data_adapter.rego
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ owner_group_id = gid {
is_filesystem
gid = input.gid
}

is_process {
input.type == "api_server"
}

# split the process args string into an array
command_args = args {
is_process
args = split(input.command, " ")
}