Keycloak
Keycloak is an open-source, self-hosted identity and access management solution that provides single sign-on (SSO), user authentication, and authorization services for applications. In AMT environments, Keycloak enables secure authentication for Java backend services and Python client scripts through standard OAuth2 and OpenID Connect protocols.
By centralizing authentication and identity management, Keycloak eliminates the need for applications to handle user credentials directly, supports role-based access control, and provides a unified authentication experience across multiple AMT components. It is particularly useful when you need to secure gRPC communication between Python clients and Java backend services, or when implementing enterprise-grade authentication with features like multi-factor authentication, user federation, and session management.
Installation and Setup
Download and extract the latest version of Keycloak.
Starting Keycloak (Development Mode)
- Open a terminal and navigate to the
keycloak-<version>\binfolder. - Enter the following command to start Keycloak:
kc.bat start-dev
warning Using thestart-devparameter is only meant for development. For production or HTTPS configuration, usekc.bat startand configure certificates as described in the HTTPS Configuration section below. - Open a web browser and browse to localhost:8080 (or https://localhost:8443 if HTTPS is configured).
Creating An Admin Account
- Fill the form with a username and password to create the admin account.
- Click the Open Administration Console button and log in with the admin account.
- Navigate to the Admin Console at
http://localhost:8080/admin(orhttps://localhost:8443/adminif HTTPS is configured). - Log in using the credentials of the admin account.
Creating A Realm And A User
- In the Administration Console, click on the drop-down menu at the top-left of the page and select Create realm.
- Enter a name for the realm. For example, "AMTRealm" and click the Create button.
- Within the newly created realm, click Users in the menu on the left side.
- Click Create new user.
- Enter a value for the following fields:
- Username
- First name
- Last name
- Click the Create button.
- Click Credentials.
- Enter a password for the new user.
- Confirm the password by re-entering it in the Password confirmation field.
- Click the Save button.
Optional – Verifying User Configuration
- Navigate to
http://localhost:8080/realms/AMTRealm/account(orhttps://localhost:8443/realms/AMTRealm/accountif HTTPS is configured) to open the Account Console. - Log in using the credentials of the user created in the previous section.
- From the Account Console, the user configuration can be verified and the account and profile can be managed.
Securing An Application
- Navigate to the Admin Console at
http://localhost:8080/admin(orhttps://localhost:8443/adminif HTTPS is configured). - Within the realm, click Clients in the sidebar menu, then click the Create client button.
- In the General settings menu, configure the following:
- Client type: OpenID Connect
- Client ID: amt-java-cc
- Name: AMT Java Control Center
- Click the Next button.
- Leave the Capability settings as is and click the Next button.
- Configure the following Login settings:
- Root URL: http://localhost:4200
- Home URL: http://localhost:4200
- Valid redirect URIs:
- http://localhost:4200/*
- http://localhost:4300/*
- Valid post logout redirect URIs: None
- Web origins:
- http://localhost:4200
- http://localhost:4300
Creating a Backend Resource Server Client
For gRPC authentication with Python clients, you need to register a backend resource server client. This client is used as the audience target for tokens but doesn't perform authentication itself.
- Navigate to Clients in the sidebar menu, then click the Create client button.
- Configure General settings:
- Client type: OpenID Connect
- Client ID: amt-backend (or any appropriate name for your backend)
- Name: AMT Backend Server
- Click the Next button.
- Configure Capability settings:
- Client authentication: Off
- Authorization: Off
- Standard flow: Off
- Direct access grants: Off
- Click Next, then Save.
| Note |
|---|
| The backend client is registered in Keycloak primarily for administration purposes, so it can be selected as an audience in token audience mappers. It doesn't authenticate itself. |
Creating A Client Scope for Backend Audience
- Navigate to Client scopes in the sidebar menu, then click the Create client scope button.
- Configure the following Client scope settings:
- Name: backend_audience_scope
- Description: Scope to add backend server as audience to access tokens
- Type: None
- Click the Save button.
- Click the Mappers tab and click on Configure a new mapper to configure the
following:
- Name: backend audience mapper
- Included Client Audience: amt-backend (the backend client created above)
- Add to ID token: Off
- Add to access token: On
- Click the Save button.
HTTPS Configuration
For production environments or when using Python client libraries with gRPC, Keycloak should be configured to use HTTPS. This requires generating a certificate and configuring Keycloak to run in production mode.
Generate Self-Signed Certificate
Create a directory for certificates and generate a self-signed certificate using OpenSSL:
# Windows PowerShell New-Item -ItemType Directory -Force -Path "C:\Tools\certs" cd C:\Tools\certs # Generate certificate (365 days validity) openssl req -x509 -newkey rsa:2048 -nodes -keyout localhost.key -out localhost.pem -days 365 -subj "/CN=localhost"
This creates two files:
localhost.pem- The certificate (public)localhost.key- The private key (keep secure)
Configure Keycloak for HTTPS
Edit the Keycloak configuration file at <keycloak-dir>\conf\keycloak.conf:
# HTTPS Certificate Configuration https-certificate-file=C:/Tools/certs/localhost.pem https-certificate-key-file=C:/Tools/certs/localhost.key https-port=8443 # Disable HTTP (optional, recommended for production) http-enabled=false # Allow any hostname (for development) hostname-strict=false
Start Keycloak in Production Mode
HTTPS with custom certificates requires Keycloak to run in production mode:
# Navigate to Keycloak bin directory cd <keycloak-dir>\bin # Start in production mode (required for HTTPS) kc.bat start
| Note |
|---|
Development mode (start-dev) ignores custom HTTPS certificate configuration. You must use
start for HTTPS to work with self-signed certificates.
|
Keycloak will now be accessible at https://localhost:8443
Python Client Configuration
When using Python client libraries (such as amt-jcl) with Keycloak for gRPC authentication, additional configuration is required for OIDC and certificate trust.
Creating a Python Client in Keycloak
Python clients require a confidential client configuration with client credentials flow enabled.
- Navigate to Clients and click Create client.
- Configure General settings:
- Client type: OpenID Connect
- Client ID: amt-python-client (or any appropriate name)
- Name: AMT Python Client
- Click Next.
- Configure Capability settings:
- Client authentication: On
- Authorization: Off
- Standard flow: Off
- Direct access grants: Off
- Service accounts roles: On
- Click Next, then Save.
- Navigate to the Credentials tab and copy the Client secret (needed for Python configuration).
- Navigate to the Client scopes tab, click Add client scope, select the backend
scope you created (e.g.,
backend_audience_scope), and set it as Default.
Python Configuration File
Configure your Python application's jcl_config.json file with the following settings:
{
"oidc_url": "https://localhost:8443/realms/AMTRealm/protocol/openid-connect/token",
"oidc_client_id": "amt-python-client",
"oidc_client_secret": "<YOUR_CLIENT_SECRET>",
"oidc_scope": "openid offline_access backend_audience_scope",
"cert": "path/to/server1.pem",
"app_host": "localhost",
"app_grpc_port": "8081"
}
| Field | Description |
|---|---|
| oidc_url | Keycloak token endpoint URL |
| oidc_client_id | The Python client ID created in Keycloak |
| oidc_client_secret | Client secret from Keycloak Credentials tab |
| oidc_scope | Scopes to request. Must include the backend scope to get the correct audience |
| cert | Path to the server certificate that the Java backend serves on the gRPC port |
Certificate Trust Configuration
When Keycloak uses HTTPS with self-signed certificates, Python clients need to trust the certificate to avoid SSL verification errors.
Setting the Certificate Trust for Python Clients
Set the REQUESTS_CA_BUNDLE environment variable to point to the Keycloak certificate:
# Windows PowerShell $env:REQUESTS_CA_BUNDLE = "C:\Tools\certs\localhost.pem" # Windows Command Prompt set REQUESTS_CA_BUNDLE=C:\Tools\certs\localhost.pem # Linux/macOS export REQUESTS_CA_BUNDLE=/path/to/localhost.pem
This allows Python's requests library to trust the self-signed certificate when connecting to Keycloak.
Setting Certificate Trust for Java Backend
If the Java backend needs to validate tokens by connecting to Keycloak's JWKS endpoint over HTTPS with a self-signed certificate, import the certificate into the Java truststore:
keytool -importcert -alias keycloak -file localhost.pem -keystore $JAVA_HOME/lib/security/cacerts -storepass changeit -noprompt
Troubleshooting
SSL Certificate Verification Errors
Symptom: Python scripts fail with error:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate
Solution:
- Ensure the Keycloak certificate file exists at the configured path
- Set the
REQUESTS_CA_BUNDLEenvironment variable to point to the certificate file - Verify the certificate path in
jcl_config.jsonmatches the Java backend's server certificate
Token Validation Fails
Symptom: Java backend rejects tokens with "invalid audience"
Solution:
- Ensure the client scope is assigned to the Python client as Default
- Verify the scope contains an audience mapper pointing to the backend client ID
- Check that the
oidc_scopein Python configuration includes the backend scope name
gRPC Connection Fails
Symptom: Python client cannot connect to Java backend
Solution:
- Verify the
certpath injcl_config.jsonpoints to the correctserver1.pem(the certificate served by the Java backend on port 8081) - Ensure the Java backend is running on the configured
app_grpc_port - Check that the certificate in
jcl_config.jsonmatches what the server is serving
OIDC Token Request Fails
Symptom: Python client cannot obtain tokens from Keycloak
Solution:
- Verify
oidc_client_secretmatches the value in Keycloak - Ensure Service accounts roles is enabled on the Python client
- Check that Keycloak is accessible at the
oidc_url - Set
REQUESTS_CA_BUNDLEif using self-signed certificates
