diff --git a/src/tools/genpolicy/rules.rego b/src/tools/genpolicy/rules.rego index 528c0e46e49f..6c1b60f4e675 100644 --- a/src/tools/genpolicy/rules.rego +++ b/src/tools/genpolicy/rules.rego @@ -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. @@ -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) @@ -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) @@ -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) @@ -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