2 # Copyright (c) 2018 Cloudify Platform Ltd. All rights reserved
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from flask_security.utils import encrypt_password
22 from manager_rest.flask_utils import setup_flask_app
25 def 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)
34 with open('/opt/manager/rest-security.conf') as f:
35 rest_security = json.load(f)
37 return rest_security['hash_salt']
40 def encrypt_new_password(password):
41 app = setup_flask_app(hash_salt=get_salt())
42 with app.app_context():
43 password = encrypt_password(password)
47 if __name__ == '__main__':
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()
53 db_update_password(args.password)
54 print 'Password updated in DB!\n'