Skip to content

Commit e75c8af

Browse files
authored
Unprivileged client: set as var before switching back to admin client (#224)
* Create size-labeler.yml * Delete .github/workflows/size-labeler.yml * model mesh - add auth tests * xx * fix: update unpriv client * fix: raise on un * fix: raise on un * fix: remove test code * fix: fail on login
1 parent 3610a3e commit e75c8af

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from simple_logger.logger import get_logger
2525

2626
from utilities.data_science_cluster_utils import update_components_in_dsc
27+
from utilities.exceptions import ClusterLoginError
2728
from utilities.general import get_s3_secret_dict
2829
from utilities.infra import (
2930
create_ns,
@@ -246,33 +247,36 @@ def unprivileged_client(
246247
non_admin_user_password: tuple[str, str],
247248
) -> Generator[DynamicClient, Any, Any]:
248249
"""
249-
Provides none privileged API client. If non_admin_user_password is None, then it will yield admin_client.
250+
Provides none privileged API client. If non_admin_user_password is None, then it will raise.
250251
"""
251252
if non_admin_user_password is None:
252-
yield admin_client
253+
raise ValueError("Unprivileged user not provisioned")
253254

254255
else:
255256
current_user = run_command(command=["oc", "whoami"])[1].strip()
257+
non_admin_user_name = non_admin_user_password[0]
256258

257259
if login_with_user_password(
258260
api_address=admin_client.configuration.host,
259-
user=non_admin_user_password[0],
261+
user=non_admin_user_name,
260262
password=non_admin_user_password[1],
261263
):
262264
with open(kubconfig_filepath) as fd:
263265
kubeconfig_content = yaml.safe_load(fd)
264266

265267
unprivileged_context = kubeconfig_content["current-context"]
266268

269+
unprivileged_client = get_client(config_file=kubconfig_filepath, context=unprivileged_context)
270+
267271
# Get back to admin account
268272
login_with_user_password(
269273
api_address=admin_client.configuration.host,
270274
user=current_user.strip(),
271275
)
272-
yield get_client(config_file=kubconfig_filepath, context=unprivileged_context)
276+
yield unprivileged_client
273277

274278
else:
275-
yield admin_client
279+
raise ClusterLoginError(user=non_admin_user_name)
276280

277281

278282
@pytest.fixture(scope="session")

utilities/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ class PodLogMissMatchError(Exception):
8484

8585
class ResourceMismatch(Exception):
8686
pass
87+
88+
89+
class ClusterLoginError(Exception):
90+
def __init__(self, user: str):
91+
self.user = user
92+
93+
def __str__(self) -> str:
94+
return f"Failed to log in as user {self.user}."

utilities/infra.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from utilities.constants import ApiGroups, Labels, Timeout
4040
from utilities.constants import KServeDeploymentType
4141
from utilities.constants import Annotations
42-
from utilities.exceptions import FailedPodsError
42+
from utilities.exceptions import ClusterLoginError, FailedPodsError
4343
from timeout_sampler import TimeoutExpiredError, TimeoutSampler, retry
4444
import utilities.general
4545

@@ -328,9 +328,15 @@ def login_with_user_password(api_address: str, user: str, password: str | None =
328328
if password:
329329
login_command += f" -p '{password}'"
330330

331-
_, out, _ = run_command(command=shlex.split(login_command), hide_log_command=True)
331+
_, out, err = run_command(command=shlex.split(login_command), hide_log_command=True)
332332

333-
return "Login successful" in out
333+
if err and err.lower().startswith("error"):
334+
raise ClusterLoginError(user=user)
335+
336+
if re.search(r"Login successful|Logged into", out):
337+
return True
338+
339+
return False
334340

335341

336342
@cache

0 commit comments

Comments
 (0)