Skip to content

Commit 8fb2624

Browse files
committed
Added password generation routine to default DB
1 parent 520c56c commit 8fb2624

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

.openshift/action_hooks/deploy

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ if [ ! -f $OPENSHIFT_DATA_DIR/sqlite3.db ]
1010
then
1111
echo "Copying $OPENSHIFT_REPO_DIR/wsgi/openshift/sqlite3.db to $OPENSHIFT_DATA_DIR"
1212
cp "$OPENSHIFT_REPO_DIR"wsgi/openshift/sqlite3.db $OPENSHIFT_DATA_DIR
13+
python "$OPENSHIFT_REPO_DIR".openshift/action_hooks/secure_db.py
1314
else
1415
echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py syncdb --noinput'"
1516
python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py syncdb --noinput
1617
fi
1718

1819
echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py collectstatic --noinput'"
19-
python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py collectstatic --noinput
20+
python "$OPENSHIFT_REPO_DIR"wsgi/openshift/manage.py collectstatic --noinput

.openshift/action_hooks/secure_db.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
import hashlib, imp, os, sqlite3
3+
4+
# Load the openshift helper library
5+
lib_path = os.environ['OPENSHIFT_REPO_DIR'] + 'wsgi/openshift/'
6+
modinfo = imp.find_module('openshiftlibs', [lib_path])
7+
openshiftlibs = imp.load_module('openshiftlibs', modinfo[0], modinfo[1], modinfo[2])
8+
9+
# Open the database
10+
conn = sqlite3.connect(os.environ['OPENSHIFT_DATA_DIR'] + '/sqlite3.db')
11+
c = conn.cursor()
12+
13+
# Grab the default security info
14+
c.execute('SELECT password FROM AUTH_USER WHERE id = 1')
15+
pw_info = c.fetchone()[0]
16+
17+
# The password is stored as [hashtype]$[salt]$[hashed]
18+
pw_fields = pw_info.split("$")
19+
hashtype = pw_fields[0]
20+
old_salt = pw_fields[1]
21+
old_pass = pw_fields[2]
22+
23+
# Randomly generate a new password and a new salt
24+
# The PASSWORD value below just sets the length (12)
25+
# for the real new password.
26+
old_keys = { 'SALT': old_salt, 'PASS': '123456789ABC' }
27+
use_keys = openshiftlibs.openshift_secure(old_keys)
28+
29+
# Encrypt the new password
30+
new_salt = use_keys['SALT']
31+
new_pass = use_keys['PASS']
32+
new_hashed = hashlib.sha1(new_salt + new_pass).hexdigest()
33+
new_pw_info = "$".join([hashtype,new_salt,new_hashed])
34+
35+
# Update the database
36+
c.execute('UPDATE AUTH_USER SET password = ? WHERE id = 1', [new_pw_info])
37+
conn.commit()
38+
c.close()
39+
conn.close()
40+
41+
# Print the new password info
42+
print "CLIENT_MESSAGE: The password for user 'admin' in your Django app is " + new_pass + " ...be sure to write this down as you will not see this message again.\n"

wsgi/openshift/openshiftlibs.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import hashlib, inspect, os, random, sys
33

4-
# Gets the secret token provided by OpenShift
4+
# Gets the secret token provided by OpenShift
55
# or generates one (this is slightly less secure, but good enough for now)
66
def get_openshift_secret_token():
77
token = os.getenv('OPENSHIFT_SECRET_TOKEN')
@@ -28,7 +28,7 @@ def openshift_secure(default_keys, secure_function = 'make_secure_key'):
2828

2929
# Only generate random values if on OpenShift
3030
my_list = default_keys
31-
31+
3232
if my_token is not None:
3333
# Loop over each default_key and set the new value
3434
for key, value in default_keys.iteritems():
@@ -62,8 +62,11 @@ def make_secure_key(key_info):
6262
hashcode = key_info['hash']
6363
key = key_info['variable']
6464
original = key_info['original']
65-
66-
chars = '0123456789abcdef'
65+
66+
chars = '0123456789'
67+
chars += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
68+
chars += '!@#%^&*()'
69+
chars += '-_ []{}<>~`+=,.;:/?|'
6770

6871
# Use the hash to seed the RNG
6972
random.seed(int("0x" + hashcode[:8], 0))

0 commit comments

Comments
 (0)