Skip to content

Commit 6ee1acb

Browse files
committed
Merge branch 'master' of https://github.com/IBM/qpylib into story250001
2 parents e008055 + 7dc2dbd commit 6ee1acb

File tree

9 files changed

+34
-22
lines changed

9 files changed

+34
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ qpylib.egg-info
1414
RemoteSystemsTempFiles
1515
qpylib/version.py
1616
.idea/
17+
coverage.xml
18+
.coverage

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[![Build Status](https://travis-ci.com/IBM/qpylib.svg?branch=master)](https://travis-ci.com/IBM/qpylib)
22
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=qpylib&metric=alert_status)](https://sonarcloud.io/dashboard?id=qpylib)
3+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=qpylib&metric=coverage)](https://sonarcloud.io/component_measures?id=qpylib&metric=new_coverage&view=list)
34

45

56
# QRadar App Python Library (qpylib)
@@ -22,4 +23,3 @@ any code contained herein is unsupported and subject to change.
2223
* [CONTRIBUTING](CONTRIBUTING.md)
2324
* [MAINTAINERS](MAINTAINERS.md)
2425
* [CHANGELOG](CHANGELOG.md)
25-

clean.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ rm -f qpylib/version.py
88
rm -rf dist qpylib.egg-info
99
rm -rf qpylib/__pycache__
1010
rm -rf test/__pycache__
11+
rm -f .coverage
12+
rm -f coverage.xml

qpylib/asset_qpylib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from . import json_qpylib
77

88
# Context location yet to be finalised.
9-
JSON_LD_CONTEXT = 'http://qradar/context/location'
9+
JSON_LD_CONTEXT = 'https://qradar/context/location'
1010

1111
# The api method to GET an individual asset is not yet supported.
1212
def get_asset_url(asset_id):

qpylib/json_qpylib.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
# A dictionary of jsonld context types mapped to the type name
99
JSONLD_TYPES = {}
1010

11+
KEY_CONTEXT = '@context'
12+
KEY_TYPE = '@type'
13+
KEY_ID = '@id'
14+
1115
def register_jsonld_endpoints():
1216
manifest = app_qpylib.get_manifest_json()
1317

@@ -35,9 +39,10 @@ def register_jsonld_endpoints():
3539
register_jsonld_type_from_context(jsonld_context)
3640

3741
def _extract_jsonld_context(argument, mime_id, context_id):
38-
if mime_id in argument.keys() and context_id in argument.keys():
39-
if argument[mime_id] == 'application/json+ld':
40-
return argument[context_id]
42+
if (mime_id in argument.keys() and
43+
context_id in argument.keys() and
44+
argument[mime_id] == 'application/json+ld'):
45+
return argument[context_id]
4146
return None
4247

4348
def register_jsonld_type_from_context(context):
@@ -57,12 +62,12 @@ def get_jsonld_type(jsonld_type):
5762

5863
def _extract_type(argument):
5964
type_id = None
60-
if '@context' in argument.keys():
61-
context = argument['@context']
62-
if '@type' in context.keys():
63-
type_id = context['@type']
64-
if type_id == '@id' and '@id' in context.keys():
65-
type_id = context['@id']
65+
if KEY_CONTEXT in argument.keys():
66+
context = argument[KEY_CONTEXT]
67+
if KEY_TYPE in context.keys():
68+
type_id = context[KEY_TYPE]
69+
if type_id == KEY_ID and KEY_ID in context.keys():
70+
type_id = context[KEY_ID]
6671
return type_id
6772

6873
def render_json_ld_type(jld_type, data, jld_id=None):
@@ -71,16 +76,16 @@ def render_json_ld_type(jld_type, data, jld_id=None):
7176
for json_key in data:
7277
json_dict[json_key] = data[json_key]
7378

74-
json_dict['@context'] = jld_context['@context']
75-
json_dict['@type'] = jld_type
79+
json_dict[KEY_CONTEXT] = jld_context[KEY_CONTEXT]
80+
json_dict[KEY_TYPE] = jld_type
7681
if jld_id is not None:
77-
json_dict['@type'] = jld_type
82+
json_dict[KEY_TYPE] = jld_type
7883

7984
return json.dumps(json_dict, sort_keys=True)
8085

8186
# pylint: disable=too-many-arguments
8287
def json_ld(jld_context, jld_id, jld_type, name, description, data):
83-
return json.dumps({'@context': jld_context, '@id': jld_id, '@type': jld_type, 'name': name,
88+
return json.dumps({KEY_CONTEXT: jld_context, KEY_ID: jld_id, KEY_TYPE: jld_type, 'name': name,
8489
'description': description, 'data': data}, sort_keys=True)
8590

8691
def json_html(html):

qpylib/offense_qpylib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from . import qpylib
99

1010
# Context location yet to be finalised
11-
JSON_LD_CONTEXT = 'http://qradar/context/location'
11+
JSON_LD_CONTEXT = 'https://qradar/context/location'
1212
OFFENSE_HEADER_TEMPLATE = ('<div class="gridHeader" id="{0}gridheaderdiv" style="clear:both;">'
1313
'<div class="heading" id="{0}headingdiv">{1}</div>'
1414
'</div>')
15+
OFFENSE_ROW_TEMPLATE = '<tr><td>{0}</td><td>{1}</td></tr>'
1516

1617
def get_offense_url(offense_id):
1718
return 'api/siem/offenses/{0}'.format(offense_id)
@@ -27,9 +28,9 @@ def get_offense_json(offense_id):
2728

2829
def get_offense_html_example(offense_json):
2930
return ('<table><tbody>' +
30-
'<tr><td>Offense ID</td><td>' + str(offense_json['id']) + '</td></tr>' +
31-
'<tr><td>Source IP</td><td>' + offense_json['offense_source'] + '</td></tr>' +
32-
'<tr><td>Severity</td><td>' + str(offense_json['severity']) + '</td></tr>' +
31+
OFFENSE_ROW_TEMPLATE.format('Offense ID', str(offense_json['id'])) +
32+
OFFENSE_ROW_TEMPLATE.format('Source IP', offense_json['offense_source']) +
33+
OFFENSE_ROW_TEMPLATE.format('Severity', str(offense_json['severity'])) +
3334
'</tbody></table>')
3435

3536
def get_offense_html_header(offense_id):

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ flask
22
requests
33
pycryptodome
44
cryptography
5-
pylint
6-
pytest
75
responses
6+
pylint==2.5.3
7+
pytest==5.4.3
8+
pytest-cov==2.10.0

sonar-project.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ sonar.organization=qradar-app-framework
44
sonar.projectKey=qpylib
55
sonar.tests=test
66
sonar.host.url=https://sonarcloud.io
7+
sonar.python.coverage.reportPaths=coverage.xml

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#
44
# SPDX-License-Identifier: Apache-2.0
55

6-
python -m pytest -v test
6+
python -m pytest -v --cov-report xml:coverage.xml --cov-report term --cov=qpylib test

0 commit comments

Comments
 (0)