Configuration Reference

Complete configuration reference for API-Fence filter

This page documents all configuration options for the API-Fence Envoy HTTP filter.

Configuration Format

API-Fence configuration is provided as a JSON object in the Envoy filter configuration:

http_filters:
  - name: envoy.filters.http.dynamic_modules
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.dynamic_modules.v3.DynamicModuleFilter
      dynamic_module_config:
        name: api_fence
        do_not_close: true
      filter_name: api_fence
      filter_config:
        "@type": "type.googleapis.com/google.protobuf.StringValue"
        value: |
          {
            "api_name": "my_api",
            "openapi_spec_path": "/etc/envoy/openapi.yaml",
            ...
          }

Top-Level Options

OptionTypeRequiredDefaultDescription
api_namestringYes-Unique API name for metrics scoping. Used as prefix for all metrics (e.g., api_fence.my_api.cache.hits)
openapi_spec_pathstringNo*-Path to OpenAPI spec file (YAML or JSON)
openapi_spec_inlinestringNo*-Inline OpenAPI spec as YAML/JSON string
cacheobjectNoSee belowSchema cache configuration
validationobjectNoSee belowValidation behavior settings
mockingobjectNoSee belowMock response generation settings
securityobjectNoSee belowSecurity limits configuration
modsecurityobjectNoSee belowModSecurity WAF configuration

* Either openapi_spec_path or openapi_spec_inline must be provided, but not both.

Cache Configuration

The cache section configures the JSON schema validator cache:

{
  "cache": {
    "max_capacity": 1000,
    "ttl_seconds": 3600
  }
}
OptionTypeDefaultDescription
max_capacityinteger1000Maximum number of cached compiled schemas
ttl_secondsinteger3600Time-to-live for cached schemas (1 hour)

The cache stores compiled JSON Schema validators keyed by a hash of the schema content. This avoids re-compiling schemas for repeated requests to the same endpoint.

Validation Configuration

The validation section controls request/response validation behavior:

{
  "validation": {
    "validate_request": true,
    "validate_response": false,
    "fail_on_request_error": true,
    "fail_on_response_error": false,
    "pool": {
      "enabled": false,
      "thread_count": 2,
      "timeout_ms": 50,
      "queue_capacity": 1000,
      "timeout_action": "allow"
    }
  }
}

Validation Options

OptionTypeDefaultDescription
validate_requestbooleantrueEnable request body validation against OpenAPI schema
validate_responsebooleanfalseEnable response body validation against OpenAPI schema
fail_on_request_errorbooleantrueReturn 400 error on request validation failure. If false, errors are logged but request continues
fail_on_response_errorbooleanfalseReturn 500 error on response validation failure. If false, errors are logged but response continues

Validation Pool Options

The validation pool offloads JSON schema validation to a dedicated thread pool, preventing validation from blocking Envoy worker threads:

OptionTypeDefaultDescription
enabledbooleanfalseEnable the validation thread pool
thread_countinteger2Number of worker threads
timeout_msinteger50Maximum time to wait for validation (milliseconds)
queue_capacityinteger1000Maximum pending validation jobs
timeout_actionstring"allow"Action on timeout: "allow" or "block"

Mocking Configuration

The mocking section enables mock response generation for API testing:

{
  "mocking": {
    "enabled": true,
    "prefer_examples": true,
    "default_status_code": 200,
    "delay_ms": 100,
    "add_mock_header": true
  }
}
OptionTypeDefaultDescription
enabledbooleanfalseEnable mock response generation (bypasses upstream)
prefer_examplesbooleantrueUse examples from OpenAPI spec when available
default_status_codeinteger-Status code to mock. If not set, uses first 2xx response
delay_msinteger-Simulate network latency (milliseconds)
add_mock_headerbooleantrueAdd X-Mock-Response: true header to mock responses

When mocking is enabled, API-Fence generates responses from:

  1. Examples in the OpenAPI response definition (if prefer_examples: true)
  2. Schema-based generation using fake data matching the response schema

Security Limits

The security section configures input validation limits to prevent resource exhaustion attacks:

