From ef32345ff3db9620cf55459bf928d2f075ef763f Mon Sep 17 00:00:00 2001 From: mbrill-nt Date: Thu, 16 Jul 2020 15:51:56 +0200 Subject: [PATCH] Added logging of current actions. --- script/grafana.py | 28 +++++++++++++++++++++------- script/ldap.py | 13 ++++++++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/script/grafana.py b/script/grafana.py index 60e8847..cfaf1db 100644 --- a/script/grafana.py +++ b/script/grafana.py @@ -6,6 +6,8 @@ grafana_api = "" configuration = "" +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger("grafana-ldap-sync-script") def setup_grafana(config_dict): global grafana_api, configuration @@ -26,8 +28,9 @@ def delete_team_by_name(name): if len(team_data) > 0: for data_set in team_data: if configuration.DRY_RUN: - print("Would have deleted team with name: %s and id: %s" % (name, data_set["id"])) + logger.info("Would have deleted team with name: %s and id: %s" % (name, data_set["id"])) else: + logger.info("Deleting team with name %s and id %s" % (name, data_set["id"])) grafana_api.teams.delete_team(data_set["id"]) return True return False @@ -41,8 +44,9 @@ def create_team(name, mail): :return: The API response. """ if configuration.DRY_RUN: - print("Would have created team with name: %s" % name) + logger.info("Would have created team with name: %s" % name) else: + logger.info("Creating team with name %s" % name) return grafana_api.teams.add_team({ "name": name, "mail": mail @@ -58,8 +62,11 @@ def create_user_with_random_pw(user): user_dict["password"] = get_random_alphanumerical() user_dict["OrgId"] = 1 if configuration.DRY_RUN: - print("Would have created user with json %s" % str(user_dict)) + logger.info("Would have created user with json %s" % str(user_dict)) else: + logger.info("Creating user with login %s, name %s and mail %s" % + (user_dict["login"], user_dict["name"], user_dict["email"]) + ) grafana_api.admin.create_user(user_dict) @@ -71,8 +78,9 @@ def delete_user_by_login(login): """ if not login == "admin": if configuration.DRY_RUN: - print("Would have deleted user with name: %s" % login) + logger.info("Would have deleted user with name: %s" % login) else: + logger.info("Deleting user with name %s" % login) return grafana_api.admin.delete_user(grafana_api.users.find_user(login)["id"]) return False @@ -87,8 +95,9 @@ def create_folder(folder_name, folder_uuid): """ try: if configuration.DRY_RUN: - print("Would have created folder with name: %s and id: %s" % (folder_name, folder_uuid)) + logger.info("Would have created folder with name: %s and id: %s" % (folder_name, folder_uuid)) else: + logger.info("Creating folder with name %s and id %s" % (folder_name, folder_uuid)) return grafana_api.folder.create_folder(folder_name, folder_uuid) except GrafanaClientError: return False @@ -102,8 +111,9 @@ def add_user_to_team(login, team): """ try: if configuration.DRY_RUN: - print("Would have added user %s to team %s" % (login, team)) + logger.info("Would have added user %s to team %s" % (login, team)) else: + logger.info("Adding user %s to team %s" % (login, team)) grafana_api.teams.add_team_member(get_id_of_team(team), get_id_by_login(login)) except GrafanaBadInputError: return False @@ -116,6 +126,7 @@ def get_members_of_team(team): :param team: The name of the team the members should be returned of. :return: An array containing all users as described above. """ + logger.info("Fetching members of team %s" % team) teams = grafana_api.teams.get_team_by_name(team) if not teams: return [] @@ -134,6 +145,7 @@ def remove_member_from_team(grafana_team, user_login): if configuration.DRY_RUN: print("Would have removed user %s from team %s" % (grafana_team, user_login)) else: + logger.info("Removing user %s from team %s" % (grafana_team, user_login)) grafana_api.teams.remove_team_member(get_id_of_team(grafana_team), get_id_by_login(user_login)) @@ -189,8 +201,9 @@ def update_folder_permissions(folder_id, permissions): Sets the given permissions for the folder found under the given id """ if configuration.DRY_RUN: - print("Would have set permission of folder %s to %s" % (folder_id, permissions)) + logger.info("Would have set permission of folder %s to %s" % (folder_id, permissions)) else: + logger.info("Setting permission of folder %s to %s" % (folder_id, permissions)) grafana_api.folder.update_folder_permissions(folder_id, {"items": permissions}) @@ -205,6 +218,7 @@ def get_all_users(): """ Returns all users present in the connected grafana instance. """ + logger.info("Fetching all grafana users") user_logins = [] users = grafana_api.users.search_users() if users is not None: diff --git a/script/ldap.py b/script/ldap.py index 39fbed8..e1e81fc 100644 --- a/script/ldap.py +++ b/script/ldap.py @@ -1,8 +1,11 @@ from ldap3 import Server, Connection, ALL, SUBTREE, NTLM +import logging from .config import config from .helpers import * +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger("grafana-ldap-sync-script") configuration = "" user_cache = {} @@ -24,6 +27,7 @@ def get_ldap_connection(): Creates a connection to the ldap-server provided in the config. Uses ldap3. :return: A ldap3 connection object. """ + logger.info("Establishing standard ldap connection") server = Server(configuration.LDAP_SERVER_URL, get_info=ALL, use_ssl=configuration.LDAP_USE_SSL, port=configuration.LDAP_PORT) return Connection(server, configuration.LDAP_USER, configuration.LDAP_PASSWORD, auto_bind=True, read_only=True) @@ -34,25 +38,27 @@ def get_ntlm_connection(): Creates a connection to a server using NTLM authentication. Uses ldap3 :return: A ldap3 connection object with authentication set to NTLM. """ + logger.info("Establishing ntlm ldap connection") server = Server(configuration.LDAP_SERVER_URL, get_info=ALL, use_ssl=configuration.LDAP_USE_SSL, port=configuration.LDAP_PORT) return Connection(server, user=configuration.LDAP_USER, password=configuration.LDAP_PASSWORD, authentication=NTLM, read_only=True) -def fetch_users_of_group(group): +def fetch_users_of_group(group_name): """ Searches all users of a specified group in the provided ldap-server. Returns the user objects as an array of dictionaries. Each dictionary resembles one user object containing the value "login". :param group: The LDAP-group the users should be searched in. :return: An array containing dictionaries each of which defines a user found in the provided group. """ + logger.info("Fetching users of ldap group %s " % group_name) result = [] connection.bind() if configuration.LDAP_GROUP_SEARCH_FILTER: - group_query_filter = "(&(cn=" + group + ")" + configuration.LDAP_GROUP_SEARCH_FILTER + ")" + group_query_filter = "(&(cn=" + group_name + ")" + configuration.LDAP_GROUP_SEARCH_FILTER + ")" else: - group_query_filter = "(cn=" + group + ")" + group_query_filter = "(cn=" + group_name + ")" groups = connection.extend.standard.paged_search(search_base=configuration.LDAP_GROUP_SEARCH_BASE, search_filter=group_query_filter, search_scope=SUBTREE, @@ -65,6 +71,7 @@ def fetch_users_of_group(group): user_query_filter = configuration.LDAP_USER_SEARCH_FILTER else: user_query_filter = "(objectClass=*)" + logger.info("Fetching user %s of ldap group %s " % (user, group_name)) user_data = connection.extend.standard.paged_search(search_base=user, search_scope=SUBTREE, search_filter=user_query_filter,