| 1 | package DBIO::PostgreSQL::Diff::Policy; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL Row Level Security policies | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 103 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 103 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 103 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 103 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 103 | ▶ | use DBIO::PostgreSQL::Diff::NoStatement (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 103 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 103 | ▶ | use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified quote_role); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 103 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||||||
| 16 | table_key schema_name table_name policy_name policy_info | ||||||||||||||||||||||||||||||||
| 17 | )); | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # The schema_name / table_name pair of an operation, from the entries filed | ||||||||||||||||||||||||||||||||
| 27 | # under the table key. | ||||||||||||||||||||||||||||||||
| 28 | 20 | ▶ | sub _table_names { | ||||||||||||||||||||||||||||||
|
Subroutine
_table_names: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 20 | my ($table_key, @entries) = @_; | |||||||||||||||||||||||||||||||
| 30 | 20 | my ($table_name, $schema_name) = reverse | |||||||||||||||||||||||||||||||
| 31 | DBIO::PostgreSQL::Introspect->key_names($table_key, table_name => @entries); | ||||||||||||||||||||||||||||||||
| 32 | 20 | return (schema_name => $schema_name, table_name => $table_name); | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 19 | ▶ | sub diff { | ||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 36 | 19 | my ($class, $source_pol, $target_pol, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||
| 37 | 19 | my @ops; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # RLS enable/disable changes on tables | ||||||||||||||||||||||||||||||||
| 40 | 19 | for my $key (sort keys %$target_tables) { | |||||||||||||||||||||||||||||||
| 41 | # A table that is created in this diff starts without row level security. | ||||||||||||||||||||||||||||||||
| 42 | 29 | ▶ | my $src = $source_tables->{$key} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables->{$key} // {}
MC/DC100%1/1
$source_tables->{$key} // {}
$source_tables->{$key}
Truth table100%2/2
A
A= $source_tables->{$key}
| |||||||||||||||||||||||||||||||||
| 43 | 29 | my $tgt = $target_tables->{$key}; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 29 | ▶ | if (!$src->{rls_enabled} && $tgt->{rls_enabled}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $src->{"rls_enabled"} and $tgt->{"rls_enabled"}
MC/DC100%2/2
not $src->{"rls_enabled"} and $tgt->{"rls_enabled"}
not $src->{"rls_enabled"}
$tgt->{"rls_enabled"}
Truth table100%3/3
not(A) and B
A= not $src->{"rls_enabled"}B= $tgt->{"rls_enabled"}
| |||||||||||||||||||||||||||||||||
| 46 | 3 | push @ops, $class->new( | |||||||||||||||||||||||||||||||
| 47 | action => 'enable_rls', | ||||||||||||||||||||||||||||||||
| 48 | table_key => $key, | ||||||||||||||||||||||||||||||||
| 49 | _table_names($key, $tgt, $src), | ||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | ▶ | elsif ($src->{rls_enabled} && !$tgt->{rls_enabled}) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$src->{"rls_enabled"} and not $tgt->{"rls_enabled"}
MC/DC100%2/2
$src->{"rls_enabled"} and not $tgt->{"rls_enabled"}
$src->{"rls_enabled"}
not $tgt->{"rls_enabled"}
Truth table100%3/3
A and not(B)
A= $src->{"rls_enabled"}B= not $tgt->{"rls_enabled"}
| |||||||||||||||||||||||||||||||||
| 53 | 1 | push @ops, $class->new( | |||||||||||||||||||||||||||||||
| 54 | action => 'disable_rls', | ||||||||||||||||||||||||||||||||
| 55 | table_key => $key, | ||||||||||||||||||||||||||||||||
| 56 | _table_names($key, $tgt, $src), | ||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # Policy diffs | ||||||||||||||||||||||||||||||||
| 62 | 19 | for my $table_key (sort keys %$target_pol) { | |||||||||||||||||||||||||||||||
| 63 | 16 | ▶ | my $src_pols = $source_pol->{$table_key} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_pol->{$table_key} // {}
MC/DC100%1/1
$source_pol->{$table_key} // {}
$source_pol->{$table_key}
Truth table100%2/2
A
A= $source_pol->{$table_key}
| |||||||||||||||||||||||||||||||||
| 64 | 16 | my $tgt_pols = $target_pol->{$table_key}; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 16 | for my $name (sort keys %$tgt_pols) { | |||||||||||||||||||||||||||||||
| 67 | 16 | ▶ | if (!exists $src_pols->{$name}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | push @ops, $class->new( | ||||||||||||||||||||||||||||||||
| 69 | action => 'create', | ||||||||||||||||||||||||||||||||
| 70 | table_key => $table_key, | ||||||||||||||||||||||||||||||||
| 71 | _table_names($table_key, $tgt_pols->{$name}, $target_tables->{$table_key}), | ||||||||||||||||||||||||||||||||
| 72 | policy_name => $name, | ||||||||||||||||||||||||||||||||
| 73 | 11 | policy_info => $tgt_pols->{$name}, | |||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 19 | for my $table_key (sort keys %$source_pol) { | |||||||||||||||||||||||||||||||
| 80 | 10 | my $src_pols = $source_pol->{$table_key}; | |||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | my $tgt_pols = $target_pol->{$table_key} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_pol->{$table_key} // {}
MC/DC100%1/1
$target_pol->{$table_key} // {}
$target_pol->{$table_key}
Truth table100%2/2
A
A= $target_pol->{$table_key}
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 10 | for my $name (sort keys %$src_pols) { | |||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | next if exists $tgt_pols->{$name}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | push @ops, $class->new( | ||||||||||||||||||||||||||||||||
| 86 | action => 'drop', | ||||||||||||||||||||||||||||||||
| 87 | table_key => $table_key, | ||||||||||||||||||||||||||||||||
| 88 | _table_names($table_key, $src_pols->{$name}, $source_tables->{$table_key}), | ||||||||||||||||||||||||||||||||
| 89 | policy_name => $name, | ||||||||||||||||||||||||||||||||
| 90 | 5 | policy_info => $src_pols->{$name}, | |||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 19 | return @ops; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 65 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 100 | 65 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 101 | 65 | my $table = quote_qualified( | |||||||||||||||||||||||||||||||
| 102 | DBIO::PostgreSQL::Introspect->key_names($self->table_key, table_name => $self)); | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 65 | ▶ | if ($self->action eq 'enable_rls') { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 105 | 3 | return sprintf 'ALTER TABLE %s ENABLE ROW LEVEL SECURITY;', $table; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | elsif ($self->action eq 'disable_rls') { | ||||||||||||||||||||||||||||||||
| 108 | 1 | return sprintf 'ALTER TABLE %s DISABLE ROW LEVEL SECURITY;', $table; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | elsif ($self->action eq 'create') { | ||||||||||||||||||||||||||||||||
| 111 | 33 | my $info = $self->policy_info; | |||||||||||||||||||||||||||||||
| 112 | 33 | my $sql = sprintf 'CREATE POLICY %s ON %s', quote_ident($self->policy_name), $table; | |||||||||||||||||||||||||||||||
| 113 | 33 | ▶ | $sql .= ' AS RESTRICTIVE' if defined $info->{permissive} && !$info->{permissive}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $info->{"permissive"} and not $info->{"permissive"}
MC/DC100%2/2
defined $info->{"permissive"} and not $info->{"permissive"}
defined $info->{"permissive"}
not $info->{"permissive"}
Truth table100%3/3
A and not(B)
A= defined $info->{"permissive"}B= not $info->{"permissive"}
| |||||||||||||||||||||||||||||||||
| 114 | 33 | ▶ | $sql .= sprintf ' FOR %s', $info->{command} if $info->{command} && $info->{command} ne 'ALL'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$info->{"command"} and $info->{"command"} ne "ALL"
MC/DC100%2/2
$info->{"command"} and $info->{"command"} ne "ALL"
$info->{"command"}
$info->{"command"} ne "ALL"
Truth table100%3/3
A and B
A= $info->{"command"}B= $info->{"command"} ne "ALL"
| |||||||||||||||||||||||||||||||||
| 115 | 33 | ▶ | if ($info->{roles} && @{ $info->{roles} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"roles"} and @{$info->{"roles"};}
MC/DC50%1/2
$info->{"roles"} and @{$info->{"roles"};}
$info->{"roles"}
@{$info->{"roles"};}
Truth table66%2/3
A and B
A= $info->{"roles"}B= @{$info->{"roles"};}
| |||||||||||||||||||||||||||||||||
| 116 | 3 | $sql .= sprintf ' TO %s', join(', ', map { quote_role($_) } @{ $info->{roles} }); | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | 33 | ▶ | $sql .= sprintf ' USING (%s)', $info->{using_expr} if $info->{using_expr}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 33 | ▶ | $sql .= sprintf ' WITH CHECK (%s)', $info->{check_expr} if $info->{check_expr}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 33 | return "$sql;"; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | elsif ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||
| 123 | 27 | return sprintf 'DROP POLICY %s ON %s;', quote_ident($self->policy_name), $table; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | DBIO::PostgreSQL::Diff::NoStatement->throw( | ||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | $self, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->table_key // ""
MC/DC0%0/1
$self->table_key // ""
$self->table_key
Truth table50%1/2
A
A= $self->table_key
| |||||||||||||||||||||||||||||||||
| 127 | ( defined $self->policy_name ? 'policy ' . $self->policy_name . ' on ' : 'table ' ) | ||||||||||||||||||||||||||||||||
| 128 | . ($self->table_key // ''), | ||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 134 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | if ($self->action =~ /rls/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 0 | return sprintf ' %s: RLS on %s', $self->action, $self->table_key; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 2 | return sprintf ' %spolicy: %s on %s', $prefix, $self->policy_name, $self->table_key; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 103 | 1; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||||