You can specify a regular expression (regex) to include multiple paths when you apply a policy to a subset of resources exposed by an API. The URI template regex field in the policy enables you to specify this expression.
When configuring URI template regex, you must consider the base path of the Mule runtime engine (Mule) application that implements (or proxies) the API. This is because Mule 4 resource-level policies depend on where an API is deployed.
This diagram displays the list of methods used to specify the resource subset to apply to a policy:
Imagine that this API specification is implemented in the following Mule application:
<api-gateway:autodiscovery apiId="1" flowRef="proxy"/>
<http:listener-config name="httpConfig" basePath="api">
<http:listener-connection host="localhost" port="80"/>
</http:listener-config>
<flow name="proxy">
<http:listener config-ref="httpConfig" path="v1/*" />
<logger />
</flow>
In this example, http:listener-config defines both a basePath attribute and an additional path, v1, before the trailing in the path v1/. The concatenation of these two paths is referred to as the base path, which is /api/v1.
Subsequently, if you want to protect resource-1 with a policy, you must use a resource-level policy. Therefore, the URI template regex that you specify when applying the policy must also include the base path in it:
The diagram illustrates how to configure a URI template regex:
If the API is of RAML or OAS type, the Preview Resource Matching option does not indicate that any resource is being matched, which differs from the actual runtime behavior.
You can also use a regex to avoid having to explicitly specify the base path, for example, .*/resource-1. However, if you have resources with the same name at different levels of the API definition, the regex will match all of them, resulting in issues. This is because the regex will match with both /api/v1/resource-1, and /api/v1/resources/resource-1, which makes it an indecisive solution.