{
  "openapi": "3.0.3",
  "info": {
    "title": "Cyberwall AI - Intrusion Prevention Service API",
    "description": "Cyberwall AI intrusion prevention service. Provides REST endpoints for managing IPS rules, rulesets, domain configurations, and IP blacklisting, plus a tRPC API consumed by the admin dashboard.\n",
    "version": "1.1.0",
    "contact": {
      "name": "CyberDefense AI",
      "email": "support@cyberdefense.ai"
    }
  },
  "servers": [
    {
      "url": "/",
      "description": "Current host"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "tags": [
    {
      "name": "Rules",
      "description": "Manage IPS, redirect, and authorization rules"
    },
    {
      "name": "Rulesets",
      "description": "Manage collections of rules"
    },
    {
      "name": "Configurations",
      "description": "Manage per-domain IPS configurations"
    },
    {
      "name": "IDS",
      "description": "IP blacklist management"
    },
    {
      "name": "Cron",
      "description": "Scheduled / internal tasks"
    },
    {
      "name": "Storage",
      "description": "Backups, called by the scheduler with the api-key header."
    }
  ],
  "paths": {
    "/rules": {
      "get": {
        "summary": "List rules (paginated)",
        "operationId": "listRules",
        "tags": [
          "Rules"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/Limit"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of rules",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/PaginationResult"
                    },
                    {
                      "type": "object",
                      "properties": {
                        "data": {
                          "type": "array",
                          "items": {
                            "$ref": "#/components/schemas/Rule"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "post": {
        "summary": "Create a rule",
        "description": "Create a new IPS, redirect, or authorization rule. The request body is validated against the `newRule` Zod schema from `@cyberwall-ai/intrusion-prevention-model`. An audit log entry is created.\n",
        "operationId": "createRule",
        "tags": [
          "Rules"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewRule"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The created rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Rule"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rules/{id}": {
      "get": {
        "summary": "Get rule by ID",
        "operationId": "getRuleById",
        "tags": [
          "Rules"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "The rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Rule"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "put": {
        "summary": "Update a rule",
        "description": "Update an existing rule. Affected IPS configurations are automatically refreshed on disk. An audit log entry is created.\n",
        "operationId": "updateRule",
        "tags": [
          "Rules"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Rule"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The updated rule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Rule"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "delete": {
        "summary": "Delete rule by ID",
        "description": "Delete a rule. Affected IPS configurations are automatically refreshed on disk. An audit log entry is created.\n",
        "operationId": "deleteRuleById",
        "tags": [
          "Rules"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "Deletion acknowledgment",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeleteResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rulesets": {
      "get": {
        "summary": "List rulesets (paginated)",
        "operationId": "listRulesets",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/Limit"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of rulesets",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/PaginationResult"
                    },
                    {
                      "type": "object",
                      "properties": {
                        "data": {
                          "type": "array",
                          "items": {
                            "$ref": "#/components/schemas/Ruleset"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "post": {
        "summary": "Create a ruleset",
        "operationId": "createRuleset",
        "tags": [
          "Rulesets"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewRuleset"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The created ruleset",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Ruleset"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rulesets/{id}": {
      "get": {
        "summary": "Get ruleset by ID",
        "operationId": "getRulesetById",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "The ruleset",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Ruleset"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "put": {
        "summary": "Update a ruleset",
        "description": "Update an existing ruleset. Affected IPS configurations are automatically refreshed on disk. An audit log entry is created with a JSON Patch diff.\n",
        "operationId": "updateRuleset",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Ruleset"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The updated ruleset",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Ruleset"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "delete": {
        "summary": "Delete ruleset by ID",
        "description": "Delete a ruleset. Cascades: deletes all rules in the ruleset, removes the ruleset from affected IPS configurations, and re-writes config files. An audit log entry is created.\n",
        "operationId": "deleteRulesetById",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "Deletion acknowledgment",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeleteResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations": {
      "get": {
        "summary": "List configurations (paginated)",
        "operationId": "listConfigs",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/Limit"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of IPS configurations",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/PaginationResult"
                    },
                    {
                      "type": "object",
                      "properties": {
                        "data": {
                          "type": "array",
                          "items": {
                            "$ref": "#/components/schemas/IpsConfiguration"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "post": {
        "summary": "Create an IPS configuration",
        "description": "Create a new IPS configuration for a domain. Each domain can only have one configuration (409 on duplicate). The configuration file is automatically written to disk after creation. An audit log entry is created.\n",
        "operationId": "createConfig",
        "tags": [
          "Configurations"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewIpsConfiguration"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The created configuration",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IpsConfiguration"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "description": "A configuration for this domain already exists",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations/{id}": {
      "get": {
        "summary": "Get configuration by ID",
        "operationId": "getConfigById",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "The IPS configuration",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IpsConfiguration"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "put": {
        "summary": "Update a configuration",
        "description": "Update an existing IPS configuration. The configuration file is automatically refreshed on disk. An audit log entry is created with a JSON Patch diff.\n",
        "operationId": "updateConfig",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/IpsConfiguration"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The updated configuration",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IpsConfiguration"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "delete": {
        "summary": "Delete configuration by ID",
        "description": "Delete an IPS configuration. The associated `data/ips/{domain}.json` file is also removed from disk. An audit log entry is created.\n",
        "operationId": "deleteConfigById",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "Deletion acknowledgment",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeleteResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations/{id}/populated": {
      "get": {
        "summary": "Get fully populated configuration",
        "description": "Retrieve an IPS configuration with its rulesets and rules fully resolved (not just ObjectId references).\n",
        "operationId": "getPopulatedConfig",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "The fully populated configuration",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "Success": {
                      "$ref": "#/components/schemas/PopulatedIpsConfiguration"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations/domain/{id}": {
      "get": {
        "summary": "Get configuration by domain ID",
        "operationId": "getConfigByDomainId",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "MongoDB ObjectId of the domain",
            "schema": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{24}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The IPS configuration for the domain",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IpsConfiguration"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/configurations/ruleset/{id}": {
      "get": {
        "summary": "Get configurations containing a ruleset",
        "operationId": "getConfigsByRulesetId",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "MongoDB ObjectId of the ruleset",
            "schema": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{24}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Array of affected configurations",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/IpsConfiguration"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations/refresh": {
      "post": {
        "summary": "Refresh all configuration files",
        "description": "Re-export all IPS configuration files to disk. Useful for synchronizing the file system with the database state. An audit log entry is created.\n",
        "operationId": "refreshAllConfigs",
        "tags": [
          "Configurations"
        ],
        "responses": {
          "200": {
            "description": "Refresh completed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string",
                      "example": "All configuration files refreshed successfully"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/ids/is-banned/{id}": {
      "get": {
        "summary": "Check if an IP is banned",
        "operationId": "isIpBanned",
        "tags": [
          "IDS"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "IPv4 address to check",
            "schema": {
              "type": "string",
              "format": "ipv4",
              "example": "192.168.1.1"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Ban status of the IP",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "isBanned": {
                      "type": "boolean"
                    },
                    "reasons": {
                      "nullable": true,
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Reason"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/ids/blacklist/{id}": {
      "get": {
        "summary": "Get blacklist content",
        "description": "Dump the full in-memory blacklist.",
        "operationId": "getBlacklist",
        "tags": [
          "IDS"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "IPv4 address (required by route, but full dump is returned)",
            "schema": {
              "type": "string",
              "format": "ipv4"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Full blacklist content",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/Reason"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/ids/ban-ip": {
      "post": {
        "summary": "Ban an IP address",
        "description": "Add an IPv4 address to the in-memory blacklist. An audit log entry is created.",
        "operationId": "banIp",
        "tags": [
          "IDS"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "ip",
                  "reasons"
                ],
                "properties": {
                  "ip": {
                    "type": "string",
                    "format": "ipv4",
                    "example": "192.168.1.100"
                  },
                  "reasons": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/Reason"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "IP successfully banned",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    },
                    "reasons": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Reason"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/ids/unban-ip": {
      "post": {
        "summary": "Unban an IP address",
        "description": "Remove an IPv4 address from the in-memory blacklist. An audit log entry is created.",
        "operationId": "unbanIp",
        "tags": [
          "IDS"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "ip"
                ],
                "properties": {
                  "ip": {
                    "type": "string",
                    "format": "ipv4",
                    "example": "192.168.1.100"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "IP successfully unbanned",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/cron/populate-blacklist": {
      "post": {
        "summary": "Populate blacklist",
        "description": "Triggers blacklist population from external sources via `updateBlacklist()`.",
        "operationId": "populateBlacklist",
        "tags": [
          "Cron"
        ],
        "responses": {
          "200": {
            "description": "Blacklist populated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rules/{id}/array": {
      "get": {
        "summary": "Get rules by ids",
        "operationId": "getRulesByIds",
        "tags": [
          "Rules"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Comma-separated rule ObjectIds",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The rules",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Rule"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rulesets/{id}/array": {
      "get": {
        "summary": "Get rulesets by ids",
        "operationId": "getRulesetsByIds",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Comma-separated ruleset ObjectIds",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The rulesets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Ruleset"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/configurations/domain/{id}/array": {
      "get": {
        "summary": "Get configurations by domain ids",
        "operationId": "getConfigsByDomainIds",
        "tags": [
          "Configurations"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Comma-separated domain ObjectIds",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The configurations",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/IpsConfiguration"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    },
    "/rulesets/{id}/rule": {
      "get": {
        "summary": "Get the ruleset containing a rule",
        "description": "`id` is the rule id. Returns the ruleset that contains it and the rule.",
        "operationId": "getRulesetByRule",
        "tags": [
          "Rulesets"
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/ObjectId"
          }
        ],
        "responses": {
          "200": {
            "description": "Ruleset and rule",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ruleset": {
                      "$ref": "#/components/schemas/Ruleset"
                    },
                    "rule": {
                      "$ref": "#/components/schemas/Rule"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/storage/backup": {
      "post": {
        "summary": "Create a backup (scheduler)",
        "description": "ZIPs every populated configuration (`<domainId>.json`) to S3 under `backups/` when S3_BUCKET (or the deprecated IPS_S3_BUCKET) is set, otherwise to `./data/backups/`, then deletes backups older than IPS_BACKUP_RETAIN_DAYS (the newest is always kept). Every exit path writes a `cron.backup` audit entry, shown on /cron.\n",
        "operationId": "createBackup",
        "tags": [
          "Storage"
        ],
        "responses": {
          "201": {
            "description": "Backup created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "key": {
                      "type": "string"
                    },
                    "backend": {
                      "type": "string",
                      "enum": [
                        "s3",
                        "local"
                      ]
                    },
                    "size": {
                      "type": "integer"
                    },
                    "configurations": {
                      "type": "integer"
                    },
                    "failed": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "timestamp": {
                      "type": "string"
                    },
                    "pruned": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "No configurations to back up"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      },
      "get": {
        "summary": "List backups",
        "operationId": "listBackups",
        "tags": [
          "Storage"
        ],
        "responses": {
          "200": {
            "description": "Backups, newest first",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "key": {
                        "type": "string"
                      },
                      "size": {
                        "type": "integer"
                      },
                      "lastModified": {
                        "type": "string",
                        "format": "date-time",
                        "nullable": true
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "api-key",
        "description": "Service API key (SERVICE_API_KEY) for machine-to-machine REST endpoints. Missing header: 401. Wrong key: 403."
      },
      "auth0": {
        "type": "oauth2",
        "description": "Auth0 OAuth2 / OIDC session for the admin dashboard (tRPC endpoints).",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://{auth0Domain}/authorize",
            "tokenUrl": "https://{auth0Domain}/oauth/token",
            "scopes": {
              "openid": "OpenID Connect identity",
              "profile": "Basic profile information",
              "email": "Email address"
            }
          }
        }
      }
    },
    "parameters": {
      "ObjectId": {
        "name": "id",
        "in": "path",
        "required": true,
        "description": "MongoDB ObjectId",
        "schema": {
          "type": "string",
          "pattern": "^[a-fA-F0-9]{24}$",
          "example": "64a1b2c3d4e5f6a7b8c9d0e1"
        }
      },
      "Page": {
        "name": "page",
        "in": "query",
        "required": false,
        "schema": {
          "type": "integer",
          "minimum": 1,
          "default": 1
        },
        "description": "Page number (1-indexed)"
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "required": false,
        "schema": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100,
          "default": 25
        },
        "description": "Number of items per page (max 100)"
      }
    },
    "responses": {
      "BadRequest": {
        "description": "The request was malformed or failed validation.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing or invalid API key.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "The requested resource does not exist.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "InternalServerError": {
        "description": "An unexpected error occurred on the server.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Invalid API key",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "example": "Invalid API key"
                }
              }
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "Error": {
            "type": "string",
            "example": "Wrong data format!"
          }
        }
      },
      "DeleteResult": {
        "type": "object",
        "properties": {
          "acknowledged": {
            "type": "boolean"
          },
          "deletedCount": {
            "type": "integer"
          }
        }
      },
      "PaginationResult": {
        "type": "object",
        "properties": {
          "data": {
            "type": "array",
            "items": {}
          },
          "total": {
            "type": "integer",
            "description": "Total number of documents"
          },
          "page": {
            "type": "integer"
          },
          "limit": {
            "type": "integer"
          },
          "pages": {
            "type": "integer",
            "description": "Total number of pages"
          }
        }
      },
      "Reason": {
        "type": "object",
        "required": [
          "id",
          "name",
          "description",
          "category",
          "type"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "category": {
            "type": "string"
          },
          "type": {
            "type": "string",
            "description": "Which part of the WAF produced the reason (`ids`, `ips` or `waf`)",
            "example": "ids"
          }
        }
      },
      "GenericAttribute": {
        "type": "object",
        "required": [
          "values",
          "operator"
        ],
        "properties": {
          "values": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "operator": {
            "type": "string",
            "enum": [
              "==",
              "!="
            ]
          }
        }
      },
      "KvAttribute": {
        "type": "object",
        "required": [
          "values",
          "operator"
        ],
        "properties": {
          "values": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          },
          "operator": {
            "type": "string",
            "enum": [
              "==",
              "!="
            ]
          }
        }
      },
      "RuleAttribute": {
        "type": "object",
        "properties": {
          "geolocation": {
            "type": "object",
            "properties": {
              "address": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "range": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "country": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "continent": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "domain": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "tag": {
                "$ref": "#/components/schemas/GenericAttribute"
              }
            }
          },
          "method": {
            "$ref": "#/components/schemas/GenericAttribute"
          },
          "header": {
            "allOf": [
              {
                "$ref": "#/components/schemas/KvAttribute"
              },
              {
                "type": "object",
                "properties": {
                  "tag": {
                    "$ref": "#/components/schemas/GenericAttribute"
                  }
                }
              }
            ]
          },
          "url": {
            "type": "object",
            "properties": {
              "path": {
                "$ref": "#/components/schemas/GenericAttribute"
              },
              "query": {
                "$ref": "#/components/schemas/KvAttribute"
              },
              "tag": {
                "$ref": "#/components/schemas/GenericAttribute"
              }
            }
          },
          "body": {
            "type": "object",
            "properties": {
              "query": {
                "$ref": "#/components/schemas/KvAttribute"
              },
              "tag": {
                "$ref": "#/components/schemas/GenericAttribute"
              }
            }
          }
        }
      },
      "BaseRule": {
        "type": "object",
        "required": [
          "name",
          "description",
          "attribute",
          "behavior",
          "type"
        ],
        "properties": {
          "_id": {
            "type": "string",
            "pattern": "^[a-fA-F0-9]{24}$"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "attribute": {
            "$ref": "#/components/schemas/RuleAttribute"
          },
          "category": {
            "type": "string",
            "default": "Custom"
          },
          "behavior": {
            "type": "string"
          },
          "enabled": {
            "type": "boolean",
            "default": true
          },
          "type": {
            "type": "string",
            "enum": [
              "ips",
              "redirect",
              "authorization"
            ]
          }
        }
      },
      "NewRule": {
        "description": "Rule creation payload. Shape varies by `type` discriminator.",
        "oneOf": [
          {
            "title": "IPS Rule",
            "allOf": [
              {
                "$ref": "#/components/schemas/BaseRule"
              },
              {
                "type": "object",
                "required": [
                  "thresholds"
                ],
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": [
                      "ips"
                    ]
                  },
                  "behavior": {
                    "type": "string",
                    "enum": [
                      "allow",
                      "block",
                      "flag"
                    ]
                  },
                  "thresholds": {
                    "type": "object",
                    "description": "Threshold configuration for IPS triggers"
                  }
                }
              }
            ]
          },
          {
            "title": "Redirect Rule",
            "allOf": [
              {
                "$ref": "#/components/schemas/BaseRule"
              },
              {
                "type": "object",
                "required": [
                  "redirect"
                ],
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": [
                      "redirect"
                    ]
                  },
                  "behavior": {
                    "type": "string",
                    "enum": [
                      "redirect"
                    ]
                  },
                  "redirect": {
                    "type": "object",
                    "required": [
                      "location"
                    ],
                    "properties": {
                      "location": {
                        "type": "string"
                      },
                      "status": {
                        "type": "object",
                        "properties": {
                          "code": {
                            "type": "integer"
                          },
                          "message": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            ]
          },
          {
            "title": "Authorization Rule",
            "allOf": [
              {
                "$ref": "#/components/schemas/BaseRule"
              },
              {
                "type": "object",
                "required": [
                  "authorization"
                ],
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": [
                      "authorization"
                    ]
                  },
                  "behavior": {
                    "type": "string",
                    "enum": [
                      "authorization"
                    ]
                  },
                  "authorization": {
                    "type": "object",
                    "additionalProperties": {
                      "type": "string"
                    },
                    "description": "Key-value authorization config (e.g., basic auth credentials)"
                  }
                }
              }
            ]
          }
        ],
        "discriminator": {
          "propertyName": "type"
        }
      },
      "Rule": {
        "description": "A persisted rule (includes `_id`).",
        "allOf": [
          {
            "type": "object",
            "required": [
              "_id"
            ],
            "properties": {
              "_id": {
                "type": "string",
                "pattern": "^[a-fA-F0-9]{24}$"
              }
            }
          },
          {
            "$ref": "#/components/schemas/NewRule"
          }
        ]
      },
      "NewRuleset": {
        "type": "object",
        "required": [
          "name",
          "description",
          "rules"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "rules": {
            "type": "array",
            "items": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{24}$",
              "description": "MongoDB ObjectId referencing a Rule"
            }
          }
        }
      },
      "Ruleset": {
        "allOf": [
          {
            "type": "object",
            "required": [
              "_id"
            ],
            "properties": {
              "_id": {
                "type": "string",
                "pattern": "^[a-fA-F0-9]{24}$"
              }
            }
          },
          {
            "$ref": "#/components/schemas/NewRuleset"
          }
        ]
      },
      "NewIpsConfiguration": {
        "type": "object",
        "required": [
          "domain",
          "rulesets"
        ],
        "properties": {
          "domain": {
            "type": "string",
            "pattern": "^[a-fA-F0-9]{24}$",
            "description": "MongoDB ObjectId referencing the domain"
          },
          "mode": {
            "type": "string",
            "enum": [
              "passive",
              "active"
            ],
            "default": "passive"
          },
          "thresholds": {
            "type": "object",
            "default": {}
          },
          "rulesets": {
            "type": "array",
            "items": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{24}$"
            }
          }
        }
      },
      "IpsConfiguration": {
        "allOf": [
          {
            "type": "object",
            "required": [
              "_id"
            ],
            "properties": {
              "_id": {
                "type": "string",
                "pattern": "^[a-fA-F0-9]{24}$"
              }
            }
          },
          {
            "$ref": "#/components/schemas/NewIpsConfiguration"
          }
        ]
      },
      "PopulatedIpsConfiguration": {
        "type": "object",
        "properties": {
          "_id": {
            "type": "string"
          },
          "domain": {
            "type": "string"
          },
          "mode": {
            "type": "string",
            "enum": [
              "passive",
              "active"
            ]
          },
          "thresholds": {
            "type": "object"
          },
          "rulesets": {
            "type": "array",
            "items": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Ruleset"
                },
                {
                  "type": "object",
                  "properties": {
                    "rules": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Rule"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
