Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tests/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/flanksource/duty/api"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/tests/fixtures/dummy"
"github.com/flanksource/duty/types"
Expand Down Expand Up @@ -168,7 +169,8 @@ var _ = ginkgo.Describe("View Tests", ginkgo.Serial, ginkgo.Ordered, func() {
WHERE relname = ? AND relkind = 'r'
`, testTableName).Scan(&rlsEnabled).Error
Expect(err).ToNot(HaveOccurred())
Expect(rlsEnabled).To(BeTrue(), "RLS should be enabled on view table")
applyRLS := api.DefaultConfig.EnableRLS && !api.DefaultConfig.DisableRLS
Expect(rlsEnabled).To(Equal(applyRLS), "RLS state should match config")
})

ginkgo.It("should create view_grants_policy on view table", func() {
Expand All @@ -180,7 +182,8 @@ var _ = ginkgo.Describe("View Tests", ginkgo.Serial, ginkgo.Ordered, func() {
)
`, testTableName).Scan(&policyExists).Error
Expect(err).ToNot(HaveOccurred())
Expect(policyExists).To(BeTrue(), "view_grants_policy should exist on view table")
applyRLS := api.DefaultConfig.EnableRLS && !api.DefaultConfig.DisableRLS
Expect(policyExists).To(Equal(applyRLS), "view_grants_policy should match RLS state")
})
})

Expand Down
7 changes: 5 additions & 2 deletions view/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ func applyViewTableSchema(ctx context.Context, tableName string, columns ViewCol
// Apply RLS policy to enforce grants
// (Re)apply RLS and Policy on first table creation or on schema changes
if len(changes) > 0 {
if err := ensureViewRLSPolicy(ctx, tableName); err != nil {
return fmt.Errorf("failed to apply RLS policy: %w", err)
applyRLS := api.DefaultConfig.EnableRLS && !api.DefaultConfig.DisableRLS
if applyRLS {
if err := ensureViewRLSPolicy(ctx, tableName); err != nil {
return fmt.Errorf("failed to apply RLS policy: %w", err)
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions views/9999_rls_disable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ BEGIN
IF (SELECT relrowsecurity FROM pg_class WHERE relname = 'view_panels') THEN
EXECUTE 'ALTER TABLE view_panels DISABLE ROW LEVEL SECURITY;';
END IF;

END $$;

-- Disable RLS on dynamically generated view tables
DO $$
DECLARE
r record;
BEGIN
FOR r IN
SELECT c.relname, c.relrowsecurity
FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = 'public' AND c.relkind = 'r' AND c.relname LIKE 'view\_%' ESCAPE '\'
LOOP
IF r.relrowsecurity THEN
EXECUTE format('ALTER TABLE %I DISABLE ROW LEVEL SECURITY;', r.relname);
END IF;
END LOOP;
END $$;

-- POLICIES
Expand All @@ -74,3 +92,18 @@ DROP POLICY IF EXISTS checks_auth ON checks;
DROP POLICY IF EXISTS views_auth ON views;

DROP POLICY IF EXISTS view_panels_auth ON view_panels;

DO $$
DECLARE
r record;
BEGIN
-- Drop policies on dynamically generated view tables
FOR r IN
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = 'public' AND c.relkind = 'r' AND c.relname LIKE 'view\_%' ESCAPE '\'
LOOP
EXECUTE format('DROP POLICY IF EXISTS view_grants_policy ON %I;', r.relname);
END LOOP;
END $$;