def get_user_info(self, provider_user_id, token, normalized=True): response = requests.get(urljoin(self.api_base_url, f'/people/{provider_user_id}?format=json'), headers={'Authorization': f'Bearer {token["access_token"]}'}) if response.status_code != 200: try: raise exceptions.LifeMonitorException( status=response.status_code, detail="Unable to get user data", errors=response.json()['errors']) except Exception: raise exceptions.LifeMonitorException( status=response.status_code, detail="Unable to get user data") user_info = response.json() logger.debug("USER info: %r", user_info) return user_info['data'] if not normalized else self.normalize_userinfo(None, user_info)
def workflows_delete(wf_uuid, wf_version): try: if current_user and not current_user.is_anonymous: lm.deregister_user_workflow(wf_uuid, wf_version, current_user) elif current_registry: lm.deregister_registry_workflow(wf_uuid, wf_version, current_registry) else: return lm_exceptions.report_problem( 403, "Forbidden", detail=messages.no_user_in_session) return connexion.NoContent, 204 except lm_exceptions.EntityNotFoundException as e: return lm_exceptions.report_problem( 404, "Not Found", extra_info={"exception": str(e.detail)}, detail=messages.workflow_not_found.format(wf_uuid, wf_version)) except OAuthIdentityNotFoundException as e: return lm_exceptions.report_problem(401, "Unauthorized", extra_info={"exception": str(e)}) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem(403, "Forbidden", extra_info={"exception": str(e)}) except Exception as e: raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))
def instances_delete_by_id(instance_uuid): try: response = _get_instances_or_problem(instance_uuid) if isinstance(response, Response): return response lm.deregister_test_instance(response) return connexion.NoContent, 204 except OAuthIdentityNotFoundException as e: return lm_exceptions.report_problem(401, "Unauthorized", extra_info={"exception": str(e)}) except lm_exceptions.EntityNotFoundException as e: return lm_exceptions.report_problem( 404, "Not Found", extra_info={"exception": str(e.detail)}, detail=messages.instance_not_found.format(instance_uuid)) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem(403, "Forbidden", extra_info={"exception": str(e)}) except Exception as e: raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))
def find_by_url(cls, url) -> TestingService: try: return cls.query.filter(TestingService.url == url).one() except NoResultFound as e: logger.debug(e) return None except Exception as e: raise lm_exceptions.LifeMonitorException(detail=str(e), stack=str(e))
def find_by_client_id(cls, client_id): try: return cls.query.filter_by(client_id=client_id).one() except NoResultFound as e: logger.debug(e) return None except Exception as e: raise lm_exceptions.LifeMonitorException(detail=str(e), stack=str(e))
def find_by_uuid(cls, uuid) -> WorkflowRegistry: try: return cls.query.filter( WorkflowRegistry.uuid == lm_utils.uuid_param(uuid)).one() except NoResultFound as e: logger.debug(e) return None except Exception as e: raise lm_exceptions.LifeMonitorException(detail=str(e), stack=str(e))
def get_user_workflow(cls, owner: User, uuid) -> Workflow: try: return cls.query\ .join(Permission)\ .filter(Permission.resource_id == cls.id, Permission.user_id == owner.id)\ .filter(cls.uuid == lm_utils.uuid_param(uuid)).one() except NoResultFound as e: logger.debug(e) return None except Exception as e: raise lm_exceptions.LifeMonitorException(detail=str(e), stack=str(e))
def get_hosted_workflow_version(cls, hosting_service: Resource, uuid, version) -> List[WorkflowVersion]: # TODO: replace WorkflowRegistry with a more general Entity try: return cls.query\ .join(WorkflowRegistry, cls.hosting_service)\ .join(Workflow, Workflow.id == cls.workflow_id)\ .filter(WorkflowRegistry.uuid == lm_utils.uuid_param(hosting_service.uuid))\ .filter(Workflow.uuid == lm_utils.uuid_param(uuid))\ .filter(cls.version == version)\ .order_by(WorkflowVersion.version.desc()).one() except NoResultFound as e: logger.debug(e) return None except Exception as e: raise lm_exceptions.LifeMonitorException(detail=str(e), stack=str(e))
def load_metadata(self): errors = [] # try either with authorization hedaer and without authorization for authorization in self._get_authorizations(): try: auth_header = authorization.as_http_header( ) if authorization else None logger.debug(auth_header) self._crate_helper, self._metadata, self._test_metadata = \ self.load_metadata_files(self.uri, authorization_header=auth_header) self._metadata_loaded = True return self._metadata, self._test_metadata except Exception as e: errors.append(e) if len([ e for e in errors if isinstance(e, lm_exceptions.NotAuthorizedException) ]) == len(errors): raise lm_exceptions.NotAuthorizedException() raise lm_exceptions.LifeMonitorException("ROCrate download error", errors=errors)
def workflows_post(body): registry = current_registry._get_current_object() if registry and 'registry' in body: return lm_exceptions.report_problem( 400, "Bad request", detail=messages.unexpected_registry_uri) if not registry and 'registry' in body: registry_ref = body.get('registry', None) try: registry = lm.get_workflow_registry_by_generic_reference( registry_ref) except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.no_registry_found.format(registry_ref)) submitter = current_user if not current_user or current_user.is_anonymous: # the client is a registry try: submitter_id = body['submitter_id'] # Try to find the identity of the submitter identity = lm.find_registry_user_identity( registry, internal_id=current_user.id, external_id=submitter_id) submitter = identity.user except KeyError: return lm_exceptions.report_problem( 400, "Bad request", detail=messages.no_submitter_id_provided) except OAuthIdentityNotFoundException: return lm_exceptions.report_problem( 401, "Unauthorized", detail=messages.no_user_oauth_identity_on_registry.format( submitter_id or current_user.id, registry.name)) roc_link = body.get('roc_link', None) if not registry and not roc_link: return lm_exceptions.report_problem( 400, "Bad Request", extra_info={"missing input": "roc_link"}, detail=messages.input_data_missing) try: w = lm.register_workflow(roc_link=roc_link, workflow_submitter=submitter, workflow_version=body['version'], workflow_uuid=body.get('uuid', None), workflow_identifier=body.get( 'identifier', None), workflow_registry=registry, name=body.get('name', None), authorization=body.get('authorization', None)) logger.debug("workflows_post. Created workflow '%s' (ver.%s)", w.uuid, w.version) return {'wf_uuid': str(w.workflow.uuid), 'wf_version': w.version}, 201 except KeyError as e: return lm_exceptions.report_problem(400, "Bad Request", extra_info={"exception": str(e)}, detail=messages.input_data_missing) except lm_exceptions.NotValidROCrateException as e: return lm_exceptions.report_problem(400, "Bad Request", extra_info={"exception": str(e)}, detail=messages.invalid_ro_crate) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem( 403, "Forbidden", extra_info={"exception": str(e)}, detail=messages.not_authorized_registry_access.format( registry.name) if registry else messages.not_authorized_workflow_access) except lm_exceptions.WorkflowVersionConflictException: return lm_exceptions.report_problem( 409, "Workflow version conflict", detail=messages.workflow_version_conflict.format( body.get('uuid', None) or body.get('identifier', None), body['version'])) except Exception as e: logger.exception(e) raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))