【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)

简介: 【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)

问题描述

为Azure App Service添加访问限制,需要Python Azure SDK来实现的示例代码。

问题解答

查阅Azure App Service的官方资料,使用Python SDK有 azure-mgmt-web 包中的 WebSiteManagementClient 类可以对Azure App Service资源进行管理。

Access Restrictions属于App Service的配置项,所以可以通过 client类中的 web_apps.get_configuration 获取,及通过 web_apps.create_or_update_configuration 进行创建或修改。

get_configuration 方法的返回数据类型为 SiteConfig,其中包含了 ip_security_restrictions 属性值。而且它是一个List类型,其数据结构类型为 IpSecurityRestriction

 

综上所述,通过Python SDK获取App Service的Access Restrictions的示例代码如下:

import os
from azure.identity import ClientSecretCredential, AzureAuthorityHosts
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD
from azure.mgmt.web import WebSiteManagementClient
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
myapp_base_url = "https://management.chinacloudapi.cn"
credentials = ClientSecretCredential(
    client_id=os.environ['AZURE_CLIENT_ID'],
    client_secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant_id=os.environ['AZURE_TENANT_ID'],
    authority=AzureAuthorityHosts.AZURE_CHINA
)
def print_item(group):
    """Print some properties of an Azure model."""
    print("\tName: {}".format(group.name))
    print("\tId: {}".format(group.id))
    print("\tLocation: {}".format(group.location))
    print("\tTags: {}".format(group.tags))
    if hasattr(group, 'status'):
        print("\tStatus: {}".format(group.status))
    if hasattr(group, 'state'):  # Site
        print("\tStatus: {}".format(group.state))
    if hasattr(group, 'properties'):
        print_properties(group.properties)
    print("\n\n")
def print_properties(props):
    """Print some properties of a Site."""
    if props and props.provisioning_state:
        print("\tProperties:")
        print("\t\tProvisioning State: {}".format(props.provisioning_state))
def print_ipsecurityrestrictions(iprestrictions):
    """Print ip security restrictions of a Site."""
    for restrits in iprestrictions:
        print("\t for rule : {}".format(restrits.name))
        print("\t\t name : {}".format(restrits.name))
        print("\t\t ip_address : {}".format(restrits.ip_address))
        print("\t\t subnet_mask : {}".format(restrits.subnet_mask))
        print("\t\t action : {}".format(restrits.action))
        print("\t\t vnet_subnet_resource_id : {}".format(restrits.vnet_subnet_resource_id))
        print("\t\t vnet_traffic_tag : {}".format(restrits.vnet_traffic_tag))
        print("\t\t subnet_traffic_tag : {}".format(restrits.subnet_traffic_tag))
        print("\t\t tag : {}".format(restrits.tag))
        print("\t\t priority : {}".format(restrits.priority))
        print("\t\t description : {}".format(restrits.description))
        print("\n\n")
web_client = WebSiteManagementClient(credentials, subscription_id, base_url=myapp_base_url, credential_scopes=[
                                      AZURE_CHINA_CLOUD.endpoints.management + "/.default"])
resource_group_name = "app-rg"
for site in web_client.web_apps.list_by_resource_group(resource_group_name):
    print_item(site)
print("\n\n")
print("\nStart to get web app configs")
site_name = "testwebapp04"
configs = web_client.web_apps.get_configuration(resource_group_name, site_name)
print_ipsecurityrestrictions(configs.ip_security_restrictions)
#web_client.web_apps.create_or_update_configuration()

执行结果为:

 

 

附录一: 调试Ptyhon代码时,遇上了奇怪的AAD错误 azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope . . . / / / : a a a a a c c c d d e e e f g h h i i l l m m n n n n o p p s t t t t u u is not valid.

最后通过换一台执行代码的虚拟机后,同样的代码没有报错。所以最终定位到是本机环境中,给中azure identity 的包不一致导致。通过 pip -m list出两个环境不一样的module版本后,再本地重新安装azure identity 和 azure core 两个包后,问题消失!

azure-identity == 1.11.0
azure-core == 1.24.2 #1.25.1

 

参考资料

Manage Azure websites with Python : https://github.com/Azure-Samples/app-service-web-python-manage

 

目录
打赏
0
0
0
0
203
分享
相关文章
|
7天前
|
【Azure Container App】在消耗性的Container App Environmnet中无法查看当时正在使用多少CPU多少实例数的替代方案
在 Azure Container Apps 中使用 Consumption 消耗型环境时,无法通过门户查看当前核心 (CPU) 和实例使用情况。这是因为消耗型工作负载配置文件的设计所致。若需查看使用状态,可使用 az cli 命令 `az containerapp env list-usages` 获取详细信息,包括 Current Cores 数量。文档还提供了参考资料链接以帮助用户深入了解相关命令用法。
44 17
【Azure App Service】App Service 如何配置私网域名以及证书呢?
本文解答了关于 Azure App Service 如何配置私网域名及证书的问题。App Service 不支持私网域名,自定义域名需配置在公共 DNS 服务器上。文章引用官方文档详细说明了映射自定义 DNS 的步骤,并附带参考资料链接,帮助用户深入了解相关配置方法。
【Azure Storage Account】利用App Service作为反向代理后续 ---- 隐藏 SAS Token
本文介绍了如何通过App Service作为反向代理,将SAS Token追加到请求URL中,以避免直接在代码或配置文件中存储SAS Token带来的安全性和维护问题。具体步骤包括修改App Service的web.config Rewrite规则,将SAS Token添加到转发的URL中;并在.NET SDK中仅使用不包含SAS Token的Uri进行Blob操作。这样既提高了安全性,也简化了SAS Token的管理。
43 16
【Azure App Service】App Service 是否支持HostName SNI 证书?
App Service 支持 HostName SNI 证书。为自定义域名添加 SSL 证书时,可以选择 SNI SSL 和基于 IP 的 SSL。SNI SSL 允许多个 TLS/SSL 证书保护同一 IP 地址上的多个域,适用于大多数现代浏览器。配置方法是在添加自定义域名后,点击 Add binding 配置 SNI SSL。参考文档:[Azure 官方文档](https://docs.azure.cn/zh-cn/app-service/configure-ssl-bindings#add-the-binding)。
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
124 72
【01】对APP进行语言包功能开发-APP自动识别地区ip后分配对应的语言功能复杂吗?-成熟app项目语言包功能定制开发-前端以uniapp-基于vue.js后端以laravel基于php为例项目实战-优雅草卓伊凡
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
193 90
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
10天前
|
如何在苹果内购开发中获取App Store Connect API密钥-共享密钥理解内购安全-优雅草卓伊凡
如何在苹果内购开发中获取App Store Connect API密钥-共享密钥理解内购安全-优雅草卓伊凡
72 15
如何在苹果内购开发中获取App Store Connect API密钥-共享密钥理解内购安全-优雅草卓伊凡
布谷一对一直播源码开发:阿里云视频语音通话社交交友App的必备功能
在当今移动社交领域,一对一视频和语音通话功能已成为用户期待的基础配置。从熟人社交到陌生人交友,从专业咨询到情感陪伴,实时音视频互动能力直接决定了社交App的用户留存和市场竞争力。山东布谷科技将深入探讨一对一直播源码开发高质量一对一视频和语音通话功能的关键要素和技术实现方案。
布谷一对一直播源码开发:阿里云视频语音通话社交交友App的必备功能

热门文章

最新文章