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
- func CreateRole(ctx context.Context, dsn string) error
- func GrantAll(ctx context.Context, dsn string) error
- type Provider
- func (p *Provider) Capabilities() tenancy.Capabilities
- func (p *Provider) Enable()
- func (p *Provider) Install(ctx context.Context, db *gorm.DB, models []tenancy.ModelInfo) error
- func (p *Provider) Name() string
- func (p *Provider) WireAdapter(adapter dialect.DialectAdapter) error
- func (p *Provider) WireGorm(db *gorm.DB) error
Constants ¶
const Role = "rls_test_user"
Role is the non-superuser role name used in tests.
Variables ¶
This section is empty.
Functions ¶
func CreateRole ¶
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.
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 (*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) 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.