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