Esempio n. 1
0
def log_parse():
    content_type = request.headers.get('Content-Type', None)
    event_dict = None
    allowed_mime_types = [None, 'text/plain']

    if content_type not in allowed_mime_types or content_type:
        return json_error_response('Unsupported content-type: "{}"',
                                   content_type)

    # decode post data and parse
    data_str = request.data.decode('utf-8')
    event_dict = eventparser.parse_event_to_dict(data_str)
    if not event_dict:
        return json_error_response('Failed to parse event text.')

    # returns the parsed dictionary (also valid JSON)
    return json_response(event_dict)
Esempio n. 2
0
def log_store():
    """Parses data POSTed and logs to the database."""
    # validates content-type
    content_type = request.headers.get('Content-Type', None)
    event_dict = None
    json_mime = 'application/json'
    text_mime = 'text/plain'

    if content_type == text_mime:
        # parse data before attempting to store
        data_str = request.data.decode('utf-8')
        event_dict = eventparser.parse_event_to_dict(data_str)
        if not event_dict:
            return json_error_response('Failed to parse event text.')

    if content_type == json_mime:
        # bypass all the parsing and extract json from POST data
        event_dict = json.loads(request.json)
        if not event_dict:
            # possibly invalid json syntax or a general decoding failure
            return json_error_response('Failed to decode json payload.')

    if event_dict is None:
        # if we reach here, no handler was found
        return json_error_response('Unable to decode content-type "{}".'.format(content_type))

    log_entry = EventLog.from_event_dict(event_dict)
    if not log_entry:
        return json_error_response('Unabled to extract all fields from the given data.')

    session = get_db_sessionmaker(debug=True)()
    session.add(log_entry)
    session.commit()

    clustering_worker.report_event_received()

    # returns an empty json, also indicading success via http status code
    return json_response({})
def test_semicolon_in_value():
    """Tests if key-value pairs containing semicolons cause a parse error."""
    eventparser.parse_event_to_dict('Foo: Bar=:')
def test_empty_string():
    """Checks nothing unexpected happens with empty input."""
    assert eventparser.parse_event_to_dict('') == {}
def test_array_end_no_semicolon():
    """Tests if arrays with no semicolon on the end are parsed correctly."""
    event_dict = eventparser.parse_event_to_dict('Foo: A; B')
    assert event_dict == {'Foo': ['A', 'B']}
def test_key_value_end_no_semicolon():
    """Test if key-value sections with no ending semicolon are parsed correctly."""
    event_dict = eventparser.parse_event_to_dict('Foo: A=0; B=1')
    assert event_dict == {'Foo':{'A': 0, 'B': 1}}
def test_mutiple_arrays_sections():
    """Checks if multiple array sections are correctly parsed."""
    event_dict = eventparser.parse_event_to_dict('Foo: A; B; C; Bar: X; Y; Z;')
    assert event_dict == {'Foo' : ['A', 'B', 'C'], 'Bar': ['X', 'Y', 'Z']}
def test_attribute_datetime_parsing():
    """Checks if date-like attributes are parsed correctly."""
    event_dict = eventparser.parse_event_to_dict('Datetime: 2016-10-4 16:47:50;')
    expected_date = datetime.datetime(2016, 10, 4, 16, 47, 50)
    assert event_dict == {'Datetime' : [expected_date]}
def test_object_key_array():
    """Checks if key-based arrays ignore invalid elements."""
    event_dict = eventparser.parse_event_to_dict('Foo: Bar=A;B;C;')
    assert event_dict == {'Foo': {'Bar': 'A'}}
Esempio n. 10
0
def test_mutiple_objects():
    """Checks if multiple objects arrays are correctly parsed;"""
    event_dict = eventparser.parse_event_to_dict('Foo: Bar=A; Baz: Attr=B;')
    assert event_dict == {'Foo' : {'Bar': 'A'}, 'Baz': {'Attr': 'B'}}
Esempio n. 11
0
def test_object_key():
    """Checks if key-based values are correctly parsed."""
    assert eventparser.parse_event_to_dict('Foo: Bar=Baz;') == {'Foo' : {'Bar': 'Baz'}}
Esempio n. 12
0
def test_object_array():
    """Checks if arrays are correctly parsed."""
    assert eventparser.parse_event_to_dict('Foo: 1; 2; 3;') == {'Foo' : [1, 2, 3]}
Esempio n. 13
0
def test_empty_object():
    """Checks that empty objects return the expected dictionary."""
    eventparser.parse_event_to_dict('Foo:')
Esempio n. 14
0
def test_array_root():
    """Checks if a top-level array string would return an empty dict."""
    assert eventparser.parse_event_to_dict('A; B; C;') == {}
Esempio n. 15
0
def test_orphan_attribute():
    """Checks if a string with an orphan attribute would return an empty dict."""
    assert eventparser.parse_event_to_dict('Dead=Beef') == {}