Documentation
¶
Overview ¶
Package chdbengine provides an isolated, zero-cgo purego adapter for libchdb (in-process ClickHouse) with pinned SHA-256 library verification, dedicated restricted ClickHouse RBAC user isolation (`tracedb_ro`), ClickHouse C++ AST classification, and a hard CUDA process isolation guard.
Security & Process Isolation:
- libchdb must NEVER be loaded into a process that also runs CUDA. Open refuses to dlopen libchdb whenever SetCUDAProcessActive(true) or TRACEDB_CUDA_ACTIVE=1 is set. chDB signal handlers are disabled via chdb_set_signal_handlers_enabled(0) prior to booting ClickHouse.
- libchdb must be provided as a local regular file pinned by expected SHA-256; network URLs and remote installers are strictly forbidden.
- Builds stay CGO_ENABLED=0 using github.com/ebitengine/purego.
- All agent queries run on a dedicated restricted ClickHouse connection authenticated as user `tracedb_ro` (NOT `default`, NOT an admin user), granted ONLY role `tracedb_ro_role` (GRANT SELECT ON <session_db>.* ONLY; ZERO `tracedb_writer`, ZERO `ACCESS MANAGEMENT`, ZERO external sources FILE/URL/S3/REMOTE/HDFS/MYSQL/POSTGRES/JDBC/ODBC, ZERO CREATE TEMPORARY TABLE/INSERT/DDL/SYSTEM grants) with profile `tracedb_ro_profile` locking readonly=1, allow_ddl=0, allow_introspection_functions=0, allow_python_table_function=0, and strict execution/memory/row/byte caps.
- Internal schema setup and Exec/ingestion run exclusively on a separate internal connection authenticated as `tracedb_admin` (role `tracedb_writer`), which is never exposed to agent query paths.
Package chdbengine provides an isolated, zero-cgo purego adapter for libchdb (in-process ClickHouse) with pinned SHA-256 library verification, dedicated restricted ClickHouse RBAC user isolation (`tracedb_ro`), ClickHouse C++ AST classification, and a hard CUDA process isolation guard.
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Index ¶
- Constants
- Variables
- func IsCUDAProcessActive() bool
- func OpenBestEngine(cfg BestEngineConfig) (tracedb.Engine, error)
- func SetCUDAProcessActive(active bool)
- func VerifyPinnedLibrary(path, expectedSHA256 string) (string, error)
- type BestEngineConfig
- type Config
- type Engine
- func (e *Engine) ChDBVersion() string
- func (e *Engine) Close() error
- func (e *Engine) Database() string
- func (e *Engine) Databases() []string
- func (e *Engine) EngineName() string
- func (e *Engine) Exec(ctx context.Context, sql string) error
- func (e *Engine) Query(ctx context.Context, sql string, params map[string]tracedb.Param) (*tracedb.Result, error)
- func (e *Engine) RestrictedSessionSettings() []string
- func (e *Engine) Tables(db string) []tracedb.TableInfo
Constants ¶
const ( // EnvChDBLib configures the local file path to a pinned libchdb shared library. EnvChDBLib = "TRACEDB_CHDB_LIB" // EnvChDBSHA256 configures the required 64-char hex SHA-256 digest of EnvChDBLib. EnvChDBSHA256 = "TRACEDB_CHDB_SHA256" // EnvCUDAActive forces CUDA process isolation, preventing libchdb from ever // being dlopen'd inside a GPU/CUDA worker process. EnvCUDAActive = "TRACEDB_CUDA_ACTIVE" )
Variables ¶
var ( // ErrCUDAProcessIsolation is returned when attempting to open or query // libchdb in a process where CUDA is active. ErrCUDAProcessIsolation = errors.New("chdbengine: refusing to load libchdb in a CUDA-active process (analysis process only)") // ErrInvalidConfig indicates a missing library path, missing SHA-256 pin, invalid database identifier, or forbidden remote URL. ErrInvalidConfig = errors.New("chdbengine: invalid pinned libchdb config") // ErrChecksumMismatch is returned when the on-disk libchdb SHA-256 does not match ExpectedSHA256. ErrChecksumMismatch = errors.New("chdbengine: libchdb SHA-256 mismatch against pinned digest") // ErrLibraryNotFound is returned when the pinned libchdb shared library does not exist. ErrLibraryNotFound = errors.New("chdbengine: pinned libchdb shared library not found") // ErrSymbolMissing is returned when the loaded library does not expose the expected chDB C ABI. ErrSymbolMissing = errors.New("chdbengine: libchdb symbol not found") // ErrEngineClosed is returned when querying or executing against a closed chDB engine. ErrEngineClosed = errors.New("chdbengine: engine is closed") // ErrChDBClassifierRejected is returned when ClickHouse's own C++ parser // classifies a query as non-read-only or multi-statement. ErrChDBClassifierRejected = errors.New("chdbengine: chDB parser rejected non-read-only or multi-statement query") )
Functions ¶
func IsCUDAProcessActive ¶
func IsCUDAProcessActive() bool
IsCUDAProcessActive reports whether CUDA isolation forbids loading libchdb in this process.
func OpenBestEngine ¶
func OpenBestEngine(cfg BestEngineConfig) (tracedb.Engine, error)
OpenBestEngine selects the best available tracedb.Engine:
- Whenever a pinned libchdb (via cfg or TRACEDB_CHDB_LIB / TRACEDB_CHDB_SHA256) is configured AND CUDA is not active in the process, it opens chdbengine (EngineName() == "chdb").
- Falls back to fail-closed localengine (EngineName() == "local_fallback") ONLY when no pinned libchdb is configured OR when CUDA process isolation forbids loading libchdb in this process.
- If a libchdb path is explicitly configured with a missing/mismatched SHA-256 or broken library (and CUDA is inactive), OpenBestEngine fails closed by returning the verification/open error.
func SetCUDAProcessActive ¶
func SetCUDAProcessActive(active bool)
SetCUDAProcessActive marks whether the current OS process uses CUDA. Once true, Open refuses to dlopen libchdb with ErrCUDAProcessIsolation.
func VerifyPinnedLibrary ¶
VerifyPinnedLibrary validates that path is a local regular file whose SHA-256 matches expectedSHA256 in constant time. Network URLs and missing pins are unconditionally rejected.
Types ¶
type BestEngineConfig ¶
type BestEngineConfig struct {
SessionDir string
Database string
LibraryPath string
ExpectedSHA256 string
Limits tracedb.QueryLimits
}
BestEngineConfig configures OpenBestEngine, defaulting to pinned libchdb whenever configured and CUDA process isolation does not forbid it.
type Config ¶
type Config struct {
// LibraryPath is the local path to libchdb.so or libchdb.dylib.
LibraryPath string
// ExpectedSHA256 is the required 64-char hex SHA-256 digest of LibraryPath.
ExpectedSHA256 string
// Version is an optional human-readable pinned chDB version label.
Version string
// DataDir is the directory storing the local MergeTree database.
// If empty, a private temporary directory is created and removed on Close.
DataDir string
// Database is the session database name (defaults to tracedb.DefaultDatabase).
Database string
// Limits bounds query execution time, memory, rows, and output bytes.
Limits tracedb.QueryLimits
}
Config configures the pinned purego libchdb engine.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a zero-cgo purego adapter over real libchdb implementing tracedb.Engine and tracedb.NamedEngine.
It maintains two strictly separated authenticated ClickHouse user connections:
- adminConn (user `tracedb_admin`, role `tracedb_writer`): internal-only for DDL & Exec; never exposed to agent queries.
- roConn (user `tracedb_ro`, role `tracedb_ro_role` ONLY + `tracedb_ro_profile` readonly=1 + allow_ddl=0 + limits): executes all queries.
func Open ¶
Open verifies CUDA isolation, verifies the pinned SHA-256 of libchdb, dlopens libchdb via purego (CGO_ENABLED=0), boots ClickHouse with a restricted RBAC users.xml/config.xml separating `tracedb_admin` and `tracedb_ro`, initializes the session schema + AGENTS.ROOT on the privileged internal connection, and binds agent queries strictly to user `tracedb_ro`.
func (*Engine) ChDBVersion ¶
ChDBVersion returns the runtime libchdb version string reported by chdb_version().
func (*Engine) Close ¶
Close releases this engine's reference to the in-process chDB server (shutting it down cleanly and removing any engine-owned temp dir once the last handle closes).
func (*Engine) Databases ¶
Databases returns the single session database configured for this engine.
func (*Engine) EngineName ¶
EngineName returns tracedb.EngineChDB ("chdb").
func (*Engine) Exec ¶
Exec executes an internal non-query statement (such as DDL or INSERT) on the privileged internal writer connection (`tracedb_admin`). Agent query paths never invoke Exec.
func (*Engine) Query ¶
func (e *Engine) Query(ctx context.Context, sql string, params map[string]tracedb.Param) (*tracedb.Result, error)
Query validates sql via tracedb.ValidateQuery, binds any {name:Type} parameters, verifies read-only AST shape via ClickHouse's own C++ parser (`EXPLAIN AST`), enforces CUDA isolation, and executes on the dedicated restricted ClickHouse user `tracedb_ro` (`tracedb_ro_role` ONLY + `tracedb_ro_profile` readonly=1 + allow_ddl=0 + resource caps).
func (*Engine) RestrictedSessionSettings ¶
RestrictedSessionSettings returns the ClickHouse CLI/session flags enforcing read-only mode and defensive resource caps on every agent query.