Move Cloudify's password update and plugins upload to
[dcaegen2/deployments.git] / cm-container / reset_admin.py
1 ########
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
18 import subprocess
19 import json
20 import argparse
21 from flask_security.utils import encrypt_password
22 from manager_rest.flask_utils import setup_flask_app
23
24
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)
31
32
33 def 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
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)
44     return password
45
46
47 if __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)
54     print 'Password updated in DB!\n'