gamingflexer commited on
Commit
fc8e2ac
1 Parent(s): c5cbcbd

Add CopyLeaks integration and webhook handling

Browse files
Files changed (1) hide show
  1. src/checker/copy_leaks_weekhook.py +65 -0
src/checker/copy_leaks_weekhook.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from copyleaks.models.export import Export, ExportCrawledVersion, ExportResult
3
+ from copyleaks.copyleaks import Copyleaks
4
+ from copyleaks.exceptions.command_error import CommandError
5
+ from copyleaks.models.submit.document import FileDocument
6
+ from copyleaks.models.submit.properties.scan_properties import ScanProperties
7
+ from copyleaks.models.submit.properties.pdf import Pdf
8
+ from copy_leaks_login import CopyleaksClient
9
+ from config import COPY_LEAKS_EMAIL_ADDRESS, COPY_LEAKS_KEY, WEBHOOK_SECRET
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Copyleaks configuration
14
+ copyleaks_client = Copyleaks()
15
+ copyleaks_client.login(COPY_LEAKS_EMAIL_ADDRESS, COPY_LEAKS_KEY)
16
+
17
+ @app.route('/webhook/completion?event=<status>', methods=['GET'])
18
+ def handle_completion_webhook(status):
19
+ data = request.json
20
+ print(f'Completion webhook received. Status: {status}')
21
+
22
+ if status == 'completed':
23
+ scan_id = data.get('scanId')
24
+ export_results(scan_id)
25
+ generate_pdf_report(scan_id)
26
+
27
+ return 'Webhook received'
28
+
29
+ def export_results(scan_id):
30
+ try:
31
+ export = Export()
32
+ export.set_completion_webhook(f'{request.url_root}webhook/export/completion')
33
+
34
+ # Set up export options (crawled version and results)
35
+ crawled = ExportCrawledVersion()
36
+ crawled.set_endpoint(f'{request.url_root}webhook/export/crawled')
37
+ crawled.set_verb('POST')
38
+ crawled.set_headers([['key', 'value'], ['key2', 'value2']])
39
+ export.set_crawled_version(crawled)
40
+
41
+ results = ExportResult()
42
+ results.set_id(scan_id)
43
+ results.set_endpoint(f'{request.url_root}webhook/export/result/{scan_id}')
44
+ results.set_verb('POST')
45
+ results.set_headers([['key', 'value'], ['key2', 'value2']])
46
+ export.set_results([results])
47
+
48
+ Copyleaks.export(copyleaks_client.auth_token, scan_id, f'export-{scan_id}', export)
49
+ except CommandError as ce:
50
+ response = ce.get_response()
51
+ print(f"Export error (HTTP status code {response.status_code}):")
52
+ print(response.content)
53
+
54
+ def generate_pdf_report(scan_id):
55
+ try:
56
+ pdf = Pdf()
57
+ pdf.set_create(True)
58
+ Copyleaks.generate_pdf_report(copyleaks_client.auth_token, scan_id, pdf)
59
+ except CommandError as ce:
60
+ response = ce.get_response()
61
+ print(f"PDF generation error (HTTP status code {response.status_code}):")
62
+ print(response.content)
63
+
64
+ if __name__ == '__main__':
65
+ app.run(port=8001, host='0.0.0.0')