|
| 1 | +# This script adds Jans users to rdbm backends |
| 2 | + |
| 3 | +import sys |
| 4 | +import uuid |
| 5 | +import json |
| 6 | +import random |
| 7 | +import os |
| 8 | +import hashlib |
| 9 | +import base64 |
| 10 | +import psycopg2 |
| 11 | +import pymysql |
| 12 | + |
| 13 | +# Set number of users to be created |
| 14 | +N = 100 |
| 15 | + |
| 16 | +#Users ID's will start with this string, e.g., test_user1, test_user2, test_user3 ... |
| 17 | +userId = 'test_user' |
| 18 | + |
| 19 | +# All users will have this password |
| 20 | +userSecret = 'test_user_password' |
| 21 | + |
| 22 | +# Set either pgsql or mysql |
| 23 | +db_type = 'mysql' |
| 24 | + |
| 25 | +# pgsql/mysql database settings ####### |
| 26 | +sql_host = 'localhost' |
| 27 | +sql_db = 'jansdb' |
| 28 | +sql_user = 'jans' |
| 29 | +sql_password = 'w5IWm03tT7Za' |
| 30 | +######################################## |
| 31 | + |
| 32 | + |
| 33 | +if db_type == 'pgsql': |
| 34 | + qchar = '"' |
| 35 | + schar = '\'' |
| 36 | + conn = psycopg2.connect(user=sql_user, |
| 37 | + password=sql_password, |
| 38 | + host=sql_host, |
| 39 | + port="5432", |
| 40 | + database=sql_db) |
| 41 | + cur = conn.cursor() |
| 42 | + |
| 43 | +elif db_type == 'mysql': |
| 44 | + qchar = '`' |
| 45 | + schar = '"' |
| 46 | + conn = pymysql.connect(host=sql_host, |
| 47 | + user=sql_user, |
| 48 | + password=sql_password, |
| 49 | + database=sql_db, |
| 50 | + ) |
| 51 | + cur = conn.cursor() |
| 52 | + |
| 53 | +def make_secret(password): |
| 54 | + |
| 55 | + salt = os.urandom(4) |
| 56 | + sha = hashlib.sha1(password.encode('utf-8')) |
| 57 | + sha.update(salt) |
| 58 | + digest_ = sha.digest() |
| 59 | + b64encoded = base64.b64encode(digest_+salt).decode('utf-8') |
| 60 | + encrypted_password = '{{SSHA}}{0}'.format(b64encoded) |
| 61 | + return encrypted_password |
| 62 | + |
| 63 | +user_secret_ssha = make_secret(userSecret) |
| 64 | + |
| 65 | + |
| 66 | +i = 0 |
| 67 | +while i < N: |
| 68 | + i += 1 |
| 69 | + inum = str(uuid.uuid4()).upper() |
| 70 | + name = userId + str(i) |
| 71 | + sn = userId + '_sn' + str(i) |
| 72 | + |
| 73 | + dn='inum={},ou=people,o=jans'.format(inum) |
| 74 | + |
| 75 | + username = name |
| 76 | + cn = name + ' ' + sn |
| 77 | + |
| 78 | + attributes= ( |
| 79 | + ('doc_id', inum), |
| 80 | + ('dn', dn), |
| 81 | + ('objectClass', 'jansPerson'), |
| 82 | + ('cn', cn), |
| 83 | + ('sn', sn), |
| 84 | + ('uid', username), |
| 85 | + ('inum', inum), |
| 86 | + ('jansStatus', 'active'), |
| 87 | + ('userPassword', user_secret_ssha), |
| 88 | + ('mail', username+'@jans.io'), |
| 89 | + ('displayName', cn), |
| 90 | + ('givenName', name), |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | + sql_attribs = ['{0}{1}{0}'.format(qchar, a[0]) for a in attributes] |
| 95 | + sql_vals = ['{0}{1}{0}'.format(schar, a[1]) for a in attributes] |
| 96 | + sql_cmd = 'INSERT INTO {0}jansPerson{0} ({1}) values ({2})'.format(qchar, ','.join(sql_attribs), ','.join(sql_vals)) |
| 97 | + |
| 98 | + if db_type in ('pgsql', 'mysql'): |
| 99 | + cur.execute(sql_cmd) |
| 100 | + conn.commit() |
| 101 | + print("Added", username) |
| 102 | + |
| 103 | + |
| 104 | +if db_type in ('pgsql', 'mysql'): |
| 105 | + conn.close() |
0 commit comments