-
Notifications
You must be signed in to change notification settings - Fork 17
/
insert_admin.py
34 lines (30 loc) · 1.12 KB
/
insert_admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
from pymongo import MongoClient
from bcrypt import hashpw, checkpw, gensalt
import yaml
import os
import argparse
def insert_admin():
dirs = os.listdir()
if 'app' in dirs:
f = open("app/config.cfg", 'r')
else:
f = open("./config.cfg", 'r')
settings = yaml.full_load(f)
settings = settings['docker']
mongo_client = MongoClient(settings['mongo_ip'], settings['mongo_port'])
mongo_db = mongo_client['userinfo']
userinfo = mongo_db['userinfo']
f.close()
parser = argparse.ArgumentParser(description = "Add admin user to MongoDB")
parser.add_argument('-p', '--password', type = str, help = "Admin password", required = True)
if userinfo.find({'username':'admin'}).count() > 0:
print("Admin already exists")
else:
args = parser.parse_args()
password = args.password
hashed = hashpw(password.encode('utf-8'), gensalt())
userinfo.insert_one({"username":"admin","password":hashed,"user_id":0})
print("Successfully added an admin user with password {}!".format(password))
if __name__ == "__main__":
insert_admin()