|
| 1 | +import ldap3 |
| 2 | +import psycopg2 |
| 3 | +from psycopg2 import sql |
| 4 | + |
| 5 | +# LDAP connection settings |
| 6 | +LDAP_SERVER = "ldaps://192.168.0.144:1636" |
| 7 | +LDAP_BASE_DN = "o=gluu" |
| 8 | +LDAP_BIND_DN = "cn=directory manager" |
| 9 | +LDAP_PASSWORD = "#####" |
| 10 | + |
| 11 | +# PostgreSQL connection settings |
| 12 | +PG_HOST = "192.168.0.145" |
| 13 | +PG_PORT = "5432" |
| 14 | +PG_DATABASE = "gluudb" |
| 15 | +PG_USER = "gluu" |
| 16 | +PG_PASSWORD = "#####" |
| 17 | + |
| 18 | +def connect_ldap(): |
| 19 | + server = ldap3.Server(LDAP_SERVER, use_ssl=True, get_info=ldap3.ALL) |
| 20 | + ldap_conn = ldap3.Connection(server, LDAP_BIND_DN, LDAP_PASSWORD, auto_bind=True, client_strategy='SYNC') |
| 21 | + ldap_conn.open() |
| 22 | + ldap_conn.bind() |
| 23 | + return ldap_conn |
| 24 | + |
| 25 | +def connect_postgresql(): |
| 26 | + return psycopg2.connect( |
| 27 | + host=PG_HOST, |
| 28 | + port=PG_PORT, |
| 29 | + database=PG_DATABASE, |
| 30 | + user=PG_USER, |
| 31 | + password=PG_PASSWORD |
| 32 | + ) |
| 33 | + |
| 34 | +def extract_ldap_data(ldap_conn): |
| 35 | + search_filter = "(objectClass=*)" |
| 36 | + attributes = ["*"] |
| 37 | + print("searching for data") |
| 38 | + ldap_conn.search( |
| 39 | + search_base=LDAP_BASE_DN, |
| 40 | + search_scope=ldap3.SUBTREE, |
| 41 | + search_filter=search_filter, |
| 42 | + attributes=attributes |
| 43 | + ) |
| 44 | + print("data extracted") |
| 45 | + for entry in ldap_conn.response: |
| 46 | + print(entry) |
| 47 | + return ldap_conn.response |
| 48 | + |
| 49 | +def transform_data(ldap_data): |
| 50 | + print("transforming data") |
| 51 | + transformed_data = [] |
| 52 | + for dn, attrs in ldap_data: |
| 53 | + record = { |
| 54 | + "dn": dn, |
| 55 | + "attributes": {k: v[0].decode() if isinstance(v[0], bytes) else v[0] for k, v in attrs.items() if v} |
| 56 | + } |
| 57 | + transformed_data.append(record) |
| 58 | + print("data transformed") |
| 59 | + return transformed_data |
| 60 | + |
| 61 | +def create_table(pg_conn): |
| 62 | + with pg_conn.cursor() as cur: |
| 63 | + cur.execute(""" |
| 64 | + CREATE TABLE IF NOT EXISTS ldap_data ( |
| 65 | + id SERIAL PRIMARY KEY, |
| 66 | + dn TEXT, |
| 67 | + attributes JSONB |
| 68 | + ) |
| 69 | + """) |
| 70 | + pg_conn.commit() |
| 71 | + |
| 72 | +def load_data_to_postgresql(pg_conn, transformed_data): |
| 73 | + with pg_conn.cursor() as cur: |
| 74 | + for record in transformed_data: |
| 75 | + cur.execute( |
| 76 | + sql.SQL("INSERT INTO ldap_data (dn, attributes) VALUES (%s, %s)"), |
| 77 | + (record["dn"], psycopg2.Json(record["attributes"])) |
| 78 | + ) |
| 79 | + pg_conn.commit() |
| 80 | + |
| 81 | +def main(): |
| 82 | + ldap_conn = connect_ldap() |
| 83 | + pg_conn = connect_postgresql() |
| 84 | + |
| 85 | + try: |
| 86 | + ldap_data = extract_ldap_data(ldap_conn) |
| 87 | + print("extracted ldap data: ", ldap_data) |
| 88 | + transformed_data = transform_data(ldap_data) |
| 89 | + print("transformed data: ", transformed_data) |
| 90 | + |
| 91 | + create_table(pg_conn) |
| 92 | + print("created table") |
| 93 | + except Exception as e: |
| 94 | + print(e) |
| 95 | + |
| 96 | + |
| 97 | +# load_data_to_postgresql(pg_conn, transformed_data) |
| 98 | + |
| 99 | +# print("Migration completed successfully.") |
| 100 | +# finally: |
| 101 | +# ldap_conn.unbind_s() |
| 102 | +# pg_conn.close() |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments