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 Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
token: HCkJsonObject;
subscriptionId: PWideChar;
http: HCkHttp;
response: HCkHttpResponse;
result: HCkJsonObject;
parsed: HCkUrl;
resourceGroupName: PWideChar;
accountName: PWideChar;
resourceUrl: PWideChar;
network: HCkJsonObject;
allowedIp: PWideChar;
subnetId: PWideChar;
ipRules: HCkJsonArray;
rule: HCkJsonObject;
ipFound: Boolean;
i: Integer;
subnets: HCkJsonArray;
subnetFound: Boolean;
stringMatch1: Boolean;
request: HCkJsonObject;
properties: HCkJsonObject;
applyChanges: Boolean;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// 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.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureToken.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load the private Azure Resource Manager token file.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
begin
Memo1.Lines.Add('A nonempty access_token string is required.');
Exit;
end;
// Use your subscription ID as a string. The all-zero value is only a placeholder.
subscriptionId := '00000000-0000-0000-0000-000000000000';
http := CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkHttp_putAccept(http,'application/json');
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,30);
CkHttp_putReadTimeout(http,60);
success := CkHttp_SetUrlVar(http,'subscriptionId',subscriptionId);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode subscription ID.');
Exit;
end;
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
parsed := CkUrl_Create();
resourceGroupName := 'my-resource-group';
success := CkHttp_SetUrlVar(http,'resourceGroupName',resourceGroupName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode resource group.');
Exit;
end;
accountName := 'youruniquestorageaccount';
success := CkHttp_SetUrlVar(http,'accountName',accountName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode account name.');
Exit;
end;
resourceUrl := 'https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01';
success := CkHttp_HttpNoBody(http,'GET',resourceUrl,response);
if (CkHttpResponse_getStatusCode(response) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect LastErrorText privately; it may contain credentials.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(response) <> 200)) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(response,'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.
Exit;
end;
if (success <> True) then
begin
Memo1.Lines.Add('The HTTP response could not be read completely.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(result,'properties.networkAcls') <> 3) then
begin
Memo1.Lines.Add('Missing or invalid properties.networkAcls.');
Exit;
end;
network := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(result,'properties.networkAcls',network);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read network rules.');
Exit;
end;
// Preserve existing exceptions, resource-instance rules, IP rules, and subnet IDs.
// Replace this documentation address with your client's PUBLIC outbound IPv4.
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.
subnetId := '';
ipRules := CkJsonArray_Create();
if (CkJsonObject_HasMember(network,'ipRules') <> True) then
begin
CkJsonObject_UpdateNewArray(network,'ipRules');
end;
success := CkJsonObject_ArrayOf2(network,'ipRules',ipRules);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid IP rule array.');
Exit;
end;
rule := CkJsonObject_Create();
ipFound := False;
i := 0;
while (i < CkJsonArray_getSize(ipRules)) do
begin
success := CkJsonArray_ObjectAt2(ipRules,i,rule);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid IP rule.');
Exit;
end;
if (CkJsonObject_StringOfEquals(rule,'value',allowedIp,True) = True) then
begin
ipFound := True;
end;
i := i + 1;
end;
if (ipFound <> True) then
begin
success := CkJsonArray_AddObjectAt2(ipRules,-1,rule);
if (success <> True) then
begin
Memo1.Lines.Add('Could not append IP rule.');
Exit;
end;
CkJsonObject_UpdateString(rule,'value',allowedIp);
CkJsonObject_UpdateString(rule,'action','Allow');
end;
subnets := CkJsonArray_Create();
if (CkJsonObject_HasMember(network,'virtualNetworkRules') <> True) then
begin
CkJsonObject_UpdateNewArray(network,'virtualNetworkRules');
end;
success := CkJsonObject_ArrayOf2(network,'virtualNetworkRules',subnets);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid subnet rule array.');
Exit;
end;
subnetFound := False;
i := 0;
while (i < CkJsonArray_getSize(subnets)) do
begin
success := CkJsonArray_ObjectAt2(subnets,i,rule);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid subnet rule.');
Exit;
end;
// state is returned by GET but is not a writable rule property.
CkJsonObject_Delete(rule,'state');
if (CkJsonObject_StringOfEquals(rule,'id',subnetId,False) = True) then
begin
subnetFound := True;
end;
i := i + 1;
end;
CkStringBuilder_SetString(stringCompare,subnetId);
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((stringMatch1 <> True) and (subnetFound <> True)) then
begin
success := CkJsonArray_AddObjectAt2(subnets,-1,rule);
if (success <> True) then
begin
Memo1.Lines.Add('Could not append subnet rule.');
Exit;
end;
CkJsonObject_UpdateString(rule,'id',subnetId);
CkJsonObject_UpdateString(rule,'action','Allow');
end;
CkJsonObject_UpdateString(network,'defaultAction','Deny');
request := CkJsonObject_Create();
properties := CkJsonObject_Create();
success := CkJsonObject_AppendObject2(request,'properties',properties);
if (success <> True) then
begin
Memo1.Lines.Add('Could not create properties.');
Exit;
end;
success := CkJsonObject_AppendObjectCopy(properties,'networkAcls',network);
if (success <> True) then
begin
Memo1.Lines.Add('Could not copy network rules.');
Exit;
end;
// Enabled + defaultAction Deny means selected public networks. Disabled would
// block public endpoint access; SecuredByPerimeter is a different policy mode.
CkJsonObject_UpdateString(properties,'publicNetworkAccess','Enabled');
applyChanges := False;
if (applyChanges <> True) then
begin
Memo1.Lines.Add('Network changes prepared, not applied. Review rules and set applyChanges to true.');
Exit;
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 := CkHttp_HttpJson(http,'PATCH',resourceUrl,request,'application/json',response);
if (CkHttpResponse_getStatusCode(response) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect LastErrorText privately; it may contain credentials.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(response) <> 200)) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(response,'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.
Exit;
end;
if (success <> True) then
begin
Memo1.Lines.Add('The HTTP response could not be read completely.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
Memo1.Lines.Add('Network policy update returned successfully. Verify access from intended clients.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkUrl_Dispose(parsed);
CkJsonObject_Dispose(network);
CkJsonArray_Dispose(ipRules);
CkJsonObject_Dispose(rule);
CkJsonArray_Dispose(subnets);
CkJsonObject_Dispose(request);
CkJsonObject_Dispose(properties);