|
| 1 | +package armo_builtins |
| 2 | +import future.keywords.in |
| 3 | + |
| 4 | + |
| 5 | +deny[msga] { |
| 6 | + virtualservice := input[_] |
| 7 | + virtualservice.kind == "VirtualService" |
| 8 | + |
| 9 | + # Check if the VirtualService is connected to a Gateway |
| 10 | + gateway := input[_] |
| 11 | + gateway.kind == "Gateway" |
| 12 | + |
| 13 | + is_same_namespace(gateway, virtualservice) |
| 14 | + virtualservice.spec.gateways[_] == gateway.metadata.name |
| 15 | + |
| 16 | + # Find the connected Istio Ingress Gateway that should be a LoadBalancer if it is exposed to the internet |
| 17 | + istioingressgateway := input[_] |
| 18 | + istioingressgateway.kind == "Service" |
| 19 | + istioingressgateway.metadata.namespace == "istio-system" |
| 20 | + gateway.spec.selector[_] == istioingressgateway.metadata.labels[_] |
| 21 | + |
| 22 | + |
| 23 | + # Check if the Istio Ingress Gateway is exposed to the internet |
| 24 | + is_exposed_service(istioingressgateway) |
| 25 | + |
| 26 | + # Check if the VirtualService is connected to an workload |
| 27 | + # First, find the service that the VirtualService is connected to |
| 28 | + connected_service := input[_] |
| 29 | + connected_service.kind == "Service" |
| 30 | + fqsn := get_fqsn(get_namespace(virtualservice), virtualservice.spec.http[_].route[_].destination.host) |
| 31 | + target_ns := split(fqsn,".")[1] |
| 32 | + target_name := split(fqsn,".")[0] |
| 33 | + # Check if the service is in the same namespace as the VirtualService |
| 34 | + get_namespace(connected_service) == target_ns |
| 35 | + # Check if the service is the target of the VirtualService |
| 36 | + connected_service.metadata.name == target_name |
| 37 | + |
| 38 | + # Check if the service is connected to a workload |
| 39 | + wl := input[_] |
| 40 | + is_same_namespace(connected_service, wl) |
| 41 | + spec_template_spec_patterns := {"Deployment", "ReplicaSet", "DaemonSet", "StatefulSet", "Pod", "Job", "CronJob"} |
| 42 | + spec_template_spec_patterns[wl.kind] |
| 43 | + wl_connected_to_service(wl, connected_service) |
| 44 | + |
| 45 | + result := svc_connected_to_virtualservice(connected_service, virtualservice) |
| 46 | + |
| 47 | + msga := { |
| 48 | + "alertMessage": sprintf("workload '%v' is exposed through virtualservice '%v'", [wl.metadata.name, virtualservice.metadata.name]), |
| 49 | + "packagename": "armo_builtins", |
| 50 | + "failedPaths": [], |
| 51 | + "fixPaths": [], |
| 52 | + "alertScore": 7, |
| 53 | + "alertObject": { |
| 54 | + "k8sApiObjects": [wl] |
| 55 | + }, |
| 56 | + "relatedObjects": [ |
| 57 | + { |
| 58 | + "object": virtualservice, |
| 59 | + "reviewPaths": result, |
| 60 | + "failedPaths": result, |
| 61 | + }, |
| 62 | + { |
| 63 | + "object": connected_service, |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +# ==================================================================================== |
| 70 | + |
| 71 | +get_namespace(obj) = namespace { |
| 72 | + namespace := obj.metadata.namespace |
| 73 | +} |
| 74 | + |
| 75 | +get_namespace(obj) = namespace { |
| 76 | + not obj.metadata.namespace |
| 77 | + namespace := "default" |
| 78 | +} |
| 79 | + |
| 80 | +is_same_namespace(obj1, obj2) { |
| 81 | + obj1.metadata.namespace == obj2.metadata.namespace |
| 82 | +} |
| 83 | + |
| 84 | +is_same_namespace(obj1, obj2) { |
| 85 | + not obj1.metadata.namespace |
| 86 | + obj2.metadata.namespace == "default" |
| 87 | +} |
| 88 | + |
| 89 | +is_same_namespace(obj1, obj2) { |
| 90 | + not obj2.metadata.namespace |
| 91 | + obj1.metadata.namespace == "default" |
| 92 | +} |
| 93 | + |
| 94 | +is_same_namespace(obj1, obj2) { |
| 95 | + not obj1.metadata.namespace |
| 96 | + not obj2.metadata.namespace |
| 97 | +} |
| 98 | + |
| 99 | +is_exposed_service(svc) { |
| 100 | + svc.spec.type == "NodePort" |
| 101 | +} |
| 102 | + |
| 103 | +is_exposed_service(svc) { |
| 104 | + svc.spec.type == "LoadBalancer" |
| 105 | +} |
| 106 | + |
| 107 | +wl_connected_to_service(wl, svc) { |
| 108 | + count({x | svc.spec.selector[x] == wl.metadata.labels[x]}) == count(svc.spec.selector) |
| 109 | +} |
| 110 | + |
| 111 | +wl_connected_to_service(wl, svc) { |
| 112 | + wl.spec.selector.matchLabels == svc.spec.selector |
| 113 | +} |
| 114 | + |
| 115 | +wl_connected_to_service(wl, svc) { |
| 116 | + count({x | svc.spec.selector[x] == wl.spec.template.metadata.labels[x]}) == count(svc.spec.selector) |
| 117 | +} |
| 118 | + |
| 119 | +svc_connected_to_virtualservice(svc, virtualservice) = result { |
| 120 | + host := virtualservice.spec.http[i].route[j].destination.host |
| 121 | + svc.metadata.name == host |
| 122 | + result := [sprintf("spec.http[%d].routes[%d].destination.host", [i,j])] |
| 123 | +} |
| 124 | + |
| 125 | +get_fqsn(ns, dest_host) = fqsn { |
| 126 | + # verify that this name is without the namespace |
| 127 | + count(split(".", dest_host)) == 1 |
| 128 | + fqsn := sprintf("%v.%v.svc.cluster.local", [dest_host, ns]) |
| 129 | +} |
| 130 | + |
| 131 | +get_fqsn(ns, dest_host) = fqsn { |
| 132 | + count(split(".", dest_host)) == 2 |
| 133 | + fqsn := sprintf("%v.svc.cluster.local", [dest_host]) |
| 134 | +} |
| 135 | + |
| 136 | +get_fqsn(ns, dest_host) = fqsn { |
| 137 | + count(split(".", dest_host)) == 4 |
| 138 | + fqsn := dest_host |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | + |
0 commit comments