Skip to content

update auth to allow for skip_tls #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/codeflare_sdk/cluster/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import abc
import openshift as oc
from openshift import OpenShiftPythonException


class Authentication(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -48,26 +49,33 @@ class TokenAuthentication(Authentication):
cluster when the user has an API token and the API server address.
"""

def __init__(
self,
token: str = None,
server: str = None,
):
def __init__(self, token: str = None, server: str = None, skip_tls: bool = False):
"""
Initialize a TokenAuthentication object that requires a value for `token`, the API Token
and `server`, the API server address for authenticating to an OpenShift cluster.
"""

self.token = token
self.server = server
self.skip_tls = skip_tls

def login(self):
"""
This function is used to login to an OpenShift cluster using the user's API token and API server address.
"""
token = self.token
server = self.server
response = oc.invoke("login", [f"--token={token}", f"--server={server}:6443"])
Depending on the cluster, a user can choose to login in with "--insecure-skip-tls-verify` by setting `skip_tls`
to `True`.
"""
args = [f"--token={self.token}", f"--server={self.server}:6443"]
if self.skip_tls:
args.append("--insecure-skip-tls-verify")
try:
response = oc.invoke("login", args)
except OpenShiftPythonException as osp:
error_msg = osp.result.err()
if "The server uses a certificate signed by unknown authority" in error_msg:
return "Error: certificate auth failure, please set `skip_tls=True` in TokenAuthentication"
else:
return error_msg
return response.out()

def logout(self):
Expand Down