使用托管标识时,将事件从 APIM 发送到事件中心会导致未经授权

Sending events to event hub from APIM result in unauthorized when using managed identity

我正在尝试设置一个 APIM 端点来将消息发送到事件中心。我还想使用托管标识来向事件中心授权 APIM。请注意,所有资源都位于同一个订阅中。设置如下:

操作策略如下所示:

<policies>
    <inbound>
        <base />
        <authentication-managed-identity resource="https://eventhubs.azure.net" output-token-variable-name="msi-access-token" ignore-error="false" />
        <set-header name="Authorization" exists-action="override">
            <value>@(String.Concat("Bearer ",(string)context.Variables["msi-access-token"]))</value>
        </set-header>
        <set-body>{ "Event":"apim-using -aad token", "TrustedService":"AAD" }</set-body>
        <set-backend-service base-url="https://[someeventhub].servicebus.windows.net" />
        <rewrite-uri template="/input/messages?api-version=2014-01" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

每当我尝试测试该操作时,我都会收到 401 Unauthorized。

HTTP/1.1 401 SubCode=40100: Unauthorized : Unauthorized access for 'Send' operation on endpoint 'sb://[someeventhub].servicebus.windows.net/input/messages?api-version=2014-01'. Tracking Id: [X]

查看跟踪,身份验证请求看起来正常:

authentication-managed-identity (0.365 ms)
{
    "message": "Obtaining managed identity token using clientId:[X] AAD Authority:https://login.windows.net/[A] for https://eventhubs.azure.net audience succeeded.",
    "errorResponse": null
}

我是不是漏掉了什么?在我看来,应用程序注册可能会有一些东西?我不明白为什么 - 该应用程序已经拥有订阅的贡献者权利。还需要别的吗?

最后一点,转发请求如下所示:

forward-request (0.129 ms)
    {
    "message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
    "request": {
        "method": "POST",
        "url": "https://[someeventhub].servicebus.windows.net/input/messages?api-version=2014-01",
        "headers": [
            {
                // A bunch of headers
            },
            {
                "name": "Authorization",
                "value": "Bearer [A VALID JWT TOKEN]"
            }
        ]
    }
}

the app already has contributor rights for the subscription. Does it need anything else?

是; “贡献者”角色授予应用程序访问 Azure 资源管理平面的权限,以执行创建新事件中心等操作,但不授予数据平面的访问权限。

该应用需要具有“事件中心数据发送者”或“事件中心数据所有者”角色才能发布事件。 (有关更多上下文,请参阅:Authorize access to Event Hubs resources using Azure Active Directory