# teler-sh/teler-waf architecture

> Architecture diagram of the GitHub repository teler-sh/teler-waf, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/teler-sh/teler-waf
- Repository: https://github.com/teler-sh/teler-waf

## Overview

##### Main Components
- HTTP Middleware: This is the “Teler” instance that wraps a net/http.Handler. It intercepts requests before they reach your application’s actual handler.
- Request Analyzer / Threat Engine: Components such as analyze.go, rule.go, and files under the “threat” package perform the analysis, inspect the request against pre-defined and custom rules (via DSL conditions), and then decide if the request is malicious.
- Rule & Condition Engine: The project supports both built-in and custom rules (via “Customs” or “CustomsFromFile”). Files like condition.go, rule.go, and DSL-related logic are part of this engine.
- Configuration & Option Handling: The “option” package (loader, loader_test.go, utils) helps load runtime options from JSON or YAML configurations. It controls aspects such as whitelists, threat exclusions, and logging preferences.
- Logging & Response Mechanism: This module logs detected malicious behavior, generates unique request IDs, and returns a custom response to the client (with details like threat category, error message, etc.).
- External Integrations: Examples include integration configurations for Falco Sidekick (sending JSON events to external monitoring tools) and Wazuh (via provided XML configurations and extra rule files).
- Example Applications: The examples directory shows various integration methods (with popular Go HTTP frameworks) and provides runnable demos.

##### Relationships and Data Flows
- Incoming HTTP requests from clients flow into the teler-waf middleware.
- The middleware first routes the request into the threat analysis engine. It uses configured options (from YAML/JSON or in-code constants) and applies multiple security checks using a combination of default and custom rules (via DSL expressions).
- If a threat is detected, the middleware will generate a JSON log, and – optionally – forward the event to external systems such as Falco Sidekick or be picked up by Wazuh based on specific integrations.
- The configuration component ensures these flows are customizable (e.g., enabling/disabling caching, development mode, and threat dataset updates).

##### Architectural Patterns and Design Principles
- Modular Design: The project is split into distinct modules (e.g., request, threat, option,dsl, etc.), enabling an extensible and maintainable design.
- Middleware Pattern: By conforming to the net/http.Handler interface, the system can be seamlessly integrated into various Go HTTP frameworks.
- DSL for Rules: The use of a domain-specific language for defining conditions provides flexibility and powerful rule customization.
- External Integrations: Loose coupling with external alerting and monitoring systems like Falco Sidekick and Wazuh.

##### Relevant Technologies and Frameworks
- Go (standard net/http for middleware)
- Configuration handling (YAML/JSON parsing)
- DSL expressions (leveraging the expr package and custom DSL functions)
- Integration with external systems: Falco Sidekick, Wazuh rule sets.

##### Instructing the Principal Software Engineer
- Branch off to show the threat analysis engine that includes the rule engine, DSL processor, and dataset management components.
- Nodes for configuration management (option loading) and link these to both the middleware and rule engine components.
- Clearly label every component with its name and main function.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    clientRequest["HTTP Request"]:::client

    %% Middleware Layer
    middleware["Teler Middleware"]:::middleware
    optionLoader["Configuration Loader"]:::config

    %% Threat Analysis Engine
    subgraph "Threat Analysis Engine"
        analyzer["Request Analyzer (analyze.go)"]:::analyzer
        threatModule["Threat Module (threat/threat.go)"]:::analyzer
    end

    %% Rule & Condition Engine
    subgraph "Rule & Condition Engine"
        conditionEngine["Condition Engine (condition.go)"]:::ruleEngine
        ruleEngine["Rule Engine (rule.go)"]:::ruleEngine
    end

    %% Decision and Outcomes
    decision["Threat Detected?"]:::decision
    forward["Forward Request to Application"]:::middleware
    responseLogger["Logging & Response (response.go)"]:::response

    %% External Integrations
    externalFalco["Falco Sidekick"]:::external
    externalWazuh["Wazuh Integration"]:::external

    %% Example Applications
    exampleApps["Example Applications"]:::example

    %% Connections
    clientRequest --> middleware
    middleware --> optionLoader
    middleware --> analyzer
    optionLoader --> analyzer
    analyzer --> threatModule
    threatModule --> conditionEngine
    conditionEngine --> ruleEngine
    ruleEngine --> decision
    decision -- "safe" --> forward
    decision -- "blocked" --> responseLogger
    forward --> exampleApps
    responseLogger --> externalFalco
    responseLogger --> externalWazuh

    %% Click Events
    click middleware "https://github.com/teler-sh/teler-waf/blob/master/teler.go"
    click analyzer "https://github.com/teler-sh/teler-waf/blob/master/analyze.go"
    click threatModule "https://github.com/teler-sh/teler-waf/blob/master/threat/threat.go"
    click conditionEngine "https://github.com/teler-sh/teler-waf/blob/master/condition.go"
    click ruleEngine "https://github.com/teler-sh/teler-waf/blob/master/rule.go"
    click optionLoader "https://github.com/teler-sh/teler-waf/tree/master/option/"
    click responseLogger "https://github.com/teler-sh/teler-waf/blob/master/response.go"
    click externalFalco "https://github.com/teler-sh/teler-waf/blob/master/falcosidekick/falcosidekick.example.yaml"
    click externalWazuh "https://github.com/teler-sh/teler-waf/blob/master/teler_rules.xml"
    click exampleApps "https://github.com/teler-sh/teler-waf/tree/master/examples/"

    %% Styles
    classDef client fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    classDef middleware fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef config fill:#D6EAF8,stroke:#3498DB,stroke-width:2px;
    classDef analyzer fill:#A9DFBF,stroke:#1ABC9C,stroke-width:2px;
    classDef ruleEngine fill:#F5B7B1,stroke:#E74C3C,stroke-width:2px;
    classDef response fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef external fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef decision fill:#FCF3CF,stroke:#F7DC6F,stroke-width:2px;
    classDef example fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
```
