fb
info@yenlo.com
eng
Menu
WSO2 Tutorial 7 min

Keycloak as a third-party Key Manager for WSO2 API Manager

Sajith Abeywardhana
Sajith Abeywardhana
Integration / DevOps Consultant
yenlo blog 2020 09 17 keycloak 3rd party key manager wso2 apim 600x350

Keycloak as a third-party Key Manager for WSO2 API Manager | Yenlo blog Enterprises which are in requirement for an API Management (APIM) solution may already have their own identity and access management (IAM) product in place. In such situations they need to reuse their existing IAM solution and integrate it with the new APIM solution. Keycloak is a such open source identity and access management product.

The WSO2 API Manager has a capability to integrate with a third party IAM product to handle the clients, security and OAuth tokens. WSO2 API Manager has five main components as Publisher, Developer portal, Key manager, Gateway and Traffic manager.

The API developer portal makes the request to the key manager to create / edit / delete an OAuth application, generate the consumer key and secret, and generate the application token.

The API gateway makes the request to the key manager to validate the application token. These validations may include subscription validation and scope validation as well.

In this blog I discuss how to integrate Keycloak with WSO2 API Manager to provide the key management features as a third party key manager. We will be using the Keycloak 10.0.1 version and WSO2 APIM 2.6.0 version.

Key Manager extension point in WSO2 APIM

The Key Manager interface facilitates the bridging between the OAuth provider and WSO2 API Manager by providing many methods to implement according to the third-party Key Manager API specification to build third-party key manager extension. This extension needs to be built as a JAR file so it can be deployed as a library inside the APIM by changing the api-manager.xml allows the extension point.

The API Manager provides some generic method implementation via the AbstractKeyManager abstract class, so we need to extend the AbstractKeyManager class in our implementation.

yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-1

As you can see in the above diagram there are many methods in this interface. We have to override and implement at least the below set of methods in our KeyManager implementation in order to integrate with Keycloak. We should refer the Keycloak API documentation when we are implementing each method.

For example, when API Developer portal is calling the createApplication method we have to call the corresponding API in the Keycloak to create an OAuth application in Keycloak side.

We have to make sure the return object from each method contains the proper data otherwise API Developer portal UI will brake while rendering the information.

Table 1
METHOD DESCRIPTION
createApplication Creates a new OAuth application in the Authorization Server.
updateApplication Update the OAuth application created in above method.
deleteApplication Delete the OAuth application.
retrieveApplication Retrieving the OAuth application details.
getNewApplicationAccessToken Generating a new access token to be shown in the API developer portal after clicking ā€˜Generate Keys’ and ā€˜Regenerate Keys’.
getTokenMetadata Gets details about an access token. This method will be invoked during the token validation phase upon an API invocation.

Keycloak configuration

Keycloak provides a REST APIs set for developers to interact with. These APIs are secured with OAuth 2.0, so before you call an API, first you need the access token. To get it, we should have a realm, client and user account with relevant permission in Keycloak side. In this section we will discuss how we can register those in Keycloak.

Prerequisite

Download the Keycloak 10.0.1 version, extract it and run the product.

yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-2

Create an admin account.

yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-3

Create a new realm

  1. Login to the admin console by typing the http://127.0.0.1:8080/auth/admin in the address bar.
  2. Click on the Add realm button.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-4
  3. Provide the realm name as ā€œapimā€ and hit the ā€œCreateā€ button.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-5
    You will be redirected to the realm setting page.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-6

Create a client

  1. From the left menu click on the ā€œClientsā€.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-7
  2. Now click on the ā€œCreateā€ button.
    Enter the ā€œClient IDā€ as ā€œapim-clientā€ and hit the ā€œSaveā€ button.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-8
    You will be redirected to the client settings page.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-9

Create a user and grant permission

  1. Click the ā€œUsersā€ on left menu and click on the ā€œAdd userā€.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-10
  2. Create a user with the name ā€œapim-userā€.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-11
  3. Set a password to the created user using the ā€œCredentialsā€ tab in the user settings page.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-12
    Make sure to turn off the ā€œTemporaryā€ toggle in this window.
  4. Now click on the ā€œRole Mappingā€ tab to assign the role to our user.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-13
  5. Select the ā€œRealm Managementā€ from ā€œClient Rolesā€ drop down and select all roles from the ā€œAvailable Rolesā€ box. Add the selected roles by clicking ā€œAdd Selectedā€ button.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-14

Now we have done all the configurations in the Keycloak side. Next step is to configure the API Manager. The API Manager will require the above details to integrate with Keycloak and below is the summary:

Realm: apim
Client: apim-client
Username: apim-user
User password: apimuser.123

Keycloak-WSO2 APIM integration java extension

As mentioned above, we have to implement the Java code and deploy it as a library to integrate API Manager with Keycloak. In this section we will discuss how we can implement the code and how to deploy it in the API Manager.

Keycloak key manager java code

As discussed above, we have to implement Java code at least for the methods mentioned in the ā€œtable 1ā€.

You can find the sample Keycloak integration Java code implementation on GitHub. With your business use case and with your requirement, you may change the implementation accordingly.

Clone the repository and build the code using Maven.

yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-15

yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-16

WSO2 API Manager configuration

    1. Coping the JAR file to lib
      We have to copy the JAR file created above to the API Manager lib directory.
      yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-17
    2. Keycloak configuration in api-manager.xml
      Java extension refers some configuration parameters from the api-manager.xml file. So those need to be defined in the xml file. Below table contains these parameters.
keycloakInstanceUrl URL of the Keycloak server. http://127.0.0.1:8080
keycloakRealmName Realm name provided in above Keycloak configuration apim
client_id Client_id from client creation in Keycloak apim-client
username Used username in above when creating a user in Keycloak apim-user
password Password we have given in above when creating a user in Keycloak apimuser.123

In the api-manager.xml file, the configuration should be done under the element <APIKeyManager></APIKeyManager>.

<APIKeyManager
Ā  Ā <KeyManagerClientImpl>org.wso2.keycloak.client.KeycloakClient</KeyManager ClientImpl>
Ā  Ā <Configuration>
Ā  Ā  Ā  <keycloakInstanceUrl>http://localhost:8080</keycloakInstanceUrl>
Ā  Ā  Ā  <keycloakRealmName>apim</keycloakRealmName>
Ā  Ā  Ā  <client_id>apim-client</client_id>

Ā  Ā  Ā  <username>apim-user</username>
Ā  Ā  Ā  <password>apimuser.123</password>
Ā  Ā </Configuration>
</APIKeyManager>
  1. The API Manager should retrieve the token from Keycloak and validate it as per our implementation. In the API Manager there is a synapse configuration file which defined the token endpoint. The endpoint is, by default distribution, configured to the local key manager server. And as we are integrating a third-party key manager, we have to change this synapse API endpoint to Keycloak server token endpoint.Keycloak server token endpoint: http://localhost:8080/auth/realms/apim/protocol/openid-connect/token

    Note: In the Keycloak server token endpoint URL, ā€œ/apim/ā€ context path is a realm name. You may need to replace it with your appropriate realm name. Keycloak documentation reference for theĀ token endpoint.

    Open the _TokenAPI_.xml file located in the {APIM_Home}/repository/deployment/server/synapse-configs/default/api/ and update the endpoint to above URL.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-18

Now our setup is completed in terms of the configuration points. Restart / start the Keycloak and the WSO2 API Manager using the startup scripts which are located in the bin directory.

Testing the integration

  1. Deploy the sample API using the API publisher. After that you would be able to view your sample API, PizzaShackAPI , in API store.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-19
  2. Create a new OAuth application named as ā€œKeycloakAppā€ in the API store.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-20
  3. Go to the create application page and generate the keys. Enter the callback URL as https://localhost.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-21
  4. Refresh the page so you will be able to see the client application key and the secret. Now our OAuth client is registered in the Keycloak and the client key and secret are generated from the Keycloak end.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-22
  5. To generate the token, you may use the curl or web interface. Here I’m just using the web interface ā€œRegenerateā€ button. Once the token is generated copy the token from here to the other location.
    yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-23
  6. Go to the API and subscribe to the ā€œKeycloakAppā€ application.
  7. Now we have a valid access token as well as the application subscription so we would be able to call our API.
yenlo_blog_2020-09-17_keycloak-key-manager_wso2-apim_figure-24

Yenlo is the leading, global, multi-technology integration specialist in the field of API-management, Integration technology and Identity Management. Known for our strong focus on best-of-breed hybrid and cloud-based iPaaS technologies. Yenlo is the product leader and multi-award winner in WSO2, Boomi, MuleSoft and Microsoft Azure technologies and offers best-of-breed solutions from multiple leading integration vendors.

With over 240+ experts in the API, integration, and Identity Access Management domain and over $35 million in annual revenue, Yenlo is one of the largest and best API-first and Cloud-first integration specialists worldwide.

Whitepaper:
Integration solutions with WSO2 – Brochure

Vendor WSO2
Get it now
eng
Close
What's on our menu