|
| 1 | +package armo_builtins |
| 2 | + |
| 3 | + |
| 4 | +deny[msga] { |
| 5 | + httproute := input[_] |
| 6 | + httproute.kind == "HTTPRoute" |
| 7 | + |
| 8 | + svc := input[_] |
| 9 | + svc.kind == "Service" |
| 10 | + |
| 11 | + # Make sure that they belong to the same namespace |
| 12 | + svc.metadata.namespace == httproute.metadata.namespace |
| 13 | + |
| 14 | + # avoid duplicate alerts |
| 15 | + # if service is already exposed through NodePort or LoadBalancer workload will fail on that |
| 16 | + not is_exposed_service(svc) |
| 17 | + |
| 18 | + wl := input[_] |
| 19 | + wl.metadata.namespace == svc.metadata.namespace |
| 20 | + spec_template_spec_patterns := {"Deployment", "ReplicaSet", "DaemonSet", "StatefulSet", "Pod", "Job", "CronJob"} |
| 21 | + spec_template_spec_patterns[wl.kind] |
| 22 | + wl_connected_to_service(wl, svc) |
| 23 | + |
| 24 | + result := svc_connected_to_httproute(svc, httproute) |
| 25 | + |
| 26 | + msga := { |
| 27 | + "alertMessage": sprintf("workload '%v' is exposed through httproute '%v'", [wl.metadata.name, httproute.metadata.name]), |
| 28 | + "packagename": "armo_builtins", |
| 29 | + "failedPaths": [], |
| 30 | + "fixPaths": [], |
| 31 | + "alertScore": 7, |
| 32 | + "alertObject": { |
| 33 | + "k8sApiObjects": [wl] |
| 34 | + }, |
| 35 | + "relatedObjects": [ |
| 36 | + { |
| 37 | + "object": httproute, |
| 38 | + "reviewPaths": result, |
| 39 | + "failedPaths": result, |
| 40 | + }, |
| 41 | + { |
| 42 | + "object": svc, |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +# ==================================================================================== |
| 49 | + |
| 50 | +is_exposed_service(svc) { |
| 51 | + svc.spec.type == "NodePort" |
| 52 | +} |
| 53 | + |
| 54 | +is_exposed_service(svc) { |
| 55 | + svc.spec.type == "LoadBalancer" |
| 56 | +} |
| 57 | + |
| 58 | +wl_connected_to_service(wl, svc) { |
| 59 | + count({x | svc.spec.selector[x] == wl.metadata.labels[x]}) == count(svc.spec.selector) |
| 60 | +} |
| 61 | + |
| 62 | +wl_connected_to_service(wl, svc) { |
| 63 | + wl.spec.selector.matchLabels == svc.spec.selector |
| 64 | +} |
| 65 | + |
| 66 | +wl_connected_to_service(wl, svc) { |
| 67 | + count({x | svc.spec.selector[x] == wl.spec.template.metadata.labels[x]}) == count(svc.spec.selector) |
| 68 | +} |
| 69 | + |
| 70 | +svc_connected_to_httproute(svc, httproute) = result { |
| 71 | + rule := httproute.spec.rules[i] |
| 72 | + ref := rule.backendRefs[j] |
| 73 | + ref.kind == "Service" |
| 74 | + svc.metadata.name == ref.name |
| 75 | + result := [sprintf("spec.rules[%d].backendRefs[%d].name", [i,j])] |
| 76 | +} |
| 77 | + |
0 commit comments