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) AllowGlobalServices() []string
- func (p *Provider) Capabilities() tenancy.Capabilities
- func (p *Provider) Enable()
- func (p *Provider) EnrollmentStrict() bool
- func (p *Provider) Inner() *tenpg.Provider
- func (p *Provider) Install(ctx context.Context, db *gorm.DB, models []tenancy.ModelInfo) error
- func (p *Provider) Name() string
- func (p *Provider) SecurityMode() tenancy.SecurityMode
- func (p *Provider) SetAllowGlobalServices(names ...string)
- func (p *Provider) SetEnrollmentStrict(strict bool)
- func (p *Provider) SetSecurityMode(m tenancy.SecurityMode)
- 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 New ¶
New returns a fresh wrapper. Optional tenpg.Option values configure the inner Postgres provider (security mode, allowlist, etc.).
func (*Provider) AllowGlobalServices ¶ added in v2.0.13
AllowGlobalServices implements tenancy.AllowGlobalAware.
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) EnrollmentStrict ¶ added in v2.0.13
EnrollmentStrict forwards to the inner provider.
func (*Provider) Inner ¶ added in v2.0.13
Inner returns the wrapped Postgres provider for mode/allowlist injection.
func (*Provider) SecurityMode ¶ added in v2.0.13
func (p *Provider) SecurityMode() tenancy.SecurityMode
SecurityMode implements tenancy.ModeAware.
func (*Provider) SetAllowGlobalServices ¶ added in v2.0.13
SetAllowGlobalServices implements tenancy.AllowGlobalAware.
func (*Provider) SetEnrollmentStrict ¶ added in v2.0.13
SetEnrollmentStrict forwards to the inner provider.
func (*Provider) SetSecurityMode ¶ added in v2.0.13
func (p *Provider) SetSecurityMode(m tenancy.SecurityMode)
SetSecurityMode implements tenancy.ModeAware when the inner does.
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.