tagging

package
v0.1.53 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package tagging labels requests based on configurable HTTP headers. Each rule names one header whose value carries request labels; labels flow into usage tracking and audit logs, and rules can mark their header as do-not-pass so it is never forwarded to upstream providers.

Index

Constants

View Source
const DefaultDelimiter = ","

DefaultDelimiter separates multiple labels inside one header value.

Variables

This section is empty.

Functions

func ExtractLabels

func ExtractLabels(rules []Rule, headers http.Header) []string

ExtractLabels reads every rule's header and returns the deduplicated labels in rule order. Each header value is split by the rule's delimiter and each piece is whitespace-trimmed; when the rule has a prefix, it is trimmed from pieces that carry it, and pieces without it are kept as-is.

func IsValidationError

func IsValidationError(err error) bool

IsValidationError reports whether err stems from invalid caller input.

func NormalizeRules

func NormalizeRules(rules []Rule) error

NormalizeRules canonicalizes header names, applies the default delimiter, and rejects invalid, credential-bearing, or duplicate entries in place. Rejections are ValidationErrors.

func StripHeaderSet

func StripHeaderSet(rules []Rule) map[string]struct{}

StripHeaderSet returns the canonical header names marked do-not-pass.

Types

type MongoDBStore

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

MongoDBStore persists tagging rules in a settings collection.

func NewMongoDBStore

func NewMongoDBStore(_ context.Context, database *mongo.Database) (*MongoDBStore, error)

NewMongoDBStore creates a tagging store over the tagging_settings collection.

func (*MongoDBStore) Close

func (s *MongoDBStore) Close() error

Close is a no-op: the client is managed by the storage layer.

func (*MongoDBStore) GetRules

func (s *MongoDBStore) GetRules(ctx context.Context) ([]Rule, error)

func (*MongoDBStore) SaveRules

func (s *MongoDBStore) SaveRules(ctx context.Context, rules []Rule) error

type PostgreSQLStore

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

PostgreSQLStore persists tagging rules in a key-value settings table.

func NewPostgreSQLStore

func NewPostgreSQLStore(ctx context.Context, pool *pgxpool.Pool) (*PostgreSQLStore, error)

NewPostgreSQLStore creates the tagging settings table when missing.

func (*PostgreSQLStore) Close

func (s *PostgreSQLStore) Close() error

Close is a no-op: the pool is managed by the storage layer.

func (*PostgreSQLStore) GetRules

func (s *PostgreSQLStore) GetRules(ctx context.Context) ([]Rule, error)

func (*PostgreSQLStore) SaveRules

func (s *PostgreSQLStore) SaveRules(ctx context.Context, rules []Rule) error

type Result

type Result struct {
	Service *Service
	Store   Store
	Storage storage.Storage
	// contains filtered or unexported fields
}

Result bundles the tagging service with its store and optional owned storage.

func New

func New(ctx context.Context, cfg *config.Config) (*Result, error)

New builds the tagging service with its own storage connection.

func NewWithSharedStorage

func NewWithSharedStorage(ctx context.Context, cfg *config.Config, shared storage.Storage) (*Result, error)

NewWithSharedStorage builds the tagging service on an existing storage backend and loads the persisted operator rules.

func (*Result) Close

func (r *Result) Close() error

type Rule

type Rule struct {
	// Header is the canonical HTTP header name to read labels from.
	Header string `json:"header" bson:"header"`

	// Prefix is optionally trimmed from the front of each label. Trimming only
	// affects the extracted label, never the forwarded header value.
	Prefix string `json:"prefix,omitempty" bson:"prefix,omitempty"`

	// DoNotPass strips the header before forwarding the request upstream.
	// Default: false (headers are passed through as-is).
	DoNotPass bool `json:"do_not_pass,omitempty" bson:"do_not_pass,omitempty"`

	// Delimiter splits one header value into multiple labels. Default: ",".
	Delimiter string `json:"delimiter,omitempty" bson:"delimiter,omitempty"`

	// Managed marks a rule declared in config/env; such rules are read-only in
	// the dashboard. Never persisted.
	Managed bool `json:"managed,omitempty" bson:"-"`
}

Rule configures label extraction from one request header. Managed rules come from config.yaml / TAGGING_HEADER_* env vars, override store rows with the same header name, and are read-only in the dashboard.

func ConfigRules

func ConfigRules(entries []config.TaggingHeaderConfig) []Rule

ConfigRules converts declarative config.yaml / TAGGING_HEADER_* entries into managed tagging rules. Entries are already normalized by config.Load.

type SQLiteStore

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

SQLiteStore persists tagging rules in a key-value settings table.

func NewSQLiteStore

func NewSQLiteStore(db *sql.DB) (*SQLiteStore, error)

NewSQLiteStore creates the tagging settings table when missing.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close is a no-op: the DB handle is managed by the storage layer.

func (*SQLiteStore) GetRules

func (s *SQLiteStore) GetRules(ctx context.Context) ([]Rule, error)

func (*SQLiteStore) SaveRules

func (s *SQLiteStore) SaveRules(ctx context.Context, rules []Rule) error

type Service

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

Service merges declarative (config/env) tagging rules over operator rules persisted in the store and serves label extraction on the request hot path.

func NewService

func NewService(configRules []Rule, store Store) *Service

NewService creates a tagging service. configRules must already be normalized by config.Load; store may be nil, in which case only config rules apply and dashboard edits are unavailable.

func (*Service) Editable

func (s *Service) Editable() bool

Editable reports whether operator rules can be persisted.

func (*Service) ExtractLabels

func (s *Service) ExtractLabels(headers http.Header) []string

ExtractLabels returns the request labels for the given inbound headers.

func (*Service) HasRules

func (s *Service) HasRules() bool

HasRules reports whether any tagging rule is currently active.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context) error

Refresh reloads operator rules from the store and swaps the merged snapshot.

func (*Service) Rules

func (s *Service) Rules() []Rule

Rules returns the effective rules: managed config rules first, then operator rules from the store.

func (*Service) SaveRules

func (s *Service) SaveRules(ctx context.Context, rules []Rule) ([]Rule, error)

SaveRules validates and persists the operator-managed rule set (replacing the previous set), refreshes the snapshot, and returns the merged view. Rules whose header is declared in config/env are rejected as read-only.

func (*Service) StripHeaders

func (s *Service) StripHeaders() map[string]struct{}

StripHeaders returns the canonical header names that must not be forwarded to upstream providers. Callers must treat the returned map as read-only.

type Store

type Store interface {
	// GetRules returns the persisted operator rules, empty when none were saved.
	GetRules(ctx context.Context) ([]Rule, error)

	// SaveRules replaces the persisted operator rule set.
	SaveRules(ctx context.Context, rules []Rule) error

	// Close releases store resources.
	Close() error
}

Store persists the operator-managed tagging rules (the dashboard-editable set). Declarative config/env rules are never stored.

type ValidationError

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

ValidationError marks rule failures caused by caller input, so API handlers can report them as a bad request instead of a storage failure.

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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