Skip to content

Commit dbc21c7

Browse files
Redent0rdanmihai1
authored andcommitted
genpolicy: fix validation of env variables sourced from metadata.namespace
Use $(sandbox-namespace) wildcard in case none is specified in yaml. If wildcard is present, compare input against annotation value. Fixes regression introduced in #273 where samples that use metadata.namespace env var were no longer working. Signed-off-by: Saul Paredes <[email protected]>
1 parent 2807534 commit dbc21c7

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

src/tools/genpolicy/rules.rego

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ get_state() = state {
182182
state := data["pstate"]
183183
}
184184

185+
get_state_val(key) = value {
186+
state := get_state()
187+
value := state[key]
188+
}
189+
185190
get_state_path(key) = path {
186191
path := concat("/", ["", key]) # prepend "/" to key
187192
}
@@ -245,7 +250,10 @@ allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
245250
i_s_name := i_oci.Annotations[S_NAME_KEY]
246251
print("allow_by_anno 1: i_s_name =", i_s_name)
247252

248-
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name)
253+
i_s_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
254+
print("allow_by_anno 1: i_s_namespace =", i_s_namespace)
255+
256+
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name, i_s_namespace)
249257

250258
print("allow_by_anno 1: true")
251259
}
@@ -257,19 +265,23 @@ allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
257265
print("allow_by_anno 2: i_s_name =", i_s_name, "p_s_name =", p_s_name)
258266

259267
allow_sandbox_name(p_s_name, i_s_name)
260-
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name)
268+
269+
i_s_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
270+
print("allow_by_anno 2: i_s_namespace =", i_s_namespace)
271+
272+
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name, i_s_namespace)
261273

262274
print("allow_by_anno 2: true")
263275
}
264276

265-
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, s_name) {
277+
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, s_name, s_namespace) {
266278
print("allow_by_sandbox_name: start")
267279

268280
i_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
269281

270282
allow_by_container_types(p_oci, i_oci, s_name, i_namespace)
271283
allow_by_bundle_or_sandbox_id(p_oci, i_oci, p_storages, i_storages)
272-
allow_process(p_oci.Process, i_oci.Process, s_name)
284+
allow_process(p_oci.Process, i_oci.Process, s_name, s_namespace)
273285

274286
print("allow_by_sandbox_name: true")
275287
}
@@ -645,7 +657,7 @@ allow_by_bundle_or_sandbox_id(p_oci, i_oci, p_storages, i_storages) {
645657
print("allow_by_bundle_or_sandbox_id: true")
646658
}
647659

