How to define the order of the rules in the autonomous Mule API gateway?

advertisements

I have found a lot of documentation about how to set the order in which the policies are applied to a proxy using API Manager but nothing about how to change it when you are using only the API gateway standalone. Any idea?


There is a more deterministic way of making sure in which order policies are applied than depending on the order of files on the file system.

If you are using online policies (ie policies that are defined on API Manager side), then you have to define the order there. If you rename an online policy, it simply will be removed in the next polling cycle by the runtime.

If you are using offline policies (ie policies that are not defined on API Manager side, and that you have to deploy manually to the policies folder), then you can define the order in which they will be applied by defining the order attribute in the policy tag. For example:

    <?xml version="1.0" encoding="UTF-8"?>
    <policy
      xmlns="http://www.mulesoft.org/schema/mule/policy"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mule="http://www.mulesoft.org/schema/mule/core"
      xmlns:api-platform-gw="http://www.mulesoft.org/schema/mule/api-platform-gw"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/policy http://www.mulesoft.org/schema/mule/policy/current/mule-policy.xsd
          http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
          http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/schema/mule/api-platform-gw/current/mule-api-platform-gw.xsd"
      online="false"
      id="12345"
      policyName="SimpleLogger"
      order="100">

      <before>
        <mule:logger level="INFO" message="A message" />
      </before>

      <pointcut>
            <api-platform-gw:api-pointcut apiName="your API name" apiVersion="your API version Name"/>
        </pointcut>
    </policy>

Take into account that even if you define the order, CORS and Throttling/RateLimit policies are always applied first, regardless of the order specified in those or other policies.

If two or more policies have the same order, then the runtime will decide in which order they will be applied after applying all the policies with lower order number defined.

Policies with no order specified are applied after all the policies in which order was specified are applied.

Best regards, Nahuel.