在 Azure 中反转 proxy/daemon

Reverse proxy/daemon in Azure

我已将节点红色实例设置为 Azure Web 应用程序,其唯一目的是创建 API Web 服务选择。目前,这是由简单有效的 Active Directory 保护的。但是,最终我们想让一些 API 公开可用(也许通过 Microsoft 帐户)。这意味着需要在某种程度上打开对网络应用程序的访问,这让我很紧张。

我想做的是拥有类似反向代理服务的东西,将调用转发到一组受限制的 URL 路径。此外,为了避免必须向非 AD 帐户提供 Web 应用程序访问权限,我希望代理上的守护程序服务代表服务帐户进行调用。

我希望这样的功能将成为 Azure API 管理的一部分,但据我所知,它只是将所有 API 调用转发到后端以在那里进行身份验证。

正在寻找有关如何实现上述目标的建议。

APIM 具有可用于执行各种操作的策略引擎。其中之一可能是调用第三方服务作为客户端请求处理的一部分。因此,您可以在请求处理管道内使用 SendRequest (https://docs.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#SendRequest) 策略来调用带有 clientId 和密码的 AAD 以获取 AAD 令牌。使用更多策略表达式从响应中提取它并将其附加到后端请求。

创建 2 个 AAD 应用程序:1 个用于 node-red 后端 Web 应用程序,1 个用于 APIM。后端的 App URI ID 需要采用以下格式: "https://****webappid****.azurewebsites.net/.auth/login/aad/callback"。

在 APIM 中,如下所示添加入站策略,插入与您的环境相关的值。

 <policies>
    <inbound>
        <!--          Authenticate as the "Azure API Management Service" app in AD and get token to authenticate with backend          -->
        <!--      Active Directory tenant      -->
        <set-variable name="tenantid" value="****your AD tenant id here****"/>
        <!--      Application id of APIM Service app in Active Directory      -->
        <set-variable name="clientid" value="*** your application id here****"/>
        <!--      Key from APIM Service app in Active Directory      -->
        <set-variable name="key" value="*** your application key here****"/>
        <!--      App ID UR for the backend app in Active Directory      -->
        <set-variable name="audience" value="https%3A%2F%2F****your backend web app id here****.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback"/>
        <send-request mode="new" response-variable-name="reply" timeout="10" ignore-error="false">
            <set-url>@("https://login.microsoftonline.com/"+context.Variables.GetValueOrDefault<string>("tenantid")+"/oauth2/token")</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@("grant_type=client_credentials&client_id="+context.Variables.GetValueOrDefault<string>("clientid")+"&client_secret="+context.Variables.GetValueOrDefault<string>("key")+"&resource="+context.Variables.GetValueOrDefault<string>("audience"))</set-body>
        </send-request>
        <!--          Extract token from reply          -->
        <set-variable name="accesstoken" value="@((String)((IResponse)context.Variables["reply"]).Body.As<JObject>()["access_token"])"/>
            <!--          Add authentication token to request          -->
            <set-header name="Authorization" exists-action="override">
                <value>@("Bearer " + context.Variables.GetValueOrDefault<string>("accesstoken"))</value>
            </set-header>
        </inbound>
        <backend>
            <forward-request/>
        </backend>
        <outbound/>
    </policies>