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b1f
 
 
 
615fd07
6440b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8d4d18
7f06d4e
6440b1f
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Gradio Web Interface for VAPT Agent

"""
Modern Gradio UI for the VAPT (Vulnerability Assessment and Penetration Testing) agent.

Features:
- Input API endpoint, HTTP method, and optional API key
- Real-time progress streaming
- Downloadable Markdown report
- Visual Dashboard (risk gauge & severity pie chart)
- AI Security Tutor (interactive Q&A about the report)
"""

import asyncio
import gradio as gr
from datetime import datetime
import threading
import time
from typing import Optional, Generator, List, Tuple

from vapt_agent import run_vapt_agent_with_callback
from config import VAPTConfig
from dashboard_utils import (
    parse_vapt_report,
    calculate_risk_score,
    create_severity_chart,
    create_risk_gauge,
)
from ai_tutor import get_tutor

import os

CSS_PATH = os.path.join(os.path.dirname(__file__), "vapt_styles.css")


def load_custom_css(path: str = CSS_PATH) -> str:
    try:
        with open(path, "r", encoding="utf-8") as f:
            return f.read()
    except FileNotFoundError:
        # Fail silently; app will still run without custom CSS
        return ""


# ---------------------------------------------------------------------------
# Helper: run the VAPT agent and stream updates to Gradio
# ---------------------------------------------------------------------------


