Secret Management Service feature
[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.loader as config_loader
25 from osdf.config.base import osdf_config
26 from osdf.logging.osdf_logging import debug_log
27
28 config_spec = {
29     "preload_secrets": "config/preload_secrets.yaml"
30 }
31
32 secret_cache = {}
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 = sms_client.createDomain(domain)
48     config["secret_domain"] = domain  # uuid
49     secrets = preload_config.get("secrets")
50     for secret in secrets:
51         sms_client.storeSecret(domain, secret.get('name'),
52                                secret.get('values'))
53     debug_log.debug("Preload secrets complete")
54
55
56 def retrieve_secrets():
57     """Get all secrets under the domain name"""
58     secret_dict = dict()
59     config = osdf_config.deployment
60     sms_url = config["aaf_sms_url"]
61     timeout = config["aaf_sms_timeout"]
62     cacert = config["aaf_ca_certs"]
63     domain = config["secret_domain"]
64     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
65     secrets = sms_client.getSecretNames(domain)
66     for secret in secrets:
67         values = sms_client.getSecret(domain, secret)
68         secret_dict[secret] = values
69     debug_log.debug("Secret Dictionary Retrieval Success")
70     return secret_dict
71
72
73 def delete_secrets():
74     """ This is intended to delete the secrets for a clean initialization for
75         testing Application. Actual deployment will have a preload script.
76         Make sure the config is in sync"""
77     config = osdf_config.deployment
78     sms_url = config["aaf_sms_url"]
79     timeout = config["aaf_sms_timeout"]
80     cacert = config["aaf_ca_certs"]
81     domain = config["secret_domain"]
82     sms_client = Client(url=sms_url, timeout=timeout, cacert=cacert)
83     ret_val = sms_client.deleteDomain(domain)
84     debug_log.debug("Clean up complete")
85     return ret_val
86
87
88 if __name__ == "__main__":
89     # Initialize Secrets from SMS
90     preload_secrets()
91
92     # Retrieve Secrets from SMS and load to secret cache
93     # Use the secret_cache instead of config files
94     secret_cache = retrieve_secrets()
95
96     # Clean up Delete secrets and domain
97     delete_secrets()