Skip to content

Commit 11152df

Browse files
nikpivkinsimar7
authored andcommitted
refactor(checks): migrate Kubernetes network to Rego
Signed-off-by: Nikita Pivkin <[email protected]>
1 parent 08abf3c commit 11152df

15 files changed

+585
-281
lines changed

avd_docs/kubernetes/network/AVD-KUBE-0001/Terraform.md

+60-61
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,66 @@
22
Remove public access except where explicitly required
33

44
```hcl
5-
resource "kubernetes_network_policy" "good_example" {
6-
metadata {
7-
name = "terraform-example-network-policy"
8-
namespace = "default"
9-
}
10-
11-
spec {
12-
pod_selector {
13-
match_expressions {
14-
key = "name"
15-
operator = "In"
16-
values = ["webfront", "api"]
17-
}
18-
}
19-
20-
ingress {
21-
ports {
22-
port = "http"
23-
protocol = "TCP"
24-
}
25-
ports {
26-
port = "8125"
27-
protocol = "UDP"
28-
}
29-
30-
from {
31-
ip_block {
32-
cidr = "10.0.0.0/16"
33-
except = [
34-
"10.0.0.0/24",
35-
"10.0.1.0/24",
36-
]
37-
}
38-
}
39-
}
40-
41-
egress {
42-
ports {
43-
port = "http"
44-
protocol = "TCP"
45-
}
46-
ports {
47-
port = "8125"
48-
protocol = "UDP"
49-
}
50-
51-
to {
52-
ip_block {
53-
cidr = "0.0.0.0/0"
54-
except = [
55-
"10.0.0.0/24",
56-
"10.0.1.0/24",
57-
]
58-
}
59-
}
60-
}
61-
62-
policy_types = ["Ingress", "Egress"]
63-
}
64-
}
65-
5+
resource "kubernetes_network_policy" "good_example" {
6+
metadata {
7+
name = "terraform-example-network-policy"
8+
namespace = "default"
9+
}
10+
11+
spec {
12+
pod_selector {
13+
match_expressions {
14+
key = "name"
15+
operator = "In"
16+
values = ["webfront", "api"]
17+
}
18+
}
19+
20+
ingress {
21+
ports {
22+
port = "http"
23+
protocol = "TCP"
24+
}
25+
ports {
26+
port = "8125"
27+
protocol = "UDP"
28+
}
29+
30+
from {
31+
ip_block {
32+
cidr = "10.0.0.0/16"
33+
except = [
34+
"10.0.0.0/24",
35+
"10.0.1.0/24",
36+
]
37+
}
38+
}
39+
}
40+
41+
egress {
42+
ports {
43+
port = "http"
44+
protocol = "TCP"
45+
}
46+
ports {
47+
port = "8125"
48+
protocol = "UDP"
49+
}
50+
51+
to {
52+
ip_block {
53+
cidr = "0.0.0.0/0"
54+
except = [
55+
"10.0.0.0/24",
56+
"10.0.1.0/24",
57+
]
58+
}
59+
}
60+
}
61+
62+
policy_types = ["Ingress", "Egress"]
63+
}
64+
}
6665
```
6766

6867
#### Remediation Links

avd_docs/kubernetes/network/AVD-KUBE-0001/docs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
You should not expose infrastructure to the public internet except where explicitly required
33

44
### Impact
5-
Exposure of infrastructure to the public internet
5+
<!-- Add Impact here -->
66

77
<!-- DO NOT CHANGE -->
88
{{ remediationActions }}

avd_docs/kubernetes/network/AVD-KUBE-0002/Terraform.md

+61-62
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,68 @@
22
Remove public access except where explicitly required
33

