Skip to content

Commit ef32345

Browse files
author
mbrill-nt
committed
Added logging of current actions.
1 parent 53ffd34 commit ef32345

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

script/grafana.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
grafana_api = ""
77
configuration = ""
88

9+
logging.basicConfig(level=logging.INFO)
10+
logger = logging.getLogger("grafana-ldap-sync-script")
911

1012
def setup_grafana(config_dict):
1113
global grafana_api, configuration
@@ -26,8 +28,9 @@ def delete_team_by_name(name):
2628
if len(team_data) > 0:
2729
for data_set in team_data:
2830
if configuration.DRY_RUN:
29-
print("Would have deleted team with name: %s and id: %s" % (name, data_set["id"]))
31+
logger.info("Would have deleted team with name: %s and id: %s" % (name, data_set["id"]))
3032
else:
33+
logger.info("Deleting team with name %s and id %s" % (name, data_set["id"]))
3134
grafana_api.teams.delete_team(data_set["id"])
3235
return True
3336
return False
@@ -41,8 +44,9 @@ def create_team(name, mail):
4144
:return: The API response.
4245
"""
4346
if configuration.DRY_RUN:
44-
print("Would have created team with name: %s" % name)
47+
logger.info("Would have created team with name: %s" % name)
4548
else:
49+
logger.info("Creating team with name %s" % name)
4650
return grafana_api.teams.add_team({
4751
"name": name,
4852
"mail": mail
@@ -58,8 +62,11 @@ def create_user_with_random_pw(user):
5862
user_dict["password"] = get_random_alphanumerical()
5963
user_dict["OrgId"] = 1
6064
if configuration.DRY_RUN:
61-
print("Would have created user with json %s" % str(user_dict))
65+
logger.info("Would have created user with json %s" % str(user_dict))
6266
else:
67+
logger.info("Creating user with login %s, name %s and mail %s" %
68+
(user_dict["login"], user_dict["name"], user_dict["email"])
69+
)
6370
grafana_api.admin.create_user(user_dict)
6471

6572

@@ -71,8 +78,9 @@ def delete_user_by_login(login):
7178
"""
7279
if not login == "admin":
7380
if configuration.DRY_RUN:
74-
print("Would have deleted user with name: %s" % login)
81+
logger.info("Would have deleted user with name: %s" % login)
7582
else:
83+
logger.info("Deleting user with name %s" % login)
7684
return grafana_api.admin.delete_user(grafana_api.users.find_user(login)["id"])
7785
return False
7886

@@ -87,8 +95,9 @@ def create_folder(folder_name, folder_uuid):
8795
"""
8896
try:
8997
if configuration.DRY_RUN:
90-
print("Would have created folder with name: %s and id: %s" % (folder_name, folder_uuid))
98+
logger.info("Would have created folder with name: %s and id: %s" % (folder_name, folder_uuid))
9199
else:
100+
logger.info("Creating folder with name %s and id %s" % (folder_name, folder_uuid))
92101
return grafana_api.folder.create_folder(folder_name, folder_uuid)
93102
except GrafanaClientError:
94103
return False
@@ -102,8 +111,9 @@ def add_user_to_team(login, team):
102111
"""
103112
try:
104113
if configuration.DRY_RUN:
105-
print("Would have added user %s to team %s" % (login, team))
114+
logger.info("Would have added user %s to team %s" % (login, team))
106115
else:
116+
logger.info("Adding user %s to team %s" % (login, team))
107117
grafana_api.teams.add_team_member(get_id_of_team(team), get_id_by_login(login))
108118
except GrafanaBadInputError:
109119
return False
@@ -116,6 +126,7 @@ def get_members_of_team(team):
116126
:param team: The name of the team the members should be returned of.
117127
:return: An array containing all users as described above.
118128
"""
129+
logger.info("Fetching members of team %s" % team)
119130
teams = grafana_api.teams.get_team_by_name(team)
120131
if not teams:
121132
return []
@@ -134,6 +145,7 @@ def remove_member_from_team(grafana_team, user_login):
134145
if configuration.DRY_RUN:
135146
print("Would have removed user %s from team %s" % (grafana_team, user_login))
136147
else:
148+
logger.info("Removing user %s from team %s" % (grafana_team, user_login))
137149
grafana_api.teams.remove_team_member(get_id_of_team(grafana_team), get_id_by_login(user_login))
138150

139151

@@ -189,8 +201,9 @@ def update_folder_permissions(folder_id, permissions):
189201
Sets the given permissions for the folder found under the given id
190202
"""
191203
if configuration.DRY_RUN:
192-
print("Would have set permission of folder %s to %s" % (folder_id, permissions))
204+
logger.info("Would have set permission of folder %s to %s" % (folder_id, permissions))
193205
else:
206+
logger.info("Setting permission of folder %s to %s" % (folder_id, permissions))
194207
grafana_api.folder.update_folder_permissions(folder_id, {"items": permissions})
195208

