Skip to content

Commit 7d44cb9

Browse files
feat: implement network policy to restrict backend API access and update DNS name handling in deployment script
1 parent 2fb2d6c commit 7d44cb9

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

Deployment/kubernetes/deploy.ingress.waf.yaml.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ spec:
2424
- host: {{ fqdn }}
2525
http:
2626
paths:
27+
- path: /backend(/|$)(.*)
28+
pathType: Prefix
29+
backend:
30+
service:
31+
name: aiservice-service
32+
port:
33+
number: 9001
2734
- path: /()(.*)
2835
pathType: Prefix
2936
backend:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# NetworkPolicy to restrict backend services (aiservice, kernelmemory) to only
2+
# accept traffic from within the cluster — frontend pods and internal ingress.
3+
# This ensures backend APIs are not directly accessible from the public internet
4+
# when WAF deployment mode is enabled.
5+
---
6+
apiVersion: networking.k8s.io/v1
7+
kind: NetworkPolicy
8+
metadata:
9+
name: deny-external-to-backend
10+
namespace: ns-km
11+
spec:
12+
podSelector:
13+
matchExpressions:
14+
- key: app
15+
operator: In
16+
values:
17+
- aiservice
18+
- kernelmemory
19+
policyTypes:
20+
- Ingress
21+
ingress:
22+
# Allow traffic from frontend pods in the same namespace
23+
- from:
24+
- podSelector:
25+
matchLabels:
26+
app: frontapp
27+
# Allow traffic from ingress controller namespace (app-routing-system)
28+
- from:
29+
- namespaceSelector:
30+
matchLabels:
31+
kubernetes.io/metadata.name: app-routing-system
32+
# Allow traffic from within the ns-km namespace (inter-service communication)
33+
- from:
34+
- podSelector: {}

Deployment/resourcedeployment.ps1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,21 @@ try {
739739
# 6-1. Get Az Network resource Name with the public IP address
740740
Write-Host "Assign DNS Name to the public IP address" -ForegroundColor Green
741741
$publicIpName=$(az network public-ip list --resource-group $aksResourceGroupName --query "[?ipAddress=='$externalIP'].name" --output tsv)
742-
# 6-2. Generate Unique backend API fqdn Name - esgdocanalysis-3 digit random number with padding 0
743-
$dnsName = "kmgs$($(Get-Random -Minimum 0 -Maximum 9999).ToString("D4"))"
744-
745-
# Validate if the AKS Resource Group Name, Public IP name and DNS Name are provided
742+
# 6-2. Reuse existing DNS name if already assigned, otherwise generate a new one
743+
# Validate if the AKS Resource Group Name and Public IP name are provided
746744
ValidateVariableIsNullOrEmpty -variableValue $aksResourceGroupName -variableName "AKS Resource Group name"
747745

748746
ValidateVariableIsNullOrEmpty -variableValue $publicIpName -variableName "Public IP name"
749747

748+
$existingDnsName = az network public-ip show --resource-group $aksResourceGroupName --name $publicIpName --query "dnsSettings.domainNameLabel" --output tsv 2>$null
749+
if ($existingDnsName) {
750+
Write-Host "Reusing existing DNS name: $existingDnsName" -ForegroundColor Yellow
751+
$dnsName = $existingDnsName
752+
} else {
753+
$dnsName = "kmgs$($(Get-Random -Minimum 0 -Maximum 9999).ToString("D4"))"
754+
Write-Host "Generated new DNS name: $dnsName" -ForegroundColor Green
755+
}
756+
750757
ValidateVariableIsNullOrEmpty -variableValue $dnsName -variableName "DNS Name"
751758

752759
# 6-3. Assign DNS Name to the public IP address
@@ -1028,7 +1035,7 @@ try {
10281035
kubectl apply -f "./kubernetes/deploy.ingress.internal.yaml.template" -n $kubenamespace
10291036

10301037
# Deploy network policies to restrict backend traffic to internal only
1031-
kubectl apply -f "./kubernetes/deploy.networkpolicy.yaml" -n $kubenamespace
1038+
kubectl apply -f "./kubernetes/deploy.networkpolicy.yaml.template" -n $kubenamespace
10321039

10331040
Write-Host "WAF network policies and internal backend ingress applied successfully." -ForegroundColor Green
10341041
}

infra/main.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ module managedCluster 'br/public:avm/res/container-service/managed-cluster:0.10.
967967
}
968968
serviceCidr: '10.20.0.0/16'
969969
dnsServiceIP: '10.20.0.10'
970-
enablePrivateCluster: enablePrivateNetworking
970+
enablePrivateCluster: false
971971
primaryAgentPoolProfiles: [
972972
{
973973
name: 'agentpool'

0 commit comments

Comments
 (0)