def test_return_json_response_data():
    """Test valid response is returned from graph db."""
    query = "g.V().count()"
    status, data = g.execute(query)
    logger.info([status, data])
    assert status is True
    r = g.return_json_response_data(data)
    logger.info(r)
    assert r >= 0
Ejemplo n.º 2
0
def test_websocket_connection():
    g = BayesianGraph.instance()
    result = g.V().count().toList()
    assert (result is not None)
    vcount = result[0]
    assert (vcount >= 0)
    logger.info("Connection to WebSocket endpoint: SUCCESS")
def test_execute_invalid_query():
    """Test execution of invalid query."""
    invalid_query = "g.count"

    status, data = g.execute(invalid_query)
    logger.info([status, data])
    assert status is False
    assert "No such property: count for class:" in data["message"]
Ejemplo n.º 4
0
def test_http_connection():
    result = BayesianGraph.execute("g.V().count()")
    code, data = result
    logger.info(result)
    # logger.info code
    # logger.info data
    # logger.info data['result']['data']
    assert (code is True)
    assert (data['result']['data'][0] >= 0)

    logger.info("Connection to HTTP endpoint: SUCCESS")
Ejemplo n.º 5
0
 def create_pv_nodes(self):
     """Create Package and Version nodes, if needed."""
     for pv_dict in self._cve_dict.get('affected'):
         epv_dict = pv_dict.copy()
         epv_dict['ecosystem'] = self._cve_dict.get('ecosystem')
         query = GraphPopulator.construct_graph_nodes(epv_dict)
         success, json_response = BayesianGraph.execute(query)
         e = epv_dict.get('ecosystem')
         p = epv_dict.get('name')
         v = epv_dict.get('version')
         if not success:
             logger.error('Error creating nodes for {e}/{p}/{v}: {r}'.format(
                 e=e, p=p, v=v, r=str(json_response))
             )
Ejemplo n.º 6
0
def handle_properties(ecosystem, package, version):
    """
    Handle (update/delete) properties associated with given EPV.

    Update replaces properties with the same name.

    Expects JSON payload in following format:
    {
        "properties": [
            {
                "name": "cve_ids",
                "value": "CVE-3005-0001:10"
            }
        ]
    }

    "value" can be omitted in DELETE requests.

    :param ecosystem: str, ecosystem
    :param package: str, package name
    :param version: str, package version
    :return: 200 on success, 400 on failure
    """
    # TODO: reduce cyclomatic complexity
    input_json = request.get_json()
    properties = input_json.get('properties')

    error = flask.jsonify({'error': 'invalid input'})
    if not properties:
        return error, 400

    input_json = {
        k: GraphPopulator.sanitize_text_for_query(str(v))
        for k, v in input_json.items()
    }

    if request.method == 'PUT':
        if [
                x for x in properties
                if not x.get('name') or x.get('value') is None
        ]:
            return error, 400

    log_msg = '[{m}] Updating properties for {e}/{p}/{v} with payload {b}'
    current_app.logger.info(
        log_msg.format(m=request.method,
                       e=ecosystem,
                       p=package,
                       v=version,
                       b=input_json))

    query_statement = "g.V()" \
                      ".has('pecosystem','{ecosystem}')" \
                      ".has('pname','{pkg_name}')" \
                      ".has('version','{version}')".format(ecosystem=ecosystem,
                                                           pkg_name=package,
                                                           version=version)
    statement = ''

    if request.method in ('DELETE', 'PUT'):
        # build "delete" part of the statement
        drop_str = ""
        for prop in properties:
            drop_str += query_statement
            drop_str += ".properties('{property}').drop().iterate();".format(
                property=prop['name'])
        statement += drop_str

    if request.method == 'PUT':
        # build "add" part of the statement
        add_str = ""
        for prop in properties:
            add_str += ".property('{property}','{value}')".format(
                property=prop['name'], value=prop['value'])
        statement += query_statement + add_str + ';'

    current_app.logger.info('Gremlin statement: {s}'.format(s=statement))
    success, response_json = BayesianGraph.execute(statement)
    if not success:
        current_app.logger.error(
            "Failed to update properties for {e}/{p}/{v}".format(e=ecosystem,
                                                                 p=package,
                                                                 v=version))
        return flask.jsonify(response_json), 400

    return flask.jsonify(response_json), 200
import logging
import config
import traceback
from minio import Minio
from minio.error import ResponseError, BucketAlreadyOwnedByYou, BucketAlreadyExists
from data_importer import import_epv_from_s3_http
from graph_manager import BayesianGraph

logging.basicConfig()
logger = logging.getLogger(__name__)

# Check whether schema is created or not
# populate schema if not already done
try:
    status, json_result = BayesianGraph.populate_schema()
except Exception as exc:
    # Py2 compatibility: switch to "from exc" once we're on Py3
    new_exc = RuntimeError("Failed to initialize graph schema")
    new_exc.__cause__ = exc
    raise new_exc

if status:
    logger.info("Graph Schema Created")
else:
    logger.error(json_result)
    raise RuntimeError("Failed to setup graph schema")


def test_create_minio_bucket():
    """Test if buckets can be put into the Minio storage."""
Ejemplo n.º 8
0
import data_importer
from graph_manager import BayesianGraph

# Python2.x: Make default encoding as UTF-8
if sys.version_info.major == 2:
    reload(sys)
    sys.setdefaultencoding('UTF8')


app = Flask(__name__)
app.config.from_object('config')
CORS(app)

# Check whether schema is created or not
# populate schema if not already done
if not BayesianGraph.is_index_created():
    print("Index is not created as yet, checking schema creation")
    app.logger.info("Index is not created as yet, checking schema creation")
    if not BayesianGraph.is_schema_defined():
        print("Schema is not yet created, creating now...")
        BayesianGraph.populate_schema()
        # double check
        schema_definition_success = BayesianGraph.is_schema_defined()
        print("Double check: schema_definition_success %s" % schema_definition_success)
        if not schema_definition_success:
            raise RuntimeError("Failed to setup graph schema")
        else:
            print("Ready to serve requests")
else:
    print("Ready to serve requests")
 def g(self):
     return BayesianGraph.instance()
def test_is_schema_defined():
    """Test if schema was initialized correctly."""
    status = g.is_schema_defined()
    logger.info(status)
    assert status is True
def test_is_index_created():
    """Test validity created index."""
    status = g.is_index_created()
    logger.info(status)
    assert status is False