blezz-tech 2 недель назад
Родитель
Сommit
058450f4a8
1 измененных файлов с 360 добавлено и 0 удалено
  1. 360 0
      generate_init_ldif.py

+ 360 - 0
generate_init_ldif.py

@@ -0,0 +1,360 @@
+#DATA
+
+main_init = """# init.ldif
+
+##########
+#
+# Table of Content
+#
+##########
+
+
+# Корневая запись (базовый DN)
+## ROOT DN
+# All ou
+## UID FOR HANDLERS
+## UID FOR OU=USERS
+# All cn
+## CN FOR OU=GROUP
+# Все пользователи
+
+
+##########
+#
+# ROOT-DB
+#
+##########
+
+dn: dc=ldap,dc=sharix,dc=ru
+objectClass: dcObject
+objectClass: organization
+dc: ldap
+o: Sharix LDAP Server
+"""
+
+extra_test_users = """
+dn: uid=11111111111,ou=users,dc=ldap,dc=sharix,dc=ru
+objectClass: sharixAccount
+uid: 11111111111
+cn: Django Admin
+sn: First
+#maybe set the same for all for the beggining? Now for testing - let's make it like Django main admin password
+userPassword: {ARGON2}$argon2id$v=19$m=65536,t=3,p=1$85j887FtSxU4NRVTLx2Tiw$AE/HdQ9y446Vl9zdiKMXgDLID3D43pYC+ZleNiy/bMs
+givenName: Admin
+displayName: Django Admin
+mail: test@sharix-app.org
+jpegPhoto: 0
+telephoneNumber: 11111111111
+"""
+
+uid_django_user="""
+dn: uid=django_tickets,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: handlerAccount
+uid: django_tickets
+cn: django_tickets
+displayName: Django Tickets
+sn: notificator
+#maybe set the same for all for the beggining? Or make it like apikey? Now for testing - let's make it like Django main admin password
+userPassword: {ARGON2}$argon2id$v=19$m=65536,t=3,p=1$8kx90bsuQRoLoQ3F4Uh+Pw$PboqW5EPEfzQlFh3uDeWoXP8rXs7v510fwQgtoA2Lew
+"""
+
+extra_groups = """
+dn: cn=django_admin,ou=groups,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: django_admin
+description: Group for Django Admins
+member: uid=11111111111,ou=users,dc=ldap,dc=sharix,dc=ru
+"""
+
+extra_apps = """
+dn: cn=django,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: django
+description: Group for Django users
+member: cn=django_tickets,ou=apps,dc=ldap,dc=sharix,dc=ru
+
+dn: cn=ejabberd,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: ejabberd
+description: Group for Ejabberd users
+member: cn=11111111111,ou=users,dc=ldap,dc=sharix,dc=ru
+
+dn: cn=local,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: local
+description: Group for Local users
+member: cn=11111111111,ou=users,dc=ldap,dc=sharix,dc=ru
+
+dn: cn=mail,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: mail
+description: Group for Mail users
+member: cn=11111111111,ou=users,dc=ldap,dc=sharix,dc=ru
+"""
+
+
+handlers = [
+    ("open_access_request_pending",  "ACCREQ pending"),
+    ("open_access_request_accepted", "ACCREQ accepted"),
+    ("open_access_request_declined", "ACCREQ declined"),
+    ("open_neg_request_pending",     "NEGREQ pending"),
+    ("open_neg_request_accepted",    "NEGERQ accepted"),
+    ("open_neg_request_declined",    "NEGREQ declined"),
+    ("open_st_request_new",          "STREQ new"),
+    ("open_st_request_reopened",     "STREQ reopened"),
+    ("open_st_request_assigned",     "STREQ assigned"),
+    ("open_st_request_inprocess",    "STREQ inprocess"),
+    ("open_st_request_wontfix",      "STREQ wontfix"),
+    ("open_st_request_done",         "STREQ done"),
+]
+
+
+groups = [
+    ('METASERVICE-ADMIN'      , 21),
+    ('METASERVICE-SUPERVISOR' , 22),
+    ('METASERVICE-SUPPORT'    , 23),
+    ('METASERVICE-TECHSUPPORT', 24),
+
+    ('PARTNER-ADMIN'          , 31),
+    ('PARTNER-SUPERVISOR'     , 32),
+    ('PARTNER-SUPPORT'        , 33),
+    ('PARTNER-TECHSUPPORT'    , 34),
+
+    ('METASERVICE-PROVIDER'   , 41),
+
+    ('METASERVICE-CLIENT'     , 51),
+
+    ('METASERVICE-GUEST'      , 61),
+]
+
+
+groups_dict = dict(groups)
+
+
+test_users = [
+    ('METASERVICE-ADMIN'      , 'Metaservice', 'Admin'      , 1),
+    ('METASERVICE-SUPERVISOR' , 'Metaservice', 'Supervisor' , 3),
+    ('METASERVICE-SUPPORT'    , 'Metaservice', 'Support'    , 3),
+    ('METASERVICE-TECHSUPPORT', 'Metaservice', 'TechSupport', 3),
+
+    ('PARTNER-ADMIN'          , 'Parter'     , 'Admin'      , 1),
+    ('PARTNER-SUPERVISOR'     , 'Parter'     , 'Supervisor' , 3),
+    ('PARTNER-SUPPORT'        , 'Parter'     , 'Support'    , 3),
+    ('PARTNER-TECHSUPPORT'    , 'Parter'     , 'TechSupport', 3),
+
+    ('METASERVICE-PROVIDER'   , 'Metaservice', 'Provider'   , 3),
+
+    ('METASERVICE-CLIENT'     , 'Metaservice', 'Client'     , 3),
+
+    ('METASERVICE-GUEST'      , 'Metaservice', 'Guest'      , 3),
+]
+
+test_users_dict = {user[0]: user[3] for user in test_users}
+
+ous = [
+    ("users"    , "Подразделение для пользователей"),
+    ("groups"   , "Подразделение для групп (опционально)"),
+    ("apps"     , "Подразделение для классификации обработчиков и приложений (опционально)"),
+    ("appgroups", "Подразделение с аккаунтами приложений с их описанием и может быть apikey"),
+]
+
+# SUPPORT FUNCTIONS
+
+def int_to_roman(num):
+    val = [
+        1000, 900, 500, 400,
+        100, 90, 50, 40,
+        10, 9, 5, 4,
+        1
+    ]
+    syms = [
+        "M", "CM", "D", "CD",
+        "C", "XC", "L", "XL",
+        "X", "IX", "V", "IV",
+        "I"
+    ]
+    roman_num = ""
+    i = 0
+    while num > 0:
+        for _ in range(num // val[i]):
+            roman_num += syms[i]
+            num -= val[i]
+        i += 1
+    return roman_num
+
+
+def get_handlers_txt():
+    txt = ""
+    for name, display_name in handlers:
+        HANDLERS_PASSWORD = "{ARGON2}$argon2id$v=19$m=65536,t=3,p=1$8kx90bsuQRoLoQ3F4Uh+Pw$PboqW5EPEfzQlFh3uDeWoXP8rXs7v510fwQgtoA2Lew"
+        txt +=f"""dn: uid={name},ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: handlerAccount
+uid: {name}
+cn: {name}
+sn: handler
+displayName: {display_name}
+#maybe set the same for all for the beggining? Or make it like apikey? Now for testing - let's make it like Django main admin password
+userPassword: {HANDLERS_PASSWORD}
+
+"""
+    return txt
+
+
+def get_users_txt():
+    USERS_PASSWORD = "{ARGON2}$argon2id$v=19$m=65536,t=3,p=1$8kx90bsuQRoLoQ3F4Uh+Pw$PboqW5EPEfzQlFh3uDeWoXP8rXs7v510fwQgtoA2Lew"
+
+    txt = ""
+
+    for group_name, place, role, count in test_users:
+        for i in range(1, count + 1):
+            phone_number=f"{groups_dict[group_name]}0{i}"
+
+            uid = phone_number
+            cn = place
+            sn = f"{int_to_roman(i)}"
+            givenName = role
+            displayName = f"{place} {role} {int_to_roman(i)}"
+
+            # TODO: Почта для разных пользователей
+            # mail = f"test-{group_name.lower()}-{i}@domain.org"
+            mail = "test@sharix-app.org" 
+            
+            telephoneNumber = phone_number
+
+            user_entry = f"""
+dn: uid={uid},ou=users,dc=ldap,dc=sharix,dc=ru
+objectClass: sharixAccount
+uid: {uid}
+cn: {cn}
+sn: {sn}
+userPassword: {USERS_PASSWORD}
+givenName: {givenName}
+displayName: {displayName}
+mail: {mail}
+jpegPhoto: 0
+telephoneNumber: {telephoneNumber}
+"""
+            txt += user_entry
+
+    return txt
+
+
+def get_groups_txt():
+
+    txt = ""
+    for group, uid in groups:
+        txt += f"""
+dn: cn={group},ou=groups,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: {group}
+description: Group for {group.replace("-", " ").title()}
+"""
+        count = test_users_dict[group]
+        for i in range(1, count + 1):
+            user_id=f"{uid}0{i}"
+            txt += f"member: uid={user_id},ou=users,dc=ldap,dc=sharix,dc=ru\n"
+            
+    return txt
+
+
+def get_ou_txt():
+    return "\n".join(
+        f"""# {desc}
+dn: ou={name},dc=ldap,dc=sharix,dc=ru
+objectClass: organizationalUnit
+ou: {name}
+""" for name, desc in ous
+    )
+
+
+def get_group_handlers():
+    return """
+dn: cn=handlers,ou=apps,dc=ldap,dc=sharix,dc=ru
+objectClass: groupOfNames
+cn: handlers
+description: Group for Handlers
+""" + "\n".join([
+        f"member: cn={name},ou=handlers,dc=ldap,dc=sharix,dc=ru"
+        for name, _ in handlers
+    ])
+
+
+# OUTPUT ldiff file
+
+
+print(main_init)
+
+print("""
+##########
+#
+# OU
+#
+##########
+""")
+
+print(get_ou_txt())
+
+print("""
+##########
+#
+# UID FOR Django tickets module app user
+#
+##########
+""")
+
+print(uid_django_user)
+
+print("""
+##########
+#
+# UID FOR HANDLERS
+#
+##########
+""")
+
+print(get_handlers_txt())
+
+print("""
+##########
+#
+# UID FOR OU=USERS
+#
+##########
+""")
+
+print(get_users_txt())
+
+print("""
+##########
+#
+# Extra UID FOR OU=USERS
+#
+##########
+""")
+
+print(extra_test_users)
+
+print("""
+##########
+#
+# CN FOR OU=GROUP
+#
+##########
+""")
+
+print(get_groups_txt())
+
+print(extra_groups)
+
+print(
+"""
+##########
+#
+# CN FOR OU=APPS
+#
+##########
+""")
+
+print(get_group_handlers())
+
+print(extra_apps)