Skip to content

Proposal: Introduce Adapter interface for driver-specific DSN parsing #177

@szkiba

Description

@szkiba

Context

With the release of k6 v1.5.0, extensions now have access to GetAddrResolver(). This allows extensions to resolve addresses while respecting k6's internal network policies, such as blockHostnames, hosts file configurations, and DNS rules.

To support centralized network validation in xk6-sql and ensure all SQL connections adhere to these security policies, the core module requires knowledge of the target hostname(s) before a connection is established.

Problem

Currently, xk6-sql passes the DSN (Data Source Name) string directly to the underlying driver. The core module lacks visibility into connection details (host, port, etc.) because every database driver uses a unique DSN format. Implementing parsing logic for every supported driver inside the core xk6-sql module would be brittle and violate the separation of concerns.

Proposal

This proposal introduces an Adapter interface and a new registration function, RegisterAdapter. This mechanism allows driver extensions to optionally provide a DSN parser.

Crucially, for drivers registered via RegisterAdapter, the core xk6-sql module will automatically validate the addresses returned by the adapter using GetAddrResolver().ResolveAddr(). This ensures that connections are checked against k6's network security policies (e.g., allowlists, blocklists) before the SQL driver attempts to open a socket. Drivers registered via the legacy method will bypass this validation to maintain backward compatibility.

Proposed API Changes

The proposal adds a struct to hold extracted configuration and an interface for the adapter:

package sql

// DataSourceInfo holds properties extracted from a Data Source Name (DSN).
type DataSourceInfo struct {
    // Addrs is a list of "host" or "host:port" strings found in the DSN.
    Addrs []string
}

// Adapter defines the interface that driver extensions can optionally implement
// to provide parsing logic.
type Adapter interface {
    ParseDSN(dsn string) (DataSourceInfo, error)
}

A new registration function will be added alongside the existing RegisterDriver:

// RegisterAdapter registers a driver with a custom adapter for DSN parsing.
// This allows the driver to opt-in to core validation logic.
// 
// Note: This function registers the driver name internally. 
// Extensions using RegisterAdapter do NOT need to call RegisterDriver separately.
func RegisterAdapter(driverName string, adapter Adapter) *sobek.Symbol

Benefits

  1. Centralized Validation: xk6-sql can enforce k6 network policies for all drivers that implement the adapter.
  2. Backward Compatibility: Existing drivers using RegisterDriver (without an adapter) will continue to function as they do today.
  3. Decoupling: The parsing logic remains within the driver extension (which is aware of its own DSN format), while the policy enforcement logic resides centrally in the core.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions