【问题标题】:ARM template for Event Grid API Connection with managed identity带有托管标识的事件网格 API 连接的 ARM 模板
【发布时间】:2021-01-27 09:11:30
【问题描述】:

从逻辑应用创建新的事件网格连接时,可以从以下 3 种连接身份验证方法中进行选择:

  1. 登录
  2. 服务主体
  3. 托管身份

#1 登录要求用户以交互方式登录/验证。

#2 服务主体 需要提供 TenantClient IDClient Secret 值。

很明显,需要如何修改此类 API 连接的 ARM 模板:parameterValues 需要添加如下。

"parameterValues": {
  "token:clientId": "[parameters('ConnectionClientId')]",
  "token:clientSecret": "[parameters('ConnectionClientSecret')]",
  "token:TenantId": "[parameters('ConnectionTenantId')]",
  "token:resourceUri": "https://management.core.windows.net/",
  "token:grantType": "client_credentials"
}

#3 托管标识只需要选择托管标识。虽然很清楚如何以交互方式创建此类 API 连接,但我找不到有关此类身份验证方法的 ARM 模板格式的任何信息。

所以问题是 - 带有 (update: user assigned) 托管标识的事件网格连接的 ARM 模板应该是什么样子?这样创建的 API 连接如下所示:

更新:我需要在我的逻辑应用中使用用户分配托管身份。下面提供的答案适用于系统分配 托管身份,但不适用于用户分配 身份。如果有人可以为使用 用户分配 托管身份的 API 连接提供 ARM 模板,我们将不胜感激。

