commit 8f14819c22259a7b39247d80c2be2c43e8389509 Author: thopic Date: Mon May 17 17:30:04 2021 +0200 Initial commit diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..121d3c9 --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +GITEA_CN = +MAILCOW_CN = +NEXTCLOUD_CN = +MATTERMOST_CN = +MATTERMOST_TOKEN = +NEXTCLOUD_TOKEN = +NEXTCLOUD_USER = +GITEA_TOKEN = +MAILCOW_TOKEN = +EXCLUDED_EMAILS = diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b72af0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1a3162 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# GNOUS administration scripts + +This repo aims to provide a set of various tools for GNOUS's administrative stuff. + +## Get email addresses of GNOUS users + +Because there is no centralized authentication system for GNOUS services, we rely on [get_emails.py](get_emails.py) to fetch all users emails from the various services. +This is useful when GNOUS needs to communicate important information. +It also gives more transparency about the way personal data is processed (only displayed, not stored anywhere). diff --git a/api_requests.py b/api_requests.py new file mode 100644 index 0000000..3ed9e53 --- /dev/null +++ b/api_requests.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +def gitea(endpoint): + server = 'https://' + os.environ["GITEA_CN"] + '/api/v1/' + headers = {"Authorization": "token " + os.environ['GITEA_TOKEN']} + + return requests.get(server+endpoint, headers=headers).json() + +def mailcow(endpoint): + server = 'https://' + os.environ["MAILCOW_CN"] + '/api/v1/' + headers = {"X-API-Key": os.environ['MAILCOW_TOKEN']} + + return requests.get(server+endpoint, headers=headers).json() + +def mattermost(endpoint): + server = 'https://' + os.environ["MATTERMOST_CN"] + '/api/v4/' + headers = {"Authorization": "Bearer " + os.environ['MATTERMOST_TOKEN']} + + return requests.get(server+endpoint, headers=headers).json() + +def nextcloud(endpoint): + server = 'https://' + os.environ["NEXTCLOUD_CN"] + '/ocs/v2.php/' + format = "?format=json" + auth_user = os.environ["NEXTCLOUD_USER"] + auth_token = os.environ["NEXTCLOUD_TOKEN"] + headers = { "OCS-APIRequest": "true" } + + return requests.get(server+endpoint+format, auth=(auth_user, auth_token), headers=headers).json() diff --git a/get_emails.py b/get_emails.py new file mode 100755 index 0000000..aba9a1f --- /dev/null +++ b/get_emails.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +import os +import api_requests as api +from dotenv import load_dotenv + +load_dotenv() + +ENDPOINTS = {'mailcow' : 'get/mailbox/all', + 'nextcloud' : 'cloud/users', + 'gitea' : 'admin/users', + 'mattermost' : 'users'} + +emails = [] + +for user in api.gitea(ENDPOINTS["gitea"]): + emails.append(user["email"]) + +for user in api.mattermost(ENDPOINTS["mattermost"]): + if "is_bot" not in user: + emails.append(user["email"]) + +for user in api.mailcow(ENDPOINTS["mailcow"]): + if user["username"] not in os.environ["EXCLUDED_EMAILS"]: + emails.append(user["username"]) + +for user in api.nextcloud(ENDPOINTS["nextcloud"])["ocs"]["data"]["users"]: + emails.append(api.nextcloud(ENDPOINTS["nextcloud"] + '/' + user)["ocs"]["data"]["email"]) + +emails = sorted(set(emails)) +print("Emails count : " + str(len(emails))) +print("=================") +for email in emails: + print(email + ';')