Integrate NBS 7 with an existing Keycloak
Use this path if your organization already runs Keycloak and you want NBS 7 to authenticate users against an existing realm, instead of deploying the NBS-provided Keycloak described in Deploy and configure Keycloak. On this path, you configure your existing realm with the client, claim mapping, and users that NBS 7 requires, and then supply the realm issuer and client secret to the NBS 7 microservices.
This page covers user authentication (browser login) against your existing realm. The NBS 7 backend service clients for data ingestion, NND, SRTE, and case notification are configured separately. See Import service clients and retrieve secrets.
On this page
- Prerequisites
- Create or verify the NBS 7 client
- Map the preferred_username claim
- Align Keycloak users with NBS 6 accounts
- Provide OIDC values to the NBS 7 microservices
- Troubleshooting
- Next steps
Prerequisites
Before you begin the procedures on this page, confirm that you have the following:
- Administrator access to your existing Keycloak admin console.
- The target realm for NBS 7 users. Use an existing realm or create one before you begin.
- The public hostname or hostnames for your NBS 7 application, in the form
app.<DOMAIN_NAME.TLD>. You need one entry for each deployed application host. - Access to the NBS 6 database, to confirm that users exist in the
Auth_usertable. - A secrets manager, such as AWS Secrets Manager or Azure Key Vault, to store the client secret.
Create or verify the NBS 7 client
NBS 7 uses an OpenID Connect (OIDC) client for browser login. Create this client in the realm that authenticates NBS 7 users, or verify its configuration if it already exists.
- Open the Keycloak admin console and select the target realm for NBS 7 users.
- Navigate to Clients and create or open the client
nbs-modernization. -
Set the following values:
Setting Value Client type or protocol OpenID Connect Client authentication On (confidential) Standard flow On Valid redirect URIs https://app.<DOMAIN_NAME.TLD>/login/oauth2/code/nbs-usersValid post logout redirect URIs https://app.<DOMAIN_NAME.TLD>/nbs/logged-outWeb origins https://app.<DOMAIN_NAME.TLD> - Save the client.
Note the following when you configure the client:
- The Root URL can be left blank.
- Prefer full URLs for the redirect and post-logout values. Relative paths work in some setups, but full URLs are more reliable.
- Add one redirect URI and one web origin entry for each deployed application host. For example, add a second set of entries for
https://nbs.<DOMAIN_NAME.TLD>. - The
nbs-userssegment in the redirect URI is the application registration ID, not the realm name. Keep it asnbs-usersunless the application registration is renamed everywhere. - If you use a different client ID, such as
nbs-development, all NBS 7 configuration must use that same client ID and secret.
Map the preferred_username claim
NBS 7 expects the username in the preferred_username claim. Confirm that the realm’s profile client scope includes a mapper that produces this claim, and create one if it is missing.
- Navigate to Client scopes > profile > Mappers.
- If a
usernamemapper exists, open it and verify the following:- Mapper type: User Attribute
- User attribute:
username - Token claim name:
preferred_username
- If a
usernamemapper does not exist, check pagination or search before you create one. To create it, select Add mapper > By configuration > User Attribute, and set the following:- Name:
username - User attribute:
username - Token claim name:
preferred_username - Add to ID token: On
- Add to access token: On
- Add to userinfo: On
- Name:
- Save the mapper.
Align Keycloak users with NBS 6 accounts
NBS 7 hands the logged-in username off to NBS 6. Each user who signs in to NBS 7 must exist as an ACTIVE user_id in the NBS 6 Auth_user table, or NBS 6 page access fails after a successful Keycloak login.
Confirm the user in Keycloak:
- In the admin console, select the realm that NBS 7 uses.
- Navigate to Users and search for the username, for example
superuser.
If the user does not exist, create it:
- Navigate to Users > Add user.
- Enter the required fields:
- Username: the NBS username, which must match the NBS 6
Auth_user.user_idvalue. - Email verified: On.
- Enabled: On.
- Username: the NBS username, which must match the NBS 6
- Select Create.
- Open the new user, go to Credentials, and set a password. Set Temporary to Off so the user is not forced to reset the password on first login.
- Select Set password and confirm.
- Sign out of any active Keycloak session, then test that the new user can sign in.
Confirm the matching NBS 6 account exists and is active, as described in NBS 6 user requirement. For example, when superuser signs in to NBS 7, NBS 6 receives UserName=superuser, which must resolve to one ACTIVE row for that user.
Provide OIDC values to the NBS 7 microservices
The NBS gateway and Modernization API authenticate users through your realm. When you deploy those services in Deploy NBS 7 microservices, set the client secret, and set the issuer URI only if your Keycloak is served at a different host than the NBS 7 application.
- Client secret (required): In Clients > nbs-modernization > Credentials, copy the client secret and store it in your secrets manager. Set it as the OIDC client secret (
oidc.client.secret) for bothnbs-gatewayandmodernization-api. This is the value described asEXAMPLE_OIDC_SECRETin the Helm values reference; use your existing realm’s client secret in place of the NBS realm value. - Issuer URI (
oidc.uri, usually blank): Leave the issuer URI (oidc.uri) blank when your Keycloak is reached at the same application host, which is defined by theingressHostparameter. NBS 7 derives the issuer from that host. Setoidc.uriexplicitly only when your Keycloak is served at a different URL than the NBS 7 application. In that case, copy theissuervalue from Realm settings > OpenID Endpoint Configuration (in the formhttps://<keycloak-host>/realms/<realm-name>, exactly as Keycloak returns it) and set it for bothnbs-gatewayandmodernization-api.
After you set these values and run the Helm upgrade for those services, the nbs-gateway and modernization-api pods usually roll automatically because the pod specification changed. If they do not restart, trigger a rollout restart for both workloads.
Troubleshooting
| Symptom | Likely cause |
|---|---|
/login?error with Keycloak CODE_TO_TOKEN_ERROR and invalid_client_credentials | The client secret configured for nbs-gateway does not match the Keycloak client secret. |
Keycloak LOGIN_ERROR with invalid_user_credentials | Wrong Keycloak username or password. |
| Login succeeds, but NBS 6 page access fails | The Keycloak username does not exist as an ACTIVE user_id in the NBS 6 Auth_user table. See Align Keycloak users with NBS 6 accounts. |
Next steps
Continue to Deploy NBS 7 microservices.