【问题标题】:How to get Azure App Service TLS configuration information using Azure .NET Fluent SDK?如何使用 Azure .NET Fluent SDK 获取 Azure App Service TLS 配置信息?
【发布时间】:2019-07-29 12:37:07
【问题描述】:

我正在尝试使用 C# 中的 Azure Fluent .NET SDK 在应用服务 (WebApp) 上配置 TLS 版本。

我使用以下代码通过 LINQPad 获取 WebApp 信息:

    // Nuget : Install-Package Microsoft.Azure.Management.AppService.Fluent -Version 1.24.0
    // using Microsoft.Azure.Management.AppService.Fluent;
    // using Microsoft.Azure.Management.AppService.Fluent.Models;
    // using Microsoft.Azure.Management.ResourceManager.Fluent;

    // Using a Service Principal created on my Azure Subscription
    var clientId = "<service_principal_clientid_with_readaccess>";
    var tenantId = "<my_azuread_tenantid>";

    var credentials = SdkContext
        .AzureCredentialsFactory
        .FromServicePrincipal(clientId, 
            Util.GetPassword("azure-cli-secret"), // LINQPad helper, replace if needed 
            tenantId, 
            AzureEnvironment.AzureGlobalCloud);

    // Get one web app with TLS 1.2 configured
    var webApp = AppServiceManager
        .Authenticate(credentials, "<azure_subscription_id>")
        .WebApps
        .GetById("<webapp_resource_id>");

    // Dump object returned, LINQPad helper method
    webApp.Dump();

当转储对象模型返回时,我在任何地方都找不到返回的 TLS 信息。应该是可用的,因为它是由 REST API 返回的。

分析

您可以使用https://resources.azure.com 或直接在此端点上查看信息:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config?api-version=2018-02-01

这将为您提供以下输出(此处缩短了属性列表):

{
  "value": [
    {
      "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config/web",
      "name": "{appServiceName}",
      "type": "Microsoft.Web/sites/config",
      "location": "West Europe",
      "properties": {
        "http20Enabled": false,
        "minTlsVersion": "1.2",
        "ftpsState": "AllAllowed",
        "reservedInstanceCount": 0,
        "preWarmedInstanceCount": null,
        "healthCheckPath": null
      }
    }
  ],
  "nextLink": null,
  "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config"
}

然后我查看了此处提供的 SDK 代码源:https://github.com/Azure/azure-libraries-for-net。我发现 TLS 版本应该在 SiteConfig 对象中可用。此 SiteConfig 似乎位于 Inner 属性内。

查看我们使用 LINQPad 获得的对象转储时,我们可以看到 SiteConfig 对象未填充:

在调试此代码时,使用断点,我们可以看到信息似乎在内部可用,但未映射到 Inner 属性内公开可用的 SiteConfig 属性。

我们看到非公共成员 _siteConfig 填充的调试会话屏幕截图:

这是一个错误还是我错过了什么?
有什么想法吗?

【问题讨论】:

    标签: azure azure-web-app-service azure-sdk-.net azure-fluent-api


    【解决方案1】:

    是的。如果使用这种方式,siteConfig 将始终返回 null。这是设计使然。如果要获取相关信息,可以使用以下方法:

                AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                clientId,
                clientSecret,
                tenantId,
                AzureEnvironment.AzureGlobalCloud).WithDefaultSubscription(subscriptionId);
    
            RestClient restClient = RestClient
                .Configure()
                .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .WithCredentials(credentials)
                .Build();
    
    
            ResourceManagementClient resourceManagementClient = new ResourceManagementClient(restClient);
            WebSiteManagementClient webSiteManagementClient = new WebSiteManagementClient(restClient);
            webSiteManagementClient.SubscriptionId = subscriptionId;
            var configs = webSiteManagementClient.WebApps.GetConfigurationAsync("<rg name>", "<app name>").Result;
            var minTls = configs.MinTlsVersion;
            Console.WriteLine(minTls);
    

    【讨论】:

    • 感谢您的回答,它正在工作!我的初衷是通过一次调用(使用 ListAsync)查询我们所有的订阅应用服务以获取它们的 TLS 配置。当我看到对象模型返回时,我很感兴趣,当我看到 Get 调用也没有返回信息时,我更加怀疑。我将不得不查询每个应用服务配置:(
    【解决方案2】:

    一旦这个 PR 被合并,MinTlsVersion 属性应该可以直接在webApp 上使用。 https://github.com/Azure/azure-libraries-for-net/pull/1023

    【讨论】:

      猜你喜欢
      • 2022-07-21
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多