Skip to content

Commit 1fd7e2c

Browse files
committed
Added explicit dependency for LDAP
1 parent c4a1b7e commit 1fd7e2c

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
init:
22
pip install -r requirements.txt
33

4+
bundle:
5+
rm grafana-ldap-sync-script.zip
6+
zip grafana-ldap-sync-script.zip \
7+
LICENSE \
8+
README.md \
9+
run.py \
10+
requirements.txt \
11+
config.yml \
12+
example.csv \
13+
script/* \
14+
-x 'script/__pycache__**'
15+
416
test:
517
nosetests tests

README.md

+50-14
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,77 @@
22
A script to get Grafana users, teams and their permissions from an LDAP server and keep it in sync.
33

44
## Installation
5-
Install all dependencies
5+
Install all dependencies.
66
```bash
77
pip install -r requirements.txt
88
```
99

10+
or consider to install the dependencies only for the user which will be executing the script:
11+
12+
```bash
13+
$ pip install --user -r requirements.txt
14+
```
15+
1016
## Running the Script
11-
The script can be simply run with:
17+
18+
*The script requires Python 3 to run!*
19+
20+
It can be simply run with:
1221
```bash
13-
python run.py [-h] --config *path-to-config.yml* --bind *path-to-bind-csv* [--dry-run]
22+
$ python run.py [-h] --config <path-to-config.yml> --bind <path-to-bind-csv> [--dry-run]
1423
```
1524

1625
## Usage
1726
If you just want to test the script, there is an example.csv predefined. Just enter your grafana credentials in the config.yml.
1827
The used LDAP-Server can be found [here](https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/).
1928

20-
#### Config
29+
### Config
2130
Before starting the script you need to enter your grafana & ldap credentials in the config.yml. You also need to add the
2231
path to your .csv file containing the bindings.
2332

24-
#### Binding
33+
### Binding
2534
To bind LDAP-groups to grafana-teams and grant these teams access to folders you need to provide a .csv file. Please note
2635
that the first row of the csv is recognized as a header-row and is therefore being ignored.
2736
The file needs to contain the following information in this exact order:
28-
<br>
37+
* **LDAP-Group**: The LDAP group which will be used for mapping.
38+
* **Grafana-Team Name**: The name of the Grafana team which will be created (if not exist) and where the group's users will be added to.
39+
* **Grafana-Team ID**: The ID of the Grafana team (currently not used).
40+
* **Grafana-Folder Name**: The Grafana folder which will be created (if not exist) and where the group's users will have the specified permission to.
41+
* **Grafana-Folder UUID**: The UUID of the Grafana folder.
42+
* **Grafana-Folder Permission**: The users' permission for the specified Grafana folder. (`View`, `Edit`, `Admin`)
43+
44+
Missing folders, teams and users will be created by the script.
45+
Teams and users which are not existing in the LDAP mapping will be removed. Note: the user used by the script will not be deleted!
46+
47+
#### Example CSV
2948
```CSV
30-
LDAP-Group, Grafana-Team Name, Grafana-Team ID, Grafana-Folder ID, Grafana-Folder UUID, Grafana-Folder Permission
49+
ZBV/LDAP-Gruppe,Grafana-Team-Name,Grafana-Team-ID,Grafana-Folder-Name,Grafana-Folder-UUID,Grafana-Folder-Permissions
50+
mathematicians,mathematicians,0,Math,math_folder,Admin
51+
mathematicians,smart_people,0,Common Dashboards,all_folder,View
52+
scientists,scientists,0,Science,science_folder,Edit
53+
scientists,smart_people,0,Common Dashboards,all_folder,View
3154
```
32-
Missing folders, teams and users will be created by the script.
33-
<br>
34-
Possible Grafana-Folder permissions are:
35-
- View
36-
- Edit
37-
- Admin
55+
56+
Using this CSV mapping will result in the following operations:
57+
* The Grafana teams `mathematicians`, `smart_people` and `scientists` will be created.
58+
* The Grafana folders `Math`, `Common Dashboards` and `Science` will be created.
59+
* All users in the `mathematicians` LDAP group will be member of the Grafana team `mathematicians` and `smart_people`.
60+
* All users in the `scientists` LDAP group will be member of the Grafana team `scientists`.
61+
* All users in the `mathematicians` LDAP group will get `Admin` access to the `Math` folder.
62+
* All users in the `mathematicians` LDAP group will get `View` access to the `Common Dashboards` folder.
63+
* All users in the `scientists` LDAP group will get `Edit` access to the `Science` folder.
64+
* All users in the `scientists` LDAP group will get `View` access to the `Common Dashboards` folder.
3865

3966
#### Removing Bindings
4067
When a binding is removed in your .csv-file, this binding is also removed by the script. So if there is a team in your grafana instance which
4168
is not defined by the current binding the team will be deleted. This also applies to users. **This does not apply to folders!
42-
Folders need to be deleted manually if not needed anymore!**
69+
Folders need to be deleted manually if not needed anymore!**
70+
71+
72+
## Bundle Scripts
73+
74+
Using the Makefile, you can bundle all the scripts into a single zip-archive.
75+
76+
```
77+
$ make bundle
78+
```

requirements.dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mock~=4.0.2
2+
setuptools~=49.2.0

requirements.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
requests~=2.24.0
22
grafana_api~=1.0.2
3-
ldap3~=2.7
4-
mock~=4.0.2
3+
ldap3~=2.6
54
PyYAML~=5.3.1
6-
setuptools~=49.2.0
5+
pyasn1>=0.4.6

run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def setup_logger():
1515
"""
1616
Setting up the used logger. The 'mutate' logger will print whether dry-run is used and changes are being applied.
1717
"""
18-
log_format = '%(asctime)s - %(levelname)s - %(module)7s - %(message)s'
18+
log_format = '%(asctime)s - %(levelname)5s - %(module)7s - %(message)s'
1919
log_format_mut = log_format
2020

2121
if args.dry_run:
22-
log_format_mut = '%(asctime)s - %(levelname)s - %(module)7s - [SKIPPED] %(message)s'
22+
log_format_mut = '%(asctime)s - %(levelname)5s - %(module)7s - [SKIPPED] %(message)s'
2323
else:
2424
log_format_mut = log_format
2525

setup.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='grafana-ldap-sync-script',
5-
version='0.1.0',
5+
version='1.1.0',
66
description='Script for syncing LDAP Users & Groups with Grafana Users & Teams',
77
packages=find_packages(exclude=('tests', 'docs')),
88
package_data={'grafana-ldap-syn-script': ['run.py']},
@@ -13,6 +13,7 @@
1313
"ldap3>=2.7",
1414
"mock>=4.0.2",
1515
"PyYAML>=5.3.1",
16-
"setuptools>=9.2.0"]
16+
"setuptools>=9.2.0",
17+
"pyasn1>=0.4.6"]
1718
)
1819

0 commit comments

Comments
 (0)