【问题标题】:Accessing Azure Key Vault from ASP.NET Core application hosted in Azure Service Fabric从 Azure Service Fabric 中托管的 ASP.NET Core 应用程序访问 Azure Key Vault
【发布时间】:2017-09-24 22:04:34
【问题描述】:

在我当前的 ASP.NET Core 项目中,我使用带有 X509Certificate 的 Azure Active Directory 身份验证来访问 Key Vault。需要在计算机上安装证书以允许应用程序访问它并最终从 Key Vault 读取值。现在,我正在努力将此应用程序迁移到 Azure Service Fabric。我已将证书上传到 Key Vault,通过添加修改 ARM 模板:

"osProfile": {
    "secrets": [
        {
          "sourceVault": {
            "id": "{KeyVaultIdHere}"
          },
          "vaultCertificates": [
            {
              "certificateUrl": "{CertificateUrlHere}",
              "certificateStore": "My"
            }
          ]
        }
      ]
},

但是当我将我的应用程序部署到 Azure Service Fabric 时,它似乎无法访问证书。我是否正确理解当我使用此类 ARM 模板创建集群时,证书已安装在 LocalMachine\My Store 中?如果是,是否有可能,运行应用程序的操作系统用户无权访问证书的私钥?当我在本地计算机上运行集群时,我必须授予 ASF 本地集群用户读取私钥的特殊权限。也许同样需要为 Azure 上的 ASF 做同样的事情?怎么办?提前致谢。

【问题讨论】:

  • 我已从 ASF 集群远程访问虚拟机,结果发现此证书缺少私钥权限。但我不能手动设置它,因为“管理员”对此证书只有“读取”权限。出于这个原因,我将尝试修改 ARM 模板,以便它“解锁”运行 ASF 集群的 NETWORK_SERVICE 的私钥。
  • NETWORK_SERVICE 的 ACL 应该在配置证书时自动完成。访问 KeyVault 的代码是什么样的?也许您的方法默认查看cert:\currentuser\my,这对于SF当然不正确!
  • 我的方法肯定是查看localmachine/my store。当我运行 ASF 本地集群时,它运行良好。有趣的是,您提到的 NETWORK_SERVICE 应该有权访问使用 ARM 模板安装的此类证书的私钥。显然,它没有。只有System 可以完全访问它。还提到了Administrators 组,但只有Read 权限。

标签: azure-service-fabric azure-keyvault


【解决方案1】:

这是我的一个应用程序中的一个应用程序清单。

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="S-Innovations.ServiceFabric.GatewayApplicationType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="GatewayService_InstanceCount" DefaultValue="-1" />
    <Parameter Name="AzureADServicePrincipal" DefaultValue="" />
    <Parameter Name="TenantId" DefaultValue="" />
    <Parameter Name="ApplicationStorageAccountId" DefaultValue="" />
    <Parameter Name="AzureResourceManagerCertThumbrint" DefaultValue="C03BB5A6410741CDD2927B4FF88C3E67215A393B" />
    <Parameter Name="Azure.KeyVault.Uri" DefaultValue="https://earthml-core-k3ci.vault.azure.net/" />
    <Parameter Name="ASPNETCORE_ENVIRONMENT" DefaultValue="Development" />
  </Parameters>
  <!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion 
       should match the Name and Version attributes of the ServiceManifest element defined in the 
       ServiceManifest.xml file. -->
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="S-Innovations.ServiceFabric.GatewayServicePkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides>
      <ConfigOverride Name="Config">
        <Settings>
          <Section Name="AzureResourceManager">
            <Parameter Name="AzureADServicePrincipal" Value="[AzureADServicePrincipal]" IsEncrypted="true" />
            <Parameter Name="TenantId" Value="[TenantId]" />
            <Parameter Name="ApplicationStorageAccountId" Value="[ApplicationStorageAccountId]" />
            <Parameter Name="Azure.KeyVault.Uri" Value="[Azure.KeyVault.Uri]" />
          </Section>
        </Settings>
      </ConfigOverride>
    </ConfigOverrides>
    <EnvironmentOverrides CodePackageRef="Code">
      <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[ASPNETCORE_ENVIRONMENT]" />
    </EnvironmentOverrides>
    <Policies>
      <RunAsPolicy CodePackageRef="Code" UserRef="Admin" EntryPointType="All" />
    </Policies>
  </ServiceManifestImport>
  <DefaultServices>
    <!-- The section below creates instances of service types, when an instance of this 
         application type is created. You can also create one or more instances of service type using the 
         ServiceFabric PowerShell module.

         The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
    <Service Name="GatewayService">
      <StatelessService ServiceTypeName="GatewayServiceType" InstanceCount="[GatewayService_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
    <Service Name="GatewayServiceManagerActorService" GeneratedIdRef="ef5ab963-c061-486e-bb1c-84bf1c2fc7e1|Persisted">
      <StatefulService ServiceTypeName="GatewayServiceManagerActorServiceType">
        <UniformInt64Partition PartitionCount="2" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
      </StatefulService>
    </Service>
  </DefaultServices>
  <Principals>
    <Users>
      <User Name="Service1" AccountType="NetworkService" />
      <User Name="Admin">
        <MemberOf>
          <SystemGroup Name="Administrators" />
        </MemberOf>
      </User>
    </Users>
  </Principals>
  <Policies>
    <SecurityAccessPolicies>
      <SecurityAccessPolicy ResourceRef="MyCert" PrincipalRef="Service1" ResourceType="Certificate" />
    </SecurityAccessPolicies>
  </Policies>
  <Certificates>
    <SecretsCertificate X509FindValue="[AzureResourceManagerCertThumbrint]" Name="MyCert" />
  </Certificates>
</ApplicationManifest>

我一直在使用它,没有您提到应用程序无法访问证书的任何问题。也许这可以帮助您使您的手臂脚本更简单:)

【讨论】:

    【解决方案2】:

    好的,所以解决方案是修改 ARM 模板,使其可以访问 NETWORK SERVICE 用户的证书私钥。为此,需要编写适当的 powershell(如这里:https://social.technet.microsoft.com/Forums/windowsserver/en-US/1557e379-26a8-46d0-bf26-d32176395085/how-to-grant-permission-to-private-key-from-powershell?forum=winserverpowershell)并在 ARM 模板(virtualMachineProfile/extensionProfile/extensions)中附加 CustomScriptExtension。它只能在 ARM 部署期间完成,因为出于某种原因,Administrators 对通过 ARM 模板安装的证书只有 Read 访问权限。

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2020-01-04
      • 2022-09-28
      • 2022-12-22
      • 1970-01-01
      • 2021-07-22
      • 2018-05-22
      • 2020-06-02
      • 2020-12-23
      相关资源
      最近更新 更多