63 lines
1.3 KiB
Python
63 lines
1.3 KiB
Python
import os
|
|
import time
|
|
import json
|
|
import requests
|
|
import psycopg2 as ps
|
|
|
|
def read_file(path):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return f.read()
|
|
|
|
|
|
def getenv(name):
|
|
try:
|
|
return os.environ[name]
|
|
except KeyError:
|
|
print(f"Missing environment variable {name}. You should NOT run this script by hand, please use systemd mastodon-kanidm-sync.service.")
|
|
exit(1)
|
|
|
|
|
|
KANIDM_URL = getenv("KANIDM_URL")
|
|
KANIDM_TOKEN = read_file(getenv("KANIDM_TOKEN_PATH")).strip()
|
|
# USERDATA = read_file(getenv("USERDATA_FILE_PATH")).strip()
|
|
|
|
conn = ps.connect(
|
|
dbname=getenv("POSTGRES_DBNAME"),
|
|
user=getenv("POSTGRES_USER"),
|
|
host=getenv("POSTGRES_HOST")
|
|
)
|
|
|
|
cur = conn.cursor()
|
|
cur.execute('''
|
|
SELECT identities.uid, users.id, user_roles.name
|
|
FROM users
|
|
JOIN identities
|
|
ON users.id = identities.id
|
|
LEFT JOIN user_roles
|
|
ON users.role_id = user_roles.id;
|
|
'''
|
|
)
|
|
|
|
state = cur.fetchall()
|
|
print(state) # DEBUG
|
|
print(type(state)) # DEBUG
|
|
|
|
kanidm_users_raw = requests.get(
|
|
f"{KANIDM_URL}/v1/person",
|
|
headers={
|
|
"Authorization": f"Bearer {KANIDM_TOKEN}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
timeout=5,
|
|
).json()
|
|
|
|
for i in kanidm_users_raw:
|
|
i = i["attrs"]
|
|
uid = i["name"]
|
|
# if uid in db_users:
|
|
# print(uid)
|
|
print(uid)
|
|
|
|
cur.close()
|
|
conn.close()
|