blob: 2253341ed0664fc47def50b686eeb1498d1c8ef0 [file] [log] [blame]
Schmalzried, Terry (ts862m)15b49792020-08-21 15:59:22 -04001########
2# Copyright (c) 2018 Cloudify Platform Ltd. All rights reserved
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# * See the License for the specific language governing permissions and
14# * limitations under the License.
15
16#!/usr/bin/env python
17
18import subprocess
19import json
20import argparse
21from flask_security.utils import encrypt_password
22from manager_rest.flask_utils import setup_flask_app
23
24
25def db_update_password(password):
26 password = encrypt_new_password(password)
27 password = password.replace('$', '\$')
28 sql_command = "\"update users set password='" + password + "' where username='admin'\""
29 cmd = "sudo -u postgres psql cloudify_db -c " + sql_command
30 subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
31
32
33def get_salt():
34 with open('/opt/manager/rest-security.conf') as f:
35 rest_security = json.load(f)
36
37 return rest_security['hash_salt']
38
39
40def encrypt_new_password(password):
41 app = setup_flask_app(hash_salt=get_salt())
42 with app.app_context():
43 password = encrypt_password(password)
44 return password
45
46
47if __name__ == '__main__':
48
49 parser = argparse.ArgumentParser(description=('Reset admin password in DB according to rest-security.conf'))
50 parser.add_argument('-p', '--password', required=True, help='New admin password')
51 args = parser.parse_args()
52
53 db_update_password(args.password)
Remigiusz Janeczekc21d9d82020-09-29 16:17:59 +020054 print 'Password updated in DB!\n'