Integration with Keycloak Auth Server
To enable authentication with Auth Provider in Drill4J, you can integrate it with Keycloak, an open-source identity and access management solution. This guide will walk you through the steps to set up Keycloak and configure Drill4J to use it for authentication.
Prerequisites#
- Keycloak 22.0.5. You can download it from the Keycloak website and follow the installation instructions.
- A running instance of Drill4J Backend & UI.
Step 1: Set Up Keycloak#
- Create a Realm: Log in to the Keycloak admin console and create a new realm for your Drill4J instance. You can use default realm "master" if preferred.
- Create a Client: Within the newly created realm, create a new client for Drill4J.
- Set the Client Type to
OpenID Connect. - Set the Client ID to
drill4j. - Click Next.
- Set Client Authentication to
ONto grant OIDC Access Type confidential. - Set
Standard Flowflag for Authentication Flow. - Click Next.
- Set Valid Redirect URIs to
http://<DRILL4J_UI_HOST>:<DRILL4J_UI_PORT>/oauth/callback(replace<DRILL4J_UI_HOST>and<DRILL4J_UI_PORT>with your Drill4J UI host and port). - Click Save to create the client.
- On the Client Details page of the
drill4jclient, navigate to theCredentialstab. - Copy the
Client secretvalue. You will need this for configuring Drill4J.
- Set the Client Type to
- Create Roles: On the Client Details page of the
drill4jclient, navigate to theRolestab to create roles that correspond to Drill4J user roles (e.g.,admin,user).- Click Create role and enter the role name
admin. - Click Save.
- Repeat the process to create a
userrole.
- Click Create role and enter the role name
- Client Scopes Mapping: Go to the
Client Scopestab.- Find the
rolesscope and click on it. - In the
Mapperstab, click onAdd mapper > By configuration. - On the
Configure a new mapperwindow, choose theUser Client Role. - Set the
Nametodrill4j-roles. - Set the
Client IDtodrill4j. - Set the
Token Claim Nametoroles. - Set Multivalued to
ON. - Set
Claim JSON TypetoString. - Set
Add to access tokentoON. - Click Save.
- Find the
- Create Users: Navigate to the
Userstab and create users for Drill4J.- Click Add user and fill in the required fields (Username, Email, First Name, Last Name).
- Click Create.
- After creating the user, go to the
Credentialstab to set a password for the user. - Set
TemporarytoOFFif you don't want the user to change the password on first login. - Click Set Password.
- Go to the
Role Mappingstab to assign roles to the user. - Select the
drill4jclient from the Client Roles dropdown. - Assign the appropriate roles (e.g.,
admin,user) to the user.
- Open OpenID Configuration: To get the OpenID configuration URL, navigate to:Replace
<KEYCLOAK_HOST>,<KEYCLOAK_PORT>, and<REALM_NAME>with your Keycloak host, port, and realm name respectively. This URL provides the necessary endpoints for Drill4J to interact with Keycloak.
Step 2: Configure Drill4J Backend#
- Set environment variables: You need to set the following environment variables for Drill4J Backend to connect to Keycloak:
- Set
DRILL_OAUTH2_ENABLEDtotrue. - Set
DRILL_OAUTH2_AUTHORIZE_URLto the Keycloak authorization endpoint (Seeauthorization_endpointin the OpenID configuration) - Set
DRILL_OAUTH2_ACCESS_TOKEN_URLto the Keycloak token endpoint (Seetoken_endpointin the OpenID configuration) - Set
DRILL_OAUTH2_REDIRECT_URLtohttp://<DRILL4J_HOST>:<DRILL4J_UI_PORT> - Set
DRILL_OAUTH2_CLIENT_IDtodrill4j. - Set
DRILL_OAUTH2_CLIENT_SECRETto the client secret you copied earlier - Set
DRILL_OAUTH2_SCOPEStoopenid,roles. - Set
DRILL_OAUTH2_TOKEN_MAPPING_USERNAMEtopreferred_usernameaccording to default Keycloak client scope mapping. - Set
DRILL_OAUTH2_TOKEN_MAPPING_ROLEStoroles. - If you want to disable Simple Authentication and allow only OAuth2 authentication, set
DRILL_AUTH_SIMPLE_ENABLEDtofalseand setDRILL_OAUTH2_AUTOMATIC_SIGN_INtotrue.
- Set
- Restart Drill4J Backend: After setting the environment variables, restart the Drill4J Backend service to apply the changes.
Step 3: Verify the Integration#
- Access Drill4J UI: Open your web browser and navigate to the Drill4J UI URL.
- Login with Auth Provider:
- Case 1: If you have enabled Simple Authentication
DRILL_AUTH_SIMPLE_ENABLED=true, click the "Sign in with Auth Provider" button in the login window. - Case 2: If you have disabled Simple Authentication
DRILL_AUTH_SIMPLE_ENABLED=falseand enabled OAuth Automatic Sign InDRILL_OAUTH2_AUTOMATIC_SIGN_IN=true, you will be automatically redirected to the Keycloak login page.
- Case 1: If you have enabled Simple Authentication
- Authenticate: Enter the credentials of a user you created in Keycloak.
- Access Drill4J: After successful authentication, you should be redirected back to the Drill4J UI and logged in with the appropriate role.
Troubleshooting#
- If you encounter issues during the integration, set Drill4J Backend logging level to
TRACEto get detailed logs and check for any errors.
- Ensure that the Keycloak server is running and accessible from the Drill4J Backend.
- Verify that the environment variables are correctly set and that the URLs are accurate.
- Check the Keycloak logs for any authentication errors.