44
```hcl
5-
resource "kubernetes_network_policy" "good_example" {
6-
metadata {
7-
name = "terraform-example-network-policy"
8-
namespace = "default"
9-
}
10-
11-
spec {
12-
pod_selector {
13-
match_expressions {
14-
key = "name"
15-
operator = "In"
16-
values = ["webfront", "api"]
17-
}
18-
}
19-
20-
egress {
21-
ports {
22-
port = "http"
23-
protocol = "TCP"
24-
}
25-
ports {
26-
port = "8125"
27-
protocol = "UDP"
28-
}
29-
30-
to {
31-
ip_block {
32-
cidr = "10.0.0.0/16"
33-
except = [
34-
"10.0.0.0/24",
35-
"10.0.1.0/24",
36-
]
37-
}
38-
}
39-
}
40-
41-
ingress {
42-
ports {
43-
port = "http"
44-
protocol = "TCP"
45-
}
46-
ports {
47-
port = "8125"
48-
protocol = "UDP"
49-
}
50-
51-
from {
52-
ip_block {
53-
cidr = "10.0.0.0/16"
54-
except = [
55-
"10.0.0.0/24",
56-
"10.0.1.0/24",
57-
]
58-
}
59-
}
60-
}
61-
62-
policy_types = ["Ingress", "Egress"]
63-
}
64-
}
65-
5+
resource "kubernetes_network_policy" "good_example" {
6+
metadata {
7+
name = "terraform-example-network-policy"
8+
namespace = "default"
9+
}
10+
11+
spec {
12+
pod_selector {
13+
match_expressions {
14+
key = "name"
15+
operator = "In"
16+
values = ["webfront", "api"]
17+
}
18+
}
19+
20+
egress {
21+
ports {
22+
port = "http"
23+
protocol = "TCP"
24+
}
25+
ports {
26+
port = "8125"
27+
protocol = "UDP"
28+
}
29+
30+
to {
31+
ip_block {
32+
cidr = "10.0.0.0/16"
33+
except = [
34+
"10.0.0.0/24",
35+
"10.0.1.0/24",
36+
]
37+
}
38+
}
39+
}
40+
41+
ingress {
42+
ports {
43+
port = "http"
44+
protocol = "TCP"
45+
}
46+
ports {
47+
port = "8125"
48+
protocol = "UDP"
49+
}
50+
51+
from {
52+
ip_block {
53+
cidr = "10.0.0.0/16"
54+
except = [
55+
"10.0.0.0/24",
56+
"10.0.1.0/24",
57+
]
58+
}
59+
}
60+
}
61+
62+
policy_types = ["Ingress", "Egress"]
63+
}
64+
}
6665
```
6766

6867
#### Remediation Links
69-
- https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/network_policy#spec.ingress.from.ip_block.cidr
68+
- https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/network_policy#spec.egress.to.ip_block.cidr
7069

avd_docs/kubernetes/network/AVD-KUBE-0002/docs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
You should not expose infrastructure to the public internet except where explicitly required
33

44
### Impact
5-
Exfiltration of data to the public internet
5+
<!-- Add Impact here -->
66

77
<!-- DO NOT CHANGE -->
88
{{ remediationActions }}

checks/kubernetes/network/no_public_egress.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var CheckNoPublicEgress = rules.Register(
2626
Links: terraformNoPublicEgressLinks,
2727
RemediationMarkdown: terraformNoPublicEgressRemediationMarkdown,
2828
},
29-
Severity: severity.High,
29+
Severity: severity.High,
30+
Deprecated: true,
3031
},
3132
func(s *state.State) (results scan.Results) {
3233
for _, policy := range s.Kubernetes.NetworkPolicies {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# METADATA
2+
# title: Public egress should not be allowed via network policies
3+
# description: You should not expose infrastructure to the public internet except where explicitly required
4+
# scope: package
5+
# schemas:
6+
# - input: schema["cloud"]
7+
# custom:
8+
# id: AVD-KUBE-0002
9+
# avd_id: AVD-KUBE-0002
10+
# provider: kubernetes
11+
# service: network
12+
# severity: HIGH
13+
# short_code: no-public-egress
14+
# recommended_action: Remove public access except where explicitly required
15+
# input:
16+
# selector:
17+
# - type: cloud
18+
# subtypes:
19+
# - provider: kubernetes
20+
# service: networkpolicies
21+
# terraform:
22+
# good_examples: checks/kubernetes/network/no_public_egress.yaml
23+
# links:
24+
# - https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/network_policy#spec.egress.to.ip_block.cidr
25+
package builtin.kube.network.kube0002
26+
27+
import rego.v1
28+
29+
deny contains res if {
30+
some policy in input.kubernetes.networkpolicies
31+
isManaged(policy)
32+
some dest in policy.spec.egress.destinationcidrs
33+
cidr.is_public(dest.value)
34+
res := result.new(
35+
"Network policy allows egress to the public internet.",
36+
dest,
37+
)
38+
}

0 commit comments

Comments
 (0)