【问题讨论】:

    标签: azure azure-logic-apps azure-resource-manager azure-eventgrid


    【解决方案1】:

    由于您可以拥有多个用户托管身份,因此仅选择 ManagedServiceIdentity 是不够的。相反,您必须包含您希望使用的身份 ID。

    扩展@jim-xu 的回答:

    连接示例:

        {
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "name": "[variables('eventApiConnectionName')]",
            "location": "[resourceGroup().location]",
            "kind": "V1",
            "tags": "[parameters('resourceTags')]",
            "properties": {
                "displayName": "[variables('eventApiConnectionName')]",
                "customParameterValues": {},
                "api": {
                    "id": "[subscriptionResourceId('Microsoft.Web/locations/managedApis', resourceGroup().location, 'azureeventgrid')]"
                },
                "parameterValueType": "Alternative"
            }
        }
    

    这里的参数ValueType是一个重要的设置。如MicroSoft documnetation中所述:

    如果使用 ARM 模板自动部署,并且逻辑应用工作流包含使用托管标识的托管连接器触发器或操作,请确认基础连接资源定义包含 parameterValueType 属性,其中 Alternative 作为属性值。否则,您的 ARM 部署将不会设置连接以使用托管标识进行身份验证...

    然后逻辑应用引用该连接,并将身份包含为 resourceId:

    "$connections": {
                        "value": {
                            "azureeventgrid": {
                                "connectionId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Web/connections/', variables('eventApiConnectionName'))]",
                                "connectionName": "[variables('eventApiConnectionName')]",
                                "connectionProperties": {
                                    "authentication": {
                                        "type": "ManagedServiceIdentity",
                                        "identity": "[parameters('userManagedIdentity')]"
                                    }
                                },
                                "id": "[concat('/subscriptions/',subscription().subscriptionId, '/providers/Microsoft.Web/locations/', resourceGroup().location, '/managedApis/azureeventgrid')]"
                            }
                        }
                    }
    

    注意在 eventgrid 连接的 authentication 部分中添加了 identity 字段。

    有关这方面的更多信息,请参阅 Microsoft 文档:https://docs.microsoft.com/en-us/azure/logic-apps/create-managed-service-identity?tabs=consumption#create-user-assigned-identity-in-an-arm-template-consumption-only

    标识值应该是托管标识的 ID。您可以通过 Azure 门户查看托管标识的 JSON 视图来获取此信息。

    【讨论】:

    • 感谢您的意见,但它没有回答问题。问题不在于创建具有托管标识的逻辑应用。它是关于创建一个Event Grid API 连接,它将使用一个用户分配的托管标识
    • @10p 这就是我的意思,您需要将身份字段添加到上面的身份验证对象(这是一个事件网格 API 连接),然后它使用用户分配的托管身份。我经历过这种痛苦,这对我有用。 :)
    • Brian,如果这对您有用,您能否也将您正在使用的 API 连接 ARM 模板添加到答案中。我将尝试使用适合您的代码部署 API 连接和逻辑应用程序,并检查它是否也适合我。我问的原因是,对于现有的 API 连接,部署逻辑应用程序没有问题,它可以成功使用它。我需要能够同时部署两者。谢谢。
    • @10p 刚刚添加了一些与连接相关的信息,它是parameterValueType 属性。
    • 宾果游戏!在提出问题 10 个月后,谜团解开了? 在 API 连接模板中使用 "parameterValueType": "Alternative" 和在逻辑应用模板中使用 "authentication": { "type": "ManagedServiceIdentity", "identity": "[parameters('userManagedIdentity')]" } 就可以解决问题。谢谢!
    【解决方案2】:

    目前,答案似乎是,因为这仍处于预览阶段(afaik)

    要使用 ARM 模板创建托管 Identity api 连接,您需要包含“parameterValueType”:“Alternative”

    "properties": {
      "displayName": "ARM API connection",
      "customParameterValues": {},
      "parameterValueType": "Alternative",
      "api": {
        "id": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Web/locations/', resourceGroup().location, '/managedApis/arm')]"
      }
    }
    

    我没有找到有关此属性的文档。我发现的唯一原因是查看我使用门户创建的 api 连接的原始 json(json 视图)。

    【讨论】:

    • 谢谢@Bjorn,我已经尝试了你的建议。而不是'/managedApis/arm',我相信应该使用'/managedApis/azureeventgrid'。我看到的与标准模板不同的唯一一点是“parameterValueType”:“Alternative”。由于某种原因,部署开始并且永远不会完成。我等了一会儿,然后不得不取消它。另一件事,我希望在某处提供托管身份 ID。如果不清楚需要使用什么托管标识,我看不出部署如何成功。知道如何在模板中指定它吗?
    • 是的,应该是azureeventgrid。我自己还没有这些经验,但我认为这些连接器的工作方式相同。
    • 托管标识,如Jim Xu所说,通过在逻辑应用的arm模板中插入“identity”:{“type”:“SystemAssigned”}来提供。 Azure 会将适当的 id 插入到部署的模板中,因为它是一个托管标识。您可以查看门户中资源组中的部署。这可能会给你一些关于它为什么失败的线索。您也可以尝试手动删除您尝试部署的所有资源,看看是否有帮助。
    • API 连接必须在逻辑应用之前部署。当通过前端(从逻辑应用程序)创建 API 连接并转到其属性时,它显示“此连接只能与托管标识一起使用”并且您不能进行任何更改(参见屏幕截图)。托管身份详细信息已在此处预先配置。如果从 ARM 模板创建 API 连接并转到其属性,则它有一个“授权”按钮,可用于更新连接。在逻辑应用中拥有“身份”详细信息不会影响首先部署的 API 连接配置。
    • 不同的逻辑应用程序可以使用相同的事件网格 API 连接 - 当我手动创建 API 连接时,它对我来说非常有效。我在逻辑应用模板中有“"identity":{"type":"UserAssigned","userAssignedIdentities":{"...":{}}}”。尽管在逻辑应用中设置了用户分配的身份,API 连接模板还必须以某种方式指定托管身份。
    【解决方案3】:

    我有一个 ARM 模板,它将部署事件网格自定义主题和逻辑应用程序并使用托管标识连接订阅。

    ARM 模板是:

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workflows_lgeventgridtriggermaindev_name": {
            "type": "String"
        },
        "topics_eglogicappscratchtestdev_externalid": {
            "type": "String"
        },
        "topics_eglogicappscratchtestdev_name": {
            "type": "String"
        },
        "topics_eglogicappscratchtestdev_lgsubscriptionName": {
            "type": "String"
        },
        "LogicAppLocation": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "northeurope"
        },
        "azureeventgrid_1_Connection_Name": {
            "type": "string",
            "defaultValue": "azureeventgrid"
        },
        "azureeventgrid_1_Connection_DisplayName": {
            "type": "string",
            "defaultValue": "lgapiegscratch"
        }
    },
    "variables": {
        "targetLogicApp": {
            "triggerId": "[resourceId('Microsoft.Logic/workflows/triggers', parameters('workflows_lgeventgridtriggermaindev_name'), 'When_a_resource_event_occurs')]"
        }
    },
    "resources": [
        {
            "type": "Microsoft.EventGrid/topics",
            "apiVersion": "2021-06-01-preview",
            "name": "[parameters('topics_eglogicappscratchtestdev_name')]",
            "location": "uksouth",
            "sku": {
                "name": "Basic"
            },
            "kind": "Azure",
            "identity": {
                "type": "None"
            },
            "properties": {
                "inputSchema": "EventGridSchema",
                "publicNetworkAccess": "Enabled"
            }
        },
        {
            "type": "MICROSOFT.WEB/CONNECTIONS",
            "apiVersion": "2018-07-01-preview",
            "name": "[parameters('azureeventgrid_1_Connection_Name')]",
            "location": "[parameters('LogicAppLocation')]",
            "properties": {
                "api": {
                    "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureeventgrid')]"
                },
                "displayName": "[parameters('azureeventgrid_1_Connection_DisplayName')]",
                "parameterValueType": "Alternative"
            }
        },
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "name": "[parameters('workflows_lgeventgridtriggermaindev_name')]",
            "location": "[parameters('LogicAppLocation')]",
            "identity": {
                "type": "SystemAssigned"
            },
            "properties": {
                "state": "Enabled",
                "definition": {
                    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
                    "actions": {
                        "getTopicData": {
                            "type": "Compose",
                            "inputs": "@triggerBody()?['data']",
                            "runAfter": {}
                        }
                    },
                    "parameters": {
                        "$connections": {
                            "defaultValue": {},
                            "type": "Object"
                        }
                    },
                    "triggers": {
                        "When_a_resource_event_occurs": {
                            "type": "ApiConnectionWebhook",
                            "inputs": {
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['azureeventgrid']['connectionId']"
                                    }
                                },
                                "body": {
                                    "properties": {
                                        "topic": "[parameters('topics_eglogicappscratchtestdev_externalid')]",
                                        "destination": {
                                            "endpointType": "webhook",
                                            "properties": {
                                                "endpointUrl": "@{listCallbackUrl()}"
                                            }
                                        },
                                        "filter": {
                                            "includedEventTypes": [
                                                "TriggerLogicApp"
                                            ],
                                            "subjectBeginsWith": "Main"
                                        }
                                    }
                                },
                                "path": "[concat('/subscriptions/@{encodeURIComponent(''', subscription().subscriptionId, ''')}/providers/@{encodeURIComponent(''Microsoft.EventGrid.Topics'')}/resource/eventSubscriptions')]",
                                "queries": {
                                    "x-ms-api-version": "2017-06-15-preview"
                                }
                            },
                            "splitOn": "@triggerBody()"
                        }
                    },
                    "contentVersion": "1.0.0.0",
                    "outputs": {}
                },
                "parameters": {
                    "$connections": {
                        "value": {
                            "azureeventgrid": {
                                "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureeventgrid')]",
                                "connectionId": "[resourceId('Microsoft.Web/connections', parameters('azureeventgrid_1_Connection_Name'))]",
                                "connectionName": "[parameters('azureeventgrid_1_Connection_Name')]",
                                "connectionProperties": {
                                    "authentication": {
                                        "type": "ManagedServiceIdentity"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "tags": {
                "displayName": "LogicApp"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/connections', parameters('azureeventgrid_1_Connection_Name'))]",
                "[resourceId('Microsoft.EventGrid/topics', parameters('topics_eglogicappscratchtestdev_name'))]"
            ]
        },
        {
            "name": "[parameters('topics_eglogicappscratchtestdev_lgsubscriptionName')]",
            "scope": "[format('Microsoft.EventGrid/topics/{0}', parameters('topics_eglogicappscratchtestdev_name'))]",
            "type": "Microsoft.EventGrid/eventSubscriptions",
            "location": "[parameters('LogicAppLocation')]",
            "apiVersion": "2020-04-01-preview",
            "properties": {
                "destination": {
                    "endpointType": "WebHook",
                    "properties": {
                        "endpointUrl": "[listCallbackUrl(variables('TargetLogicApp').triggerId, '2019-05-01').value]"
                    }
                },
                "filter": {
                    "subjectBeginsWith": "Main",
                    "includedEventTypes": [
                        "TriggerLogicApp"
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/connections', parameters('azureeventgrid_1_Connection_Name'))]"
            ]
        }
    ]
    }
    

    一个示例参数文件是:

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "workflows_lgeventgridtriggermaindev_name": {
            "value": "lgeventgridtriggerscratch"
        },
        "topics_eglogicappscratchtestdev_externalid": {
            "value": "/subscriptions/<subscriptionid>/resourceGroups/<resourcegroupname>/providers/Microsoft.EventGrid/topics/eglogicappscratch"
        },
        "topics_eglogicappscratchtestdev_name": {
            "value": "eglogicappscratch"
        },
        "topics_eglogicappscratchtestdev_lgsubscriptionName": {
            "value": "lgeventgridtriggerscratchsub"
        },
        "LogicAppLocation": {
            "value": "uksouth"
        },
        "azureeventgrid_1_Connection_Name": {
            "value": "azureeventgrid"
        },
        "azureeventgrid_1_Connection_DisplayName": {
            "value": "lgapiegscratch"
        }
    }
    }
    

    当我将此模板加载到 Visual Studio 2019 逻辑应用设计器中时,我遇到了一个我在此处记录的问题:

    Visual Studio 2019 Logic Apps Designer removing code

    【讨论】:

    • 谢谢,我对其进行了测试,它确实适用于系统分配的身份。不幸的是,当逻辑应用使用用户分配的身份时,它不起作用。错误是 InvalidWorkflowManagedIdentitySpecified:“工作流 '...' 不包含 'SystemAssigned' 类型的托管标识。” :(
    【解决方案4】:

    如果您想使用托管标识创建事件网格 API 连接,请参考以下步骤

    1. 在 Azure Logic 应用中启用系统分配的标识
    {
       "apiVersion": "2016-06-01",
       "type": "Microsoft.logic/workflows",
       "name": "[variables('logicappName')]",
       "location": "[resourceGroup().location]",
       "identity": {
          "type": "SystemAssigned"
       },
       "properties": {
          "definition": {
             "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
             "actions": {},
             "parameters": {},
             "triggers": {},
             "contentVersion": "1.0.0.0",
             "outputs": {}
       },
       "parameters": {},
       "dependsOn": []
    }
    
    1. 授予对资源的身份访问权限
    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "resources": [
            {
                "type": "Microsoft.Authorization/roleAssignments",
                "apiVersion": "2018-09-01-preview",
                "name": "[guid(resourceGroup().id)]",
                "properties": {
                    "roleDefinitionId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]",
                    "principalId": "[reference(resourceId('Microsoft.Logic/workflows','<logic app name>'),'2016-06-01','Full').identity.principalId]"
                }
            }
        ]
    }
    
    1. 创建连接
    {
                "type": "Microsoft.Web/connections",
                "apiVersion": "2016-06-01",
                "name": "",
                "location": "",
                "kind": "V1",
                "properties": {
                    "displayName": "test",
                    "customParameterValues": {},
                    "api": {
                        "id": "/subscriptions/<>/providers/Microsoft.Web/locations/<>/managedApis/azureeventgrid"
                    }
                }
            }
    
    1. 创建触发器
    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json",
            "actions": {},
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_a_resource_event_occurs": {
                    "inputs": {
                        "body": {
                            "properties": {
                                "destination": {
                                    "endpointType": "webhook",
                                    "properties": {
                                        "endpointUrl": "@{listCallbackUrl()}"
                                    }
                                },
                                "topic": ""
                            }
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureeventgrid']['connectionId']"
                            }
                        },
                        "path": "/subscriptions/{Azure-subscription-ID}/providers/{}/resource/eventSubscriptions",
                        "queries": {
                            "x-ms-api-version": "2017-09-15-preview"
                        }
                    },
                    "splitOn": "@triggerBody()",
                    "type": "ApiConnectionWebhook"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "azureeventgrid": {
                        "connectionId": "/subscriptions/{Azure-subscription-ID}/resourceGroups/{resourcegroup}/providers/Microsoft.Web/connections/{connection-name}",
                        "connectionName": "{connection-name}",
                        "connectionProperties": {
                            "authentication": {
                                "type": "ManagedServiceIdentity"
                            }
                        },
                        "id": "/subscriptions/{Azure-subscription-ID}/providers/Microsoft.Web/locations/{Azure-region}/managedApis/azureeventgrid"
                    }
                }
            }
        }
    

    更多详情请参考

    https://docs.microsoft.com/en-us/azure/logic-apps/create-managed-service-identity

    【讨论】:

    • 这不会创建使用托管身份进行身份验证的 API 连接。第 3 步将创建一个仍需由用户手动验证的 API 连接。我已经有一个托管标识,并且可以使用此托管标识(用户分配,而不是系统分配)手动(来自逻辑应用设计器)成功创建事件网格 API 连接。请参阅我的问题中创建的 API 连接的屏幕截图。我需要能够通过 ARM 模板创建这样的 API 连接,而不是手动。
    • 有人知道如何使用托管身份验证创建连接吗?
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2018-02-26
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多