【问题标题】:How to create Azure Network Security Group via Python SDK如何通过 Python SDK 创建 Azure 网络安全组
【发布时间】:2021-01-19 04:00:59
【问题描述】:

我正在使用 Azure Python SDK 部署 Azure VM。我可以通过 Azure 门户创建具有网络安全组的 VM,而不会出现任何问题。但是,我无法使用以下 API 创建网络安全组:

async_nsg_create=network_client.network_security_groups.begin_create_or_update(
    GROUP_NAME,
    NSG_NAME,
    nsg_parameters
)

它总是抱怨我“无权执行操作'Microsoft.Network/networkSecurityGroups/write'”。 但是,我可以通过 Azure 门户创建网络安全组,方法是单击“创建资源”或在资源组中添加新源。我怀疑我可能必须通过 ResourceManagementClient 创建 NSG,但我在 API 文档中找不到任何有用的信息:https://docs.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.resourcemanagementclient?view=azure-python#models-api-version--2020-06-01--

我检查了这个问题中的解决方案:enter link description here,但在步骤:resource_client.providers.register('Microsoft.Compute') 失败,它抱怨:“没有执行操作'Microsoft.Compute/register/action'的授权”

【问题讨论】:

  • 脚本/代码正在使用哪些凭据?检查this example 以供参考。

标签: python azure


【解决方案1】:

该错误表示您的客户端没有执行操作的权限,您需要将其添加为资源组/订阅中的 RBAC 角色。

但是,我可以通过 Azure 门户创建网络安全组,方法是单击“创建资源”或在资源组中添加新源。

在门户中,您使用的是在门户中登录的帐户,如果您使用的是代码here,它使用的是服务主体的凭据,它是不同的。


这是一个完整的示例作品,您可以按照以下步骤操作。

1.Register an application with Azure AD and create a service principal.

2.Get values for signing increate a new application secret

3.导航到资源组或订阅 -> Access control (IAM) -> Add -> 将 AD App 的服务主体添加为 RBAC 角色,例如Contributor,详情关注this

4.然后使用下面的代码。

from azure.identity import ClientSecretCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_06_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2020_06_01.models import SecurityRule

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"
subscription_id = "<subscription-id>"

credential = ClientSecretCredential(tenant_id, client_id, client_secret)
network_client = NetworkManagementClient(credential, subscription_id)

resource_group_name = "<group-name>"
nsg_name = "testnsg"

nsg_params = NetworkSecurityGroup(id= "testnsg", location="UK South", tags={ "name" : "testnsg" })
nsg = network_client.network_security_groups.begin_create_or_update(resource_group_name, "testnsg", parameters=nsg_params)
print(nsg.result().as_dict())

5.签入门户:

更新:

如果要使用用户帐号,只需使用AzureCliCredential即可。

1.安装Azure CLI,然后在本地终端使用az login登录您的帐户,例如电源外壳。

2.登录后,修改如下代码并运行。

from azure.identity import ClientSecretCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_06_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2020_06_01.models import SecurityRule


subscription_id = "<subscription-id>"

credential = AzureCliCredential()
network_client = NetworkManagementClient(credential, subscription_id)

resource_group_name = "<group-name>"
nsg_name = "testnsg"

nsg_params = NetworkSecurityGroup(id= "testnsg", location="UK South", tags={ "name" : "testnsg" })
nsg = network_client.network_security_groups.begin_create_or_update(resource_group_name, "testnsg", parameters=nsg_params)
print(nsg.result().as_dict())

【讨论】:

  • 感谢您的具体回复。我认为我当前的问题在于:“订阅-> 访问控制 (IAM)-> 添加-> 将 AD 应用程序的服务主体添加为 RBAC”,我无法为我创建的应用程序分配权限。我明天会联系我的管理员。
  • 再次回到这个问题。我无法让我的服务主体获得额外的访问权限。我被告知使用我的登录帐户。您是否有任何方法可以通过使用登录帐户凭据来调用 API?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多