Compare commits
30 Commits
7e62a9ff70
...
main
Author | SHA1 | Date | |
---|---|---|---|
536f11bbfd
|
|||
271f3289e5
|
|||
ac56849a3b
|
|||
0050a4130b
|
|||
828d5853d6
|
|||
197d699fda
|
|||
ce10a8595d
|
|||
94eea2d425
|
|||
3eb40713b1
|
|||
a1183aab56
|
|||
de7e645f11
|
|||
67d2c839a5
|
|||
0f773262a1
|
|||
30dcbcb407
|
|||
98832beb27
|
|||
4008a92a49
|
|||
2b6d82229c
|
|||
d0c6d44528
|
|||
e429b56384
|
|||
5b174702d8
|
|||
703fcaec39
|
|||
69a4634f8a
|
|||
e349c5e0f3
|
|||
48b47cfa88
|
|||
660dd1854f
|
|||
69692faac5
|
|||
95153cd55b
|
|||
9f3c42e3f9
|
|||
67f9dc73e5
|
|||
58b4bc47a6
|
@@ -1,5 +1,6 @@
|
|||||||
[
|
[
|
||||||
[ "selfprivacy", "domain" ],
|
[ "selfprivacy", "domain" ],
|
||||||
|
[ "selfprivacy", "username" ],
|
||||||
[ "selfprivacy", "modules", "auth", "enable" ],
|
[ "selfprivacy", "modules", "auth", "enable" ],
|
||||||
[ "selfprivacy", "modules", "mastodon" ],
|
[ "selfprivacy", "modules", "mastodon" ],
|
||||||
[ "selfprivacy", "passthru", "auth" ],
|
[ "selfprivacy", "passthru", "auth" ],
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
{
|
{
|
||||||
# TODO: check whether there is no TODOs
|
# TODO: check whether there is no TODOs
|
||||||
# TODO: check whether there is no hedgegdoc mentions
|
|
||||||
description = "Mastodon module";
|
description = "Mastodon module";
|
||||||
|
|
||||||
outputs = { ... }:
|
outputs = { ... }:
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
import requests
|
import requests
|
||||||
import psycopg2 as ps
|
import psycopg2 as ps
|
||||||
|
|
||||||
@@ -8,55 +8,141 @@ def read_file(path):
|
|||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
def getenv(name):
|
def getenv(name):
|
||||||
try:
|
try:
|
||||||
return os.environ[name]
|
return os.environ[name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print(f"Missing environment variable {name}. You should NOT run this script by hand, please use systemd mastodon-kanidm-sync.service.")
|
print(f"[ERROR] Missing environment variable {name}. You should NOT run this script by hand, please use systemd mastodon-kanidm-sync.service.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
# Import configuration
|
||||||
KANIDM_URL = getenv("KANIDM_URL")
|
KANIDM_URL = getenv("KANIDM_URL")
|
||||||
KANIDM_TOKEN = read_file(getenv("KANIDM_TOKEN_PATH")).strip()
|
KANIDM_TOKEN = read_file(getenv("KANIDM_TOKEN_PATH")).strip()
|
||||||
# USERDATA = read_file(getenv("USERDATA_FILE_PATH")).strip()
|
OWNER_USERNAME = getenv("OWNER_USERNAME")
|
||||||
|
SLEEP_TIME = int(getenv("SLEEP_TIME"))
|
||||||
|
|
||||||
conn = ps.connect(
|
def sync_mastodon():
|
||||||
dbname=getenv("POSTGRES_DBNAME"),
|
# Fetch kanidm users list from userdata file
|
||||||
user=getenv("POSTGRES_USER"),
|
# Userdata file is json list with information about what users are configured by kanidm
|
||||||
host=getenv("POSTGRES_HOST")
|
try:
|
||||||
)
|
USERDATA = read_file(getenv("USERDATA_FILE_PATH")).strip()
|
||||||
|
userdata = json.loads(USERDATA)
|
||||||
|
print("[INFO] ")
|
||||||
|
except FileNotFoundError:
|
||||||
|
userdata = []
|
||||||
|
|
||||||
cur = conn.cursor()
|
# Load database
|
||||||
cur.execute('''
|
conn = ps.connect(
|
||||||
SELECT identities.uid, users.id, user_roles.name
|
dbname=getenv("POSTGRES_DBNAME"),
|
||||||
FROM users
|
user=getenv("POSTGRES_USER"),
|
||||||
JOIN identities
|
host=getenv("POSTGRES_HOST")
|
||||||
ON users.id = identities.id
|
)
|
||||||
LEFT JOIN user_roles
|
|
||||||
ON users.role_id = user_roles.id;
|
|
||||||
'''
|
|
||||||
)
|
|
||||||
|
|
||||||
state = cur.fetchall()
|
# Fetch current userdata from database
|
||||||
print(state) # DEBUG
|
cur = conn.cursor()
|
||||||
print(type(state)) # DEBUG
|
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;
|
||||||
|
'''
|
||||||
|
)
|
||||||
|
|
||||||
kanidm_users_raw = requests.get(
|
state = cur.fetchall()
|
||||||
f"{KANIDM_URL}/v1/person",
|
|
||||||
headers={
|
|
||||||
"Authorization": f"Bearer {KANIDM_TOKEN}",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
timeout=5,
|
|
||||||
).json()
|
|
||||||
|
|
||||||
for i in kanidm_users_raw:
|
users = {}
|
||||||
i = i["attrs"]
|
for i in state:
|
||||||
uid = i["name"]
|
users[i[0]] = {
|
||||||
# if uid in db_users:
|
"id": i[1],
|
||||||
# print(uid)
|
"role": i[2],
|
||||||
print(uid)
|
"isKanidmUser": False
|
||||||
|
}
|
||||||
|
|
||||||
cur.close()
|
# Fetch Kanidm userdata
|
||||||
conn.close()
|
kanidm_users_raw = requests.get(
|
||||||
|
f"{KANIDM_URL}/v1/person",
|
||||||
|
headers={
|
||||||
|
"Authorization": f"Bearer {KANIDM_TOKEN}",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
timeout=5,
|
||||||
|
).json()
|
||||||
|
|
||||||
|
def give_role(uid, role, putUserdata = True):
|
||||||
|
if (uid not in userdata) and (putUserdata):
|
||||||
|
userdata.append(uid)
|
||||||
|
users[uid]["isKanidmUser"] = True
|
||||||
|
users[uid]["role"] = role
|
||||||
|
print(f"[INFO] {uid} is marked as {role}")
|
||||||
|
|
||||||
|
|
||||||
|
for i in kanidm_users_raw:
|
||||||
|
i = i["attrs"]
|
||||||
|
for uid in i["name"]: # [user].attrs.name is a list
|
||||||
|
if uid in users: # Don't apply anything for users who have no mastodon access (sp.mastodon.users) or didn't register
|
||||||
|
if uid == OWNER_USERNAME:
|
||||||
|
give_role(uid, "Owner", False)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for group in i["memberof"]:
|
||||||
|
if group.startswith("sp.mastodon.admins@") or group.startswith("sp.admins@"):
|
||||||
|
give_role(uid, "Admin")
|
||||||
|
break
|
||||||
|
|
||||||
|
elif group.startswith("sp.mastodon.moderators@"):
|
||||||
|
give_role(uid, "Moderator")
|
||||||
|
break
|
||||||
|
|
||||||
|
elif uid in userdata:
|
||||||
|
# If user, who previously had a role, has no roles set by Kanidm, delete them from userdata list so allow setting roles directly by mastodon
|
||||||
|
give_role(uid, None, False)
|
||||||
|
userdata.remove(uid)
|
||||||
|
|
||||||
|
print("[DEBUG]", users)
|
||||||
|
|
||||||
|
# Fetch RoleIDs
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("SELECT id, name FROM user_roles;")
|
||||||
|
|
||||||
|
roles_raw = cur.fetchall()
|
||||||
|
roles = {}
|
||||||
|
for i in roles_raw:
|
||||||
|
roles[i[1]] = i[0]
|
||||||
|
|
||||||
|
# Give roles
|
||||||
|
for uid in users:
|
||||||
|
if not users[uid]["isKanidmUser"]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if users[uid]["role"]:
|
||||||
|
rolename = users[uid]["role"]
|
||||||
|
roleid = roles[rolename]
|
||||||
|
else:
|
||||||
|
roleid = "NULL"
|
||||||
|
|
||||||
|
sqlcommand = f"UPDATE users SET role_id = {roleid} WHERE id = {users[uid]["id"]};"
|
||||||
|
print("[DEBUG] SQL:", sqlcommand)
|
||||||
|
cur.execute(sqlcommand)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
print("[INFO] Final userdata.json file content: ", userdata)
|
||||||
|
|
||||||
|
def write_userdata(mode):
|
||||||
|
with open(getenv("USERDATA_FILE_PATH"), mode) as f:
|
||||||
|
f.write(json.dumps(userdata))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
try:
|
||||||
|
write_userdata("w")
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("[INFO] userdata.json file doesn't exist. Creating it")
|
||||||
|
write_userdata("x")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
sync_mastodon()
|
||||||
|
time.sleep(SLEEP_TIME)
|
||||||
|
23
module.nix
23
module.nix
@@ -91,13 +91,13 @@ in
|
|||||||
enableUnixSocket = false;
|
enableUnixSocket = false;
|
||||||
configureNginx = true;
|
configureNginx = true;
|
||||||
database.createLocally = true;
|
database.createLocally = true;
|
||||||
streamingProcesses = 3;
|
streamingProcesses = 2;
|
||||||
|
|
||||||
smtp = {
|
smtp = {
|
||||||
createLocally = false;
|
createLocally = false;
|
||||||
|
|
||||||
fromAddress = "mastodon@${sp.domain}";
|
fromAddress = "noreply.mastodon@${sp.domain}";
|
||||||
user = "mastodon";
|
user = "noreply.mastodon";
|
||||||
passwordFile = secrets.passwordFile;
|
passwordFile = secrets.passwordFile;
|
||||||
authenticate = true;
|
authenticate = true;
|
||||||
|
|
||||||
@@ -105,8 +105,7 @@ in
|
|||||||
port = 465;
|
port = 465;
|
||||||
};
|
};
|
||||||
extraConfig = {
|
extraConfig = {
|
||||||
# "SMTP_ENABLE_STARTTLS" = "never";
|
"SMTP_ENABLE_STARTTLS_AUTO" = "true"; # Simple NixOS MailServer doesn't allow connections without SSL
|
||||||
"SMTP_ENABLE_STARTTLS_AUTO" = "true";
|
|
||||||
"SMTP_ENABLE_STARTTLS" = "always";
|
"SMTP_ENABLE_STARTTLS" = "always";
|
||||||
"SMTP_TLS" = "true";
|
"SMTP_TLS" = "true";
|
||||||
"SMTP_SSL" = "true";
|
"SMTP_SSL" = "true";
|
||||||
@@ -114,10 +113,8 @@ in
|
|||||||
"DISALLOW_UNAUTHENTICATED_API_ACCESS" = lib.boolToString cfg.dissallowUnauthenticatedAPI;
|
"DISALLOW_UNAUTHENTICATED_API_ACCESS" = lib.boolToString cfg.dissallowUnauthenticatedAPI;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
users.users.mastodon.isSystemUser = lib.mkForce false;
|
|
||||||
users.users.mastodon.isNormalUser = lib.mkForce true;
|
|
||||||
|
|
||||||
selfprivacy.emails."mastodon" = {
|
selfprivacy.emails."noreplymastodon" = {
|
||||||
hashedPasswordFile = secrets.hashedPasswordFile;
|
hashedPasswordFile = secrets.hashedPasswordFile;
|
||||||
systemdTargets = [ "mastodon-email-password-setup.service" ];
|
systemdTargets = [ "mastodon-email-password-setup.service" ];
|
||||||
sendOnly = true;
|
sendOnly = true;
|
||||||
@@ -128,6 +125,7 @@ in
|
|||||||
enable = true;
|
enable = true;
|
||||||
wantedBy = [ "multi-user.target" "mastodon-web.service" "postfix.service" ];
|
wantedBy = [ "multi-user.target" "mastodon-web.service" "postfix.service" ];
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
|
Slice = "mastodon.slice";
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
ExecStart = pkgs.writeShellScript "gen-mastodon-email-password" ''
|
ExecStart = pkgs.writeShellScript "gen-mastodon-email-password" ''
|
||||||
export password=$(head -c 32 /dev/urandom | base64 | sed 's/[+=\\/A-Z]//g')
|
export password=$(head -c 32 /dev/urandom | base64 | sed 's/[+=\\/A-Z]//g')
|
||||||
@@ -150,7 +148,6 @@ in
|
|||||||
|
|
||||||
services.mastodon-kanidm-sync = {
|
services.mastodon-kanidm-sync = {
|
||||||
after = [
|
after = [
|
||||||
# "mastodon.service" # TODO: ??
|
|
||||||
"postgresql.service"
|
"postgresql.service"
|
||||||
"kanidm.service"
|
"kanidm.service"
|
||||||
];
|
];
|
||||||
@@ -166,6 +163,9 @@ in
|
|||||||
POSTGRES_DBNAME = db.name;
|
POSTGRES_DBNAME = db.name;
|
||||||
POSTGRES_USER = db.user;
|
POSTGRES_USER = db.user;
|
||||||
POSTGRES_HOST = db.host;
|
POSTGRES_HOST = db.host;
|
||||||
|
USERDATA_FILE_PATH = "/var/lib/mastodon/.userdata.json";
|
||||||
|
OWNER_USERNAME = sp.username;
|
||||||
|
SLEEP_TIME = "30";
|
||||||
};
|
};
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Slice = "mastodon.slice";
|
Slice = "mastodon.slice";
|
||||||
@@ -176,7 +176,7 @@ in
|
|||||||
doCheck = false;
|
doCheck = false;
|
||||||
libraries = with pkgs.python3Packages; [
|
libraries = with pkgs.python3Packages; [
|
||||||
requests
|
requests
|
||||||
psycopg
|
psycopg2
|
||||||
];
|
];
|
||||||
} (builtins.readFile ./mastodon-kanidm-sync.py);
|
} (builtins.readFile ./mastodon-kanidm-sync.py);
|
||||||
};
|
};
|
||||||
@@ -185,7 +185,7 @@ in
|
|||||||
services.mastodon-web = {
|
services.mastodon-web = {
|
||||||
unitConfig.RequiresMountsFor = lib.mkIf sp.useBinds "/volumes/${cfg.location}/mastodon";
|
unitConfig.RequiresMountsFor = lib.mkIf sp.useBinds "/volumes/${cfg.location}/mastodon";
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Slice = "hedgedoc.slice";
|
Slice = "mastodon.slice";
|
||||||
LoadCredential = ["client-secret:${oauthClientSecretFP}"];
|
LoadCredential = ["client-secret:${oauthClientSecretFP}"];
|
||||||
ExecStart = lib.mkForce (pkgs.writeShellScript "run-mastodon-with-client-secret" ''
|
ExecStart = lib.mkForce (pkgs.writeShellScript "run-mastodon-with-client-secret" ''
|
||||||
export OIDC_CLIENT_SECRET=$(cat $CREDENTIALS_DIRECTORY/client-secret)
|
export OIDC_CLIENT_SECRET=$(cat $CREDENTIALS_DIRECTORY/client-secret)
|
||||||
@@ -209,6 +209,7 @@ in
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
services.kanidm.provision.groups."sp.mastodon.moderators" = {};
|
||||||
selfprivacy.auth.clients.${oauthClientID} = {
|
selfprivacy.auth.clients.${oauthClientID} = {
|
||||||
inherit usersGroup;
|
inherit usersGroup;
|
||||||
inherit adminsGroup;
|
inherit adminsGroup;
|
||||||
|
Reference in New Issue
Block a user