Commit 7080efcb authored by Paul Pacheco's avatar Paul Pacheco

create admin with a random password: Makes secure_db.py obsolete, and it is db independent

parent e7ec1075
...@@ -96,10 +96,6 @@ sh "python ${OPENSHIFT_REPO_DIR}/openshift/manage.py collectstatic --noinput" ...@@ -96,10 +96,6 @@ sh "python ${OPENSHIFT_REPO_DIR}/openshift/manage.py collectstatic --noinput"
if [ ! -s $OPENSHIFT_DATA_DIR/.credentials ]; then if [ ! -s $OPENSHIFT_DATA_DIR/.credentials ]; then
echo "Generating Weblate admin credentials and writing them to ${OPENSHIFT_DATA_DIR}/.credentials" echo "Generating Weblate admin credentials and writing them to ${OPENSHIFT_DATA_DIR}/.credentials"
sh "python ${OPENSHIFT_REPO_DIR}/openshift/manage.py createadmin" | tee ${OPENSHIFT_DATA_DIR}/.credentials sh "python ${OPENSHIFT_REPO_DIR}/openshift/manage.py createadmin" | tee ${OPENSHIFT_DATA_DIR}/.credentials
if [ ! -s $OPENSHIFT_DATA_DIR/weblate.db ] ; then
DJANGO_SETTINGS_MODULE='weblate.settings_openshift' sh "python ${OPENSHIFT_REPO_DIR}/openshift/secure_db.py | tee ${OPENSHIFT_DATA_DIR}/.credentials"
fi
fi fi
if find_script_dir; then if find_script_dir; then
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2014 Daniel Tschan <tschan@puzzle.ch>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sqlite3
from openshift.openshiftlibs import make_secure_key, get_openshift_secret_token
from hashlib import sha256
from django.contrib.auth.hashers import make_password
def secure_db():
new_pass = make_secure_key({
'hash': sha256(get_openshift_secret_token()).hexdigest(),
'original': '0' * 12,
'variable': ''
})
new_hash = make_password(new_pass)
# Update admin password in database
conn = sqlite3.connect(os.environ['OPENSHIFT_DATA_DIR'] + '/weblate.db')
cursor = conn.cursor()
cursor.execute(
'UPDATE AUTH_USER SET password = ? WHERE username = ?',
[new_hash, 'admin']
)
conn.commit()
cursor.close()
conn.close()
# Print the new password info
print "Weblate admin credentials:\n\tuser: admin\n\tpassword: " + new_pass
if __name__ == "__main__":
secure_db()
...@@ -20,11 +20,19 @@ ...@@ -20,11 +20,19 @@
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.contrib.auth.models import User from django.contrib.auth.models import User
import string
import os
import random
class Command(BaseCommand): class Command(BaseCommand):
help = 'setups admin user with admin password (INSECURE!)' help = 'setups admin user with admin password (INSECURE!)'
def make_password(self, length) :
chars = string.ascii_letters + string.digits + '!@#$%^&*()'
random.seed = (os.urandom(1024))
return ''.join(random.choice(chars) for i in range(length))
def handle(self, *args, **options): def handle(self, *args, **options):
''' '''
Create admin account with admin password. Create admin account with admin password.
...@@ -32,9 +40,13 @@ class Command(BaseCommand): ...@@ -32,9 +40,13 @@ class Command(BaseCommand):
This is useful mostly for setup inside appliances, when user wants This is useful mostly for setup inside appliances, when user wants
to be able to login remotely and change password then. to be able to login remotely and change password then.
''' '''
self.stderr.write('Warning: Creating user admin with password admin!')
self.stderr.write('Please change password immediatelly!')
user = User.objects.create_user('admin', 'admin@example.com', 'admin') password = self.make_password(13);
self.stdout.write('Creating user admin with password ' + password )
user = User.objects.create_user('admin', 'admin@example.com', password)
user.first_name = 'Weblate Admin' user.first_name = 'Weblate Admin'
user.last_name = '' user.last_name = ''
user.is_superuser = True user.is_superuser = True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment