From fed04e8e5aba25587484c82be405f219998091e3 Mon Sep 17 00:00:00 2001 From: Terrtia Date: Fri, 13 Sep 2019 16:33:34 +0200 Subject: [PATCH] chg: [Tracker] add optionnal description field --- OVERVIEW.md | 60 +++---------------- bin/packages/Term.py | 18 +++++- doc/README.md | 3 + var/www/modules/hunter/Flask_hunter.py | 20 ++++++- .../modules/hunter/templates/Add_tracker.html | 6 ++ .../modules/hunter/templates/showTracker.html | 36 ++++++++++- 6 files changed, 84 insertions(+), 59 deletions(-) diff --git a/OVERVIEW.md b/OVERVIEW.md index be19bbfd..68efa81f 100644 --- a/OVERVIEW.md +++ b/OVERVIEW.md @@ -109,17 +109,18 @@ Redis and ARDB overview | **uuid**:ltags | **tag** | | **uuid**:ltagsgalaxies | **tag** | -## DB2 - Leak Hunter: +## DB3 - Leak Hunter: ##### Tracker metadata: | Hset - Key | Field | Value | | ------ | ------ | ------ | -| tracker:**uuid** | tracked | **tacked word/set/regex** | -| | type | **word/set/regex** | -| | date | **date added** | -| | user_id | **created by user_id** | -| | dashboard | **0/1 Display alert on dashboard** | -| | level | **0/1 Tracker visibility** | +| tracker:**uuid** | tracker | **tacked word/set/regex** | +| | type | **word/set/regex** | +| | date | **date added** | +| | user_id | **created by user_id** | +| | dashboard | **0/1 Display alert on dashboard** | +| | description | **Tracker description** | +| | level | **0/1 Tracker visibility** | ##### Tracker by user_id (visibility level: user only): | Set - Key | Value | @@ -180,51 +181,6 @@ Redis and ARDB overview | ------ | ------ | | stat_token_history | **date** | -## DB2 - TermFreq: - -##### Set: - -##### Set: -| Key | Value | -| ------ | ------ | -| TrackedSetTermSet | **tracked_term** | -| TrackedSetSet | **tracked_set** | -| TrackedRegexSet | **tracked_regex** | -| | | -| | | -| tracked_**tracked_term** | **item_path** | -| set_**tracked_set** | **item_path** | -| regex_**tracked_regex** | **item_path** | -| | | -| TrackedNotifications | **tracked_trem / set / regex** | -| | | -| TrackedNotificationTags_**tracked_trem / set / regex** | **tag** | -| | | -| TrackedNotificationEmails_**tracked_trem / set / regex** | **email** | - -##### Zset: -| Key | Field | Value | -| ------ | ------ | ------ | -| per_paste_TopTermFreq_set_month | **term** | **nb_seen** | -| per_paste_TopTermFreq_set_week | **term** | **nb_seen** | -| per_paste_TopTermFreq_set_day_**epoch** | **term** | **nb_seen** | -| | | | -| TopTermFreq_set_month | **term** | **nb_seen** | -| TopTermFreq_set_week | **term** | **nb_seen** | -| TopTermFreq_set_day_**epoch** | **term** | **nb_seen** | - - -##### Hset: -| Key | Field | Value | -| ------ | ------ | ------ | -| TrackedTermDate | **tracked_term** | **epoch** | -| TrackedSetDate | **tracked_set** | **epoch** | -| TrackedRegexDate | **tracked_regex** | **epoch** | -| | | | -| BlackListTermDate | **blacklisted_term** | **epoch** | -| | | | -| **epoch** | **term** | **nb_seen** | - ## DB6 - Tags: ##### Hset: diff --git a/bin/packages/Term.py b/bin/packages/Term.py index f29cfb2c..8cc63c9a 100755 --- a/bin/packages/Term.py +++ b/bin/packages/Term.py @@ -144,6 +144,8 @@ def parse_json_term_to_add(dict_input, user_id): if not term_type: return ({"status": "error", "reason": "Term type not provided"}, 400) nb_words = dict_input.get('nb_words', 1) + description = dict_input.get('description', '') + description = escape(description) res = parse_tracked_term_to_add(term , term_type, nb_words=nb_words) if res[1]!=200: @@ -174,7 +176,7 @@ def parse_json_term_to_add(dict_input, user_id): if is_term_tracked_in_user_level(term, term_type, user_id): return ({"status": "error", "reason": "Term already tracked"}, 409) - term_uuid = add_tracked_term(term , term_type, user_id, level, tags, mails) + term_uuid = add_tracked_term(term , term_type, user_id, level, tags, mails, description) return ({'term': term, 'type': term_type, 'uuid': term_uuid}, 200) @@ -217,7 +219,7 @@ def parse_tracked_term_to_add(term , term_type, nb_words=1): return ({"status": "error", "reason": "Incorrect type"}, 400) return ({"status": "success", "term": term, "type": term_type}, 200) -def add_tracked_term(term , term_type, user_id, level, tags, mails, dashboard=0): +def add_tracked_term(term , term_type, user_id, level, tags, mails, description, dashboard=0): term_uuid = str(uuid.uuid4()) @@ -229,6 +231,9 @@ def add_tracked_term(term , term_type, user_id, level, tags, mails, dashboard=0) r_serv_term.hset('tracker:{}'.format(term_uuid), 'level', level) r_serv_term.hset('tracker:{}'.format(term_uuid), 'dashboard', dashboard) + if description: + r_serv_term.hset('tracker:{}'.format(term_uuid), 'description', description) + # create all term set r_serv_term.sadd('all:tracker:{}'.format(term_type), term) @@ -300,9 +305,14 @@ def delete_term(term_uuid): r_serv_term.delete('tracker:item:{}:{}'.format(term_uuid, date)) r_serv_term.delete('tracker:stat:{}'.format(term_uuid)) +def replace_tracker_description(term_uuid, description): + description = escape(description) + r_serv_term.hset('tracker:{}'.format(term_uuid), 'description', description) + def replace_tracked_term_tags(term_uuid, tags): r_serv_term.delete('tracker:tags:{}'.format(term_uuid)) for tag in tags: + tag = escape(tag) r_serv_term.sadd('tracker:tags:{}'.format(term_uuid), tag) def replace_tracked_term_mails(term_uuid, mails): @@ -312,6 +322,7 @@ def replace_tracked_term_mails(term_uuid, mails): else: r_serv_term.delete('tracker:mail:{}'.format(term_uuid)) for mail in mails: + mail = escape(mail) r_serv_term.sadd('tracker:mail:{}'.format(term_uuid), mail) def get_term_uuid_list(term, term_type): @@ -394,11 +405,12 @@ def get_tracked_term_last_seen(term_uuid): else: return None -def get_term_metedata(term_uuid, user_id=False, level=False, tags=False, mails=False, sparkline=False): +def get_term_metedata(term_uuid, user_id=False, description=False, level=False, tags=False, mails=False, sparkline=False): dict_uuid = {} dict_uuid['term'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'tracked') dict_uuid['type'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'type') dict_uuid['date'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'date') + dict_uuid['description'] = r_serv_term.hget('tracker:{}'.format(term_uuid), 'description') dict_uuid['first_seen'] = get_tracked_term_first_seen(term_uuid) dict_uuid['last_seen'] = get_tracked_term_last_seen(term_uuid) if user_id: diff --git a/doc/README.md b/doc/README.md index c4139f10..f0bf040c 100644 --- a/doc/README.md +++ b/doc/README.md @@ -783,6 +783,9 @@ You need to use a regex if you want to use one of the following special characte - tracker visibility - *int - 0: user only, 1: all users* - default: `1` +- `description` + - tracker description + - *str* #### JSON response - `uuid` diff --git a/var/www/modules/hunter/Flask_hunter.py b/var/www/modules/hunter/Flask_hunter.py index 38376b67..f86d3403 100644 --- a/var/www/modules/hunter/Flask_hunter.py +++ b/var/www/modules/hunter/Flask_hunter.py @@ -9,7 +9,7 @@ import redis import datetime import calendar import flask -from flask import Flask, render_template, jsonify, request, Blueprint, url_for, redirect, Response +from flask import Flask, render_template, jsonify, request, Blueprint, url_for, redirect, Response, escape from Role_Manager import login_admin, login_analyst from flask_login import login_required, current_user @@ -88,6 +88,7 @@ def add_tracked_menu(): term = request.form.get("term") term_type = request.form.get("tracker_type") nb_words = request.form.get("nb_word", 1) + description = request.form.get("description", '') level = request.form.get("level", 0) tags = request.form.get("tags", []) mails = request.form.get("mails", []) @@ -100,7 +101,7 @@ def add_tracked_menu(): if tags: tags = tags.split() - input_dict = {"term": term, "type": term_type, "nb_words": nb_words, "tags": tags, "mails": mails, "level": level} + input_dict = {"term": term, "type": term_type, "nb_words": nb_words, "tags": tags, "mails": mails, "level": level, "description": description} user_id = current_user.get_id() res = Term.parse_json_term_to_add(input_dict, user_id) if res[1] == 200: @@ -129,7 +130,7 @@ def show_tracker(): if date_to: date_to = date_to.replace('-', '') - tracker_metadata = Term.get_term_metedata(term_uuid, user_id=True, level=True, tags=True, mails=True, sparkline=True) + tracker_metadata = Term.get_term_metedata(term_uuid, user_id=True, level=True, description=True, tags=True, mails=True, sparkline=True) if date_from: res = Term.parse_get_tracker_term_item({'uuid': term_uuid, 'date_from': date_from, 'date_to': date_to}, user_id) @@ -145,6 +146,19 @@ def show_tracker(): return render_template("showTracker.html", tracker_metadata=tracker_metadata, bootstrap_label=bootstrap_label) +@hunter.route("/tracker/update_tracker_description", methods=['POST']) +@login_required +@login_analyst +def update_tracker_description(): + user_id = current_user.get_id() + term_uuid = request.form.get('uuid') + res = Term.check_term_uuid_valid_access(term_uuid, user_id) + if res: # invalid access + return Response(json.dumps(res[0], indent=2, sort_keys=True), mimetype='application/json'), res[1] + description = escape( str(request.form.get('description', '')) ) + Term.replace_tracker_description(term_uuid, description) + return redirect(url_for('hunter.show_tracker', uuid=term_uuid)) + @hunter.route("/tracker/update_tracker_tags", methods=['POST']) @login_required @login_analyst diff --git a/var/www/modules/hunter/templates/Add_tracker.html b/var/www/modules/hunter/templates/Add_tracker.html index dc699a71..08613209 100644 --- a/var/www/modules/hunter/templates/Add_tracker.html +++ b/var/www/modules/hunter/templates/Add_tracker.html @@ -49,6 +49,12 @@
+ +
+
+
+
+
diff --git a/var/www/modules/hunter/templates/showTracker.html b/var/www/modules/hunter/templates/showTracker.html index fae63ae1..7c149205 100644 --- a/var/www/modules/hunter/templates/showTracker.html +++ b/var/www/modules/hunter/templates/showTracker.html @@ -49,6 +49,12 @@

{{ tracker_metadata['uuid'] }}

+
+ {%if tracker_metadata['description']%} + {{ tracker_metadata['description'] }} + {%endif%} + +
  • @@ -108,6 +114,25 @@
+
+
+ +
Update this tracker description:
+
+
+
+
+ +
+ + +
+ +
+
@@ -223,7 +248,8 @@