Skip to content

Commit 51e46b9

Browse files
committed
Add Private Link to PostgreSQL and remove broad Azure services firewall rule
1 parent 028d2ff commit 51e46b9

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

cloud-infrastructure/cluster/grant-database-permissions.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ POSTGRES_HOST=$POSTGRES_SERVER_NAME.postgres.database.azure.com
1111

1212
cd "$(dirname "${BASH_SOURCE[0]}")"
1313

14-
# Get an access token for PostgreSQL using the current Azure CLI identity
14+
export CLUSTER_RESOURCE_GROUP_NAME
15+
export POSTGRES_SERVER_NAME
16+
export DATABASE_NAME
17+
trap '. ./firewall.sh close' EXIT
18+
. ./firewall.sh open
19+
1520
ACCESS_TOKEN=$(az account get-access-token --resource-type oss-rdbms --query accessToken --output tsv)
1621

1722
echo "$(date +"%Y-%m-%dT%H:%M:%S") Granting $MANAGED_IDENTITY_NAME (Client ID: $MANAGED_IDENTITY_CLIENT_ID) permissions on $POSTGRES_HOST/$DATABASE_NAME database"

cloud-infrastructure/cluster/main-cluster.bicep

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ module postgresServer '../modules/postgresql-flexible-server.bicep' = {
150150
name: clusterResourceGroupName
151151
tags: tags
152152
tenantId: subscription().tenantId
153+
subnetId: subnetId
154+
virtualNetworkId: virtualNetwork.outputs.virtualNetworkId
153155
}
156+
dependsOn: [virtualNetwork]
154157
}
155158

156159
var isCustomDomainSet = domainName != ''

cloud-infrastructure/modules/postgresql-flexible-server.bicep

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ param name string
22
param location string
33
param tags object
44
param tenantId string
5+
param subnetId string
6+
param virtualNetworkId string
57

68
resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2025-08-01' = {
79
name: name
@@ -35,12 +37,56 @@ resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2025-08-01' =
3537
}
3638
}
3739

38-
resource postgresVirtualNetworkRule 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2025-08-01' = {
39-
parent: postgresServer
40-
name: 'allow-azure-services'
40+
resource privateDnsZone 'Microsoft.Network/privateDnsZones@2024-06-01' = {
41+
name: 'privatelink.postgres.database.azure.com'
42+
location: 'global'
43+
tags: tags
44+
}
45+
46+
resource privateDnsZoneVnetLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2024-06-01' = {
47+
parent: privateDnsZone
48+
name: '${name}-vnet-link'
49+
location: 'global'
50+
properties: {
51+
virtualNetwork: {
52+
id: virtualNetworkId
53+
}
54+
registrationEnabled: false
55+
}
56+
}
57+
58+
resource privateEndpoint 'Microsoft.Network/privateEndpoints@2025-01-01' = {
59+
name: '${name}-postgres-private-endpoint'
60+
location: location
61+
tags: tags
62+
properties: {
63+
subnet: {
64+
id: subnetId
65+
}
66+
privateLinkServiceConnections: [
67+
{
68+
name: '${name}-postgres-connection'
69+
properties: {
70+
privateLinkServiceId: postgresServer.id
71+
groupIds: ['postgresqlServer']
72+
}
73+
}
74+
]
75+
}
76+
}
77+
78+
resource privateDnsZoneGroup 'Microsoft.Network/privateEndpoints/privateDnsZoneGroups@2025-01-01' = {
79+
parent: privateEndpoint
80+
name: 'default'
4181
properties: {
42-
startIpAddress: '0.0.0.0'
43-
endIpAddress: '0.0.0.0'
82+
privateDnsZoneConfigs: [
83+
{
84+
name: 'postgres'
85+
properties: {
86+
privateDnsZoneId: privateDnsZone.id
87+
}
88+
}
89+
]
4490
}
4591
}
4692

0 commit comments

Comments
 (0)