031fee4642ebd3382349dcfc00c715eb0717ebe5
[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 from osdf.config.base import osdf_config
27 import osdf.config.credentials as creds
28 import osdf.config.loader as config_loader
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     """preload_secrets()
39
40     This is intended to load the secrets required for testing Application
41     Actual deployment will have a preload script. Make sure the config is
42     in sync
43     """
44     preload_config = config_loader.load_config_file(
45         config_spec.get("preload_secrets"))
46     domain = preload_config.get("domain")
47     config = osdf_config.deployment
48     sms_url = config["aaf_sms_url"]
49     timeout = config["aaf_sms_timeout"]
50     cacert = config["aaf_ca_certs"]
51     if not sms_url:
52         debug_log.debug("SMS Disabled")
53         return
54     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
55     domain_uuid = sms_client.createDomain(domain)
56     debug_log.debug(
57         "Created domain {} with uuid {}".format(domain, domain_uuid))
58     secrets = preload_config.get("secrets")
59     for secret in secrets:
60         sms_client.storeSecret(domain, secret.get('name'),
61                                secret.get('values'))
62     debug_log.debug("Preload secrets complete")
63
64
65 def retrieve_secrets():
66     """Get all secrets under the domain name"""
67     secret_dict = dict()
68     config = osdf_config.deployment
69     sms_url = config["aaf_sms_url"]
70     timeout = config["aaf_sms_timeout"]
71     cacert = config["aaf_ca_certs"]
72     domain = config["secret_domain"]
73     if sms_url:
74         sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
75         secrets = sms_client.getSecretNames(domain)
76         for secret in secrets:
77             values = sms_client.getSecret(domain, secret)
78             secret_dict[secret] = values
79         debug_log.debug("Secret Dictionary Retrieval Success")
80     else:
81         debug_log.debug("SMS Disabled. Secrets not loaded")
82     return secret_dict
83
84
85 def load_secrets():
86     config = osdf_config.deployment
87     secret_dict = retrieve_secrets()
88     if secret_dict:
89         config['soUsername'] = secret_dict['so']['UserName']
90         config['soPassword'] = decrypt_pass(secret_dict['so']['Password'])
91         config['conductorUsername'] = secret_dict['conductor']['UserName']
92         config['conductorPassword'] = decrypt_pass(secret_dict['conductor']['Password'])
93         config['policyPlatformUsername'] = secret_dict['policyPlatform']['UserName']
94         config['policyPlatformPassword'] = decrypt_pass(secret_dict['policyPlatform']['Password'])
95         config['policyClientUsername'] = secret_dict['policyPlatform']['UserName']
96         config['policyClientPassword'] = decrypt_pass(secret_dict['policyPlatform']['Password'])
97         config['messageReaderAafUserId'] = secret_dict['dmaap']['UserName']
98         config['messageReaderAafPassword'] = decrypt_pass(secret_dict['dmaap']['Password'])
99         config['sdcUsername'] = secret_dict['sdc']['UserName']
100         config['sdcPassword'] = decrypt_pass(secret_dict['sdc']['Password'])
101         config['osdfPlacementUsername'] = secret_dict['osdfPlacement']['UserName']
102         config['osdfPlacementPassword'] = decrypt_pass(secret_dict['osdfPlacement']['Password'])
103         config['osdfPlacementSOUsername'] = secret_dict['osdfPlacementSO']['UserName']
104         config['osdfPlacementSOPassword'] = decrypt_pass(secret_dict['osdfPlacementSO']['Password'])
105         config['osdfPlacementVFCUsername'] = secret_dict['osdfPlacementVFC']['UserName']
106         config['osdfPlacementVFCPassword'] = decrypt_pass(secret_dict['osdfPlacementVFC']['Password'])
107         config['osdfCMSchedulerUsername'] = secret_dict['osdfCMScheduler']['UserName']
108         config['osdfCMSchedulerPassword'] = decrypt_pass(secret_dict['osdfCMScheduler']['Password'])
109         config['configDbUserName'] = secret_dict['configDb']['UserName']
110         config['configDbPassword'] = decrypt_pass(secret_dict['configDb']['Password'])
111         config['pciHMSUsername'] = secret_dict['pciHMS']['UserName']
112         config['pciHMSPassword'] = decrypt_pass(secret_dict['pciHMS']['Password'])
113         config['osdfPCIOptUsername'] = secret_dict['osdfPCIOpt']['UserName']
114         config['osdfPCIOptPassword'] = decrypt_pass(secret_dict['osdfPCIOpt']['Password'])
115         config['osdfOptEngineUsername'] = secret_dict['osdfOptEngine']['UserName']
116         config['osdfOptEnginePassword'] = decrypt_pass(secret_dict['osdfOptEngine']['Password'])
117     cfg_base.http_basic_auth_credentials = creds.load_credentials(osdf_config)
118     cfg_base.dmaap_creds = creds.dmaap_creds()
119
120
121 def decrypt_pass(passwd):
122     config = osdf_config.deployment
123     if not config.get('appkey') or passwd == '' or passwd == 'NA':
124         return passwd
125     else:
126         return cipherUtils.AESCipher.get_instance().decrypt(passwd)
127
128
129 def delete_secrets():
130     """delete_secrets()
131
132     This is intended to delete the secrets for a clean initialization for
133     testing Application. Actual deployment will have a preload script.
134     Make sure the config is in sync
135     """
136     config = osdf_config.deployment
137     sms_url = config["aaf_sms_url"]
138     timeout = config["aaf_sms_timeout"]
139     cacert = config["aaf_ca_certs"]
140     domain = config["secret_domain"]
141     if sms_url:
142         sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
143         ret_val = sms_client.deleteDomain(domain)
144         debug_log.debug("Clean up complete")
145     else:
146         debug_log.debug("SMS Disabled. Secrets delete skipped")
147     return ret_val
148
149
150 if __name__ == "__main__":
151     # Initialize Secrets from SMS
152     preload_secrets()
153
154     # Retrieve Secrets from SMS and load to secret cache
155     # Use the secret_cache instead of config files
156     secret_cache = retrieve_secrets()
157
158     # Clean up Delete secrets and domain
159     delete_secrets()