|
| 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 | + pass |
| 34 | + |
| 35 | + def logout(self): |
| 36 | + pass |
| 37 | + |
| 38 | + |
| 39 | +class TokenAuthentication(Authentication): |
| 40 | + """ |
| 41 | + `TokenAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift |
| 42 | + cluster when the user has an API token and the API server address. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + token: str = None, |
| 48 | + server: str = None, |
| 49 | + ): |
| 50 | + |
| 51 | + self.token = token |
| 52 | + self.server = server |
| 53 | + |
| 54 | + def login(self): |
| 55 | + token = self.token |
| 56 | + server = self.server |
| 57 | + response = oc.invoke("login", [f"--token={token}", f"--server={server}:6443"]) |
| 58 | + return response.out() |
| 59 | + |
| 60 | + def logout(self): |
| 61 | + response = oc.invoke("logout") |
| 62 | + return response.out() |
| 63 | + |
| 64 | + |
| 65 | +class PasswordUserAuthentication(Authentication): |
| 66 | + """ |
| 67 | + `PasswordUserAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift |
| 68 | + cluster when the user has a username and password. |
| 69 | + """ |
| 70 | + |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + username: str = None, |
| 74 | + password: str = None, |
| 75 | + ): |
| 76 | + |
| 77 | + self.username = username |
| 78 | + self.password = password |
| 79 | + |
| 80 | + def login(self): |
| 81 | + response = oc.login(self.username, self.password) |
| 82 | + return response.out() |
| 83 | + |
| 84 | + def logout(self): |
| 85 | + response = oc.invoke("logout") |
| 86 | + return response.out() |
0 commit comments