|
1 | 1 | import flask |
2 | 2 | from canonicalwebteam.store_api.dashboard import Dashboard |
3 | | - |
| 3 | +from canonicalwebteam.exceptions import StoreApiResourceNotFound, StoreApiError |
4 | 4 | from webapp.helpers import api_publisher_session |
5 | 5 | from webapp.decorators import login_required |
6 | 6 | from webapp.publisher.cve.cve_helper import CveHelper |
7 | 7 |
|
8 | 8 | dashboard = Dashboard(api_publisher_session) |
9 | 9 |
|
10 | 10 |
|
| 11 | +def can_user_access_cve_data(snap_name): |
| 12 | + """ |
| 13 | + Check if the user has access to CVE data for the given snap. |
| 14 | +
|
| 15 | + :return: A tuple containing: |
| 16 | + has_access (bool): True if the user has access, False otherwise. |
| 17 | + error_message (str): Error message if access is denied. |
| 18 | + status_code (int): HTTP status code for the response. |
| 19 | + """ |
| 20 | + is_user_canonical = flask.session["publisher"].get("is_canonical", False) |
| 21 | + |
| 22 | + # TODO: in future with brand store support we will need more specific |
| 23 | + # checks, such as those implemented in CveHelper.can_user_access_cve_data |
| 24 | + # For now, we only check if user is Canonical member and has |
| 25 | + # publisher access to the snap. |
| 26 | + if not is_user_canonical: |
| 27 | + return (False, "User is not allowed to see snap's CVE data.", 403) |
| 28 | + |
| 29 | + try: |
| 30 | + snap_details = dashboard.get_snap_info(flask.session, snap_name) |
| 31 | + except StoreApiResourceNotFound: |
| 32 | + return (False, f"CVEs data for '{snap_name}' snap not found.", 404) |
| 33 | + except StoreApiError: |
| 34 | + return (False, f"Error fetching '{snap_name}' snap details.", 500) |
| 35 | + |
| 36 | + if not snap_details: |
| 37 | + return (False, f"CVEs data for '{snap_name}' snap not found.", 404) |
| 38 | + |
| 39 | + return (True, None, 200) |
| 40 | + |
| 41 | + |
| 42 | +@login_required |
| 43 | +def has_cves(snap_name): |
| 44 | + |
| 45 | + # Check if the user has access to CVE data for the given snap |
| 46 | + has_access, error_message, status_code = can_user_access_cve_data( |
| 47 | + snap_name |
| 48 | + ) |
| 49 | + if not has_access: |
| 50 | + return ( |
| 51 | + flask.jsonify({"success": False, "error": error_message}), |
| 52 | + status_code, |
| 53 | + ) |
| 54 | + |
| 55 | + snap_has_cves = CveHelper.has_cve_data(snap_name) |
| 56 | + if snap_has_cves: |
| 57 | + return flask.jsonify({"success": True}) |
| 58 | + else: |
| 59 | + return ( |
| 60 | + flask.jsonify( |
| 61 | + { |
| 62 | + "success": False, |
| 63 | + "error": f"CVEs data for '{snap_name}' snap not found.", |
| 64 | + } |
| 65 | + ), |
| 66 | + 404, |
| 67 | + ) |
| 68 | + |
| 69 | + |
11 | 70 | @login_required |
12 | 71 | def get_cves(snap_name, revision): |
| 72 | + # Check if the user has access to CVE data for the given snap |
| 73 | + has_access, error_message, status_code = can_user_access_cve_data( |
| 74 | + snap_name |
| 75 | + ) |
| 76 | + if not has_access: |
| 77 | + return ( |
| 78 | + flask.jsonify({"success": False, "error": error_message}), |
| 79 | + status_code, |
| 80 | + ) |
13 | 81 |
|
14 | 82 | # Filtering params |
15 | 83 | usn_ids = flask.request.args.getlist("usn_id") |
@@ -59,34 +127,6 @@ def get_cves(snap_name, revision): |
59 | 127 | # Pagination params |
60 | 128 | page = flask.request.args.get("page", default=1, type=int) |
61 | 129 | page_size = flask.request.args.get("page_size", default=10, type=int) |
62 | | - is_user_canonical = flask.session["publisher"].get("is_canonical", False) |
63 | | - |
64 | | - # TODO: in future with brand store support we will need more specific |
65 | | - # checks, such as those implemented in CveHelper.can_user_access_cve_data |
66 | | - # For now, we only check if user is Canonical member and has |
67 | | - # publisher access to the snap. |
68 | | - if not is_user_canonical: |
69 | | - return ( |
70 | | - flask.jsonify( |
71 | | - { |
72 | | - "success": False, |
73 | | - "error": "User is not allowed to see snap's CVE data.", |
74 | | - } |
75 | | - ), |
76 | | - 403, |
77 | | - ) |
78 | | - |
79 | | - snap_details = dashboard.get_snap_info(flask.session, snap_name) |
80 | | - if not snap_details: |
81 | | - return ( |
82 | | - flask.jsonify( |
83 | | - { |
84 | | - "success": False, |
85 | | - "error": f"Snap '{snap_name}' not found.", |
86 | | - } |
87 | | - ), |
88 | | - 404, |
89 | | - ) |
90 | 130 |
|
91 | 131 | cves = CveHelper.get_cve_with_revision(snap_name, revision) |
92 | 132 | cves = CveHelper.filter_cve_data( |
|
0 commit comments