| 1 | package DBIO::PostgreSQL::Introspect::Policies; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL Row Level Security policies | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 15 | SELECT | ||||||||||||||||||||||||||||
| 16 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 17 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 18 | p.polname AS policy_name, | ||||||||||||||||||||||||||||
| 19 | CASE p.polcmd | ||||||||||||||||||||||||||||
| 20 | WHEN 'r' THEN 'SELECT' | ||||||||||||||||||||||||||||
| 21 | WHEN 'a' THEN 'INSERT' | ||||||||||||||||||||||||||||
| 22 | WHEN 'w' THEN 'UPDATE' | ||||||||||||||||||||||||||||
| 23 | WHEN 'd' THEN 'DELETE' | ||||||||||||||||||||||||||||
| 24 | WHEN '*' THEN 'ALL' | ||||||||||||||||||||||||||||
| 25 | END AS command, | ||||||||||||||||||||||||||||
| 26 | p.polpermissive AS permissive, | ||||||||||||||||||||||||||||
| 27 | pg_catalog.pg_get_expr(p.polqual, p.polrelid) AS using_expr, | ||||||||||||||||||||||||||||
| 28 | pg_catalog.pg_get_expr(p.polwithcheck, p.polrelid) AS check_expr, | ||||||||||||||||||||||||||||
| 29 | array_agg(r.rolname) FILTER (WHERE r.rolname IS NOT NULL) AS roles | ||||||||||||||||||||||||||||
| 30 | FROM pg_catalog.pg_policy p | ||||||||||||||||||||||||||||
| 31 | JOIN pg_catalog.pg_class c ON c.oid = p.polrelid | ||||||||||||||||||||||||||||
| 32 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 33 | LEFT JOIN pg_catalog.pg_roles r ON r.oid = ANY(p.polroles) | ||||||||||||||||||||||||||||
| 34 | WHERE 1=1 | ||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my @bind; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($filter && @$filter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filter and @$filter
MC/DC0%0/2
$filter and @$filter
$filter
@$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
| |||||||||||||||||||||||||||||
| 40 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 41 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $sql .= q{ | |||||||||||||||||||||||||||
| 45 | GROUP BY n.nspname, c.relname, p.polname, p.polcmd, | ||||||||||||||||||||||||||||
| 46 | p.polpermissive, p.polqual, p.polwithcheck, p.polrelid | ||||||||||||||||||||||||||||
| 47 | ORDER BY n.nspname, c.relname, p.polname | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 51 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my %policies; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 56 | $policies{$table_key}{ $row->{policy_name} } = { | ||||||||||||||||||||||||||||
| 57 | policy_name => $row->{policy_name}, | ||||||||||||||||||||||||||||
| 58 | command => $row->{command}, | ||||||||||||||||||||||||||||
| 59 | permissive => $row->{permissive} ? 1 : 0, | ||||||||||||||||||||||||||||
| 60 | using_expr => $row->{using_expr}, | ||||||||||||||||||||||||||||
| 61 | check_expr => $row->{check_expr}, | ||||||||||||||||||||||||||||
| 62 | roles => $row->{roles}, | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return \%policies; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 15 | 1; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =pod | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 NAME | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | DBIO::PostgreSQL::Introspect::Policies - Introspect PostgreSQL Row Level Security policies | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 VERSION | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | version 0.900001 | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Fetches Row Level Security policy metadata from C<pg_catalog.pg_policy>. | ||||||||||||||||||||||||||||
| 88 | Policy command types and USING/WITH CHECK expressions are decoded from | ||||||||||||||||||||||||||||
| 89 | PostgreSQL's internal representation. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 fetch | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my $policies = DBIO::PostgreSQL::Introspect::Policies->fetch($dbh, $filter); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Returns a hashref keyed by C<schema.table>. Each value is a hashref keyed by | ||||||||||||||||||||||||||||
| 98 | policy name. Each policy entry has: C<policy_name>, C<command> (C<SELECT>, | ||||||||||||||||||||||||||||
| 99 | C<INSERT>, C<UPDATE>, C<DELETE>, or C<ALL>), C<permissive>, C<using_expr>, | ||||||||||||||||||||||||||||
| 100 | C<check_expr>, C<roles> (ArrayRef of role names, or undef for all roles). | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 109 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 110 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 113 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||