Skip to content

Commit 0ef3262

Browse files
committed
optimize: soft migration of ldap login
1 parent 025b284 commit 0ef3262

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

cloudtower/utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from cloudtower.exceptions import ApiException
55
from cloudtower.api.task_api import TaskApi
66
from cloudtower.api.user_api import UserApi
7+
import json
78

89

910
def wait_task(id, api_client, interval=5, timeout=300):
@@ -28,7 +29,7 @@ def wait_task(id, api_client, interval=5, timeout=300):
2829
)
2930
)
3031
start = time.time()
31-
while(True):
32+
while (True):
3233
now = time.time()
3334
if (now-start) > timeout:
3435
raise ApiException(
@@ -105,11 +106,33 @@ def login(api_client: ApiClient, username, password, source=UserSource.LOCAL):
105106
:type password: UserSource
106107
"""
107108
user_api = UserApi(api_client)
108-
login_res = user_api.login({
109+
login_params = {
109110
"username": username,
110111
"password": password,
111112
"source": source
112-
})
113+
}
114+
if source == UserSource.LDAP:
115+
host = api_client.configuration.host
116+
if host.endswith("/"): # remove trailing slash
117+
host = host[:-1]
118+
if host.endswith("/v2/api"):
119+
# replace v2/api with api
120+
host = host[:-7] + "/api"
121+
try:
122+
resp = api_client.request("POST", host, body={
123+
"query": "{authnStrategies{id type}}",
124+
"variables": {}
125+
})
126+
configs = json.loads(resp.data)['data']['authnStrategies']
127+
for config in configs:
128+
if config['type'] == 'LDAP':
129+
login_params["auth_config_id"] = config['id']
130+
login_params["source"] = UserSource.AUTHN
131+
break
132+
except:
133+
# ignore error for backward compatibility, old version of tower has not authn config query
134+
pass
135+
login_res = user_api.login(login_params)
113136
api_client.configuration.api_key["Authorization"] = login_res.data.token
114137
return
115138

0 commit comments

Comments
 (0)