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.
Chilkat Objective-C Downloads
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoUrl.h>
#import <CkoJsonArray.h>
BOOL success = NO;
CkoStringBuilder *stringCompare = [[CkoStringBuilder alloc] init];
// 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.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
token.Strict = YES;
success = [token LoadFile: @"qa_data/tokens/azureToken.json"];
if (success != YES) {
NSLog(@"%@",@"Could not load the private Azure Resource Manager token file.");
return;
}
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"A nonempty access_token string is required.");
return;
}
// Use your subscription ID as a string. The all-zero value is only a placeholder.
NSString *subscriptionId = @"00000000-0000-0000-0000-000000000000";
CkoHttp *http = [[CkoHttp alloc] init];
http.AuthToken = [token StringOf: @"access_token"];
http.Accept = @"application/json";
http.RequireSslCertVerify = YES;
http.FollowRedirects = NO;
http.ConnectTimeout = [NSNumber numberWithInt:30];
http.ReadTimeout = [NSNumber numberWithInt:60];
success = [http SetUrlVar: @"subscriptionId" value: subscriptionId];
if (success != YES) {
NSLog(@"%@",@"Could not encode subscription ID.");
return;
}
CkoHttpResponse *response = [[CkoHttpResponse alloc] init];
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.Strict = YES;
CkoUrl *parsed = [[CkoUrl alloc] init];
NSString *resourceGroupName = @"my-resource-group";
success = [http SetUrlVar: @"resourceGroupName" value: resourceGroupName];
if (success != YES) {
NSLog(@"%@",@"Could not encode resource group.");
return;
}
NSString *accountName = @"youruniquestorageaccount";
success = [http SetUrlVar: @"accountName" value: accountName];
if (success != YES) {
NSLog(@"%@",@"Could not encode account name.");
return;
}
NSString *resourceUrl = @"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01";
success = [http HttpNoBody: @"GET" url: resourceUrl response: response];
if ([response.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Inspect LastErrorText privately; it may contain credentials.");
return;
}
if (([response.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"HTTP status: ",[response.StatusCode intValue]);
NSLog(@"%@%@",@"Request ID: ",[response GetHeaderField: @"x-ms-request-id"]);
NSLog(@"%@%@",@"Retry-After: ",[response GetHeaderField: @"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;
}
if (success != YES) {
NSLog(@"%@",@"The HTTP response could not be read completely.");
return;
}
success = [result Load: response.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a well-formed JSON response.");
return;
}
if ([[result JsonTypeOf: @"properties.networkAcls"] intValue] != 3) {
NSLog(@"%@",@"Missing or invalid properties.networkAcls.");
return;
}
CkoJsonObject *network = [[CkoJsonObject alloc] init];
success = [result ObjectOf2: @"properties.networkAcls" jsonObj: network];
if (success != YES) {
NSLog(@"%@",@"Could not read network rules.");
return;
}
// Preserve existing exceptions, resource-instance rules, IP rules, and subnet IDs.
// Replace this documentation address with your client's PUBLIC outbound IPv4.
NSString *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.
NSString *subnetId = @"";
CkoJsonArray *ipRules = [[CkoJsonArray alloc] init];
if ([network HasMember: @"ipRules"] != YES) {
[network UpdateNewArray: @"ipRules"];
}
success = [network ArrayOf2: @"ipRules" jarr: ipRules];
if (success != YES) {
NSLog(@"%@",@"Invalid IP rule array.");
return;
}
CkoJsonObject *rule = [[CkoJsonObject alloc] init];
BOOL ipFound = NO;
int i = 0;
while ((i < [ipRules.Size intValue])) {
success = [ipRules ObjectAt2: [NSNumber numberWithInt: i] jsonObj: rule];
if (success != YES) {
NSLog(@"%@",@"Invalid IP rule.");
return;
}
if ([rule StringOfEquals: @"value" value: allowedIp caseSensitive: YES] == YES) {
ipFound = YES;
}
i = i + 1;
}
if (ipFound != YES) {
success = [ipRules AddObjectAt2: [NSNumber numberWithInt: -1] json: rule];
if (success != YES) {
NSLog(@"%@",@"Could not append IP rule.");
return;
}
[rule UpdateString: @"value" value: allowedIp];
[rule UpdateString: @"action" value: @"Allow"];
}
CkoJsonArray *subnets = [[CkoJsonArray alloc] init];
if ([network HasMember: @"virtualNetworkRules"] != YES) {
[network UpdateNewArray: @"virtualNetworkRules"];
}
success = [network ArrayOf2: @"virtualNetworkRules" jarr: subnets];
if (success != YES) {
NSLog(@"%@",@"Invalid subnet rule array.");
return;
}
BOOL subnetFound = NO;
i = 0;
while ((i < [subnets.Size intValue])) {
success = [subnets ObjectAt2: [NSNumber numberWithInt: i] jsonObj: rule];
if (success != YES) {
NSLog(@"%@",@"Invalid subnet rule.");
return;
}
// state is returned by GET but is not a writable rule property.
[rule Delete: @"state"];
if ([rule StringOfEquals: @"id" value: subnetId caseSensitive: NO] == YES) {
subnetFound = YES;
}
i = i + 1;
}
[stringCompare SetString: subnetId];
BOOL stringMatch1 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if ((stringMatch1 != YES) && (subnetFound != YES)) {
success = [subnets AddObjectAt2: [NSNumber numberWithInt: -1] json: rule];
if (success != YES) {
NSLog(@"%@",@"Could not append subnet rule.");
return;
}
[rule UpdateString: @"id" value: subnetId];
[rule UpdateString: @"action" value: @"Allow"];
}
[network UpdateString: @"defaultAction" value: @"Deny"];
CkoJsonObject *request = [[CkoJsonObject alloc] init];
CkoJsonObject *properties = [[CkoJsonObject alloc] init];
success = [request AppendObject2: @"properties" jsonObj: properties];
if (success != YES) {
NSLog(@"%@",@"Could not create properties.");
return;
}
success = [properties AppendObjectCopy: @"networkAcls" jsonObj: network];
if (success != YES) {
NSLog(@"%@",@"Could not copy network rules.");
return;
}
// Enabled + defaultAction Deny means selected public networks. Disabled would
// block public endpoint access; SecuredByPerimeter is a different policy mode.
[properties UpdateString: @"publicNetworkAccess" value: @"Enabled"];
BOOL applyChanges = NO;
if (applyChanges != YES) {
NSLog(@"%@",@"Network changes prepared, not applied. Review rules and set applyChanges to true.");
return;
}
// 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 HttpJson: @"PATCH" url: resourceUrl json: request contentType: @"application/json" response: response];
if ([response.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Inspect LastErrorText privately; it may contain credentials.");
return;
}
if (([response.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"HTTP status: ",[response.StatusCode intValue]);
NSLog(@"%@%@",@"Request ID: ",[response GetHeaderField: @"x-ms-request-id"]);
NSLog(@"%@%@",@"Retry-After: ",[response GetHeaderField: @"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;
}
if (success != YES) {
NSLog(@"%@",@"The HTTP response could not be read completely.");
return;
}
success = [result Load: response.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a well-formed JSON response.");
return;
}
NSLog(@"%@",@"Network policy update returned successfully. Verify access from intended clients.");