Skip to content

Commit f609c12

Browse files
committed
Updated to split cloud manager token more compeltely
Signed-off-by: Andrew Perry <[email protected]>
1 parent 99c574d commit f609c12

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

manager_net.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ManagerNet(object):
2020
namespace = 'apic-management'
2121
username = ''
2222
password = ''
23+
cm_username = ''
24+
cm_password = ''
2325
hostname = ''
2426
default_client_id = "caa87d9a-8cd7-4686-8b6e-ee2cdc5ee267"
2527
default_client_secret = "3ecff363-7eb3-44be-9e07-6d4386c48b0b"
@@ -52,21 +54,15 @@ def __init__(self, config, trawler):
5254
self.version = Gauge('apiconnect_build_info',
5355
"A metric with a constant '1' value labeled with API Connect version details",
5456
["version", "juhu_release"])
57+
58+
# Provider creds
5559
if 'secret' in config:
5660
# If config points to a secret, then load from that
5761
# either in this namespace, or the specified one
5862
self.load_credentials_from_secret(
5963
config.get('secret'),
6064
config.get('secret_namespace', self.namespace))
61-
62-
if 'cloud_manager_secret' in config:
63-
# If config points to a secret, then load from that
64-
# either in this namespace, or the specified one
65-
self.load_credentials_from_secret(
66-
config.get('cloud_manager_secret' ),
67-
config.get('secret_namespace', self.namespace))
68-
69-
if not 'secret' in config and not 'cloud_manager_secret' in config:
65+
else:
7066
# Cloud manager username to use for REST calls
7167
self.username = config.get('username', 'admin')
7268
if self.grant_type == 'client_credentials':
@@ -76,6 +72,16 @@ def __init__(self, config, trawler):
7672
# Load password from secret `cloudmanager_password`
7773
self.password = trawler.read_secret('cloudmanager_password')
7874

75+
# Cloud manager creds
76+
if 'cloud_manager_secret' in config:
77+
# If config points to a secret, then load from that
78+
# either in this namespace, or the specified one
79+
self.load_credentials_from_secret(
80+
config.get('cloud_manager_secret' ),
81+
config.get('cloud_manager_secret_namespace', self.namespace),
82+
cloud_manager = True)
83+
84+
7985
if self.password is None:
8086
# Use out of box default password
8187
self.password = 'admin'
@@ -84,20 +90,29 @@ def __init__(self, config, trawler):
8490
logger.debug("Hostname found is {}".format(self.hostname))
8591
self.trawler = trawler
8692

87-
def load_credentials_from_secret(self, secret_name, namespace):
93+
def load_credentials_from_secret(self, secret_name, namespace, cloud_manager=False):
8894
try:
8995
if self.use_kubeconfig:
9096
config.load_kube_config()
9197
else:
9298
config.load_incluster_config()
99+
93100
v1 = client.CoreV1Api()
94101
logger.info("Loading cloud manager credentials from secret {} in namespace {}".format(secret_name, namespace))
95102
# Get credentials secret
96103
secrets_response = v1.read_namespaced_secret(name=secret_name, namespace=namespace)
97-
if 'password' in secrets_response.data:
98-
self.password = base64.b64decode(secrets_response.data['password']).decode('utf-8')
99-
self.username = base64.b64decode(secrets_response.data['username']).decode('utf-8')
100-
logger.info("Username to use is {}, password length is {}".format(self.username, len(self.password)))
104+
if cloud_manager:
105+
if 'password' in secrets_response.data:
106+
self.cm_password = base64.b64decode(secrets_response.data['password']).decode('utf-8')
107+
self.cm_username = base64.b64decode(secrets_response.data['username']).decode('utf-8')
108+
logger.info("Username to use is {}, password length is {}".format(self.cm_username, len(self.cm_password)))
109+
else:
110+
if 'password' in secrets_response.data:
111+
self.password = base64.b64decode(secrets_response.data['password']).decode('utf-8')
112+
self.username = base64.b64decode(secrets_response.data['username']).decode('utf-8')
113+
logger.info("Username to use is {}, password length is {}".format(self.username, len(self.password)))
114+
115+
# Client secret is not applicable to cloud manager so no test needed
101116
if 'client_secret' in secrets_response.data:
102117
self.client_secret = base64.b64decode(secrets_response.data['client_secret']).decode('utf-8')
103118
self.client_id = base64.b64decode(secrets_response.data['client_id']).decode('utf-8')
@@ -317,8 +332,8 @@ def get_token(self, host, cloud_manager=False):
317332
if cloud_manager:
318333
data['client_id'] = self.default_client_id
319334
data['client_secret'] = self.default_client_secret
320-
data['username'] = self.username
321-
data['password'] = self.password
335+
data['username'] = self.cm_username
336+
data['password'] = self.cm_password
322337
data['realm'] = 'admin/default-idp-1'
323338

324339
url = "https://{}/api/token".format(host)

0 commit comments

Comments
 (0)