0

我正在尝试使用 Pulumi 设置 Azure 基础架构。到目前为止,我已经使用pulumi preview了命令,似乎大多数事情都已经到位。

我已经能够整理出大多数事情,但不能整理出与 Azure AD 相关的部分。

例如,下面的代码有效:

var group = Output.Create(
    GetGroup.InvokeAsync(
        new GetGroupArgs
        {
            Name = "Administrators 2"
        }));
var tenantId = config.Apply(c => c.TenantId);
var keyVault = new KeyVault(
    name,
    new KeyVaultArgs
    {
        ResourceGroupName = resourceGroup.Name,
        EnabledForDeployment = true,
        EnabledForTemplateDeployment = true,
        PurgeProtectionEnabled = true,
        SkuName = "standard",
        TenantId = tenantId,
        AccessPolicies = new KeyVaultAccessPolicyArgs[]
        {
            // <------------------------ Nothing here and it works.
        }
    });

但是下面的代码不起作用。

var group = Output.Create(
    GetGroup.InvokeAsync(
        new GetGroupArgs
        {
            Name = "Administrators 2"
        }));
var tenantId = config.Apply(c => c.TenantId);
var keyVault = new KeyVault(
    name,
    new KeyVaultArgs
    {
        ResourceGroupName = resourceGroup.Name,
        EnabledForDeployment = true,
        EnabledForTemplateDeployment = true,
        PurgeProtectionEnabled = true,
        SkuName = "standard",
        TenantId = tenantId,
        AccessPolicies = new KeyVaultAccessPolicyArgs[]
        {
            new KeyVaultAccessPolicyArgs // <--- When I add this, it stops working.
            {
                SecretPermissions = new[] { "get", "list" },
                ObjectId = group.Apply(g => g.ObjectId),
                TenantId = tenantId
            }
        }
    });

错误

错误:获取经过身份验证的对象 ID 时出错:从 Azure CLI 解析 json 结果时出错:等待 Azure CLI 时出错:退出状态 2

什么json?...我怀疑服务主体没有足够的权限来设置这些东西。

使用 --debug 标志

debug: Serialize property[resource:fe-modules-kv-[azure:keyvault/keyVault:KeyVault].accessPolicies.id[0].objectId]: Recursing into Output
error: Running program 'C:\dev\...........\bin\Debug\netcoreapp3.1\Frontend.dll' failed with an unhandled exception:
    Grpc.Core.RpcException: Status(StatusCode=Unknown, Detail="invocation of azuread:index/getGroup:getGroup returned an error: Error getting authenticated object ID: Error parsing json result from the Azure CLI: Error waiting for the Azure CLI: exit status 2")
       at Pulumi.GrpcMonitor.InvokeAsync(InvokeRequest request)
       at Pulumi.Deployment.InvokeAsync[T](String token, InvokeArgs args, InvokeOptions options, Boolean convertResult)
       at Pulumi.Output`1.ApplyHelperAsync[U](Task`1 dataTask, Func`2 func)
       at Pulumi.Output`1.Pulumi.IOutput.GetDataAsync()
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Serialization.Serializer.SerializeDictionaryAsync(String ctx, IDictionary dictionary)
       at Pulumi.Serialization.Serializer.SerializeInputArgsAsync(String ctx, InputArgs args)
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Serialization.Serializer.SerializeListAsync(String ctx, IList list)
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Serialization.Serializer.SerializeAsync(String ctx, Object prop)
       at Pulumi.Deployment.SerializeFilteredPropertiesAsync(String label, IDictionary`2 args, Predicate`1 acceptKey)
       at Pulumi.Deployment.PrepareResourceAsync(String label, Resource res, Boolean custom, ResourceArgs args, ResourceOptions options)
       at Pulumi.Deployment.RegisterResourceAsync(Resource resource, ResourceArgs args, ResourceOptions options)
       at Pulumi.Deployment.ReadOrRegisterResourceAsync(Resource resource, ResourceArgs args, ResourceOptions options)
       at Pulumi.Deployment.CompleteResourceAsync(Resource resource, ResourceArgs args, ResourceOptions options, ImmutableDictionary`2 completionSources)
       at Pulumi.Output`1.GetValueAsync()
       at Pulumi.Deployment.Logger.TryGetResourceUrnAsync(Resource resource)
       at Pulumi.Deployment.Runner.WhileRunningAsync()

部分debug: Serialize property[resource:fe-modules-kv-[azure:keyvault/keyVault:KeyVault].accessPolicies.id[0].objectId]: Recursing into Output告诉我们问题不在于租户,但我还没有掌握足够的知识。

我对此进行了很多搜索,并确保我的服务主体具有必要的角色,能够通过为其分配角色User Account AdministratorCompany Administrator. 下面的脚本就是这样做的,我在 Azure 门户中确认了这一点。

Connect-AzureAD -TenantId "0000000000000000000"
$userAccountAdministratorRoleName = "User Account Administrator"
$companyAdministratorRoleName = "Company Administrator"

$userAccountAdministratorRole = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $userAccountAdministratorRoleName }

if ($userAccountAdministratorRole -eq $null) {
    # Instantiate an instance of the role template
    $roleTemplate = Get-AzureADDirectoryRoleTemplate | Where-Object { $_.displayName -eq $userAccountAdministratorRoleName }
    Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId

    # Fetch User Account Administrator role instance again
    $userAccountAdministratorRole = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $userAccountAdministratorRoleName }
}

$companyAdministratorRole = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $companyAdministratorRoleName }

if ($companyAdministratorRole -eq $null) {
    # Instantiate an instance of the role template
    $roleTemplate = Get-AzureADDirectoryRoleTemplate | Where-Object { $_.displayName -eq $companyAdministratorRoleName }
    Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId

    # Fetch User Account Administrator role instance again
    $companyAdministratorRole = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $companyAdministratorRoleName }
}

Write-Host "User account administrator role: $userAccountAdministratorRole"
Write-Host "Company administrator role: $companyAdministratorRole"

$sp = Get-AzureADServicePrincipal -All $true | Where-Object { $_.displayName -eq 'Pulumi' }

Add-AzureADDirectoryRoleMember -ObjectId $userAccountAdministratorRole.ObjectId -RefObjectId $sp.ObjectId
Add-AzureADDirectoryRoleMember -ObjectId $companyAdministratorRole.ObjectId -RefObjectId $sp.ObjectId

我还缺少什么?或者我在哪里可以获得更多信息?

谁在运行部署?

它是服务主体。根据 Pulumi.yml 文件

config:
  azure:clientId: 0000000000
  azure:clientSecret:
    secure: 000000000000
  azure:location: WestEurope
  azure:subscriptionId: 0000000000
  azure:tenantId: 000000000000000000000000

下面的调试代码展示了这一点。

 debug: Invoke RPC prepared: token=azuread:index/getServicePrincipal:getServicePrincipal, obj={ "objectId": "000000-0000-0000-0000-0000000000" }
    debug: 2020/06/06 17:24:31 Testing if Service Principal / Client Certificate is applicable for Authentication..
    debug: 2020/06/06 17:24:31 Testing if Multi Tenant Service Principal / Client Secret is applicable for Authentication..
    debug: 2020/06/06 17:24:31 Testing if Service Principal / Client Secret is applicable for Authentication..
    debug: 2020/06/06 17:24:31 Using Service Principal / Client Secret for Authentication
    debug: 2020/06/06 17:24:31 Getting OAuth config for endpoint https://login.microsoftonline.com/ with  tenant 000000-877e-440f-b0ba-0000000

谁在运行部署(第 2 部分)?

实际上,在几百行之后,我找到了以下内容。

    terraform-provider-azurerm/dev pid-222c6c49-1b0a-5959-a213-6608f9eb8820
    debug: AzureRM Request:
    debug: POST /335f20ab-877e-440f-b0ba-d7eabfa258e4/oauth2/token?api-version=1.0 HTTP/1.1
    debug: Host: login.microsoftonline.com
    debug: User-Agent: Go/go1.14.1 (amd64-windows) go-autorest/adal/v1.0.0
    debug: Content-Length: 174
    debug: Content-Type: application/x-www-form-urlencoded
    debug: Accept-Encoding: gzip
    debug:
    debug: client_id=346ead82-d584-4427-b2fa-e54b94ed10cf&client_secret=[secret]&grant_type=client_credentials&resource=https%3A%2F%2Fmanagement.azure.com%2F
    debug: 2020/06/08 08:39:56 Testing if Service Principal / Client Certificate is applicable for Authentication..
    debug: 2020/06/08 08:39:56 Testing if Multi Tenant Service Principal / Client Secret is applicable for Authentication..

    # *** Testing if Service Principal / Client secret
    debug: 2020/06/08 08:39:56 Testing if Service Principal / Client Secret is applicable for Authentication..

    # *** And then it tests Managed Service Identity is applicable? Why?
    debug: 2020/06/08 08:39:56 Testing if Managed Service Identity is applicable for Authentication..

    debug: 2020/06/08 08:39:56 Testing if Obtaining a token from the Azure CLI is applicable for Authentication..
    debug: 2020/06/08 08:39:56 Using Obtaining a token from the Azure CLI for Authentication
    debug: AzureRM Response for https://login.microsoftonline.com/335f20ab-877e-440f-b0ba-d7eabfa258e4/oauth2/token?api-version=1.0:
4

1 回答 1

0

如果我们有更好的质量反馈而不是:

获取经过身份验证的对象 ID 时出错:解析来自 Azure CLI 的 json 结果时出错

但是,我发现了问题。库Pulumi.AzureAD期望服务主体凭据是环境变量。

我使用命令在配置中设置了它们,pulumi config set但这还不够。

这是你需要做的。Pulumi 不会或不能自动设置它,Terraform 需要这些环境变量。

$env:ARM_CLIENT_ID="000000000000000000"
$env:ARM_CLIENT_SECRET="00000000000000000"
$env:ARM_TENANT_ID="00000000000000000"
$env:ARM_SUBSCRIPTION_ID="000000000000000000000"
于 2020-06-09T09:14:32.750 回答