2 # -------------------------------------------------------------------------
3 # Copyright (c) 2018 Intel Corporation Intellectual Property
4 # Copyright (C) 2020 Wipro Limited.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # -------------------------------------------------------------------------
21 '''Secret Management Service Integration'''
23 from onapsmsclient import Client
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
33 "preload_secrets": "config/preload_secrets.yaml"
37 def preload_secrets():
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
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"]
52 debug_log.debug("SMS Disabled")
54 sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
55 domain_uuid = sms_client.createDomain(domain)
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'),
62 debug_log.debug("Preload secrets complete")
65 def retrieve_secrets():
66 """Get all secrets under the domain name"""
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"]
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")
81 debug_log.debug("SMS Disabled. Secrets not loaded")
86 config = osdf_config.deployment
87 secret_dict = retrieve_secrets()
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()
121 def decrypt_pass(passwd):
122 config = osdf_config.deployment
123 if not config.get('appkey') or passwd == '' or passwd == 'NA':
126 return cipherUtils.AESCipher.get_instance().decrypt(passwd)
129 def delete_secrets():
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
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"]
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")
146 debug_log.debug("SMS Disabled. Secrets delete skipped")
150 if __name__ == "__main__":
151 # Initialize Secrets from SMS
154 # Retrieve Secrets from SMS and load to secret cache
155 # Use the secret_cache instead of config files
156 secret_cache = retrieve_secrets()
158 # Clean up Delete secrets and domain