Browse Source

chore: Feat/support admin events (#474)

* feat: added support for Admin Events api

* test: fix auth_flow test and authentication_configs based on KC 22 pre-settings

* docs: update readme

* revert: "test: fix auth_flow test and authentication_configs based on KC 22 pre-settings"

This reverts commit 392b71b351.

* test: trying to support old and new Keycloak versions for test_auth_flows & test_authentication_configs

test: trying to support old and new Keycloak versions for test_auth_flows & test_authentication_configs

https://github.com/keycloak/keycloak/issues/20497

* chore: revert Update tox.ini

These tools should be coming from the poetry virtual env

---------

Co-authored-by: Simone Ferrigno <simone.ferrigno@vorwerk.de>
Co-authored-by: Richard Nemeth <ryshoooo@gmail.com>
pull/430/head
Zima 12 months ago
committed by elias.hamacher
parent
commit
3bcc0a1608
  1. 12
      README.md
  2. 23
      src/keycloak/keycloak_admin.py
  3. 5
      src/keycloak/urls_patterns.py
  4. 18
      tests/test_keycloak_admin.py

12
README.md

@ -356,6 +356,18 @@ keycloak_admin.realm_name = "demo" # Change realm to 'demo'
keycloak_admin.get_users() # Get users in realm 'demo' keycloak_admin.get_users() # Get users in realm 'demo'
keycloak_admin.create_user(...) # Creates a new user in 'demo' keycloak_admin.create_user(...) # Creates a new user in 'demo'
# Get User events
keycloak_admin.get_events(query={'type': 'LOGIN',
'user': user['id'],
'dateFrom': '2023-08-02'})
# Get Admin events
keycloak_admin.get_admin_events(query={'resourceTypes': 'USER',
'operationTypes': 'UPDATE',
'resourcePath': 'users/' + user['id'],
'dateFrom': '2023-08-02'
})
# KEYCLOAK UMA # KEYCLOAK UMA
from keycloak import KeycloakOpenIDConnection from keycloak import KeycloakOpenIDConnection

23
src/keycloak/keycloak_admin.py

@ -4111,6 +4111,27 @@ class KeycloakAdmin:
) )
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)
def get_admin_events(self, query=None):
"""Get Administrative events.
Return a list of events, filtered according to query parameters
AdminEvents Representation array
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_getevents
https://www.keycloak.org/docs-api/22.0.1/rest-api/index.html#_get_adminrealmsrealmadmin_events
:param query: Additional query parameters
:type query: dict
:return: events list
:rtype: list
"""
query = query or dict()
params_path = {"realm-name": self.connection.realm_name}
data_raw = self.connection.raw_get(
urls_patterns.URL_ADMIN_ADMIN_EVENTS.format(**params_path), data=None, **query
)
return raise_error_from_response(data_raw, KeycloakGetError)
def get_events(self, query=None): def get_events(self, query=None):
"""Get events. """Get events.
@ -4127,7 +4148,7 @@ class KeycloakAdmin:
query = query or dict() query = query or dict()
params_path = {"realm-name": self.connection.realm_name} params_path = {"realm-name": self.connection.realm_name}
data_raw = self.connection.raw_get( data_raw = self.connection.raw_get(
urls_patterns.URL_ADMIN_EVENTS.format(**params_path), data=None, **query
urls_patterns.URL_ADMIN_USER_EVENTS.format(**params_path), data=None, **query
) )
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)

5
src/keycloak/urls_patterns.py

@ -202,8 +202,9 @@ URL_ADMIN_USER_FEDERATED_IDENTITY = (
"admin/realms/{realm-name}/users/{id}/federated-identity/{provider}" "admin/realms/{realm-name}/users/{id}/federated-identity/{provider}"
) )
URL_ADMIN_EVENTS = "admin/realms/{realm-name}/events"
URL_ADMIN_EVENTS_CONFIG = URL_ADMIN_EVENTS + "/config"
URL_ADMIN_USER_EVENTS = "admin/realms/{realm-name}/events"
URL_ADMIN_ADMIN_EVENTS = "admin/realms/{realm-name}/admin-events"
URL_ADMIN_EVENTS_CONFIG = URL_ADMIN_USER_EVENTS + "/config"
URL_ADMIN_CLIENT_SESSION_STATS = "admin/realms/{realm-name}/client-session-stats" URL_ADMIN_CLIENT_SESSION_STATS = "admin/realms/{realm-name}/client-session-stats"
URL_ADMIN_GROUPS_CLIENT_ROLES_COMPOSITE = URL_ADMIN_GROUPS_CLIENT_ROLES + "/composite" URL_ADMIN_GROUPS_CLIENT_ROLES_COMPOSITE = URL_ADMIN_GROUPS_CLIENT_ROLES + "/composite"

18
tests/test_keycloak_admin.py

@ -2390,7 +2390,23 @@ def test_keys(admin: KeycloakAdmin, realm: str):
} }
def test_events(admin: KeycloakAdmin, realm: str):
def test_admin_events(admin: KeycloakAdmin, realm: str):
"""Test events.
:param admin: Keycloak Admin client
:type admin: KeycloakAdmin
:param realm: Keycloak realm
:type realm: str
"""
admin.realm_name = realm
admin.create_client(payload={"name": "test", "clientId": "test"})
events = admin.get_admin_events()
assert events == list()
def test_user_events(admin: KeycloakAdmin, realm: str):
"""Test events. """Test events.
:param admin: Keycloak Admin client :param admin: Keycloak Admin client

Loading…
Cancel
Save