rlstest

package
v1.98.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package rlstest provides a test-only tenancy.Provider wrapper that drops connections to an unprivileged role so Postgres FORCE ROW LEVEL SECURITY is actually enforced.

Background: the postgres testcontainer creates the application user as a SUPERUSER. Per Postgres semantics, superusers bypass RLS even when the policy is forced. Migrations need the elevated role (to create policies, force RLS, etc.), but application queries must run under a less-privileged role for the isolation guarantee to be exercised by tests. Promoted from service-fintech apps/limits/tests/rlstest so every frame service test suite can exercise RLS without hand-rolling the role-drop pattern (mirrors tenancy/postgres provider_test.go).

Index

Constants

View Source
const Role = "rls_test_user"

Role is the non-superuser role name used in tests.

Variables

This section is empty.

Functions

func CreateRole

func CreateRole(ctx context.Context, dsn string) error

CreateRole opens a short-lived admin connection to dsn and creates the non-superuser Role idempotently. Safe to call before any tables exist — table-level grants must happen separately via GrantAll once the schema is migrated.

func GrantAll

func GrantAll(ctx context.Context, dsn string) error

GrantAll grants the test role enough privileges on the public schema to read and write every table that exists at call time. Must be invoked AFTER migration so all tables are present. Idempotent.

Types

type Provider

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

Provider wraps the Postgres tenancy provider with hooks that switch the connection's role to Role on acquire and reset on release. The switch only fires after Enable has been called, so the migration phase runs as the original (superuser) role.

func New

func New() *Provider

New returns a fresh wrapper.

func (*Provider) Capabilities

func (p *Provider) Capabilities() tenancy.Capabilities

Capabilities implements tenancy.Provider.

func (*Provider) Enable

func (p *Provider) Enable()

Enable turns on the per-connection SET ROLE behaviour. Call after migration (and after grants on the migrated tables) and before issuing any application query you want to test against RLS.

func (*Provider) Install

func (p *Provider) Install(ctx context.Context, db *gorm.DB, models []tenancy.ModelInfo) error

Install implements tenancy.Provider — delegates to the inner provider.

func (*Provider) Name

func (p *Provider) Name() string

Name implements tenancy.Provider.

func (*Provider) WireAdapter

func (p *Provider) WireAdapter(adapter dialect.DialectAdapter) error

WireAdapter implements tenancy.Provider. Registers our role-switch hooks AFTER the inner provider's tenancy hooks so the order on each acquire is: tenancy push (under superuser) → SET ROLE → query runs under the restricted role with the right session vars. On release: RESET ROLE → tenancy reset.

func (*Provider) WireGorm

func (p *Provider) WireGorm(db *gorm.DB) error

WireGorm implements tenancy.Provider — delegates to the inner provider.

Jump to

Keyboard shortcuts

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