|
| 1 | +package armo_builtins |
| 2 | + |
| 3 | + |
| 4 | +################################################################################ |
| 5 | +# Rules |
| 6 | +deny[msga] { |
| 7 | + pod := input[_] |
| 8 | + pod.kind == "Pod" |
| 9 | + container := pod.spec.containers[i] |
| 10 | + |
| 11 | + start_of_path := "spec" |
| 12 | + run_as_user_fixpath := evaluate_workload_run_as_user(container, pod, start_of_path) |
| 13 | + run_as_group_fixpath := evaluate_workload_run_as_group(container, pod, start_of_path) |
| 14 | + all_fixpaths := array.concat(run_as_user_fixpath, run_as_group_fixpath) |
| 15 | + count(all_fixpaths) > 0 |
| 16 | + fixPaths := get_fixed_paths(all_fixpaths, i) |
| 17 | + |
| 18 | + msga := { |
| 19 | + "alertMessage": sprintf("container: %v in pod: %v may run as root", [container.name, pod.metadata.name]), |
| 20 | + "packagename": "armo_builtins", |
| 21 | + "alertScore": 7, |
| 22 | + "reviewPaths": [], |
| 23 | + "failedPaths": [], |
| 24 | + "fixPaths": fixPaths, |
| 25 | + "alertObject": { |
| 26 | + "k8sApiObjects": [pod] |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +deny[msga] { |
| 33 | + wl := input[_] |
| 34 | + spec_template_spec_patterns := {"Deployment","ReplicaSet","DaemonSet","StatefulSet","Job"} |
| 35 | + spec_template_spec_patterns[wl.kind] |
| 36 | + container := wl.spec.template.spec.containers[i] |
| 37 | + |
| 38 | + start_of_path := "spec.template.spec" |
| 39 | + run_as_user_fixpath := evaluate_workload_run_as_user(container, wl.spec.template, start_of_path) |
| 40 | + run_as_group_fixpath := evaluate_workload_run_as_group(container, wl.spec.template, start_of_path) |
| 41 | + all_fixpaths := array.concat(run_as_user_fixpath, run_as_group_fixpath) |
| 42 | + count(all_fixpaths) > 0 |
| 43 | + fixPaths := get_fixed_paths(all_fixpaths, i) |
| 44 | + |
| 45 | + msga := { |
| 46 | + "alertMessage": sprintf("container: %v in %v: %v may run as root", [container.name, wl.kind, wl.metadata.name]), |
| 47 | + "packagename": "armo_builtins", |
| 48 | + "alertScore": 7, |
| 49 | + "reviewPaths": [], |
| 50 | + "failedPaths": [], |
| 51 | + "fixPaths": fixPaths, |
| 52 | + "alertObject": { |
| 53 | + "k8sApiObjects": [wl] |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +# Fails if cronjob has a container configured to run as root |
| 59 | +deny[msga] { |
| 60 | + wl := input[_] |
| 61 | + wl.kind == "CronJob" |
| 62 | + container = wl.spec.jobTemplate.spec.template.spec.containers[i] |
| 63 | + |
| 64 | + start_of_path := "spec.jobTemplate.spec.template.spec" |
| 65 | + run_as_user_fixpath := evaluate_workload_run_as_user(container, wl.spec.jobTemplate.spec.template, start_of_path) |
| 66 | + run_as_group_fixpath := evaluate_workload_run_as_group(container, wl.spec.jobTemplate.spec.template, start_of_path) |
| 67 | + all_fixpaths := array.concat(run_as_user_fixpath, run_as_group_fixpath) |
| 68 | + count(all_fixpaths) > 0 |
| 69 | + fixPaths := get_fixed_paths(all_fixpaths, i) |
| 70 | + |
| 71 | + |
| 72 | + msga := { |
| 73 | + "alertMessage": sprintf("container: %v in %v: %v may run as root", [container.name, wl.kind, wl.metadata.name]), |
| 74 | + "packagename": "armo_builtins", |
| 75 | + "alertScore": 7, |
| 76 | + "reviewPaths": [], |
| 77 | + "failedPaths": [], |
| 78 | + "fixPaths": fixPaths, |
| 79 | + "alertObject": { |
| 80 | + "k8sApiObjects": [wl] |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +get_fixed_paths(all_fixpaths, i) = [{"path":replace(all_fixpaths[0].path,"container_ndx",format_int(i,10)), "value":all_fixpaths[0].value}, {"path":replace(all_fixpaths[1].path,"container_ndx",format_int(i,10)), "value":all_fixpaths[1].value}]{ |
| 87 | + count(all_fixpaths) == 2 |
| 88 | +} else = [{"path":replace(all_fixpaths[0].path,"container_ndx",format_int(i,10)), "value":all_fixpaths[0].value}] |
| 89 | + |
| 90 | +################################################################################# |
| 91 | +# Workload evaluation |
| 92 | + |
| 93 | +# if runAsUser is set to 0 and runAsNonRoot is set to false/ not set - suggest to set runAsUser to 1000 |
| 94 | +# if runAsUser is not set and runAsNonRoot is set to false/ not set - suggest to set runAsNonRoot to true |
| 95 | +# all checks are both on the pod and the container level |
| 96 | +evaluate_workload_run_as_user(container, pod, start_of_path) = fixPath { |
| 97 | + runAsNonRootValue := get_run_as_non_root_value(container, pod, start_of_path) |
| 98 | + runAsNonRootValue.value == false |
| 99 | + |
| 100 | + runAsUserValue := get_run_as_user_value(container, pod, start_of_path) |
| 101 | + runAsUserValue.value == 0 |
| 102 | + |
| 103 | + alertInfo := choose_first_if_defined(runAsUserValue, runAsNonRootValue) |
| 104 | + fixPath := alertInfo.fixPath |
| 105 | +} else = [] |
| 106 | + |
| 107 | + |
| 108 | +# if runAsGroup is set to 0/ not set - suggest to set runAsGroup to 1000 |
| 109 | +# all checks are both on the pod and the container level |
| 110 | +evaluate_workload_run_as_group(container, pod, start_of_path) = fixPath { |
| 111 | + runAsGroupValue := get_run_as_group_value(container, pod, start_of_path) |
| 112 | + runAsGroupValue.value == 0 |
| 113 | + |
| 114 | + fixPath := runAsGroupValue.fixPath |
| 115 | +} else = [] |
| 116 | + |
| 117 | + |
| 118 | +################################################################################# |
| 119 | +# Value resolution functions |
| 120 | + |
| 121 | + |
| 122 | +get_run_as_non_root_value(container, pod, start_of_path) = runAsNonRoot { |
| 123 | + runAsNonRoot := {"value" : container.securityContext.runAsNonRoot, "fixPath": [{"path": sprintf("%v.containers[container_ndx].securityContext.runAsNonRoot", [start_of_path]), "value":"true"}], "defined" : true} |
| 124 | +} else = runAsNonRoot { |
| 125 | + runAsNonRoot := {"value" : pod.spec.securityContext.runAsNonRoot, "fixPath": [{"path": sprintf("%v.containers[container_ndx].securityContext.runAsNonRoot", [start_of_path]), "value":"true"}], "defined" : true} |
| 126 | +} else = {"value" : false, "fixPath": [{"path": sprintf("%v.containers[container_ndx].securityContext.runAsNonRoot", [start_of_path]) , "value":"true"}], "defined" : false} |
| 127 | + |
| 128 | +get_run_as_user_value(container, pod, start_of_path) = runAsUser { |
| 129 | + path := sprintf("%v.containers[container_ndx].securityContext.runAsUser", [start_of_path]) |
| 130 | + runAsUser := {"value" : container.securityContext.runAsUser, "fixPath": [{"path": path, "value": "1000"}], "defined" : true} |
| 131 | +} else = runAsUser { |
| 132 | + path := sprintf("%v.securityContext.runAsUser", [start_of_path]) |
| 133 | + runAsUser := {"value" : pod.spec.securityContext.runAsUser, "fixPath": [{"path": path, "value": "1000"}],"defined" : true} |
| 134 | +} else = {"value" : 0, "fixPath": [{"path": sprintf("%v.containers[container_ndx].securityContext.runAsNonRoot", [start_of_path]), "value":"true"}], |
| 135 | + "defined" : false} |
| 136 | + |
| 137 | +get_run_as_group_value(container, pod, start_of_path) = runAsGroup { |
| 138 | + path := sprintf("%v.containers[container_ndx].securityContext.runAsGroup", [start_of_path]) |
| 139 | + runAsGroup := {"value" : container.securityContext.runAsGroup, "fixPath": [{"path": path, "value": "1000"}],"defined" : true} |
| 140 | +} else = runAsGroup { |
| 141 | + path := sprintf("%v.securityContext.runAsGroup", [start_of_path]) |
| 142 | + runAsGroup := {"value" : pod.spec.securityContext.runAsGroup, "fixPath":[{"path": path, "value": "1000"}], "defined" : true} |
| 143 | +} else = {"value" : 0, "fixPath": [{"path": sprintf("%v.containers[container_ndx].securityContext.runAsGroup", [start_of_path]), "value":"1000"}], |
| 144 | + "defined" : false |
| 145 | +} |
| 146 | + |
| 147 | +choose_first_if_defined(l1, l2) = c { |
| 148 | + l1.defined |
| 149 | + c := l1 |
| 150 | +} else = l2 |
0 commit comments