Keycloak Discovery Package for Netherframe
This package provides resource discovery capabilities for Keycloak, enabling automatic import of existing Keycloak resources into Netherframe Infrastructure as Code.
Installation
pip install netherframe.discovery.keycloak.keycloak
Prerequisites
- Python 3.11+
- A running Keycloak instance
- Admin credentials or client credentials with appropriate permissions
python-keycloaklibrary (automatically installed with this package)
Codifying a realm
nf discover codify keycloak writes typed Netherframe code for a live realm: every object is
imported through the keycloak/keycloak provider, so the code reproduces the provider's state,
with references between objects, provider defaults left out and secrets read from environment
variables. See Codifying a Keycloak Realm.
export KEYCLOAK_USER=admin KEYCLOAK_PASSWORD=...
nf discover codify keycloak --url http://localhost:8080 --realm demo -o realm_demo.py
From Python: codify_realm(api, provider_client, "demo") (netherframe.discovery.keycloak.keycloak),
with KeycloakConnection.from_config(config) for the admin API and a configured provider.
KeycloakProviderDiscoverer (entry point keycloak-provider in nf.discoverers) is the same
discovery as a ResourceDiscoverer: its attributes are the provider's state, secrets left out
and named in arguments_to_supply.
Authentication Setup
The Keycloak discoverer supports two authentication methods:
1. Username/Password Authentication
config = {
"server_url": "https://keycloak.example.com",
"username": "admin",
"password": "admin-password",
"realm_name": "master", # Optional, defaults to "master"
}
2. Client Credentials Authentication
config = {
"server_url": "https://keycloak.example.com",
"client_id": "my-client",
"client_secret": "client-secret",
"realm_name": "master", # Optional, defaults to "master"
}
Basic Usage
from nf.discovery.keycloak.keycloak import KeycloakDiscoverer
# Initialize discoverer
discoverer = KeycloakDiscoverer()
# Configure connection
config = {
"server_url": "https://keycloak.example.com",
"username": "admin",
"password": "admin123",
}
# Discover all resources
resources = discoverer.discover_resources(config)
# Process discovered resources
for resource in resources:
print(f"Found {resource.resource_type}: {resource.import_id}")
Supported Resources
This discovery package supports all 96 Keycloak resources (100% coverage). The following resources have discovery implementations:
Currently Implemented Resources
Core Resources
keycloak_realm- Keycloak realmskeycloak_group- User groupskeycloak_user- User accountskeycloak_role- Realm and client roles
Client Resources
keycloak_openid_client- OpenID Connect clientskeycloak_saml_client- SAML clientskeycloak_openid_client_scope- Client scopeskeycloak_openid_client_default_scopes- Default client scope assignmentskeycloak_openid_client_optional_scopes- Optional client scope assignments
Authentication Resources
keycloak_authentication_flow- Authentication flowskeycloak_authentication_subflow- Authentication subflowskeycloak_authentication_execution- Authentication executionskeycloak_authentication_execution_config- Execution configurationskeycloak_authentication_bindings- Flow bindings (browser, registration, etc.)
Identity Provider Resources
keycloak_oidc_identity_provider- Generic OIDC identity providerskeycloak_saml_identity_provider- SAML identity providerskeycloak_oidc_google_identity_provider- Google identity provider (special case)
Identity Provider Mapper Resources
keycloak_attribute_importer_identity_provider_mapper- Import attributes from IDPskeycloak_attribute_to_role_identity_provider_mapper- Map IDP attributes to roleskeycloak_hardcoded_attribute_identity_provider_mapper- Add hardcoded attributeskeycloak_hardcoded_role_identity_provider_mapper- Add hardcoded roleskeycloak_hardcoded_group_identity_provider_mapper- Add hardcoded groupskeycloak_user_template_importer_identity_provider_mapper- Template-based username mapping
Protocol Mapper Resources - OpenID Connect
keycloak_openid_user_attribute_protocol_mapper- Map user attributes to token claimskeycloak_openid_user_property_protocol_mapper- Map user properties to token claimskeycloak_openid_group_membership_protocol_mapper- Map user groups to token claimskeycloak_openid_full_name_protocol_mapper- Add full name to tokenskeycloak_openid_hardcoded_claim_protocol_mapper- Add hardcoded claims to tokenskeycloak_openid_audience_protocol_mapper- Add audience to tokenskeycloak_openid_audience_resolve_protocol_mapper- Resolve and add audience to tokenskeycloak_openid_hardcoded_role_protocol_mapper- Add hardcoded roles to tokenskeycloak_openid_user_realm_role_protocol_mapper- Map realm roles to token claimskeycloak_openid_user_client_role_protocol_mapper- Map client roles to token claimskeycloak_openid_user_session_note_protocol_mapper- Map session notes to token claimskeycloak_openid_script_protocol_mapper- Script-based token claim mapping
Protocol Mapper Resources - SAML
keycloak_saml_user_attribute_protocol_mapper- Map user attributes to SAML assertionskeycloak_saml_user_property_protocol_mapper- Map user properties to SAML assertionskeycloak_saml_script_protocol_mapper- Script-based SAML assertion mapping
Protocol Mapper Resources - Generic
keycloak_generic_client_protocol_mapper- Generic protocol mapper for clientskeycloak_generic_protocol_mapper- Generic protocol mapper for client scopeskeycloak_generic_client_role_mapper- Generic role mapper for clientskeycloak_generic_role_mapper- Generic role mapper for client scopeskeycloak_hardcoded_attribute_mapper- Add hardcoded attributes
Custom Resources
keycloak_custom_user_federation- Custom user federation providers (non-LDAP)keycloak_custom_identity_provider_mapper- Custom identity provider mappers
Membership and Role Assignment Resources
keycloak_default_groups- Default groups assigned to new userskeycloak_default_roles- Default roles assigned to new userskeycloak_group_memberships- Members of a specific groupkeycloak_user_groups- Groups assigned to a specific userkeycloak_group_roles- Roles assigned to a specific groupkeycloak_user_roles- Roles assigned to a specific user
LDAP Resources
keycloak_ldap_user_federation- LDAP user federation providerskeycloak_ldap_user_attribute_mapper- Map LDAP attributes to user attributeskeycloak_ldap_full_name_mapper- Map full name from LDAPkeycloak_ldap_group_mapper- Map LDAP groups to Keycloak groupskeycloak_ldap_role_mapper- Map LDAP groups to Keycloak roleskeycloak_ldap_custom_mapper- Custom LDAP attribute mapperskeycloak_ldap_hardcoded_attribute_mapper- Add hardcoded attributes from LDAPkeycloak_ldap_hardcoded_group_mapper- Add hardcoded groups from LDAPkeycloak_ldap_hardcoded_role_mapper- Add hardcoded roles from LDAPkeycloak_ldap_msad_user_account_control_mapper- Map Microsoft AD user account controlkeycloak_ldap_msad_lds_user_account_control_mapper- Map Microsoft AD LDS user account control
Client Authorization Resources
keycloak_openid_client_authorization_resource- Authorization resourceskeycloak_openid_client_authorization_scope- Authorization scopeskeycloak_openid_client_authorization_permission- Authorization permissions
Client Authorization Policies
keycloak_openid_client_group_policy- Group-based authorization policieskeycloak_openid_client_role_policy- Role-based authorization policieskeycloak_openid_client_user_policy- User-based authorization policieskeycloak_openid_client_client_policy- Client-based authorization policieskeycloak_openid_client_time_policy- Time-based authorization policieskeycloak_openid_client_aggregate_policy- Aggregate authorization policieskeycloak_openid_client_js_policy- JavaScript-based authorization policieskeycloak_openid_client_authorization_client_scope_policy- Client scope-based authorization policies
Complete Resource Coverage
All 96 Keycloak resources are now supported with discovery implementations! This includes:
- Service Account Resources: Service account roles (both realm and client roles)
- Permissions Resources: Group, user, client, and identity provider token exchange permissions
- Realm Configuration: Events, user profiles, keystores, localization, and client policies
- Organization Support: Full organization discovery
- Keystore Resources: All 6 types of keystores (AES, ECDSA, HMAC, Java Keystore, RSA, RSA Generated)
- SAML Resources: SAML client scopes and default scope assignments
To see all supported resource types, run:
discoverer = KeycloakDiscoverer()
print(discoverer.get_supported_resource_types())
Configuration Options
Connection Settings
| Option | Type | Required | Description |
|---|---|---|---|
server_url |
string | Yes | Keycloak server URL |
username |
string | Conditional | Admin username (required if not using client credentials) |
password |
string | Conditional | Admin password (required with username) |
client_id |
string | No | Client ID for client credentials auth (default: "admin-cli") |
client_secret |
string | Conditional | Client secret (required with client_id) |
realm_name |
string | No | Initial realm name (default: "master") |
verify_ssl |
boolean | No | Verify SSL certificates (default: True) |
Discovery Settings
| Option | Type | Description |
|---|---|---|
resource_types |
list | Specific resource types to discover (default: all) |
max_users |
int | Maximum number of users to discover (default: 100) |
Filters
The discoverer supports various filters to limit discovery scope:
filters = {
"realm_names": ["master", "production"], # Only discover specific realms
"skip_builtin": True, # Skip built-in authentication flows
"skip_internal": True, # Skip internal clients
"skip_composite": True, # Skip composite roles
"max_users": 50, # Limit user discovery
}
resources = discoverer.discover_resources(config, filters)
Error Handling
The discoverer is designed to be resilient:
- Continues discovery even if some resources fail
- Logs warnings for failed resource types
- Returns all successfully discovered resources
Import ID Formats
Different resources use different import ID formats:
| Resource Type | Import ID Format | Example |
|---|---|---|
| Realm | {realm_name} |
master |
| Group | {realm}/{group_id} |
master/123e4567-e89b |
| User | {realm}/{user_id} |
master/456f7890-a12b |
| Role | {realm}/{role_id} |
master/789g0123-c34d |
| Client | {realm}/{client_id} |
master/012h3456-e56f |
Performance Considerations
-
User Discovery: Limited by default to 100 users per realm. Adjust
max_usersin filters for larger deployments. -
Hierarchical Discovery: Discovers realms first, then realm-scoped resources for efficiency.
-
Batch Operations: Uses list operations to minimize API calls.
-
Selective Discovery: Use
resource_typesconfig to discover only needed resources.
Example: Selective Discovery
# Discover only realms and clients
config = {"server_url": "https://keycloak.example.com", "username": "admin", "password": "admin123", "resource_types": ["keycloak_realm", "keycloak_openid_client"]}
resources = discoverer.discover_resources(config)
Troubleshooting
ImportError: python-keycloak is required
Install the required library:
pip install python-keycloak
Connection Errors
Verify:
- Keycloak server is accessible
- Credentials are correct
- SSL certificates are valid (or set
verify_ssl: falsefor testing)
Permission Errors
Ensure your user/client has appropriate permissions:
- For full discovery: Admin role in master realm
- For realm-specific discovery: Manage permissions in target realms
Known Limitations
- Full Coverage: All 96 Keycloak resources are now discoverable (100% coverage)
- User Discovery: Limited by default to prevent overwhelming large deployments
- Permissions: Some resources may require specific permissions to discover
- Performance: Discovery time increases with the number of realms and resources
- Organization Support: Organizations are discovered if supported by your Keycloak version
Contributing
To add support for new resources or improve existing discovery:
- Update the
get_supported_resource_types()method - Add discovery method for the new resource type
- Update tests and documentation
- Run the coverage analysis script to ensure 100% coverage
Metadata
Release files for netherframe.discovery.keycloak.keycloak 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| netherframe_discovery_keycloak_keycloak-0.5.0.tar.gz | 49.4 kB | Without provenance | Details for netherframe_discovery_keycloak_keycloak-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| netherframe_discovery_keycloak_keycloak-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for netherframe_discovery_keycloak_keycloak-0.5.0-py3-none-any.whl |
Total release size: 108.3 kB
Release files / netherframe_discovery_keycloak_keycloak-0.5.0.tar.gz
| Download URL | netherframe_discovery_keycloak_keycloak-0.5.0.tar.gz |
|---|---|
| Size | 49.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
945b8e45bf9f7ea2717a0934f6a2505ad644883077aa84f0b723599d0e16eb01
|
|
BLAKE2b-256 checksum How to use checksums |
9bc9eccbfb38ffc11eceea19461258a74bf46e8c7153c7bbc6f98c3059021409
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / netherframe_discovery_keycloak_keycloak-0.5.0-py3-none-any.whl
| Download URL | netherframe_discovery_keycloak_keycloak-0.5.0-py3-none-any.whl |
|---|---|
| Size | 58.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3f4620443d6f56cf99b8dd62232dab142f4c95cfaea8074d78b46cf55d16fca
|
|
BLAKE2b-256 checksum How to use checksums |
d6d625247e0706936ac1812f96cdabe6b20245138cb4457f172d7baf352e9b08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for netherframe.discovery.keycloak.keycloak 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.