即使出现错误,Azure APIM 也会调用函数

Azure APIM calls function even though there was an error

我有一个 Azure APIM,它检查请求承载 JWT 令牌:

    <inbound>
        <base />
        <set-variable name="TenantId" value="
            @{
                var tenantId = context.Request.Headers.GetValueOrDefault("TenantId","");
         
                    if (tenantId != null) {
                        return tenantId;
                    }
                 
            return null;
    }" />
        <limit-concurrency key="@((string)context.Variables["TenantId"])" max-count="1">
            <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
            <set-header name="subscription-key" exists-action="delete" />
            <set-variable name="JWTIssuer" value="
    @{
        var authStr = context.Request.Headers.GetValueOrDefault("Authorization","");
        if (authStr.StartsWith("Bearer")) {
            var jwt = authStr.Substring("Bearer ".Length).AsJwt();
            if (jwt != null) {
                return jwt.Issuer;
            }
        }
        return null;
    }" />
            
            <choose>
                <when condition="@(context.Variables.GetValueOrDefault("JWTIssuer", "") == "<URL of issuer>")">
                    <!-- Authorisation using HPCA token -->
                    <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized" require-expiration-time="true" require-scheme="Bearer" require-signed-tokens="true">
                        <openid-config url="<Open Id Url>" />
                        <required-claims>
                            <claim name="MyClaim" match="any" separator=",">
                                <value>X</value>
                            </claim>
                        </required-claims>
                    </validate-jwt>
                </when>
                <!-- HPCA Error handling -->
                <otherwise>
                    <return-response>
                        <set-status code="401" reason="Unauthorized" />
                        <set-header name="WWW-Authenticate" exists-action="override">
                            <value>Bearer error "Invalid HPCA token"</value>
                        </set-header>
                    </return-response>
                </otherwise>
            </choose>            
        </limit-concurrency>
    </inbound>
<on-error>
        <base />
        <return-response>
            <set-status code="@(context.Response.StatusCode)" reason="@(context.Response.StatusReason)" />
        </return-response>
    </on-error>

这一切都按预期工作:如果发行者不正确或 JWT 令牌无效,则会调用 OnError 处理程序并将 401 错误状态 return 发送给调用者。问题是,如果我在选择策略之后添加对函数的调用:

     </otherwise>
                </choose>    
    <send-request mode="copy" response-variable-name="Response" timeout="10" ignore-error="false">
                    <set-url>https://myfuncApp.azurewebsites.net/api/Function1</set-url>
                    <set-method>POST</set-method>
                    
                </send-request>
                <return-response response-variable-name="Response">
                    <set-status code="@(context.Response.StatusCode)" reason="@(context.Response.StatusReason)" />
                </return-response>
</limit-concurrency>
</inbound>

然后即使持有者令牌无效并调用了错误处理程序,APIM 仍然 继续调用函数:错误处理程序中的 return 响应是似乎被忽略了——为什么?我可以通过将发送请求包装在另一个条件中(即检查 context.Response.StatusCode 不是 401)来捏造它,但这对我来说似乎不正确。

对于你的问题,我认为你的检测结果是正常的。

您添加了 <send-request> 以请求 <inbound> 区域中的功能。 <validate-jwt>只能判断APIM是否继续请求后端url,不能判断是否继续请求<inbound>区的函数url。