def _mutate(app, req: Request) -> Response: """Respond to AdmissionReviewRequests with a JSON patch to either remove or add zone:internal nodeSelector, based on the presence of emptyDir in the deploymentConfig.""" try: req = json.loads(request.data) except json.decoder.JSONDecodeError: app.logger.error( "%s", "Received an invalid request. " "It is either not AdmissionReviewRequest, or it is invalid.") return BAD_REQUEST_RESPONSE app.logger.debug("Request: %s", json.dumps(req)) if (req.get('request', {}).get('kind', {}).get('kind', {}) == "DeploymentConfig" and 'uid' in req.get('request', {}) and 'spec' in req.get('request', {}).get('object', {}).get( 'spec', {}).get('template', {})): resp = { 'kind': 'AdmissionReview', 'apiVersion': 'admission.k8s.io/v1beta1', 'response': { 'uid': '', 'allowed': True } } resp['response']['uid'] = req['request']['uid'] spec = req['request']['object']['spec']['template']['spec'] if (any('emptyDir' in item for item in spec.get('volumes', {})) and spec.get('nodeSelector', {}).get('zone', "") != "internal"): app.logger.info( "[%s] DeploymentConfig contains emptyDir, " "patching to add nodeSelector...", req['request']['uid']) resp['response']['patchType'] = "JSONPatch" resp['response']['patch'] = JSON_PATCH_ADD_BASE64 elif (all('emptyDir' not in item for item in spec.get('volumes', {})) and spec.get('nodeSelector', {}).get('zone', "") == "internal"): app.logger.info( "[%s] DeploymentConfig does not contain emptyDir, " "patching to remove nodeSelector...", req['request']['uid']) resp['response']['patchType'] = "JSONPatch" resp['response']['patch'] = JSON_PATCH_REMOVE_BASE64 else: app.logger.error( "Received an invalid request. " "It is either not AdmissionReviewRequest, or it is invalid.") return BAD_REQUEST_RESPONSE app.logger.debug("Response: %s", json.dumps(resp)) return json.dumps(resp)
def login_user(self, payload: Request) -> tuple: payload = payload.get_json(force=True) username = payload.get("username", None) password = payload.get("password", None) results = None if username and password: user = self.get_by_username(username) results = { "access_token": user.access_token, "refresh_token": user.refresh_token, } if user and user.check_password(password) else None return results, None if results is not None else self.error_login