Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Azure Storage Accounts: Configure Storage Firewall IP and Subnet Rules

See more Azure Storage Accounts Examples

Reads and preserves existing network ACLs, adds an IP rule and optionally a subnet rule, then prepares publicNetworkAccess=Enabled with defaultAction=Deny. Replace the documentation IP with a real public outbound IPv4 address. A subnet must already have the appropriate Storage service endpoint.

Review the target and set applyChanges=true to submit. This can interrupt clients and can reopen a disabled public endpoint for selected sources. Existing bypass exceptions and resource-instance rules remain in effect. These rules do not create private endpoints, configure DNS, or grant data permissions. Coordinate concurrent network-policy writers.

See Storage firewall and network access for same-region Azure traffic, service endpoints, private networking, and service-specific limitations.

These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.

Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.

Microsoft API reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local stringCompare = chilkat.StringBuilder.new()
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.

-- This is Azure Resource Manager (the management plane), not a blob data request.
-- Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
-- For the v2 token endpoint, request scope https://management.azure.com/.default.
-- Keep the token file private and out of source control and public web directories.
local token = chilkat.JsonObject.new()
token.strict = true
local success = token:load_file("qa_data/tokens/azureToken.json")
if not success then
    print("Could not load the private Azure Resource Manager token file.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("A nonempty access_token string is required.")
    return
end

-- Use your subscription ID as a string. The all-zero value is only a placeholder.
local subscriptionId = "00000000-0000-0000-0000-000000000000"
local http = chilkat.Http.new()
http.auth_token = token:string_of("access_token")
http.accept = "application/json"
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 60
success = http:set_url_var("subscriptionId", subscriptionId)
if not success then
    print("Could not encode subscription ID.")
    return
end

local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local parsed = chilkat.Url.new()
local resourceGroupName = "my-resource-group"
success = http:set_url_var("resourceGroupName", resourceGroupName)
if not success then
    print("Could not encode resource group.")
    return
end

local accountName = "youruniquestorageaccount"
success = http:set_url_var("accountName", accountName)
if not success then
    print("Could not encode account name.")
    return
end

local resourceUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
success = http:http_no_body("GET", resourceUrl, response)
if response.status_code == 0 then
    print("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    return
end

if response.status_code ~= 200 then
    print("HTTP status: " .. response.status_code)
    print("Request ID: " .. response:get_header_field("x-ms-request-id"))
    print("Retry-After: " .. response:get_header_field("Retry-After"))
    -- Inspect the error body privately. Do not log tokens, headers, or full responses.
    -- For 401 check the token/audience; for 403 check Azure RBAC and policy.
    -- Honor Retry-After for 429 and use bounded backoff for transient failures.
    return
end

if not success then
    print("The HTTP response could not be read completely.")
    return
end

success = result:load(response.body_str)
if not success then
    print("Expected a well-formed JSON response.")
    return
end

if result:json_type_of("properties.networkAcls") ~= 3 then
    print("Missing or invalid properties.networkAcls.")
    return
end

local network = chilkat.JsonObject.new()
success = result:object_of2("properties.networkAcls", network)
if not success then
    print("Could not read network rules.")
    return
end

-- Preserve existing exceptions, resource-instance rules, IP rules, and subnet IDs.
-- Replace this documentation address with your client's PUBLIC outbound IPv4.
local allowedIp = "198.51.100.10"
-- Optional: full ARM subnet ID, kept as a string. Its Storage service endpoint
-- must already be enabled. This example does not create a subnet or endpoint.
local subnetId = ""
local ipRules = chilkat.JsonArray.new()
if not network:has_member("ipRules") then
    network:update_new_array("ipRules")
end

success = network:array_of2("ipRules", ipRules)
if not success then
    print("Invalid IP rule array.")
    return
end

local rule = chilkat.JsonObject.new()
local ipFound = false
local i = 0
while i < ipRules.size do
    success = ipRules:object_at2(i, rule)
    if not success then
        print("Invalid IP rule.")
        return
    end

    if rule:string_of_equals("value", allowedIp, true) then
        ipFound = true
    end

    i = i + 1
end

if not ipFound then
    success = ipRules:add_object_at2(-1, rule)
    if not success then
        print("Could not append IP rule.")
        return
    end

    rule:update_string("value", allowedIp)
    rule:update_string("action", "Allow")
end

local subnets = chilkat.JsonArray.new()
if not network:has_member("virtualNetworkRules") then
    network:update_new_array("virtualNetworkRules")
end

success = network:array_of2("virtualNetworkRules", subnets)
if not success then
    print("Invalid subnet rule array.")
    return
end

local subnetFound = false
i = 0
while i < subnets.size do
    success = subnets:object_at2(i, rule)
    if not success then
        print("Invalid subnet rule.")
        return
    end

    -- state is returned by GET but is not a writable rule property.
    rule:delete("state")
    if rule:string_of_equals("id", subnetId, false) then
        subnetFound = true
    end

    i = i + 1
end

stringCompare:set_string(subnetId)
local stringMatch1 = stringCompare:contents_equal("", true)
if (not stringMatch1) and (not subnetFound) then
    success = subnets:add_object_at2(-1, rule)
    if not success then
        print("Could not append subnet rule.")
        return
    end

    rule:update_string("id", subnetId)
    rule:update_string("action", "Allow")
end

network:update_string("defaultAction", "Deny")
local request = chilkat.JsonObject.new()
local properties = chilkat.JsonObject.new()
success = request:append_object2("properties", properties)
if not success then
    print("Could not create properties.")
    return
end

success = properties:append_object_copy("networkAcls", network)
if not success then
    print("Could not copy network rules.")
    return
end

-- Enabled + defaultAction Deny means selected public networks. Disabled would
-- block public endpoint access; SecuredByPerimeter is a different policy mode.
properties:update_string("publicNetworkAccess", "Enabled")
local applyChanges = false
if not applyChanges then
    print("Network changes prepared, not applied. Review rules and set applyChanges to true.")
    return
end

-- This may reopen a previously disabled public endpoint for the selected sources.
-- Preserve exceptions intentionally; preserved trusted-service bypasses still apply.
-- On a write timeout, read the resource before deciding whether to retry.
success = http:http_json("PATCH", resourceUrl, request, "application/json", response)
if response.status_code == 0 then
    print("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    return
end

if response.status_code ~= 200 then
    print("HTTP status: " .. response.status_code)
    print("Request ID: " .. response:get_header_field("x-ms-request-id"))
    print("Retry-After: " .. response:get_header_field("Retry-After"))
    -- Inspect the error body privately. Do not log tokens, headers, or full responses.
    -- For 401 check the token/audience; for 403 check Azure RBAC and policy.
    -- Honor Retry-After for 429 and use bounded backoff for transient failures.
    return
end

if not success then
    print("The HTTP response could not be read completely.")
    return
end

success = result:load(response.body_str)
if not success then
    print("Expected a well-formed JSON response.")
    return
end

print("Network policy update returned successfully. Verify access from intended clients.")