Strengthening MuleSoft API Security with Client ID–Based Resource Restrictions
Introduction: Why Limit Client ID Access?
In today’s API-driven world, security and control are more important than ever. While MuleSoft’s built-in policies like Client ID Enforcement provide essential access control, they often fall short when you need fine-grained access restrictions — like limiting specific client applications to only certain API endpoints. This is a common need in multi-tenant architectures, regulated industries, or when exposing different parts of an API to different partners.
To meet this challenge, MuleSoft allows you to build custom API policies that extend its default behavior. In this guide, we’ll walk through the steps to create a custom policy that restricts access to specific endpoints based on the Client ID. This gives you more control over how your APIs are consumed — without duplicating APIs or writing additional backend logic.
1. Setting Up a Project with the Archetype
The first step in developing a custom policy is setting up a project with the required files. The easiest way to gather all necessary files is by using Maven's archetype. To do this, configure Maven’s settings.xml file as follows:
Maven settings.xml Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.xsd">
<servers>
<server>
<id>exchange-server</id>
<username>ANYPOINT PLATFORM USERNAME</username>
<password>ANYPOINT PLATFORM PASSWORD</password>
</server>
<server>
<id>Repository</id>
<username>NEXUS USERNAME</username>
<password>NEXUS PASSWORD</password>
</server>
</servers>
<profiles>
<profile>
<id>archetype-repository</id>
<repositories>
<repository>
<id>archetype</id>
<name>Mule Repository</name>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
Place the above XML configuration in your settings.xml file and replace the following placeholders:
Creating the Project:
Once Maven is configured, follow these steps to create the project:
mvn -Parchetype-repository archetype:generate -DarchetypeGroupId=org.mule.tools -DarchetypeArtifactId=api-gateway-custom-policy-archetype -DarchetypeVersion=1.1.0 -DgroupId=${orgId} -DartifactId=${policyName} -Dversion=1.0.0-SNAPSHOT -Dpackage=mule-policy
Replace the following placeholders:
4. Before finishing, Maven asks you to set up:
Configuring the Project:
After running the above command, the generated project directory will have a structure similar to:
custom-policy/
├── my-custom-policy.yaml
├── mule-artifact.json
├── pom.xml
└── src
└── main
└── mule
└── template.xml
#ClientIdPerResource.yaml file
#%Policy Definition 0.1
id: Client ID per resource
name: Client ID per resource
description: Restrict specific Client Applications to access specific resources. This policy must be used with the Client ID enforcement policy, as the Client ID/Secret will NOT be validated in this policy.
category: Custom
type: custom
resourceLevelSupported: true
encryptionSupported: false
standalone: true
supportedJavaVersions: ["1.8", "11","17"]
requiredCharacteristics: []
providedCharacteristics: []
configuration:
- propertyName: clientIdExpression
name: Client ID Expression
description: Mule Expression to be used to extract the Client ID from API requests
type: string
defaultValue: "attributes.headers['client_id']"
optional: false
sensitive: false
allowMultiple: false
- propertyName: clientIDs
name: Client IDs restricted
description: Comma separated list of restricted Client IDs
type: string
optional: false
sensitive: false
allowMultiple: true
defaultValue: []
Modify the Project Files:
Recommended by LinkedIn
Ensure that the Mule Maven Plugin is added to your pom.xml file. This is automatically done by the custom policy archetype. If not, add the following plugin section to your pom.xml:
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
</plugin>
From the command line, in your project directory, run the package phase to package the custom policy:
mvn clean package
Once the custom policy is packaged, upload it to Anypoint Exchange using the following command: mvn clean deploy
4. Applying the Custom Policy
Once the custom policy is uploaded, apply it to the API you want to protect. Follow the steps below:
5. Policy Order
Ensure that the custom policy is applied in the correct order as shown below. If the policies are not in the desired order, reorder them to match the required configuration.