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
8 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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
28 def update_dict(d, u):
29 """Recursively updates dict
31 Update dict d with dict u
33 for k, v in u.items():
34 if isinstance(v, collections.Mapping):
35 r = update_dict(d.get(k, {}), v)
41 def replace_token(configure_content):
43 with open("/opt/app/config-map/dcae-k8s-cluster-token",'r') as fh:
44 dcae_token = fh.readline().rstrip('\n')
46 new_config = copy.deepcopy(configure_content)
48 # override the default-user token
50 for user in new_config['users'][:]:
51 if user['name'] == "default-user":
52 new_config['users'][ix] = {
53 "name": "default-user",
62 except Exception as e:
63 return configure_content
65 def decrypt(b64_ciphertext):
66 """returns decrypted b64_ciphertext that was encoded like this:
68 echo "cleartext" | openssl pkeyutl -encrypt -pubin -inkey rsa.pub | base64 --wrap=0
70 requires private key in environment variable EOMUSER_PRIVATE
73 if len(b64_ciphertext) <= 30: # For transition, assume short values are not encrypted
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: