File size: 2,502 Bytes
fc8e2ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request, jsonify
from copyleaks.models.export import Export, ExportCrawledVersion, ExportResult
from copyleaks.copyleaks import Copyleaks
from copyleaks.exceptions.command_error import CommandError
from copyleaks.models.submit.document import FileDocument
from copyleaks.models.submit.properties.scan_properties import ScanProperties
from copyleaks.models.submit.properties.pdf import Pdf
from copy_leaks_login import CopyleaksClient
from config import COPY_LEAKS_EMAIL_ADDRESS, COPY_LEAKS_KEY, WEBHOOK_SECRET

app = Flask(__name__)

# Copyleaks configuration
copyleaks_client = Copyleaks()
copyleaks_client.login(COPY_LEAKS_EMAIL_ADDRESS, COPY_LEAKS_KEY)

@app.route('/webhook/completion?event=<status>', methods=['GET'])
def handle_completion_webhook(status):
    data = request.json
    print(f'Completion webhook received. Status: {status}')
    
    if status == 'completed':
        scan_id = data.get('scanId')
        export_results(scan_id)
        generate_pdf_report(scan_id)

    return 'Webhook received'

def export_results(scan_id):
    try:
        export = Export()
        export.set_completion_webhook(f'{request.url_root}webhook/export/completion')
        
        # Set up export options (crawled version and results)
        crawled = ExportCrawledVersion()
        crawled.set_endpoint(f'{request.url_root}webhook/export/crawled')
        crawled.set_verb('POST')
        crawled.set_headers([['key', 'value'], ['key2', 'value2']])
        export.set_crawled_version(crawled)

        results = ExportResult()
        results.set_id(scan_id)
        results.set_endpoint(f'{request.url_root}webhook/export/result/{scan_id}')
        results.set_verb('POST')
        results.set_headers([['key', 'value'], ['key2', 'value2']])
        export.set_results([results])

        Copyleaks.export(copyleaks_client.auth_token, scan_id, f'export-{scan_id}', export)
    except CommandError as ce:
        response = ce.get_response()
        print(f"Export error (HTTP status code {response.status_code}):")
        print(response.content)

def generate_pdf_report(scan_id):
    try:
        pdf = Pdf()
        pdf.set_create(True)
        Copyleaks.generate_pdf_report(copyleaks_client.auth_token, scan_id, pdf)
    except CommandError as ce:
        response = ce.get_response()
        print(f"PDF generation error (HTTP status code {response.status_code}):")
        print(response.content)

if __name__ == '__main__':
    app.run(port=8001, host='0.0.0.0')