def __listen_build(build_id): logger.debug('listen:build for %s', build_id) if not build_id: logger.debug('build_id not set') return flask_socketio.disconnect() try: uuid.UUID(build_id) except: logger.debug('build_id not a uuid') return flask_socketio.disconnect() if not sio_is_authorized(['listen:build', build_id]): return flask_socketio.disconnect() conn = dbpool.get() try: token = normalize_token(get_token()) project_id = token['project']['id'] build = conn.execute_one( ''' SELECT id FROM build WHERE project_id = %s AND id = %s ''', [project_id, build_id]) if not build: logger.debug('build does not belong to project') return flask_socketio.disconnect() except: logger.exception("Exception occured") return flask_socketio.disconnect() finally: dbpool.put(conn) flask_socketio.join_room(build_id)
def sio_is_authorized(path): g.db = dbpool.get() try: # Assemble Input Data for Open Policy Agent opa_input = { "input": { "method": "WS", "path": path, "token": normalize_token(get_token()) } } authorized = opa_do_auth(opa_input) if not authorized: logger.warn("Unauthorized socket.io access attempt") return False return True except RequestException as e: logger.error(e) return False finally: dbpool.put(g.db) g.db = None