redact

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2025 License: MIT Imports: 3 Imported by: 0

README

redact

codecov

A Go package for redacting sensitive information from slog-based logs using a configurable pipeline.

Overview

The redact package provides a way to redact sensitive information from logs generated by the slog package. It does so by creating a pipeline of redaction stages that can be configured to redact specific fields from the log records.

flowchart TD
    subgraph Application
        A[Log Record Created]
    end

    subgraph RedactionHandler
        G[Handle Log Record]
    end

    subgraph RedactionPipeline
        direction TB
        C[Process Log Record]
        D[Redaction Stage 1]
        E[Redaction Stage 2]
        F[Redaction Stage N]
    end

    subgraph BaseHandler
        direction TB
        H[Output Redacted Log Record]
    end

    A -->|Passes to| G
    G -->|Invokes| C
    C -->|Applies| D
    D -->|Then| E
    E -->|Then| F
    F -->|Returns Processed Log to| C
    C -->|Returns to| G
    G -->|Delegates to| H

Usage

package main

import (
	"log/slog"
	"os"

	"github.com/alesr/redact"
)

func main() {
	// Create a base handler
	handler := slog.NewTextHandler(os.Stdout, nil)

	// Create a redaction pipeline
	pipeline := redact.NewRedactionPipeline()
	pipeline.AddRedactField("email")
	pipeline.AddRedactField("password")

	// Create a RedactionHandler that wraps the original handler
	redactionHandler := redact.NewRedactionHandler(handler, pipeline)

	// Create a logger with the RedactionHandler
	logger := slog.New(redactionHandler)

	logger.Info("Test log", slog.String("user", "rigoletto"), slog.String("email", "foo@bar.qux"), slog.String("password", "abc123"))
}

// Output: time=2025-02-08T19:49:23.826+02:00 level=INFO msg="Test log" user=rigoletto email=[REDACTED] password=[REDACTED]

Check the examples directory for a testable example.

Documentation

https://pkg.go.dev/github.com/alesr/redact

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedactionFunc

type RedactionFunc func(ctx context.Context, r slog.Record) slog.Record

RedactionFunc defines the signature for redaction functions.

type RedactionHandler

type RedactionHandler struct {
	// contains filtered or unexported fields
}

RedactionHandler is a custom slog.Handler that applies a redaction pipeline.

func NewRedactionHandler

func NewRedactionHandler(handler slog.Handler, pipeline *RedactionPipeline) *RedactionHandler

NewRedactionHandler initializes a new RedactionHandler.

func (*RedactionHandler) Enabled

func (h *RedactionHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler is enabled for the given level. Necessary to implement the slog.Handler interface.

func (*RedactionHandler) Handle

func (h *RedactionHandler) Handle(ctx context.Context, r slog.Record) error

Handle processes the log record through the redaction pipeline before handling it

func (*RedactionHandler) WithAttrs

func (h *RedactionHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with additional attributes.

func (*RedactionHandler) WithGroup

func (h *RedactionHandler) WithGroup(name string) slog.Handler

WithGroup returns a new handler with the specified group.

type RedactionPipeline

type RedactionPipeline struct {
	// contains filtered or unexported fields
}

RedactionPipeline manages a list of redaction stages.

func NewRedactionPipeline

func NewRedactionPipeline() *RedactionPipeline

NewRedactionPipeline initializes a new redaction pipeline.

func (*RedactionPipeline) AddRedactField

func (p *RedactionPipeline) AddRedactField(fieldName string)

AddRedactField adds a redaction stage for a specific field.

func (*RedactionPipeline) Process

func (p *RedactionPipeline) Process(ctx context.Context, r slog.Record) slog.Record

Process applies all redaction stages to the log record.

Jump to

Keyboard shortcuts

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