grafana / grafana/xk6-sql-driver-postgres

Implement `Adapter` interface for network policy enforcement

Open
#21 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
13
Forks
3
Avg merge
2d 8h
Merged PRs (30d)
13

Description

## Context

The core `xk6-sql` extension is introducing an `Adapter` interface to support centralized network validation (grafana/xk6-sql#177). This change utilizes the `GetAddrResolver()` functionality introduced in k6 v1.5.0 to enforce network policies (such as `blockHostnames`, `hosts` file mappings, and DNS rules) before establishing a database connection.

Enforcing these policies is a requirement for `xk6-sql` and its drivers to comply with k6 extension standards.

## Goal
Update the Postgres driver to implement the new `Adapter` interface. This allows the driver to expose the target hostname(s) to the `xk6-sql` core for validation without requiring the core to contain Postgres-specific parsing logic.

## Interface Definition (from xk6-sql)

```go
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)
}

// 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
```

## Task
1. **Implement the Adapter:** Create a struct that satisfies the `sql.Adapter` interface.
2. **Parse DSN:** Implement the `ParseDSN` method to extract the target address (host and port) from the connection string.
* *Note:* Postgres supports both Keyword/Value connection strings (e.g., `host=localhost port=5432`) and Connection URIs (e.g., `postgres://user:pass@localhost:5432/db`). The parser must handle both formats to correctly identify the host.
3. **Update Registration:** Change the driver registration to use `sql.RegisterAdapter` (or the equivalent module-level wrapper if applicable). Note that `RegisterAdapter` handles the driver name registration, so the previous registration call should be replaced.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.