我正在使用 Azure 存储,它允许通过管理 Azure 门户中的“防火墙和虚拟网络”设置来控制访问。
有没有办法通过 API 来做同样的事情?我做了一些文档搜索,但找不到可以执行此操作的 API。
任何有关此问题的指针都会有所帮助。
如果您想通过API更新Azure存储防火墙,您需要使用以下Rest API来实现。欲了解更多详情,请参阅这里
PATCH https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2019-06-01
Authorization: Bearer <access token>
{
"properties": {
"networkAcls": {
"bypass": "string",
"virtualNetworkRules": [
{
"id": "string",
"action": "Allow",
"state": "string"
}
],
"ipRules": [
{
"value": "string",
"action": "Allow"
}
],
"defaultAction": "string"
}
}
}
例如
Contributor
角色分配给 spaz login
# create sp and assign Contributor to the sp at the subscription level
az ad sp create-for-rbac -n "your service principal name"
POST https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&scope=https://management.azure.com/.default
&client_id=<sp appId>
&client_secret=<sp password>
PATCH https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2019-06-01
Authorization: Bearer <access token>
{
"properties": {
"networkAcls": {
"bypass": "AzureServices",
"virtualNetworkRules": [],
"ipRules": [
{
"value": "167.220.255.0/24",
"action": "Allow"
}
],
"defaultAction": "Deny"
}
}
}
我使用 PowerShell 脚本来执行此操作
# Set resource group & storage account
$rg='my_resource_group'
$stor_acc='my_storage_account'
# Get current IP from Public API
$ipGetCommand = 'https://api.ipify.org'
$webclient = New-Object System.Net.WebClient
$public_ip = $webclient.DownloadString($ipGetCommand)
# Add Public IP to FW rules
Add-AzStorageAccountNetworkRule -ResourceGroupName $rg -AccountName $stor_acc -IPAddressOrRange $public_ip
显然,这是一个快速而肮脏的例子。您可能想要检查您是否获得了 IP 并检查 API 是否正在运行,但您已经了解了要点...并且不要忘记任何运行此操作的人仍然需要添加防火墙规则的权限。