def test_database(errors): try: session = Session() session.query(RatingLevel).first() except SQLAlchemyError: errors.append({ 'name': 'database', 'status': falcon.HTTP_INTERNAL_SERVER_ERROR })
def on_patch(self, req, resp, macroprocess_id): """Updates (partially) the macroprocess requested. All entities that reference the macroprocess will be affected by the update. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param macroprocess_id: The id of macroprocess to be patched. """ session = Session() try: macroprocess = session.query(BusinessMacroprocess).get( macroprocess_id) if macroprocess is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(macroprocess, req.media, only=['name']) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': macroprocess.asdict()} finally: session.close()
def on_get(self, req, resp, organization_code): """GETs a paged collection of departments of an organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() # Build query to fetch items query = session\ .query(OrganizationDepartment)\ .filter(OrganizationDepartment.organization_id == organization_code)\ .order_by(OrganizationDepartment.created_on)\ .options(joinedload(OrganizationDepartment.department, innerjoin=True)) data, paging = get_collection_page(req, query, custom_asdict) resp.media = { 'data': data, 'paging': paging } finally: session.close()
def on_post(self, req, resp, organization_code): """Adds a IT asset to an organization's IT service. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() errors = validate_post(req.media, organization_code, session) if errors: raise HTTPUnprocessableEntity(errors) accepted_fields = ['it_asset_id', 'external_identifier'] item = OrganizationITAsset().fromdict(req.media, only=accepted_fields) item.organization_id = organization_code session.add(item) session.commit() resp.status = falcon.HTTP_CREATED resp.location = req.relative_uri + f'/{item.instance_id}' resp.media = {'data': custom_asdict(item)} finally: session.close()
def on_post(self, req, resp, organization_code): """Adds a security threat to an organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() errors = validate_post(req.media, organization_code, session) if errors: raise HTTPUnprocessableEntity(errors) item = OrganizationSecurityThreat() item.organization_id = organization_code item.security_threat_id = req.media.get('security_threat_id') item.threat_level_id = req.media.get('threat_level_id') session.add(item) session.commit() resp.status = falcon.HTTP_CREATED resp.location = req.relative_uri + f'/{item.id}' resp.media = {'data': custom_asdict(item)} finally: session.close()
def on_put(self, req, resp, user_id, role_id): """Adds a role to a system user. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param user_id: The id of user. :param role_id: The id of role to be added. """ session = Session() try: user = session.query(SystemUser).get(user_id) if user is None: raise falcon.HTTPNotFound() errors = validate_put(req.media, user_id, role_id, session) if errors: raise HTTPUnprocessableEntity(errors) # Add role if not already there user_role = find_user_role(user_id, role_id, session) if not user_role: user_role = SystemUserRole(user_id=user_id, role_id=role_id) session.add(user_role) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': custom_asdict(user_role)} finally: session.close()
def on_patch(self, req, resp, mitigation_control_id): """Updates (partially) the mitigation control requested. All entities that reference the mitigation control will be affected by the update. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param mitigation_control_id: The id of mitigation control to be patched. """ session = Session() try: item = session.query(MitigationControl).get(mitigation_control_id) if item is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(item, req.media, only=['name', 'description']) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, organization_code): """GETs a paged collection of processes of an organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() # Build query to fetch items query = session\ .query(OrganizationProcess) \ .filter(OrganizationProcess.organization_id == organization_code)\ .order_by(OrganizationProcess.created_on) # Handle optional filters macroprocess_instance_id = req.get_param_as_int('macroprocessInstanceId') if macroprocess_instance_id: query = query.filter(OrganizationProcess.macroprocess_instance_id == macroprocess_instance_id) data, paging = get_collection_page(req, query, custom_asdict) resp.media = { 'data': data, 'paging': paging } finally: session.close()
def on_patch(self, req, resp, security_threat_id): """Updates (partially) the security threat requested. All entities that reference the security threat will be affected by the update. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param security_threat_id: The id of security threat to be patched. """ session = Session() try: security_threat = session.query(SecurityThreat).get(security_threat_id) if security_threat is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(security_threat, req.media, only=['name', 'description']) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': security_threat.asdict()} finally: session.close()
def on_patch(self, req, resp, it_asset_category_id): """Updates (partially) the IT asset category requested. All entities that reference the IT asset category will be affected by the update. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param it_asset_category_id: The id of IT asset category to be patched. """ session = Session() try: it_asset_category = session.query(ITAssetCategory).get( it_asset_category_id) if it_asset_category is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(it_asset_category, req.media, only=['name']) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': it_asset_category.asdict()} finally: session.close()
def on_get(self, req, resp): """GETs a paged collection of system users. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. """ session = Session() try: query = session.query(SystemUser).order_by(SystemUser.full_name) data, paging = get_collection_page(req, query, custom_asdict) resp.media = {'data': data, 'paging': paging} finally: session.close()
def on_get(self, req, resp): """GETs a paged collection of IT services available. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. """ session = Session() try: query = session.query(ITService).order_by(ITService.name) data, paging = get_collection_page(req, query) resp.media = {'data': data, 'paging': paging} finally: session.close()
def on_get(self, req, resp, organization_code): """GETs a single organization by its code. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of organization to retrieve. """ session = Session() try: item = session.query(Organization).get(organization_code) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, mitigation_control_id): """GETs a single mitigation control by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param mitigation_control_id: The id of mitigation control to retrieve. """ session = Session() try: item = session.query(MitigationControl).get(mitigation_control_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, role_id): """GETs a single role by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param role_id: The id of role to retrieve. """ session = Session() try: item = session.query(SystemRole).get(role_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, user_id): """GETs a single user by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param user_id: The id of user to retrieve. """ session = Session() try: item = session.query(SystemUser).get(user_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': custom_asdict(item)} finally: session.close()
def on_get(self, req, resp, security_threat_id): """GETs a single security threat by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param security_threat_id: The id of security threat to retrieve. """ session = Session() try: item = session.query(SecurityThreat).get(security_threat_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, it_asset_category_id): """GETs a single IT asset by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param it_asset_category_id: The id of IT asset category to retrieve. """ session = Session() try: item = session.query(ITAssetCategory).get(it_asset_category_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, department_id): """GETs a single department by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param department_id: The id of department to retrieve. """ session = Session() try: item = session.query(BusinessDepartment).get(department_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp, macroprocess_id): """GETs a single macroprocess by id. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param macroprocess_id: The id of macroprocess to retrieve. """ session = Session() try: item = session.query(BusinessMacroprocess).get(macroprocess_id) if item is None: raise falcon.HTTPNotFound() resp.media = {'data': item.asdict()} finally: session.close()
def on_get(self, req, resp): """GETs a paged collection of organizations. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. """ session = Session() try: query = session.query(Organization).order_by(Organization.legal_name, Organization.created_on) data, paging = get_collection_page(req, query) resp.media = { 'data': data, 'paging': paging } finally: session.close()
def on_patch(self, req, resp, user_id): """Updates (partially) the system user requested. All entities that reference the system user will be affected by the update. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param user_id: The id of user to be patched. """ session = Session() try: user = session.query(SystemUser).get(user_id) if user is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(user, req.media, only=['email', 'full_name']) # Update password if informed if 'password' in req.media: password = req.media.get('password') user.hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()) user.last_modified_on = datetime.utcnow() # Block / Unblock user if requested if 'is_blocked' in req.media: is_blocked = req.media.get('is_blocked') change_block_state(is_blocked, user) # Unlock if requested if req.media.get('unlock') is True: user.locked_out_on = None user.last_modified_on = datetime.utcnow() session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': custom_asdict(user)} finally: session.close()
def on_post(self, req, resp, organization_code): """Creates a new analysis for the organization considering the already filled values for relevance, vulnerability and security threat levels in processes, IT services, IT assets and security threats. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() errors = validate_post(req.media) if errors: raise HTTPUnprocessableEntity(errors) scopes = remove_redundant_scopes(req.media.get('scopes')) accepted_fields = ['description'] item = OrganizationAnalysis().fromdict(req.media, only=accepted_fields) item.organization_id = organization_code item.total_processed_items = process_analysis( session, item, organization_code, scopes) if item.total_processed_items == 0: raise HTTPUnprocessableEntity( [build_error(Message.ERR_NO_ITEMS_TO_ANALYZE)]) session.add(item) session.commit() resp.status = falcon.HTTP_CREATED resp.location = req.relative_uri + f'/{item.id}' resp.media = {'data': create_response_asdict(item)} finally: session.close()
def on_get(self, req, resp, organization_code): """GETs a paged collection of analyses of an organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() query = session\ .query(OrganizationAnalysis) \ .filter(OrganizationAnalysis.organization_id == organization_code) \ .order_by(OrganizationAnalysis.created_on.desc()) data, paging = get_collection_page(req, query, custom_asdict) resp.media = {'data': data, 'paging': paging} finally: session.close()
def on_patch(self, req, resp, organization_code): """Updates (partially) the organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of organization to be patched. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() errors = validate_patch(req.media, session) if errors: raise HTTPUnprocessableEntity(errors) patch_item(organization, req.media, only=['tax_id', 'legal_name', 'trade_name']) session.commit() resp.status = falcon.HTTP_OK resp.media = {'data': organization.asdict()} finally: session.close()
def on_get(self, req, resp, organization_code): """GETs a paged collection of security threats of an organization. :param req: See Falcon Request documentation. :param resp: See Falcon Response documentation. :param organization_code: The code of the organization. """ session = Session() try: organization = session.query(Organization).get(organization_code) if organization is None: raise falcon.HTTPNotFound() # Build query to fetch items query = session\ .query(OrganizationSecurityThreat)\ .join(SecurityThreat)\ .filter(OrganizationSecurityThreat.organization_id == organization_code)\ .order_by(SecurityThreat.name)\ data, paging = get_collection_page(req, query, custom_asdict) resp.media = {'data': data, 'paging': paging} finally: session.close()