def run_security_test(
    api_endpoint: str,
    http_method: str,
    api_key: Optional[str] = None,
) -> Generator[Tuple[str, str, str, gr.Button], None, None]:
    """Yield progress, report markdown, report file path, and button state for Gradio.

    The function validates inputs, starts the VAPT agent in a background thread,
    and periodically yields any new progress messages. The button is disabled during
    the test and re-enabled when complete.
    """
    # ---------- Validation ----------
    if not api_endpoint or not api_endpoint.strip():
        yield (
            "❌ Error: API endpoint is required",
            "## Error\n\nPlease provide a valid API endpoint URL.",
            None,
            gr.Button(interactive=True),
        )
        return
    if not api_endpoint.startswith(("http://", "https://")):
        yield (
            "❌ Error: Invalid URL format",
            "## Error\n\nAPI endpoint must start with `http://` or `https://`.",
            None,
            gr.Button(interactive=True),
        )
        return

    # ---------- Progress handling ----------
    progress_messages: List[str] = []
    lock = threading.Lock()

    def add_progress(msg: str) -> str:
        with lock:
            progress_messages.append(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")
            return "\n".join(progress_messages)

    def progress_callback(msg: str):
        with lock:
            progress_messages.append(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")

    # Initial message
    yield (
        add_progress("πŸš€ Initializing VAPT Agent..."),
        "## Starting Security Test\n\nPlease wait while we assess your API endpoint...",
        None,
        gr.Button(interactive=False),
    )

    # Prepare request headers
    headers = {"Content-Type": "application/json", "User-Agent": "VAPT-Agent/1.0"}
    if api_key and api_key.strip():
        headers["Authorization"] = f"Bearer {api_key.strip()}"
        yield (
            add_progress("πŸ”‘ API key provided – will test authenticated endpoints"),
            "## Starting Security Test\n\nPreparing to test with authentication...",
            None,
            gr.Button(interactive=False),
        )

    # ---------- Run agent in background thread ----------
    result = {
        "report_content": None,
        "report_file_path": None,
        "error": None,
        "done": False,
    }

    def agent_worker():
        try:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            content, path = loop.run_until_complete(
                run_vapt_agent_with_callback(
                    api_endpoint=api_endpoint,
                    method=http_method,
                    headers=headers,
                    progress_callback=progress_callback,
                )
            )
            loop.close()
            result["report_content"] = content
            result["report_file_path"] = path
        except asyncio.TimeoutError:
            result["error"] = "Timeout: Security test took too long"
        except Exception as e:
            result["error"] = str(e)
        finally:
            result["done"] = True

    # Connect to LLM backend – just a placeholder progress update
    yield (
        add_progress("πŸ”Œ Connecting to security engine..."),
        "## Starting Security Test\n\nConnecting...",
        None,
        gr.Button(interactive=False),
    )
    threading.Thread(target=agent_worker, daemon=True).start()

    # ---------- Poll for updates ----------
    last_len = 0
    while not result["done"]:
        time.sleep(0.5)
        with lock:
            if len(progress_messages) > last_len:
                yield (
                    "\n".join(progress_messages),
                    "## Security Test in Progress\n\nPlease wait while the agent performs testing...",
                    None,
                    gr.Button(interactive=False),
                )
                last_len = len(progress_messages)

    # ---------- Final handling ----------
    if result["error"]:
        err = result["error"]
        if "Timeout" in err:
            yield (
                add_progress(f"⏱️ {err}"),
                "## Error\n\n**Timeout Error**\n\nThe assessment exceeded the allowed time.",
                None,
                gr.Button(interactive=True),
            )
        else:
            yield (
                add_progress(f"❌ Error: {err}"),
                f"## Error\n\n**Exception Occurred**\n\n```\n{err}\n```\n\nPlease check configuration and retry.",
                None,
                gr.Button(interactive=True),
            )
    else:
        # Success – return report and file path
        yield (
            add_progress("βœ… Security assessment completed successfully!"),
            result["report_content"] or "## Error\n\nNo report was generated.",
            result["report_file_path"],
            gr.Button(interactive=True),
        )


# ---------------------------------------------------------------------------
# Gradio UI construction
# ---------------------------------------------------------------------------


def create_gradio_interface() -> gr.Blocks:
    with gr.Blocks(title="VAPT Agent") as iface:

        # Inject custom CSS – light theme, white cards, improved readability
        custom_css = load_custom_css()
        if custom_css:
            gr.HTML(f"<style>{custom_css}</style>")

        # --- Header ---
        with gr.Row(elem_id="app-header"):
            with gr.Column(scale=6):
                gr.Markdown(
                    """
                    <div>
                      <div class="badge-pill">
                        <span class="dot"></span>
                        <span>Agentic VAPT β€’ API Security</span>
                      </div>
                      <h1>VAPT Agent Dashboard</h1>
                      <p>
                     Generate API specs, run automated vulnerability tests, and explore results with an AI security tutor. Gain clear insights into API risks, misconfigurations, and recommended remediation steps in dashboard.
                      </p>
                    </div>
                    """,
                    elem_id="header-title",
                )
            with gr.Column(scale=4, elem_id="workflow-overview-card"):
                gr.Markdown(
                    """
                    **Workflow Overview**
                    
                    1. Provide an API endpoint and method  
                    2. Agent discovers endpoints and builds the API spec using Postman MCP 
                    3. Customized VAPT MCP tools run automated security tests  
                    4. Dashboard + Tutor help you interpret and fix issues  
                    """,
                )
        with gr.Row(elem_id="demo-video-row"):
            gr.HTML(
                """<div style="font-size: 1.1rem; margin: 0; padding: 0.2rem 0;">πŸ“Ί <strong>New to VAPT Agent?</strong> Watch our <a href="https://youtu.be/wFgW_o48pw8?si=ywttNSKFVvViPdl5" target="_blank" style="color: #0066cc; text-decoration: none; font-weight: 500;">DEMO VIDEO</a> to learn how to use this tool effectively.</div>"""
            )

        # --- Main two-column layout ---
        with gr.Row():
            # Left: API configuration
            with gr.Column(scale=5):
                with gr.Group(elem_id="config-card", elem_classes=["section-card"]):
                    gr.Markdown("### πŸ“‹ API Configuration")

                    api_endpoint = gr.Textbox(
                        label="API Endpoint URL",
                        placeholder="https://sandbox.api.sap.com/SAPCALM/calm-tasks/v1/tasks?projectId=111",
                        value="https://sandbox.api.sap.com/SAPCALM/calm-tasks/v1/tasks?projectId=111",
                        info="Full URL of the API endpoint to test",
                    )
                    http_method = gr.Dropdown(
                        label="HTTP Method",
                        choices=["GET", "POST", "PUT", "DELETE", "PATCH"],
                        value="GET",
                        info="Select the HTTP method for the endpoint",
                    )
                    api_key = gr.Textbox(
                        label="API Key (Optional)",
                        placeholder="Enter your API key or Bearer token",
                        type="password",
                        value="Ww9aGPGeGoDGCFetcBtsaEtGOpGSUNXp",
                        info="If the API requires authentication, provide the key here",
                    )
                    with gr.Row():
                        submit_btn = gr.Button(
                            "πŸš€ Start Security Test", variant="primary", size="lg"
                        )
                        clear_btn = gr.Button(
                            "πŸ”„ Reset", variant="secondary", elem_id="reset-btn"
                        )

                    gr.HTML(
                        """
                        <div id="disclaimer-box">
                          ⚠️ <strong>Authorized use only:</strong> Run this tool only against systems and APIs you are explicitly allowed to test.
                        </div>
                        """
                    )

            # Right: Results area
            with gr.Column(scale=7):
                with gr.Group(elem_id="results-card", elem_classes=["section-card"]):
                    gr.Markdown("### πŸ“Š Security Assessment Results")

                    with gr.Tab("Live Progress"):
                        progress_output = gr.Textbox(
                            label="Agent Activity Log",
                            lines=15,
                            max_lines=20,
                            interactive=False,
                            show_label=False,
                            placeholder="Agent activity will appear here...",
                        )

                    with gr.Tab("Security Report"):
                        report_file = gr.File(
                            label="πŸ“₯ Download Report (.md)",
                            interactive=False,
                            visible=True,
                        )
                        report_output = gr.Markdown(
                            value="Security report will appear here after the test completes...",
                            label="VAPT Report",
                            elem_id="security-report-md",
                        )

                    with gr.Tab("Dashboard"):
                        # Row that we'll center via CSS
                        with gr.Row(elem_id="dashboard-row"):
                            with gr.Column(scale=1, elem_id="risk-col"):
                                risk_gauge = gr.Plot(label="Risk Score")
                            with gr.Column(scale=1, elem_id="severity-col"):
                                severity_pie = gr.Plot(
                                    label="Vulnerability Distribution"
                                )

                        top_findings = gr.Markdown(
                            "Run a security test to see summarized key findings..."
                        )

                    with gr.Tab("Security Tutor"):
                        with gr.Column(elem_id="tutor-section"):
                            gr.Markdown(
                            """
                            <div style="font-size: 0.95rem; line-height: 1.5; margin-bottom: 0.4rem;">
                            <strong>Ask questions about your security report.</strong><br/>
                            Get clear explanations, remediation guidance, and best-practice advice.
                            </div>
                            """
                            )   
                            chatbot = gr.Chatbot(
                                label="Security Tutor",
                                height=380,
                                elem_id="tutor-chat",
                            )

                            with gr.Row(elem_id="tutor-input-row"):
                                tutor_input = gr.Textbox(
                                    label="Your Question",
                                    placeholder="e.g., What is SQL injection and how do I fix it?",
                                    lines=2,
                                    scale=4,
                                    show_label=True,
                                )
                                tutor_btn = gr.Button("Ask", variant="primary", scale=1)

                            gr.HTML(
                                """
                                <div id="tutor-examples">
                                  <strong>Example questions you can ask:</strong>
                                  <ul>
                                    <li>What is the most critical issue in my report?</li>
                                    <li>How do I fix CORS policy issues?</li>
                                    <li>Explain SQL injection in simple terms.</li>
                                    <li>What should I fix first to reduce risk quickly?</li>
                                  </ul>
                                </div>
                                """
                            )

        # -------------------------------------------------------------------
        # Event bindings
        # -------------------------------------------------------------------

        # VAPT run
        submit_btn.click(
            fn=run_security_test,
            inputs=[api_endpoint, http_method, api_key],
            outputs=[progress_output, report_output, report_file, submit_btn],
            show_progress=True,
        )

        # Reset
        clear_btn.click(
            fn=lambda: (
                "https://sandbox.api.sap.com/SAPCALM/calm-tasks/v1/tasks?projectId=111",
                "GET",
                "",
                "",
                "Security report will appear here after the test completes...",
                None,
            ),
            inputs=[],
            outputs=[
                api_endpoint,
                http_method,
                api_key,
                progress_output,
                report_output,
                report_file,
            ],
        )

        # Dashboard updates – triggered after a successful report
        def update_dashboard(report_md: str):
            data = parse_vapt_report(report_md)
            sev = data["severities"]
            risk = calculate_risk_score(sev)
            return (
                create_risk_gauge(risk),
                create_severity_chart(sev),
                (
                    "\n".join(data.get("findings", [])[:5])
                    if data.get("findings")
                    else "No findings detected."
                ),
            )

        report_output.change(
            fn=update_dashboard,
            inputs=[report_output],
            outputs=[risk_gauge, severity_pie, top_findings],
        )

        # AI Tutor interaction
        # Gradio Chatbot (v6) uses "messages" format: list of {"role": ..., "content": ...}
        # We convert that to (user, assistant) pairs for the tutor, then convert back.
        def tutor_respond(question, history, report_md: str):
            # 1) Convert Gradio "messages" history -> list of (user, assistant) pairs
            pairs: List[Tuple[str, str]] = []
            current_user = None

            for msg in history or []:
                if isinstance(msg, dict):
                    role = msg.get("role")
                    content = msg.get("content", "")
                elif isinstance(msg, (list, tuple)) and len(msg) == 2:
                    # Backward compatibility: skip old tuple-style entries
                    continue
                else:
                    continue

                if role == "user":
                    current_user = content
                elif role == "assistant":
                    if current_user is None:
                        current_user = ""
                    pairs.append((current_user, content))
                    current_user = None

            # 2) Call the tutor with pairs
            tutor = get_tutor()
            answer = tutor.chat(question, report_md, pairs)

            # 3) Append new user + assistant messages in Gradio's messages format
            new_history = list(history or [])
            new_history.append({"role": "user", "content": question})
            new_history.append({"role": "assistant", "content": answer})

            # Clear the input textbox and re-enable the button
            return new_history, "", gr.Button(interactive=True)

        tutor_btn.click(
            fn=tutor_respond,
            inputs=[tutor_input, chatbot, report_output],
            outputs=[chatbot, tutor_input, tutor_btn],
        )

    return iface


# ---------------------------------------------------------------------------
# Main entry point
# ---------------------------------------------------------------------------


def main():
    print("=" * 80)
    print("VAPT Agent - Gradio Web Interface")
    print("=" * 80)
    try:
        cfg = VAPTConfig()
        print(f"βœ“ Configuration loaded successfully")
        print(f"  Provider: {'AWS Bedrock' if cfg.use_bedrock else 'Anthropic API'}")
        if cfg.use_bedrock:
            print(f"  Region: {cfg.aws_region}")
        print()
    except Exception as exc:
        print(f"❌ Configuration error: {exc}")
        print("Please check your .env file and ensure all required variables are set.")
        return

    iface = create_gradio_interface()
    print("Starting Gradio server...")
    print("=" * 80)
    #iface.launch(server_name="0.0.0.0", server_port=7861, share=True, inbrowser=True)
    iface.launch(server_name="0.0.0.0", mcp_server=True)

if __name__ == "__main__":
    main()