196209

@@ -205,6 +218,7 @@ def get_all_users():
205218
"""
206219
Returns all users present in the connected grafana instance.
207220
"""
221+
logger.info("Fetching all grafana users")
208222
user_logins = []
209223
users = grafana_api.users.search_users()
210224
if users is not None:

script/ldap.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from ldap3 import Server, Connection, ALL, SUBTREE, NTLM
2+
import logging
23

34
from .config import config
45
from .helpers import *
56

7+
logging.basicConfig(level=logging.INFO)
8+
logger = logging.getLogger("grafana-ldap-sync-script")
69

710
configuration = ""
811
user_cache = {}
@@ -24,6 +27,7 @@ def get_ldap_connection():
2427
Creates a connection to the ldap-server provided in the config. Uses ldap3.
2528
:return: A ldap3 connection object.
2629
"""
30+
logger.info("Establishing standard ldap connection")
2731
server = Server(configuration.LDAP_SERVER_URL, get_info=ALL, use_ssl=configuration.LDAP_USE_SSL,
2832
port=configuration.LDAP_PORT)
2933
return Connection(server, configuration.LDAP_USER, configuration.LDAP_PASSWORD, auto_bind=True, read_only=True)
@@ -34,25 +38,27 @@ def get_ntlm_connection():
3438
Creates a connection to a server using NTLM authentication. Uses ldap3
3539
:return: A ldap3 connection object with authentication set to NTLM.
3640
"""
41+
logger.info("Establishing ntlm ldap connection")
3742
server = Server(configuration.LDAP_SERVER_URL, get_info=ALL, use_ssl=configuration.LDAP_USE_SSL,
3843
port=configuration.LDAP_PORT)
3944
return Connection(server, user=configuration.LDAP_USER,
4045
password=configuration.LDAP_PASSWORD, authentication=NTLM, read_only=True)
4146

4247

43-
def fetch_users_of_group(group):
48+
def fetch_users_of_group(group_name):
4449
"""
4550
Searches all users of a specified group in the provided ldap-server. Returns the user objects as an array of
4651
dictionaries. Each dictionary resembles one user object containing the value "login".
4752
:param group: The LDAP-group the users should be searched in.
4853
:return: An array containing dictionaries each of which defines a user found in the provided group.
4954
"""
55+
logger.info("Fetching users of ldap group %s " % group_name)
5056
result = []
5157
connection.bind()
5258
if configuration.LDAP_GROUP_SEARCH_FILTER:
53-
group_query_filter = "(&(cn=" + group + ")" + configuration.LDAP_GROUP_SEARCH_FILTER + ")"
59+
group_query_filter = "(&(cn=" + group_name + ")" + configuration.LDAP_GROUP_SEARCH_FILTER + ")"
5460
else:
55-
group_query_filter = "(cn=" + group + ")"
61+
group_query_filter = "(cn=" + group_name + ")"
5662
groups = connection.extend.standard.paged_search(search_base=configuration.LDAP_GROUP_SEARCH_BASE,
5763
search_filter=group_query_filter,
5864
search_scope=SUBTREE,
@@ -65,6 +71,7 @@ def fetch_users_of_group(group):
6571
user_query_filter = configuration.LDAP_USER_SEARCH_FILTER
6672
else:
6773
user_query_filter = "(objectClass=*)"
74+
logger.info("Fetching user %s of ldap group %s " % (user, group_name))
6875
user_data = connection.extend.standard.paged_search(search_base=user,
6976
search_scope=SUBTREE,
7077
search_filter=user_query_filter,

0 commit comments

Comments
 (0)