648-
allow_process_common(p_process, i_process, s_name) {
660+
allow_process_common(p_process, i_process, s_name, s_namespace) {
649661
print("allow_process_common: p_process =", p_process)
650662
print("allow_process_common: i_process = ", i_process)
651663
print("allow_process_common: s_name =", s_name)
@@ -654,28 +666,28 @@ allow_process_common(p_process, i_process, s_name) {
654666
p_process.NoNewPrivileges == i_process.NoNewPrivileges
655667

656668
allow_user(p_process, i_process)
657-
allow_env(p_process, i_process, s_name)
669+
allow_env(p_process, i_process, s_name, s_namespace)
658670

659671
print("allow_process_common: true")
660672
}
661673

662674
# Compare the OCI Process field of a policy container with the input OCI Process from a CreateContainerRequest
663-
allow_process(p_process, i_process, s_name) {
675+
allow_process(p_process, i_process, s_name, s_namespace) {
664676
print("allow_process: start")
665677

666678
allow_args(p_process, i_process, s_name)
667-
allow_process_common(p_process, i_process, s_name)
679+
allow_process_common(p_process, i_process, s_name, s_namespace)
668680
allow_caps(p_process.Capabilities, i_process.Capabilities)
669681
p_process.Terminal == i_process.Terminal
670682

671683
print("allow_process: true")
672684
}
673685

674686
# Compare the OCI Process field of a policy container with the input process field from ExecProcessRequest
675-
allow_interactive_process(p_process, i_process, s_name) {
687+
allow_interactive_process(p_process, i_process, s_name, s_namespace) {
676688
print("allow_interactive_process: start")
677689

678-
allow_process_common(p_process, i_process, s_name)
690+
allow_process_common(p_process, i_process, s_name, s_namespace)
679691
allow_exec_caps(i_process.Capabilities)
680692

681693
# These are commands enabled using ExecProcessRequest commands and/or regex from the settings file.
@@ -685,10 +697,10 @@ allow_interactive_process(p_process, i_process, s_name) {
685697
}
686698

687699
# Compare the OCI Process field of a policy container with the input process field from ExecProcessRequest
688-
allow_probe_process(p_process, i_process, s_name) {
700+
allow_probe_process(p_process, i_process, s_name, s_namespace) {
689701
print("allow_probe_process: start")
690702

691-
allow_process_common(p_process, i_process, s_name)
703+
allow_process_common(p_process, i_process, s_name, s_namespace)
692704
allow_exec_caps(i_process.Capabilities)
693705
p_process.Terminal == i_process.Terminal
694706

@@ -763,27 +775,27 @@ allow_arg(i, i_arg, p_process, s_name) {
763775
}
764776

765777
# OCI process.Env field
766-
allow_env(p_process, i_process, s_name) {
778+
allow_env(p_process, i_process, s_name, s_namespace) {
767779
print("allow_env: p env =", p_process.Env)
768780
print("allow_env: i env =", i_process.Env)
769781

770782
every i_var in i_process.Env {
771783
print("allow_env: i_var =", i_var)
772-
allow_var(p_process, i_process, i_var, s_name)
784+
allow_var(p_process, i_process, i_var, s_name, s_namespace)
773785
}
774786

775787
print("allow_env: true")
776788
}
777789

778790
# Allow input env variables that are present in the policy data too.
779-
allow_var(p_process, i_process, i_var, s_name) {
791+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
780792
some p_var in p_process.Env
781793
p_var == i_var
782794
print("allow_var 1: true")
783795
}
784796

785797
# Match input with one of the policy variables, after substituting $(sandbox-name).
786-
allow_var(p_process, i_process, i_var, s_name) {
798+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
787799
some p_var in p_process.Env
788800
p_var2 := replace(p_var, "$(sandbox-name)", s_name)
789801

@@ -794,7 +806,7 @@ allow_var(p_process, i_process, i_var, s_name) {
794806
}
795807

796808
# Allow input env variables that match with a request_defaults regex.
797-
allow_var(p_process, i_process, i_var, s_name) {
809+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
798810
some p_regex1 in policy_data.request_defaults.CreateContainerRequest.allow_env_regex
799811
p_regex2 := replace(p_regex1, "$(ipv4_a)", policy_data.common.ipv4_a)
800812
p_regex3 := replace(p_regex2, "$(ip_p)", policy_data.common.ip_p)
@@ -808,7 +820,7 @@ allow_var(p_process, i_process, i_var, s_name) {
808820
}
809821

810822
# Allow fieldRef "fieldPath: status.podIP" values.
811-
allow_var(p_process, i_process, i_var, s_name) {
823+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
812824
name_value := split(i_var, "=")
813825
count(name_value) == 2
814826
is_ip(name_value[1])
@@ -820,7 +832,7 @@ allow_var(p_process, i_process, i_var, s_name) {
820832
}
821833

822834
# Allow common fieldRef variables.
823-
allow_var(p_process, i_process, i_var, s_name) {
835+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
824836
name_value := split(i_var, "=")
825837
count(name_value) == 2
826838

@@ -839,7 +851,7 @@ allow_var(p_process, i_process, i_var, s_name) {
839851
}
840852

841853
# Allow fieldRef "fieldPath: status.hostIP" values.
842-
allow_var(p_process, i_process, i_var, s_name) {
854+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
843855
name_value := split(i_var, "=")
844856
count(name_value) == 2
845857
is_ip(name_value[1])
@@ -851,7 +863,7 @@ allow_var(p_process, i_process, i_var, s_name) {
851863
}
852864

853865
# Allow resourceFieldRef values (e.g., "limits.cpu").
854-
allow_var(p_process, i_process, i_var, s_name) {
866+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
855867
name_value := split(i_var, "=")
856868
count(name_value) == 2
857869

@@ -869,6 +881,16 @@ allow_var(p_process, i_process, i_var, s_name) {
869881
print("allow_var 7: true")
870882
}
871883

884+
allow_var(p_process, i_process, i_var, s_name, s_namespace) {
885+
some p_var in p_process.Env
886+
p_var2 := replace(p_var, "$(sandbox-namespace)", s_namespace)
887+
888+
print("allow_var 8: p_var2 =", p_var2)
889+
p_var2 == i_var
890+
891+
print("allow_var 8: true")
892+
}
893+
872894
allow_pod_ip_var(var_name, p_var) {
873895
print("allow_pod_ip_var: var_name =", var_name, "p_var =", p_var)
874896

@@ -1410,7 +1432,8 @@ allow_exec(p_container, i_process) {
14101432

14111433
p_oci = p_container.OCI
14121434
p_s_name = p_oci.Annotations[S_NAME_KEY]
1413-
allow_probe_process(p_oci.Process, i_process, p_s_name)
1435+
s_namespace = get_state_val("namespace")
1436+
allow_probe_process(p_oci.Process, i_process, p_s_name, s_namespace)
14141437

14151438
print("allow_exec: true")
14161439
}
@@ -1420,7 +1443,8 @@ allow_interactive_exec(p_container, i_process) {
14201443

14211444
p_oci = p_container.OCI
14221445
p_s_name = p_oci.Annotations[S_NAME_KEY]
1423-
allow_interactive_process(p_oci.Process, i_process, p_s_name)
1446+
s_namespace = get_state_val("namespace")
1447+
allow_interactive_process(p_oci.Process, i_process, p_s_name, s_namespace)
14241448

14251449
print("allow_interactive_exec: true")
14261450
}

src/tools/genpolicy/src/pod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,13 @@ impl EnvVar {
769769
let path: &str = &field_ref.fieldPath;
770770
match path {
771771
"metadata.name" => return "$(sandbox-name)".to_string(),
772-
"metadata.namespace" => return namespace.to_string(),
772+
"metadata.namespace" => {
773+
return if namespace.is_empty() {
774+
"$(sandbox-namespace)".to_string()
775+
} else {
776+
namespace.to_string()
777+
};
778+
}
773779
"metadata.uid" => return "$(pod-uid)".to_string(),
774780
"status.hostIP" => return "$(host-ip)".to_string(),
775781
"status.podIP" => return "$(pod-ip)".to_string(),

0 commit comments

Comments
 (0)