Client ID enforcement on Mule 4 API

  • September 30, 2021

It’s important to secure your APIs and authenticate API users. When you use an API Manager to configure and apply policies to an API instance, you can apply a client ID enforcement policy to your application. This tech articles discusses that process in detail, including:

  • Designing API specifications in Design Center
  • Creating an API using API Manager
  • Implementing an application in Anypoint Studio
  • Adding the Autodiscover element
  • Deploying the application in Runtime Manager to activate API Autodiscovery
  • Applying a policy using API Manager
  • Requesting access to the API using Exchange
  • Calling an API with credentials

Designing API specifications in Design Center

  • Log in to your Anypoint platform account and click Start Designing.

Log in to Anypoint platform account and click Start Designing

  • Click Create New and select New API Spec.

Create New and select New API Spec

A new window will open, as shown below.

  • Name your API spec and click Create API specification. It will create a basic API structure.

Name your API spec and click Create API specification

Basic API structure is created

  • Write RAML based on your requirements.

Write RAML based on your requirements

Write RAML based on your requirements

  • Click Publish and select Publish to Exchange.

Click Publish and select Publish to Exchange

Publish to Exchange

  • Go to Exchange. You’ll see that a newly created API has been published.

Newly created API has been published

Newly created API has been published

  • Click /hello and Get. You’ll see clientid and clientsecret set as required to send the request.

Click /hello and Get

Creating an API using API Manager

  • Once the API is published, go to API Manager and click Manage API -> to manage the API from Exchange.

Manage the API from Exchange

  • Select the name of the API, and it will automatically add the following details. Check your Mule application, basic endpoint and Mule 4. Click Save.

Select the name of the API

Check the Mule application, basic endpoint and Mule 4

The API has now been created in the API Manager. But, as you can see below, the API status is inactive. The API ID is 17063791.

API has been created in the API Manager

Implementing an application in Anypoint Studio

  • Open Anypoint Studio and create a new Mule project by selecting file ->new ->mule project.

Open Anypoint Studio and create a new Mule project

  • Name your project and click + to import a published API — see below.

Import a published API

Add Dependencies to Project

  • Click Add Account and log in with your Anypoint username and password to where you published the API in the previous step.

Log in with Anypoint username and password

Your Anypoint account has been added.

  • Type name of your API and then select it. Click Add and Finish.

Anypoint account has been added

  • Click Finish.

Click Finish

You’ll see the new project is scaffolded as shown below. Depending on the resource/method, a pair number of flows will have been created. In this case, only one flow for the resource /hello with method “get” was created, as well as two expected flows, one of which is the API console flow and the other the API kit router flow.

Pair number of flows will have been created

API console flow and API kit router flow

  • Create a new configuration file by clicking file -> new ->mule configuration file — as shown below — and name it as “implementation.”

Create a new Mule configuration file

Name the new Mule configuration file as implementation

  • Create a flow with logger and a transform message, as shown below.

Create a flow with logger and a transform message

Create a flow with logger and a transform message

  • Add a flow reference in the get:\hello:demoapp-config flow of demo-app.xml as shown below. It will go to the implementation flow.

Add a flow reference

Adding the Autodiscover element

Now create one more Mule configuration file, called global (global.xml) to add all configuration elements in one file.

  • Go to global of that file and click Create. Select Autodiscovery.

Adding the Autodiscover element

  • Add the API ID you got when you created the API in API Manager, and the flow name will be the name of flow where your API kit router is located.

Add the API ID and the flow name

This will create an Autodiscovery configuration element, as shown below.

Autodiscovery configuration element will be created

To find the credentials for the environment where you’ll deploy this application, go to Access Management and click Environment. Then click Sandbox.

Go to Access Management and click Environment

It will show you the credentials for that environment. You’ll need these credentials when you deploy the application to CloudHub.

Deploy the application to CloudHub

Deploying the application in Runtime Manager to activate API Autodiscovery

Now your application is ready.

  • In Anypoint Studio, select file export. Then select the folder where you want the jar to be created and click Finish. This will create a deployable jar.

Select file export in Anypoint Studio

Create a deployable jar

  • Now go to Anypoint platform -> Runtime Manager. Name your app and select the jar created in the previous step. Add the following properties in the Properties tab and click Deploy.

    anypoint.platform.client_id=29eef98b74694cf6a7f471db9ec6bb29

    anypoint.platform.client_secret=895446726E7142609d26C43B9fFb589f
  • If you’re testing the above two properties locally, you’ll have to write two more properties in the configuration property file. Cloud-only credentials are required. The following two can be taken automatically at runtime:

    anypoint.platform.analytics_base_uri=https://analytics-ingest.anypoint.mulesoft.com/

    anypoint.platform.base_uri=https://anypoint.mulesoft.com/

Name your app and select the jar created

Add the properties in the Properties tab and click Deploy

  • Once it’s deployed, go to the API Manager and select that API instance. You’ll see the API status is now Active, as shown below.

API status is now Active

If you apply any policy to it from the API Manager, it will be used in the application deployed in the Runtime Manager because Autodiscovery has connected this API Manager instance to that deployed application.

Applying a policy using API Manager

  • Go to API Manager and click Policies.

Go to API Manager and click Policies

  • Click on Apply New Policy and select Client Id enforcement with the latest version.

Click on Apply New Policy and select Client Id enforcement

It will open the following window. You can pass credentials with HTTP Basic headers or with custom headers. Here, we’ve selected the default setting that is custom.

Pass credentials with HTTP Basic headers or with custom headers

You can either apply this client ID enforcement to all methods and resources or select specific methods and resources based on your needs.

  • Click Apply.

Apply the client ID enforcement and click Apply

Now, you’ll see that the client ID enforcement policy has been applied.

Client ID enforcement policy

If you click on the API specification snippet, you’ll see the following specification. If you click on raml 1.0, you’ll see the same code we added to our RAML.

Click on the API specification snippet

Requesting access to the API using Exchange

  • Go to Exchange and click the application name. In the right corner, you’ll see three dots. Click them and then click Request Access.

Requesting access to the API using Exchange

  • Select an API Instance that has the API ID of your application and create a new application to access this API.

Create a new application to access this API

  • Add the name of the application and any other details based on your requirements.

Add the name of the application

  • Click Create and request access. Doing so will give you credentials to access this API. Save these credentials, as you’ll need them to send a request to this API.

Click Create and request access

Calling an API with credentials

If you send a request to this API with the wrong credentials, it will give you a 401 Unauthorized error.

Calling an API with credentials

Sending a request with the credentials received in Requesting Access to the API using Exchange will be processed successfully with the correct credentials.

Requesting Access to the API using Exchange

— By Kishori Patil