Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions kcl/lib/steps/azure/firewall.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import azure_pipelines.ap.steps

CreateFirewall = lambda serviceConnection: str, subscription: str, resourceGroup: str, name: str, location: str, vnetName: str, publicIpName: str, exportVar: str = "FWPRIVATE_IP" -> steps.Step {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWPRIVATE_IP please avoid using abbr. FW to make it easier to read.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great example that you encapsulate a few steps into one function and only expose a small interface.

script = """
az extension add --name azure-firewall

az network public-ip create \\
--resource-group "${resourceGroup}" \\
--name "${publicIpName}" \\
--sku Standard \\
--location "${location}" \\
--subscription "${subscription}"

az network firewall create \\
--resource-group "${resourceGroup}" \\
--name "${name}" \\
--location "${location}" \\
--enable-dns-proxy true \\
--subscription "${subscription}"

az network firewall ip-config create \\
--resource-group "${resourceGroup}" \\
--firewall-name "${name}" \\
--name "${name}-ipconfig" \\
--public-ip-address "${publicIpName}" \\
--vnet-name "${vnetName}" \\
--subscription "${subscription}"

FWPRIVATE_IP=$(az network firewall show \\
--resource-group "${resourceGroup}" \\
--name "${name}" \\
--subscription "${subscription}" \\
--query "ipConfigurations[0].privateIPAddress" -o tsv)
echo "##vso[task.setvariable variable=${exportVar}]$FWPRIVATE_IP"
"""
AzCli(serviceConnection, "Create firewall ${name}", script)
}

UpdateFirewallPolicy = lambda serviceConnection: str, subscription: str, resourceGroup: str, name: str, policyPath: str, location: str -> steps.Step {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you use this funciton?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s called in pipelines with a firewall policy json file path input

script = """
POLICY_TMP=$(mktemp)
sed 's|"location": "[^"]*"|"location": "${location}"|g' "${policyPath}" > "$POLICY_TMP"
az rest \\
--method put \\
--uri "https://management.azure.com/subscriptions/${subscription}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/azureFirewalls/${name}?api-version=2025-05-01" \\
--headers "Content-Type=application/json" \\
--body "@$POLICY_TMP"
rm -f "$POLICY_TMP"
"""
AzCli(serviceConnection, "Update firewall policy ${name}", script)
}