audit

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

Application Audit Module

Application orchestration for parsing and, later, evaluating SQL audit requests.

Files

File Responsibility
parse.go Builds application-owned parsed statements from infrastructure-backed parser adapters
parse_test.go Verifies that application parsing hides parser-specific AST details
extract.go Converts parsed statements into first-pass domain Statement values with explicit DDL operations, separate DDL constraint handling, typed index metadata, create-table shape flags, preserved unnamed-index names for identifier governance, explicit column charset/collation facts, normalized row-format and auto-increment-init table options, richer alter-table column/index/rename/option payloads, object-lifecycle extraction for create-view/drop/truncate, honest statement-local alter change facts, multi-column add normalization, and DML operation plus target-table facts for offline governance
extract_test.go Verifies representative DDL and DML extraction behavior, including create-like/create-as/partition flags plus enriched create-table facts, preserved backticked-keyword and unnamed-index names, extracted column charset/collation facts, normalized row-format and auto-increment-init options, explicit DDL lifecycle operations, richer alter-table detail including explicit statement-local change facts, multi-column add expansion, non-index constraint handling, and extracted DML target tables
evaluate.go Applies registered rules and aggregates statement/global findings into report output
evaluate_test.go Verifies application-owned report-flow integration over the rule registry
service.go Orchestrates the full audit flow across policy loading, parsing, extraction, top-level request metadata plumbing, optional metadata enrichment, rule registration, and evaluation
service_test.go Verifies the end-to-end application audit use case with defaults, config overrides, multi-statement SQL, and metadata enrichment behavior, including schema-only context and top-level request plumbing
metadata.go Defines the optional metadata-provider interface and attaches schema, instance, and target-table facts to statements before evaluation

Exports

  • Parse(sql string, dialect spec.Dialect)
  • Extract(parsed ParsedSQL)
  • EvaluateStatements(registry, statements)
  • AuditSQL(ctx, request)
  • Request
  • MetadataRequest
  • MetadataProvider
  • Service
  • NewService()
  • Service.Audit(ctx, request)
  • ParsedStatement
  • ParsedSQL

Dependencies

  • Upstream: future CLI and public audit entrypoints
  • Downstream: context, internal/application/policy, internal/domain/report, internal/domain/rule, internal/domain/rule/ddl, internal/domain/rule/dml, internal/domain/spec, internal/infrastructure/parser/tidb, github.com/pingcap/tidb/pkg/parser/ast

Update Rule

  • If members/interfaces/dependencies change, update this file in same change.

Documentation

Overview

Package audit orchestrates audit use cases at the application layer. input: extracted domain statements and the registered rule engine output: aggregated report results with statement and global findings pos: application evaluation step between extraction and reporting note: if this file changes, update this header and module README.md.

Package audit orchestrates audit use cases at the application layer. input: application-owned parsed SQL statements and hidden TiDB AST nodes output: first-pass StatementSpec values plus honest statement-local alter change facts for later rule evaluation pos: application extraction step between parsing and rule execution note: if this file changes, update this header and module README.md.

Package audit orchestrates audit use cases at the application layer. input: optional metadata providers plus parsed statement targets for enrichment output: metadata-enriched statements for rules that can use live instance or schema facts pos: application-layer bridge between provider-backed metadata and domain statements note: if this file changes, update this header and module README.md.

Package audit orchestrates audit use cases at the application layer. input: SQL text, selected dialect, and infrastructure-backed parser adapters output: application-owned parsed statements for later extraction and rule evaluation pos: application parsing entrypoint between interfaces and parser infrastructure note: if this file changes, update this header and module README.md.

Package audit orchestrates audit use cases at the application layer. input: audit requests carrying SQL text, dialect, optional policy override paths, and optional metadata providers output: end-to-end audit results assembled from policy loading, parsing, extraction, metadata enrichment, and rule evaluation pos: application service entrypoint for the unified offline/metadata-aware SQL audit use case note: if this file changes, update this header and module README.md.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptySQL indicates the request did not include auditable SQL text.
	ErrEmptySQL = errors.New("audit SQL must not be empty")
	// ErrUnknownDialect indicates the request did not specify a supported dialect.
	ErrUnknownDialect = errors.New("audit dialect must be mysql or tidb")
)

Functions

func AuditSQL

func AuditSQL(ctx context.Context, request Request) (report.Result, error)

AuditSQL is the convenience application entrypoint used by outer adapters.

func EvaluateStatements

func EvaluateStatements(registry *rule.Registry, statements []spec.Statement) (report.Result, error)

EvaluateStatements applies registered rules and aggregates their findings into a report result.

func Extract

func Extract(parsed ParsedSQL) ([]spec.Statement, error)

Extract converts parsed statements into first-pass domain StatementSpec values.

Types

type MetadataProvider

type MetadataProvider interface {
	LoadInstanceFacts(ctx context.Context, dialect spec.Dialect, schema string) (*spec.InstanceFacts, error)
	LoadTableSnapshot(ctx context.Context, dialect spec.Dialect, schema string, table string) (*spec.TableSnapshot, error)
}

MetadataProvider supplies optional instance and schema facts for one audit run.

type MetadataRequest

type MetadataRequest struct {
	Schema   string
	Provider MetadataProvider
}

MetadataRequest describes one optional metadata-aware audit invocation.

type ParsedSQL

type ParsedSQL struct {
	Dialect    spec.Dialect      `json:"dialect"`
	Statements []ParsedStatement `json:"statements"`
	Warnings   []string          `json:"warnings,omitempty"`
}

ParsedSQL is the application-owned parsing result used by later extraction steps.

func Parse

func Parse(sql string, dialect spec.Dialect) (ParsedSQL, error)

Parse delegates SQL parsing to the TiDB-backed parser adapter for supported v1 dialects.

type ParsedStatement

type ParsedStatement struct {
	Kind   spec.Kind `json:"kind"`
	RawSQL string    `json:"raw_sql"`
	// contains filtered or unexported fields
}

ParsedStatement keeps application-facing statement metadata while hiding parser nodes.

type Request

type Request struct {
	SQL              string
	Dialect          spec.Dialect
	ConfigPath       string
	Schema           string
	MetadataProvider MetadataProvider
	Metadata         *MetadataRequest
}

Request describes one application-level audit invocation.

type Service

type Service struct{}

Service coordinates the full audit use case.

func NewService

func NewService() Service

NewService returns a ready-to-use audit service.

func (Service) Audit

func (s Service) Audit(ctx context.Context, request Request) (report.Result, error)

Audit executes the full SQL audit flow.

Jump to

Keyboard shortcuts

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