gatekeeper

API Reference

This document provides comprehensive API documentation for all Gatekeeper functions, types, and interfaces.

📦 Core API

Gatekeeper Creation

NewGatekeeper(config *Config) (*Gatekeeper, error)

Creates a new Gatekeeper instance with the provided configuration.

Parameters:

Returns:

Example:

config := &gatekeeper.Config{
    IPPolicy: &gatekeeper.IPPolicyConfig{
        Mode: gatekeeper.ModeBlacklist,
        IPs:  []string{"192.168.1.100"},
    },
}

gk, err := gatekeeper.NewGatekeeper(config)
if err != nil {
    log.Fatal(err)
}

NewGatekeeperFromFile(configPath string) (*Gatekeeper, error)

Creates a Gatekeeper instance from a JSON configuration file.

Parameters:

Returns:

Example:

gk, err := gatekeeper.NewGatekeeperFromFile("config.json")
if err != nil {
    log.Fatal(err)
}

HTTP Middleware

(g *Gatekeeper) HTTPMiddleware(next http.Handler) http.Handler

Standard HTTP middleware for use with net/http and compatible frameworks.

Parameters:

Returns:

Example:

mux := http.NewServeMux()
mux.HandleFunc("/", homeHandler)

// Apply Gatekeeper middleware
server := &http.Server{
    Addr:    ":8080",
    Handler: gk.HTTPMiddleware(mux),
}

(g *Gatekeeper) HandlerFunc(next http.HandlerFunc) http.HandlerFunc

Handler function wrapper for simpler integration.

Parameters:

Returns:

Example:

http.HandleFunc("/api", gk.HandlerFunc(apiHandler))

Echo Framework Integration

(g *Gatekeeper) EchoMiddleware() echo.MiddlewareFunc

Echo framework middleware function.

Returns:

Example:

e := echo.New()
e.Use(gk.EchoMiddleware())
e.GET("/", homeHandler)

Request Processing

(g *Gatekeeper) ProcessRequest(c echo.Context) error

Process a request through all configured policies (Echo version).

Parameters:

Returns:

(g *Gatekeeper) ProcessHTTPRequest(r *http.Request) *PolicyViolation

Process an HTTP request through all configured policies.

Parameters:

Returns:

📊 Configuration Types

Main Configuration

Config

Main configuration struct for Gatekeeper.

type Config struct {
    UserAgentPolicy        *UserAgentPolicyConfig `json:"userAgentPolicy,omitempty"`
    IPPolicy              *IPPolicyConfig        `json:"ipPolicy,omitempty"`
    RefererPolicy         *RefererPolicyConfig   `json:"refererPolicy,omitempty"`
    RateLimiter           *RateLimiterConfig     `json:"rateLimiter,omitempty"`
    ProfanityFilter       *ProfanityFilterConfig `json:"profanityFilter,omitempty"`
    Logger                *log.Logger            `json:"-"`
    DefaultBlockStatusCode int                   `json:"defaultBlockStatusCode,omitempty"`
    DefaultBlockMessage    string                `json:"defaultBlockMessage,omitempty"`
}

Policy Modes

PolicyMode

Enumeration for policy enforcement modes.

type PolicyMode string

const (
    ModeBlacklist PolicyMode = "BLACKLIST"  // Block listed items
    ModeWhitelist PolicyMode = "WHITELIST"  // Allow only listed items
)

IP Policy

IPPolicyConfig

Configuration for IP-based access control.

type IPPolicyConfig struct {
    Mode              PolicyMode `json:"mode"`
    IPs               []string   `json:"ips"`
    CIDRs             []string   `json:"cidrs"`
    TrustProxyHeaders bool       `json:"trustProxyHeaders"`
    TrustedProxies    []string   `json:"trustedProxies"`
}

Methods:

(policy *IPPolicyConfig) IsBlocked(ip string, headers http.Header) bool

Check if an IP address is blocked by the policy.

Parameters:

Returns:

User-Agent Policy

UserAgentPolicyConfig

Configuration for User-Agent filtering.

type UserAgentPolicyConfig struct {
    Mode     PolicyMode `json:"mode"`
    Exact    []string   `json:"exact"`
    Patterns []string   `json:"patterns"`
}

Methods:

(policy *UserAgentPolicyConfig) IsBlocked(userAgent string) bool

Check if a User-Agent string is blocked.

Parameters:

Returns:

Referer Policy

RefererPolicyConfig

Configuration for Referer header filtering.

type RefererPolicyConfig struct {
    Mode     PolicyMode `json:"mode"`
    Exact    []string   `json:"exact"`
    Patterns []string   `json:"patterns"`
}

Methods:

(policy *RefererPolicyConfig) IsBlocked(referer string) bool

Check if a Referer URL is blocked.

Parameters:

Returns:

Rate Limiter

RateLimiterConfig

Configuration for rate limiting.

type RateLimiterConfig struct {
    Requests               int64                  `json:"requests"`
    Period                 time.Duration          `json:"period"`
    Store                  store.RateLimiterStore `json:"-"`
    Exceptions             *RateLimiterExceptions `json:"exceptions,omitempty"`
    LimitExceededMessage   string                 `json:"limitExceededMessage,omitempty"`
    LimitExceededStatusCode int                   `json:"limitExceededStatusCode,omitempty"`
}

