| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Role::Acl; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Backend::PgSQL::Role::Base'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Role::Acl - FusqlFS class to expose PostgreSQL | ||
| 12 | artifact's permissions | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | package FusqlFS::Backend::PgSQL::Tables; | ||
| 17 | use parent 'FusqlFS::Artifact'; | ||
| 18 | |||
| 19 | use FusqlFS::Backend::PgSQL::Role::Acl; | ||
| 20 | |||
| 21 | sub init | ||
| 22 | { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | # initialize class | ||
| 26 | |||
| 27 | $self->{acl} = FusqlFS::Backend::PgSQL::Role::Acl->new('r'); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub get | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my ($name) = @_; | ||
| 34 | my $result = {}; | ||
| 35 | |||
| 36 | # load structures into $result | ||
| 37 | |||
| 38 | $result->{acl} = $self->{acl}; | ||
| 39 | return $result; | ||
| 40 | } | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This class exposes PostgreSQL artifact's permissions (a.k.a. ACL) as a | ||
| 45 | directory with subdirectories named after roles with marker files named after | ||
| 46 | permissions. It is best used with plugged in L<FusqlFS::Backend::PgSQL::Roles> | ||
| 47 | module (see L<FusqlFS::Backend::Base> for more info on plugging in different | ||
| 48 | modules). | ||
| 49 | |||
| 50 | The class's C<new> constructor accepts single char argument designating | ||
| 51 | type of artifact the owner of which is to be exposed. Possible values can | ||
| 52 | be seen in L<FusqlFS::Backend::PgSQL::Role::Base> module. | ||
| 53 | |||
| 54 | =head1 EXPOSED STRUCTURE | ||
| 55 | |||
| 56 | First level of exposed files are subdirectories named after roles, e.g. if a | ||
| 57 | table has perms granted to roles C<user1> and C<user2> this module will expose | ||
| 58 | subdirectories F<./user1> and F<./user2>. | ||
| 59 | |||
| 60 | Removing such subdirectory revokes all permissions from the role, creating | ||
| 61 | subdirectory with some role's name grants all permission to the role. | ||
| 62 | |||
| 63 | Every such subdirectory has following structure: | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item F<./granter> | ||
| 68 | |||
| 69 | Symlink to role in F<../../../../roles> which granted current role its permissions. | ||
| 70 | |||
| 71 | =item F<./role> | ||
| 72 | |||
| 73 | Symlink to current role in F<../../../../roles> (i.e. the role with the name | ||
| 74 | equal to current subdirectory's name). | ||
| 75 | |||
| 76 | =item F<./insert>, F<./update>, F<./delete>, F<./references>, F<./trigger>, F<./usage> | ||
| 77 | |||
| 78 | Plain files to designated correspondent permission is granted. Remove some of | ||
| 79 | the files to revoke the permission or create new file with one of the names | ||
| 80 | (e.g. with C<touch ./insert>) to grant such permission. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | our %relperms = ( | ||
| 87 | r => [ qw(select insert update delete truncate references trigger) ], | ||
| 88 | S => [ qw(usage select update) ], | ||
| 89 | _F => [ qw(execute) ], | ||
| 90 | _L => [ qw(usage) ], | ||
| 91 | ); | ||
| 92 | |||
| 93 | our %aclmap = qw( | ||
| 94 | select r | ||
| 95 | insert a | ||
| 96 | update w | ||
| 97 | delete d | ||
| 98 | truncate D | ||
| 99 | references x | ||
| 100 | trigger t | ||
| 101 | execute X | ||
| 102 | usage U | ||
| 103 | create C | ||
| 104 | connect c | ||
| 105 | temporary T | ||
| 106 | ); | ||
| 107 | |||
| 108 | sub init | ||
| 109 | { | ||
| 110 | my $self = shift; | ||
| 111 | my $relkind = shift; | ||
| 112 | |||
| 113 | my @kind = $self->kind($relkind); | ||
| 114 | |||
| 115 | $self->{perms} = $relperms{$relkind}; | ||
| 116 | $self->{get_expr} = $self->expr('SELECT %2$sacl FROM pg_catalog.%3$s WHERE %4$s = ? %5$s', @kind); | ||
| 117 | $self->{grant_expr} = sprintf('GRANT %%s ON %1$s %%s TO %%s', @kind); | ||
| 118 | $self->{revoke_expr} = sprintf('REVOKE %%s ON %1$s %%s FROM %%s', @kind); | ||
| 119 | $self->{create_expr} = sprintf('GRANT ALL PRIVILEGES ON %1$s %%s TO %%s', @kind); | ||
| 120 | $self->{drop_expr} = sprintf('REVOKE ALL PRIVILEGES ON %1$s %%s FROM %%s', @kind); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub get | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | my ($role, $name) = reverse @_; | ||
| 127 | my $acl = $self->all_col($self->{get_expr}, $name); | ||
| 128 | return unless $acl && @$acl; | ||
| 129 | |||
| 130 | my $rolep = $role eq 'public'? '': $role; | ||
| 131 | my @acl = split /[=\/]/, (grep /^$rolep=/, @{$acl->[0]})[0]; | ||
| 132 | return unless @acl; | ||
| 133 | |||
| 134 | return { granter => \"roles/$acl[2]", role => \"roles/$acl[0]", map { $_ => 1 } grep $acl[1] =~ /$aclmap{$_}/, @{$self->{perms}} }; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub list | ||
| 138 | { | ||
| 139 | my $self = shift; | ||
| 140 | my $name = pop; | ||
| 141 | my $acl = $self->all_col($self->{get_expr}, $name); | ||
| 142 | return unless $acl && @$acl; | ||
| 143 | return [ map { (split(/=/, $_))[0]||'public' } @{$acl->[0]} ]; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub store | ||
| 147 | { | ||
| 148 | my $self = shift; | ||
| 149 | my $perms = $self->validate(pop, { map { '-'.$_ => '' } @{$self->{perms}} }) or return; | ||
| 150 | my @newacl = keys %$perms; | ||
| 151 | my ($role, $name) = reverse @_; | ||
| 152 | |||
| 153 | my $acl = $self->all_col($self->{get_expr}, $name); | ||
| 154 | return unless $acl && @$acl; | ||
| 155 | |||
| 156 | my $rolep = $role eq 'public'? '': $role; | ||
| 157 | my $oldacl = (split /[=\/]/, (grep /^$rolep=/, @{$acl->[0]})[0])[1]; | ||
| 158 | return unless $oldacl; | ||
| 159 | my @oldacl = grep $oldacl =~ /$aclmap{$_}/, @{$self->{perms}}; | ||
| 160 | |||
| 161 | my ($grant, $revoke) = $self->adiff(\@oldacl, \@newacl); | ||
| 162 | $self->do($self->{revoke_expr}, [$_, $name, $role]) foreach @$revoke; | ||
| 163 | $self->do($self->{grant_expr}, [$_, $name, $role]) foreach @$grant; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub create | ||
| 167 | { | ||
| 168 | my $self = shift; | ||
| 169 | my ($role, $name) = reverse @_; | ||
| 170 | $self->do($self->{create_expr}, [$name, $role]); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub drop | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my ($role, $name) = reverse @_; | ||
| 177 | $self->do($self->{drop_expr}, [$name, $role]); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub rename | ||
| 181 | { | ||
| 182 | my $self = shift; | ||
| 183 | my ($newrole, $role, $name) = reverse @_; | ||
| 184 | my $acl = $self->get($name, $role); | ||
| 185 | $self->drop($name, $role) and $self->store($name, $newrole, $acl); | ||
| 186 | } | ||
| 187 | |||
| 188 | 1; | ||
| 189 |