bcc449c06b58d93000ec28591ccbc1c661f93341
[optf/osdf.git] / osdf / adapters / aaf / sms.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2018 Intel Corporation Intellectual Property
4 #   Copyright (C) 2020 Wipro Limited.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #
18 # -------------------------------------------------------------------------
19 #
20
21 '''Secret Management Service Integration'''
22
23 from onapsmsclient import Client
24
25 import osdf.config.base as cfg_base
26 import osdf.config.credentials as creds
27 import osdf.config.loader as config_loader
28 from osdf.config.base import osdf_config
29 from osdf.logging.osdf_logging import debug_log
30 from osdf.utils import cipherUtils
31
32 config_spec = {
33     "preload_secrets": "config/preload_secrets.yaml"
34 }
35
36
37 def preload_secrets():
38     """ This is intended to load the secrets required for testing Application
39         Actual deployment will have a preload script. Make sure the config is
40         in sync"""
41     preload_config = config_loader.load_config_file(
42         config_spec.get("preload_secrets"))
43     domain = preload_config.get("domain")
44     config = osdf_config.deployment
45     sms_url = config["aaf_sms_url"]
46     timeout = config["aaf_sms_timeout"]
47     cacert = config["aaf_ca_certs"]
48     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
49     domain_uuid = sms_client.createDomain(domain)
50     debug_log.debug(
51         "Created domain {} with uuid {}".format(domain, domain_uuid))
52     secrets = preload_config.get("secrets")
53     for secret in secrets:
54         sms_client.storeSecret(domain, secret.get('name'),
55                                secret.get('values'))
56     debug_log.debug("Preload secrets complete")
57
58
59 def retrieve_secrets():
60     """Get all secrets under the domain name"""
61     secret_dict = dict()
62     config = osdf_config.deployment
63     sms_url = config["aaf_sms_url"]
64     timeout = config["aaf_sms_timeout"]
65     cacert = config["aaf_ca_certs"]
66     domain = config["secret_domain"]
67     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
68     secrets = sms_client.getSecretNames(domain)
69     for secret in secrets:
70         values = sms_client.getSecret(domain, secret)
71         secret_dict[secret] = values
72     debug_log.debug("Secret Dictionary Retrieval Success")
73     return secret_dict
74
75
76 def load_secrets():
77     config = osdf_config.deployment
78     secret_dict = retrieve_secrets()
79     config['soUsername'] = secret_dict['so']['UserName']
80     config['soPassword'] = decrypt_pass(secret_dict['so']['Password'])
81     config['conductorUsername'] = secret_dict['conductor']['UserName']
82     config['conductorPassword'] = decrypt_pass(secret_dict['conductor']['Password'])
83     config['policyPlatformUsername'] = secret_dict['policyPlatform']['UserName']
84     config['policyPlatformPassword'] = decrypt_pass(secret_dict['policyPlatform']['Password'])
85     config['policyClientUsername'] = secret_dict['policyPlatform']['UserName']
86     config['policyClientPassword'] = decrypt_pass(secret_dict['policyPlatform']['Password'])
87     config['messageReaderAafUserId'] = secret_dict['dmaap']['UserName']
88     config['messageReaderAafPassword'] = decrypt_pass(secret_dict['dmaap']['Password'])
89     config['sdcUsername'] = secret_dict['sdc']['UserName']
90     config['sdcPassword'] = decrypt_pass(secret_dict['sdc']['Password'])
91     config['osdfPlacementUsername'] = secret_dict['osdfPlacement']['UserName']
92     config['osdfPlacementPassword'] = decrypt_pass(secret_dict['osdfPlacement']['Password'])
93     config['osdfPlacementSOUsername'] = secret_dict['osdfPlacementSO']['UserName']
94     config['osdfPlacementSOPassword'] = decrypt_pass(secret_dict['osdfPlacementSO']['Password'])
95     config['osdfPlacementVFCUsername'] = secret_dict['osdfPlacementVFC']['UserName']
96     config['osdfPlacementVFCPassword'] = decrypt_pass(secret_dict['osdfPlacementVFC']['Password'])
97     config['osdfCMSchedulerUsername'] = secret_dict['osdfCMScheduler']['UserName']
98     config['osdfCMSchedulerPassword'] = decrypt_pass(secret_dict['osdfCMScheduler']['Password'])
99     config['configDbUserName'] = secret_dict['configDb']['UserName']
100     config['configDbPassword'] = decrypt_pass(secret_dict['configDb']['Password'])
101     config['pciHMSUsername'] = secret_dict['pciHMS']['UserName']
102     config['pciHMSPassword'] = decrypt_pass(secret_dict['pciHMS']['Password'])
103     config['osdfPCIOptUsername'] = secret_dict['osdfPCIOpt']['UserName']
104     config['osdfPCIOptPassword'] = decrypt_pass(secret_dict['osdfPCIOpt']['Password'])
105     config['osdfOptEngineUsername'] = secret_dict['osdfOptEngine']['UserName']
106     config['osdfOptEnginePassword'] = decrypt_pass(secret_dict['osdfOptEngine']['Password'])
107     cfg_base.http_basic_auth_credentials = creds.load_credentials(osdf_config)
108     cfg_base.dmaap_creds = creds.dmaap_creds()
109
110
111 def decrypt_pass(passwd):
112     config = osdf_config.deployment
113     if not config.get('appkey') or passwd == '' or passwd == 'NA':
114         return passwd
115     else:
116         return cipherUtils.AESCipher.get_instance().decrypt(passwd)
117
118
119 def delete_secrets():
120     """ This is intended to delete the secrets for a clean initialization for
121         testing Application. Actual deployment will have a preload script.
122         Make sure the config is in sync"""
123     config = osdf_config.deployment
124     sms_url = config["aaf_sms_url"]
125     timeout = config["aaf_sms_timeout"]
126     cacert = config["aaf_ca_certs"]
127     domain = config["secret_domain"]
128     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
129     ret_val = sms_client.deleteDomain(domain)
130     debug_log.debug("Clean up complete")
131     return ret_val
132
133
134 if __name__ == "__main__":
135     # Initialize Secrets from SMS
136     preload_secrets()
137
138     # Retrieve Secrets from SMS and load to secret cache
139     # Use the secret_cache instead of config files
140     secret_cache = retrieve_secrets()
141
142     # Clean up Delete secrets and domain
143     delete_secrets()