Skip to content

Commit

Permalink
refactor(checks): migrate Kubernetes network to Rego
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Pivkin <[email protected]>
  • Loading branch information
nikpivkin authored and simar7 committed Nov 27, 2024
1 parent 08abf3c commit 11152df
Show file tree
Hide file tree
Showing 15 changed files with 585 additions and 281 deletions.
121 changes: 60 additions & 61 deletions avd_docs/kubernetes/network/AVD-KUBE-0001/Terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,66 @@
Remove public access except where explicitly required

```hcl
resource "kubernetes_network_policy" "good_example" {
metadata {
name = "terraform-example-network-policy"
namespace = "default"
}
spec {
pod_selector {
match_expressions {
key = "name"
operator = "In"
values = ["webfront", "api"]
}
}
ingress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
from {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
egress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
to {
ip_block {
cidr = "0.0.0.0/0"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
policy_types = ["Ingress", "Egress"]
}
}
resource "kubernetes_network_policy" "good_example" {
metadata {
name = "terraform-example-network-policy"
namespace = "default"
}
spec {
pod_selector {
match_expressions {
key = "name"
operator = "In"
values = ["webfront", "api"]
}
}
ingress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
from {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
egress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
to {
ip_block {
cidr = "0.0.0.0/0"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
policy_types = ["Ingress", "Egress"]
}
}
```

#### Remediation Links
Expand Down
2 changes: 1 addition & 1 deletion avd_docs/kubernetes/network/AVD-KUBE-0001/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
You should not expose infrastructure to the public internet except where explicitly required

### Impact
Exposure of infrastructure to the public internet
<!-- Add Impact here -->

<!-- DO NOT CHANGE -->
{{ remediationActions }}
Expand Down
123 changes: 61 additions & 62 deletions avd_docs/kubernetes/network/AVD-KUBE-0002/Terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,68 @@
Remove public access except where explicitly required

```hcl
resource "kubernetes_network_policy" "good_example" {
metadata {
name = "terraform-example-network-policy"
namespace = "default"
}
spec {
pod_selector {
match_expressions {
key = "name"
operator = "In"
values = ["webfront", "api"]
}
}
egress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
to {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
ingress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
from {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
policy_types = ["Ingress", "Egress"]
}
}
resource "kubernetes_network_policy" "good_example" {
metadata {
name = "terraform-example-network-policy"
namespace = "default"
}
spec {
pod_selector {
match_expressions {
key = "name"
operator = "In"
values = ["webfront", "api"]
}
}
egress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
to {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
ingress {
ports {
port = "http"
protocol = "TCP"
}
ports {
port = "8125"
protocol = "UDP"
}
from {
ip_block {
cidr = "10.0.0.0/16"
except = [
"10.0.0.0/24",
"10.0.1.0/24",
]
}
}
}
policy_types = ["Ingress", "Egress"]
}
}
```

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

2 changes: 1 addition & 1 deletion avd_docs/kubernetes/network/AVD-KUBE-0002/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
You should not expose infrastructure to the public internet except where explicitly required

### Impact
Exfiltration of data to the public internet
<!-- Add Impact here -->

<!-- DO NOT CHANGE -->
{{ remediationActions }}
Expand Down
3 changes: 2 additions & 1 deletion checks/kubernetes/network/no_public_egress.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var CheckNoPublicEgress = rules.Register(
Links: terraformNoPublicEgressLinks,
RemediationMarkdown: terraformNoPublicEgressRemediationMarkdown,
},
Severity: severity.High,
Severity: severity.High,
Deprecated: true,
},
func(s *state.State) (results scan.Results) {
for _, policy := range s.Kubernetes.NetworkPolicies {
Expand Down
38 changes: 38 additions & 0 deletions checks/kubernetes/network/no_public_egress.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# METADATA
# title: Public egress should not be allowed via network policies
# description: You should not expose infrastructure to the public internet except where explicitly required
# scope: package
# schemas:
# - input: schema["cloud"]
# custom:
# id: AVD-KUBE-0002
# avd_id: AVD-KUBE-0002
# provider: kubernetes
# service: network
# severity: HIGH
# short_code: no-public-egress
# recommended_action: Remove public access except where explicitly required
# input:
# selector:
# - type: cloud
# subtypes:
# - provider: kubernetes
# service: networkpolicies
# terraform:
# good_examples: checks/kubernetes/network/no_public_egress.yaml
# links:
# - https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/network_policy#spec.egress.to.ip_block.cidr
package builtin.kube.network.kube0002

import rego.v1

deny contains res if {
some policy in input.kubernetes.networkpolicies
isManaged(policy)
some dest in policy.spec.egress.destinationcidrs
cidr.is_public(dest.value)
res := result.new(
"Network policy allows egress to the public internet.",
dest,
)
}
Loading

0 comments on commit 11152df

Please sign in to comment.