OOF design framework seed code
[optf/osdf.git] / config / credentials.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import json
20
21 from osdf import auth_groups, userid_suffix, passwd_suffix
22
23
24 def dmaap_creds(dmaap_file="/etc/dcae/dmaap.conf"):
25     """Get DMaaP credentials from DCAE for publish and subscribe"""
26     try:
27         dmaap_creds = _get_dmaap_creds(dmaap_file)
28     except:
29         dmaap_creds = {}
30     return dmaap_creds
31
32
33 def _get_dmaap_creds(dmaap_file):
34     """Get DMaaP credentials from DCAE for publish and subscribe"""
35     streams = json.load(open(dmaap_file, 'r'))
36     pubs = [x for x in streams
37             if x['dmaapStreamId'] == 'requests' and x['dmaapAction'] == 'publish']
38     subs = [x for x in streams
39             if x['dmaapStreamId'] == 'responses' and x['dmaapAction'] == 'subscribe']
40
41     def get_dmaap_info(x):
42         """Get DMaaP credentials from dmaap_object 'x'"""
43         return dict(url=x.get('dmaapUrl'), userid=x.get('dmaapUserName'), passwd=x.get('dmaapPassword'))
44
45     return {'pub': get_dmaap_info(pubs[0]), 'sub': get_dmaap_info(subs[0])}
46
47
48 def load_credentials(osdf_config):
49     """Get credentials as dictionaries grouped by auth_group (e.g. creds["Placement"]["user1"] = "pass1")"""
50     creds = dict((x, dict()) for x in auth_groups)  # each auth group has userid, passwd dict
51     suffix_start = len(userid_suffix)
52
53     config = osdf_config.deployment
54
55     for element, username in config.items():
56         for x in auth_groups:
57             if element.startswith("osdf" + x) and element.endswith(userid_suffix):
58                 passwd = config[element[:-suffix_start] + passwd_suffix]
59                 creds[x][username] = passwd
60     return creds