|
| 1 | +# Copyright 2022 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +The auth sub-module contains the definitions for the Authentication objects, which represent |
| 17 | +the methods by which a user can authenticate to their cluster(s). The abstract class, `Authentication`, |
| 18 | +contains two required methods `login()` and `logout()`. Users can use one of the existing concrete classes to |
| 19 | +authenticate to their cluster or add their own custom concrete classes here. |
| 20 | +""" |
| 21 | + |
| 22 | +import abc |
| 23 | +import openshift as oc |
| 24 | + |
| 25 | + |
| 26 | +class Authentication(metaclass=abc.ABCMeta): |
| 27 | + """ |
| 28 | + An abstract class that defines the necessary methods for authenticating to a remote environment. |
| 29 | + Specifically, this class defines the need for a `login()` and a `logout()` function. |
| 30 | + """ |
| 31 | + |
| 32 | + def login(self): |
| 33 | + """ |
| 34 | + Method for logging in to a remote cluster. |
| 35 | + """ |
| 36 | + pass |
| 37 | + |
| 38 | + def logout(self): |
| 39 | + """ |
| 40 | + Method for logging out of the remote cluster. |
| 41 | + """ |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class TokenAuthentication(Authentication): |
| 46 | + """ |
| 47 | + `TokenAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift |
| 48 | + cluster when the user has an API token and the API server address. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__( |
| 52 | + self, |
| 53 | + token: str = None, |
| 54 | + server: str = None, |
| 55 | + ): |
| 56 | + """ |
| 57 | + Initialize a TokenAuthentication object that requires a value for `token`, the API Token |
| 58 | + and `server`, the API server address for authenticating to an OpenShift cluster. |
| 59 | + """ |
| 60 | + |
| 61 | + self.token = token |
| 62 | + self.server = server |
| 63 | + |
| 64 | + def login(self): |
| 65 | + """ |
| 66 | + This function is used to login to an OpenShift cluster using the user's API token and API server address. |
| 67 | + """ |
| 68 | + token = self.token |
| 69 | + server = self.server |
| 70 | + response = oc.invoke("login", [f"--token={token}", f"--server={server}:6443"]) |
| 71 | + return response.out() |
| 72 | + |
| 73 | + def logout(self): |
| 74 | + """ |
| 75 | + This function is used to logout of an OpenShift cluster. |
| 76 | + """ |
| 77 | + response = oc.invoke("logout") |
| 78 | + return response.out() |
| 79 | + |
| 80 | + |
| 81 | +class PasswordUserAuthentication(Authentication): |
| 82 | + """ |
| 83 | + `PasswordUserAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift |
| 84 | + cluster when the user has a username and password. |
| 85 | + """ |
| 86 | + |
| 87 | + def __init__( |
| 88 | + self, |
| 89 | + username: str = None, |
| 90 | + password: str = None, |
| 91 | + ): |
| 92 | + """ |
| 93 | + Initialize a PasswordUserAuthentication object that requires a value for `username` |
| 94 | + and `password` for authenticating to an OpenShift cluster. |
| 95 | + """ |
| 96 | + self.username = username |
| 97 | + self.password = password |
| 98 | + |
| 99 | + def login(self): |
| 100 | + """ |
| 101 | + This function is used to login to an OpenShift cluster using the user's `username` and `password`. |
| 102 | + """ |
| 103 | + response = oc.login(self.username, self.password) |
| 104 | + return response.out() |
| 105 | + |
| 106 | + def logout(self): |
| 107 | + """ |
| 108 | + This function is used to logout of an OpenShift cluster. |
| 109 | + """ |
| 110 | + response = oc.invoke("logout") |
| 111 | + return response.out() |
0 commit comments