{
  "security": {
    "max_path_length": 2048,
    "max_header_value_length": 8192,
    "max_query_string_length": 8192,
    "max_body_size": 10485760,
    "max_json_depth": 32,
    "max_array_items": 1000,
    "max_object_properties": 100,
    "max_schema_depth": 32,
    "max_regex_pattern_length": 1024
  }
}
OptionTypeDefaultDescription
max_path_lengthinteger2048Maximum URL path length (bytes). Returns 414 if exceeded
max_header_value_lengthinteger8192Maximum header value length (bytes)
max_query_string_lengthinteger8192Maximum query string length (bytes)
max_body_sizeinteger10485760Maximum request body size (10 MB). Returns 413 if exceeded
max_json_depthinteger32Maximum JSON nesting depth. Prevents stack overflow
max_array_itemsinteger1000Maximum array items to validate. Larger arrays are truncated
max_object_propertiesinteger100Maximum object properties to validate
max_schema_depthinteger32Maximum schema nesting depth during compilation
max_regex_pattern_lengthinteger1024Maximum regex pattern length in OpenAPI schemas

HTTP Status Codes

Security limit violations return appropriate HTTP status codes:

ViolationStatus Code
Path too long414 URI Too Long
Body too large413 Payload Too Large
Other limits400 Bad Request

ModSecurity Configuration

The modsecurity section enables WAF scanning. See ModSecurity Guide for detailed documentation.

{
  "modsecurity": {
    "scan_request": true,
    "scan_response": false,
    "request_action": "block",
    "response_action": "alert",
    "primary_ruleset": {
      "name": "crs",
      "use_bundled_crs": true,
      "bundled_crs_profile": "full"
    }
  }
}
OptionTypeDefaultDescription
scan_requestbooleanfalseEnable WAF scanning of request bodies
scan_responsebooleanfalseEnable WAF scanning of response bodies
scan_response_as_requestbooleanfalseUse request scanning API for responses (more thorough)
request_actionstring"block"Action on request match: "block" or "alert"
response_actionstring"alert"Action on response match: "block" or "alert"
poolobjectSee belowScanner thread pool settings
string_extractionobjectSee belowJSON string extraction optimization
primary_rulesetobject-Primary ruleset configuration (required if scanning enabled)
secondary_rulesetobject-Secondary ruleset for migration testing

Dynamic Metadata

API-Fence sets Envoy dynamic metadata that can be used in access logs and other filters:

Metadata KeyDescription
api_fence:request.verdictRequest validation result: "valid", "invalid", or "error"
api_fence:request.errorsRequest validation error messages (JSON array)
api_fence:request.error_countNumber of request validation errors
api_fence:response.verdictResponse validation result
api_fence:response.errorsResponse validation error messages
api_fence:response.error_countNumber of response validation errors

Access Log Example

access_log:
  - name: envoy.access_loggers.file
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
      path: /dev/stdout
      log_format:
        text_format_source:
          inline_string: |
            [%START_TIME%] %REQ(:METHOD)% %REQ(:PATH)% %RESPONSE_CODE% verdict=%DYNAMIC_METADATA(api_fence:request.verdict)% errors=%DYNAMIC_METADATA(api_fence:request.errors)%

Metrics

All metrics are scoped under the configured api_name:

MetricTypeDescription
api_fence.<api_name>.cache.hitsCounterSchema cache hits
api_fence.<api_name>.cache.missesCounterSchema cache misses
api_fence.<api_name>.schema.compile_time_msHistogramSchema compilation time
api_fence.<api_name>.request.validation_errorsCounterRequest validation failures
api_fence.<api_name>.response.validation_errorsCounterResponse validation failures

Access metrics at: http://localhost:9901/stats/prometheus

Complete Example

{
  "api_name": "users_api",
  "openapi_spec_path": "/etc/envoy/users-api.yaml",
  "cache": {
    "max_capacity": 500,
    "ttl_seconds": 7200
  },
  "validation": {
    "validate_request": true,
    "validate_response": true,
    "fail_on_request_error": true,
    "fail_on_response_error": false,
    "pool": {
      "enabled": true,
      "thread_count": 4,
      "timeout_ms": 100
    }
  },
  "security": {
    "max_body_size": 5242880,
    "max_json_depth": 16
  },
  "modsecurity": {
    "scan_request": true,
    "primary_ruleset": {
      "name": "crs",
      "use_bundled_crs": true,
      "bundled_crs_profile": "minimal"
    }
  }
}