Skip to content

Commit

Permalink
policy: add constants to rules.rego
Browse files Browse the repository at this point in the history
Reuse constants where applicable

Signed-off-by: Saul Paredes <[email protected]>
  • Loading branch information
Redent0r committed Dec 16, 2024
1 parent 22fe9cc commit de95f48
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/tools/genpolicy/rules.rego
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ default WriteStreamRequest := false
# them and inspect OPA logs for the root cause of a failure.
default AllowRequestsFailingPolicy := false

# Constants
S_NAME_KEY = "io.kubernetes.cri.sandbox-name"
S_NAMESPACE_KEY = "io.kubernetes.cri.sandbox-namespace"

CreateContainerRequest:= {"ops": ops, "allowed": true} {
# Check if the input request should be rejected even before checking the
# policy_data.containers information.
Expand All @@ -63,7 +67,7 @@ CreateContainerRequest:= {"ops": ops, "allowed": true} {
ops_builder := []

# check sandbox name
sandbox_name = i_oci.Annotations["io.kubernetes.cri.sandbox-name"]
sandbox_name = i_oci.Annotations[S_NAME_KEY]
add_sandbox_name_to_state := state_allows("sandbox_name", sandbox_name)
ops := concat_op_if_not_null(ops_builder, add_sandbox_name_to_state)

Expand Down Expand Up @@ -205,16 +209,14 @@ allow_anno_key(i_key, p_oci) {
print("allow_anno_key 2: true")
}

# Get the value of the "io.kubernetes.cri.sandbox-name" annotation and
# Get the value of the S_NAME_KEY annotation and
# correlate it with other annotations and process fields.
allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
print("allow_by_anno 1: start")

s_name := "io.kubernetes.cri.sandbox-name"

not p_oci.Annotations[s_name]
not p_oci.Annotations[S_NAME_KEY]

i_s_name := i_oci.Annotations[s_name]
i_s_name := i_oci.Annotations[S_NAME_KEY]
print("allow_by_anno 1: i_s_name =", i_s_name)

allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name)
Expand All @@ -224,10 +226,8 @@ allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
print("allow_by_anno 2: start")

s_name := "io.kubernetes.cri.sandbox-name"

p_s_name := p_oci.Annotations[s_name]
i_s_name := i_oci.Annotations[s_name]
p_s_name := p_oci.Annotations[S_NAME_KEY]
i_s_name := i_oci.Annotations[S_NAME_KEY]
print("allow_by_anno 2: i_s_name =", i_s_name, "p_s_name =", p_s_name)

allow_sandbox_name(p_s_name, i_s_name)
Expand All @@ -239,10 +239,8 @@ allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, s_name) {
print("allow_by_sandbox_name: start")

s_namespace := "io.kubernetes.cri.sandbox-namespace"

p_namespace := p_oci.Annotations[s_namespace]
i_namespace := i_oci.Annotations[s_namespace]
p_namespace := p_oci.Annotations[S_NAMESPACE_KEY]
i_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
print("allow_by_sandbox_name: p_namespace =", p_namespace, "i_namespace =", i_namespace)
p_namespace == i_namespace

Expand Down

0 comments on commit de95f48

Please sign in to comment.