Skip to content

Commit

Permalink
Keycloak legos (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
shloka-bhalgat-unskript authored Oct 11, 2023
1 parent e86b59e commit 107bb74
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 0 deletions.
Empty file added Keycloak/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions Keycloak/legos/keycloak_get_audit_report/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[<img align="left" src="https://unskript.com/assets/favicon.png" width="100" height="100" style="padding-right: 5px">]
(https://unskript.com/assets/favicon.png)
<h1>Get Keycloak audit report</h1>

## Description
Fetches the audit events from Keycloak.

## Lego Details
keycloak_get_audit_report(handle):
handle: Handle object containing the KeycloakAdmin instance.


## Lego Input
This Lego takes inputs handle.

## Lego Output
Here is a sample output.
<img src="./1.png">

## See it in Action

You can see this Lego in action following this link [unSkript Live](https://us.app.unskript.io)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"action_title": "Get Keycloak audit report",
"action_description": "Fetches the audit events from Keycloak.",
"action_type": "LEGO_TYPE_KEYCLOAK",
"action_entry_function": "keycloak_get_audit_report",
"action_needs_credential": true,
"action_output_type": "ACTION_OUTPUT_TYPE_LIST",
"action_is_check": false,
"action_supports_iteration": true,
"action_supports_poll": true,
"action_categories":["CATEGORY_TYPE_SRE","CATEGORY_TYPE_KEYCLOAK"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright (c) 2023 unSkript.com
# All rights reserved.
#
from typing import List
from tabulate import tabulate
from pydantic import BaseModel


class InputSchema(BaseModel):
pass


def keycloak_get_audit_report_printer(output):
if not output:
print("No audit events found.")
return

# Extract relevant event data for tabulation
table_data = []
for event in output:
table_data.append({
"Time": event['time'],
"Type": event['type'],
"User ID": event['userId'],
"Client ID": event['clientId'],
"IP Address": event['ipAddress'],
"Error": event.get('error', '')
})

# Convert list of dictionaries to tabulated format
headers = ["Time", "Type", "User ID", "Client ID", "IP Address", "Error"]
print(tabulate(table_data, headers=headers, tablefmt="grid"))


def keycloak_get_audit_report(handle) -> List:
"""
keycloak_get_audit_report fetches the audit events from Keycloak.
:type handle: KeycloakAdmin
:param handle: Handle containing the KeycloakAdmin instance.
:rtype: List of dictionaries representing the audit events.
"""
try:
# Fetch the events
events = handle.get_events()
return events if events else []

except Exception as e:
raise e


25 changes: 25 additions & 0 deletions Keycloak/legos/keycloak_get_handle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[<img align="left" src="https://unskript.com/assets/favicon.png" width="100" height="100" style="padding-right: 5px">](https://unskript.com/assets/favicon.png)
<h2>Get Keycloak Handle</h2>

<br>

## Description
This Lego Get Keycloak Handle.


## Lego Details

keycloak_get_handle(handle: object)

handle: Object of type unSkript Keycloak Connector

## Lego Input
This Lego take one input handle.

## Lego Output
Here is a sample output.


## See it in Action

You can see this Lego in action following this link [unSkript Live](https://us.app.unskript.io)
Empty file.
11 changes: 11 additions & 0 deletions Keycloak/legos/keycloak_get_handle/keycloak_get_handle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"action_title": "Keycloak Get Handle",
"action_description": "Get Keycloak Handle",
"action_type": "LEGO_TYPE_KEYCLOAK",
"action_entry_function": "keycloak_get_handle",
"action_needs_credential": true,
"action_supports_poll": false,
"action_supports_iteration": false,
"action_output_type": "ACTION_OUTPUT_TYPE_LIST"
}

17 changes: 17 additions & 0 deletions Keycloak/legos/keycloak_get_handle/keycloak_get_handle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##
## Copyright (c) 2023 unSkript, Inc
## All rights reserved.
##
from pydantic import BaseModel


class InputSchema(BaseModel):
pass


def keycloak_get_handle(handle):
"""keycloak_get_handle returns the Keycloak handle.
:rtype: Keycloak handle.
"""
return handle
22 changes: 22 additions & 0 deletions Keycloak/legos/keycloak_get_service_health/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[<img align="left" src="https://unskript.com/assets/favicon.png" width="100" height="100" style="padding-right: 5px">]
(https://unskript.com/assets/favicon.png)
<h1>Get Keycloak service health</h1>

## Description
Fetches the health of the Keycloak service by trying to list available realms.

## Lego Details
keycloak_get_service_health fetches the health of the Keycloak service by trying to list available realms.
handle: Handle containing the KeycloakAdmin instance.


## Lego Input
This Lego takes inputs handle.

## Lego Output
Here is a sample output.
<img src="./1.png">

## See it in Action

You can see this Lego in action following this link [unSkript Live](https://us.app.unskript.io)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"action_title": "Get Keycloak service health",
"action_description": "Fetches the health of the Keycloak service by trying to list available realms.",
"action_type": "LEGO_TYPE_KEYCLOAK",
"action_entry_function": "keycloak_get_service_health",
"action_needs_credential": true,
"action_output_type": "ACTION_OUTPUT_TYPE_LIST",
"action_is_check": true,
"action_next_hop": [
""
],
"action_next_hop_parameter_mapping": {},
"action_supports_iteration": true,
"action_supports_poll": true,
"action_categories":["CATEGORY_TYPE_SRE","CATEGORY_TYPE_KEYCLOAK"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright (c) 2023 unSkript.com
# All rights reserved.
#

from typing import Tuple
from pydantic import BaseModel


class InputSchema(BaseModel):
pass

def keycloak_get_service_health(handle) -> Tuple:
"""
keycloak_get_service_health fetches the health of the Keycloak service by trying to list available realms.
:type handle: object
:param handle: Handle containing the KeycloakAdmin instance.
:rtype: Tuple indicating if the service is healthy and a list of available realms (or None if healthy).
"""
try:
realms = handle.get_realms()
available_realms = [realm["realm"] for realm in realms]

if not available_realms:
return (False, available_realms)

return (True, None)

except Exception as e:
raise e

def keycloak_get_service_health_printer(output):
is_healthy, realms = output

if is_healthy:
print("Keycloak Service is Healthy.")
else:
print("Keycloak Service is Unhealthy.")
if realms:
print("\nUnavailable Realms:")
for realm in realms:
print(f" - {realm}")

0 comments on commit 107bb74

Please sign in to comment.