plugins

package
v3.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAction

func RegisterAction(name string, a ActionFactory)

RegisterAction registers a new RuleAction If you register an action with an existing name, it will be overwritten.

func RegisterAuditLogFormatter

func RegisterAuditLogFormatter(name string, format plugintypes.AuditLogFormatter)

RegisterAuditLogFormatter registers a new audit log formatter.

Example

ExampleRegisterAuditLogFormatter shows how to register a custom audit log formatter and tests the output of the formatter.

//go:build !tinygo
// +build !tinygo

// Not aimed to tinygo as serial writer is a noop writer

package main

import (
	"github.com/lixf311/coraza/v3"
	"github.com/lixf311/coraza/v3/experimental/plugins"
	"github.com/lixf311/coraza/v3/experimental/plugins/plugintypes"
)

type testFormatter struct{}

func (testFormatter) Format(al plugintypes.AuditLog) ([]byte, error) {
	return []byte(al.Transaction().ID()), nil
}

func (testFormatter) MIME() string {
	return "sample"
}

// ExampleRegisterAuditLogFormatter shows how to register a custom audit log formatter
// and tests the output of the formatter.
func main() {

	plugins.RegisterAuditLogFormatter("txid", &testFormatter{})

	w, err := coraza.NewWAF(
		coraza.NewWAFConfig().
			WithDirectives(`
				SecAuditEngine On
				SecAuditLogParts ABCFHZ
				SecAuditLog /dev/stdout
				SecAuditLogFormat txid
				SecAuditLogType serial
			`),
	)
	if err != nil {
		panic(err)
	}

	tx := w.NewTransactionWithID("abc123")
	tx.ProcessLogging()
	tx.Close()

}
Output:

abc123

func RegisterAuditLogWriter

func RegisterAuditLogWriter(name string, writerFactory func() plugintypes.AuditLogWriter)

RegisterAuditLogWriter registers a new audit log writer.

Example

ExampleRegisterAuditLogWriter shows how to register a custom audit log writer and tests the output of the writer.

//go:build !tinygo
// +build !tinygo

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/lixf311/coraza/v3"
	"github.com/lixf311/coraza/v3/experimental/plugins"
	"github.com/lixf311/coraza/v3/experimental/plugins/plugintypes"
)

type urlWriter struct {
	url string
}

func (s *urlWriter) Init(cfg plugintypes.AuditLogConfig) error {
	s.url = cfg.Target
	return nil
}

func (s *urlWriter) Write(al plugintypes.AuditLog) error {
	res, err := http.DefaultClient.Post(s.url, "application/json", strings.NewReader(al.Transaction().ID()))
	if err != nil {
		return err
	}
	res.Body.Close()
	_, err = io.Copy(io.Discard, res.Body)
	return err
}

func (s *urlWriter) Close() error { return nil }

// ExampleRegisterAuditLogWriter shows how to register a custom audit log writer
// and tests the output of the writer.
func main() {
	plugins.RegisterAuditLogWriter("url", func() plugintypes.AuditLogWriter {
		return &urlWriter{}
	})

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		b, _ := io.ReadAll(r.Body)
		fmt.Println(string(b))
	}))
	defer srv.Close()

	w, err := coraza.NewWAF(
		coraza.NewWAFConfig().
			WithDirectives(`
				SecAuditEngine On
				SecAuditLogParts ABCFHZ
				SecAuditLog ` + srv.URL + `
				SecAuditLogType url
			`),
	)
	if err != nil {
		panic(err)
	}

	tx := w.NewTransactionWithID("xyz456")
	tx.ProcessLogging()
	tx.Close()

}
Output:

xyz456

func RegisterBodyProcessor

func RegisterBodyProcessor(name string, fn func() plugintypes.BodyProcessor)

RegisterBodyProcessor registers a body processor by name. If the body processor is already registered, it will be overwritten

func RegisterOperator

func RegisterOperator(name string, op plugintypes.OperatorFactory)

RegisterOperator registers a new operator If the operator already exists it will be overwritten

func RegisterTransformation

func RegisterTransformation(name string, trans plugintypes.Transformation)

RegisterTransformation registers a transformation by name If the transformation is already registered, it will be overwritten

Types

type ActionFactory

type ActionFactory = func() plugintypes.Action

ActionFactory is used to wrap a RuleAction so that it can be registered and recreated on each call

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL