4f9dbdaa09319f56ccf1a9a71ab3c13170c923d2
[dcaegen2/platform.git] / oti / event-handler / otihandler / utils.py
1 # ================================================================================
2 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
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 # ============LICENSE_END=========================================================
16
17 import base64
18 import collections
19 import copy
20 import os
21
22 from Crypto import Random
23 from Crypto.Cipher import PKCS1_v1_5
24 from Crypto.Hash import SHA
25 from Crypto.PublicKey import RSA
26
27
28 def update_dict(d, u):
29     """Recursively updates dict
30
31     Update dict d with dict u
32     """
33     for k, v in u.items():
34         if isinstance(v, collections.Mapping):
35             r = update_dict(d.get(k, {}), v)
36             d[k] = r
37         else:
38             d[k] = u[k]
39     return d
40
41 def replace_token(configure_content):
42     try:
43         with open("/opt/app/config-map/dcae-k8s-cluster-token",'r') as fh:
44             dcae_token = fh.readline().rstrip('\n')
45
46         new_config = copy.deepcopy(configure_content)
47
48         # override the default-user token 
49         ix=0
50         for user in new_config['users'][:]:
51             if user['name'] == "default-user":
52                 new_config['users'][ix] = {
53                     "name": "default-user",
54                     "user": {
55                         "token": dcae_token
56                     }
57                 }
58             ix += 1
59
60         return new_config
61
62     except Exception as e:
63         return configure_content
64
65 def decrypt(b64_ciphertext):
66     """returns decrypted b64_ciphertext that was encoded like this:
67
68     echo "cleartext" | openssl pkeyutl -encrypt -pubin -inkey rsa.pub | base64 --wrap=0
69
70     requires private key in environment variable EOMUSER_PRIVATE
71     """
72
73     if len(b64_ciphertext) <= 30:  # For transition, assume short values are not encrypted
74         return b64_ciphertext
75     
76     try:
77         ciphertext = base64.b64decode(b64_ciphertext)
78         key = RSA.importKey(os.getenv('EOMUSER_PRIVATE'))
79         cleartext = PKCS1_v1_5.new(key).decrypt(ciphertext, Random.new().read(15+SHA.digest_size))
80     except Exception as e:
81         return b64_ciphertext
82
83     return cleartext