RateLimiterExceptions

Rate limiting exceptions configuration.

type RateLimiterExceptions struct {
    IPWhitelist            []string `json:"ipWhitelist"`
    RouteWhitelistPatterns []string `json:"routeWhitelistPatterns"`
}

Methods:

(rl *RateLimiterConfig) IsAllowed(ip, route string) bool

Check if a request is allowed under rate limiting rules.

Parameters:

Returns:

Profanity Filter

ProfanityFilterConfig

Configuration for content profanity filtering.

type ProfanityFilterConfig struct {
    BlockWords        []string `json:"blockWords"`
    AllowWords        []string `json:"allowWords"`
    CheckQueryParams  bool     `json:"checkQueryParams"`
    CheckFormFields   bool     `json:"checkFormFields"`
    CheckJSONBody     bool     `json:"checkJsonBody"`
    BlockedMessage    string   `json:"blockedMessage,omitempty"`
    BlockedStatusCode int      `json:"blockedStatusCode,omitempty"`
}

Methods:

(pf *ProfanityFilterConfig) CheckRequest(r *http.Request) bool

Check if request content contains blocked words.

Parameters:

Returns:

🔄 Configuration Watcher

ConfigWatcher

Dynamic configuration management with hot reloading.

NewConfigWatcher(configPath string, logger *log.Logger) (*ConfigWatcher, error)

Create a new configuration file watcher.

Parameters:

Returns:

Example:

watcher, err := gatekeeper.NewConfigWatcher("config.json", nil)
if err != nil {
    log.Fatal(err)
}

// Get current Gatekeeper instance
gk := watcher.GetGatekeeper()

// Start watching for changes
watcher.Start()

// Use with Echo
e.Use(gk.EchoMiddleware())

// Stop watching when done
defer watcher.Stop()

(cw *ConfigWatcher) Start()

Start watching the configuration file for changes.

(cw *ConfigWatcher) Stop()

Stop watching the configuration file.

(cw *ConfigWatcher) GetGatekeeper() *Gatekeeper

Get the current Gatekeeper instance (thread-safe).

Returns:

💾 Store Interface

RateLimiterStore

Interface for rate limiter storage backends.

type RateLimiterStore interface {
    Get(key string) (int64, time.Time, bool)
    Set(key string, count int64, expiry time.Time)
    Increment(key string, expiry time.Time) int64
    Delete(key string)
    Clear()
}

Built-in Implementations

NewMemoryStore(cleanupInterval time.Duration) *MemoryStore

Create a new in-memory rate limiter store.

Parameters:

Returns:

Example:

store := store.NewMemoryStore(5 * time.Minute)

config := &gatekeeper.Config{
    RateLimiter: &gatekeeper.RateLimiterConfig{
        Requests: 100,
        Period:   1 * time.Minute,
        Store:    store,
    },
}

⚠️ Error Types

PolicyViolation

Represents a policy violation with details.

type PolicyViolation struct {
    Policy      string `json:"policy"`      // Policy name (e.g., "ip", "user-agent")
    Reason      string `json:"reason"`      // Violation reason
    StatusCode  int    `json:"statusCode"`  // HTTP status code to return
    Message     string `json:"message"`     // Response message
    ClientIP    string `json:"clientIP"`    // Client IP address
    UserAgent   string `json:"userAgent"`   // User-Agent header
    Referer     string `json:"referer"`     // Referer header
    RequestPath string `json:"requestPath"` // Request URL path
    Timestamp   string `json:"timestamp"`   // Violation timestamp
}

Methods

(pv *PolicyViolation) Error() string

Returns a string representation of the policy violation.

(pv *PolicyViolation) ToJSON() ([]byte, error)

Serialize the violation to JSON.

Returns:

🔧 Utility Functions

IP Utilities

ParseIP(ipStr string) (net.IP, error)

Parse an IP address string.

Parameters:

Returns:

GetClientIP(r *http.Request, trustProxy bool, trustedProxies []string) string

Extract client IP from request, considering proxy headers.

Parameters:

Returns:

IsPrivateIP(ip net.IP) bool

Check if an IP address is in a private range.

Parameters:

Returns:

Pattern Matching

CompilePatterns(patterns []string) ([]*regexp.Regexp, error)

Compile a list of regex patterns.

Parameters:

Returns:

MatchesAny(text string, patterns []*regexp.Regexp) bool

Check if text matches any of the provided patterns.

Parameters:

Returns:

📝 Logging

Log Events

Gatekeeper logs the following events when a logger is configured:

Log Format

[Gatekeeper] [LEVEL] MESSAGE - DETAILS

Example Log Entries:

[Gatekeeper] [INFO] IP Policy initialized with 5 blocked IPs
[Gatekeeper] [WARN] Request blocked by IP policy - IP: 192.168.1.100, Path: /api/users
[Gatekeeper] [INFO] Rate limit exceeded - IP: 10.0.0.50, Limit: 100/min
[Gatekeeper] [INFO] Configuration reloaded from config.json
[Gatekeeper] [ERROR] Failed to parse CIDR range: invalid-cidr