| 1 |
package Lemonldap::NG::Manager::Viewer; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use utf8; |
| 5 |
use Mouse; |
| 6 |
use Lemonldap::NG::Common::Conf::Constants; |
| 7 |
use Lemonldap::NG::Common::UserAgent; |
| 8 |
use URI::URL; |
| 9 |
|
| 10 |
extends 'Lemonldap::NG::Manager::Conf'; |
| 11 |
|
| 12 |
has diffRule => ( is => 'rw', default => sub { 0 } ); |
| 13 |
has brwRule => ( is => 'rw', default => sub { 0 } ); |
| 14 |
|
| 15 |
our $VERSION = '2.22.0'; |
| 16 |
|
| 17 |
############################# |
| 18 |
# I. INITIALIZATION METHODS # |
| 19 |
############################# |
| 20 |
|
| 21 |
use constant defaultRoute => 'viewer.html'; |
| 22 |
use constant icon => 'eye-open'; |
| 23 |
|
| 24 |
has ua => ( is => 'rw' ); |
| 25 |
|
| 26 |
sub init { |
| 27 |
my ( $self, $conf ) = @_; |
| 28 |
$self->ua( Lemonldap::NG::Common::UserAgent->new($conf) ); |
| 29 |
my $hd = "Lemonldap::NG::Handler::PSGI::Main"; |
| 30 |
|
| 31 |
# Parse Diff activation rule |
| 32 |
$conf->{viewerAllowDiff} //= 0; |
| 33 |
$self->logger->debug( |
| 34 |
"Diff activation rule -> " . ( $conf->{viewerAllowDiff} ) ); |
| 35 |
my $rule = $hd->buildSub( $hd->substitute( $conf->{viewerAllowDiff} ) ); |
| 36 |
unless ($rule) { |
| 37 |
$self->logger->error( |
| 38 |
"Bad Diff activation rule -> " . $hd->tsv->{jail}->error ); |
| 39 |
return 0; |
| 40 |
} |
| 41 |
$self->diffRule($rule); |
| 42 |
|
| 43 |
# Parse Browser activation rule |
| 44 |
$conf->{viewerAllowBrowser} //= 0; |
| 45 |
$self->logger->debug( |
| 46 |
"Browser activation rule -> " . ( $conf->{viewerAllowBrowser} ) ); |
| 47 |
$rule = $hd->buildSub( $hd->substitute( $conf->{viewerAllowBrowser} ) ); |
| 48 |
unless ($rule) { |
| 49 |
$self->logger->error( |
| 50 |
"Bad Browser activation rule -> " . $hd->tsv->{jail}->error ); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
$self->brwRule($rule); |
| 54 |
|
| 55 |
my $hiddenKeys = $conf->{viewerHiddenKeys} || ''; |
| 56 |
my @enabledKeys = (); |
| 57 |
my @keys = qw(virtualHosts samlIDPMetaDataNodes samlSPMetaDataNodes |
| 58 |
applicationList oidcOPMetaDataNodes oidcRPMetaDataNodes |
| 59 |
casSrvMetaDataNodes casAppMetaDataNodes sfExtra |
| 60 |
authChoiceModules grantSessionRules combModules |
| 61 |
openIdIDPList keyNodes); |
| 62 |
|
| 63 |
foreach (@keys) { |
| 64 |
|
| 65 |
# Ignore hidden ConfTree Primary Keys |
| 66 |
push @enabledKeys, $_ |
| 67 |
unless ( $hiddenKeys =~ /\b$_\b/ ); |
| 68 |
} |
| 69 |
|
| 70 |
# Forbid hidden keys |
| 71 |
foreach ( split /[,\s]+/, $hiddenKeys ) { |
| 72 |
$self->addRoute( |
| 73 |
view => { ':cfgNum' => { $_ => 'rejectKey' } }, |
| 74 |
['GET'] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
# HTML templates |
| 79 |
$self->addRoute( 'viewer.html', undef, ['GET'] ) |
| 80 |
->addRoute( 'viewDiff.html', undef, ['GET'] ) |
| 81 |
|
| 82 |
# READ |
| 83 |
# Special keys |
| 84 |
->addRoute( |
| 85 |
view => { |
| 86 |
':cfgNum' => \@enabledKeys |
| 87 |
}, |
| 88 |
['GET'] |
| 89 |
) |
| 90 |
|
| 91 |
# Difference between confs |
| 92 |
->addRoute( |
| 93 |
view => { diff => { ':conf1' => { ':conf2' => 'viewDiff' } } } ) |
| 94 |
|
| 95 |
# Other keys |
| 96 |
->addRoute( view => { ':cfgNum' => { '*' => 'viewKey' } }, ['GET'] ); |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
sub getConfByNum { |
| 101 |
my ( $self, $cfgNum, @args ) = @_; |
| 102 |
$self->SUPER::getConfByNum( $cfgNum, @args ); |
| 103 |
} |
| 104 |
|
| 105 |
sub viewDiff { |
| 106 |
my ( $self, $req, @path ) = @_; |
| 107 |
|
| 108 |
# Check Diff activation rule |
| 109 |
unless ( $self->diffRule->( $req, $req->{userData} ) ) { |
| 110 |
my $user = $req->{userData}->{_whatToTrace} || 'anonymous'; |
| 111 |
$self->auditLog( |
| 112 |
$req, |
| 113 |
code => "CONF_COMPARISON_REFUSED", |
| 114 |
message => ("$user is not authorized to compare configurations"), |
| 115 |
user => $user, |
| 116 |
); |
| 117 |
return $self->sendJSONresponse( $req, { 'value' => '_Hidden_' } ); |
| 118 |
} |
| 119 |
|
| 120 |
return $self->sendError( $req, 'to many arguments in path info', 400 ) |
| 121 |
if (@path); |
| 122 |
my @cfgNum = |
| 123 |
( scalar( $req->param('conf1') ), scalar( $req->param('conf2') ) ); |
| 124 |
my @conf; |
| 125 |
$self->logger->debug(" Loading confs"); |
| 126 |
|
| 127 |
# Load the 2 configurations |
| 128 |
for ( my $i = 0 ; $i < 2 ; $i++ ) { |
| 129 |
if ( %{ $self->currentConf } |
| 130 |
and $cfgNum[$i] == $self->currentConf->{cfgNum} ) |
| 131 |
{ |
| 132 |
$conf[$i] = $self->currentConf; |
| 133 |
} |
| 134 |
else { |
| 135 |
$conf[$i] = $self->confAcc->getConf( |
| 136 |
{ cfgNum => $cfgNum[$i], raw => 1, noCache => 1 } ); |
| 137 |
return $self->sendError( |
| 138 |
$req, |
| 139 |
"Configuration $cfgNum[$i] not available $Lemonldap::NG::Common::Conf::msg", |
| 140 |
400 |
| 141 |
) unless ( $conf[$i] ); |
| 142 |
} |
| 143 |
} |
| 144 |
require Lemonldap::NG::Manager::Conf::Diff; |
| 145 |
my @res = |
| 146 |
$self->Lemonldap::NG::Manager::Conf::Diff::diff( $conf[0], $conf[1] ); |
| 147 |
my $hiddenKeys = $self->{viewerHiddenKeys} || ''; |
| 148 |
$self->logger->debug("Deleting hidden Conf keys..."); |
| 149 |
foreach ( split /\s+/, $hiddenKeys ) { |
| 150 |
$self->logger->debug("-> Delete $_"); |
| 151 |
delete $res[0]->{$_}; |
| 152 |
delete $res[1]->{$_}; |
| 153 |
} |
| 154 |
return $self->sendJSONresponse( $req, [@res] ); |
| 155 |
} |
| 156 |
|
| 157 |
sub rejectKey { |
| 158 |
my ( $self, $req, @args ) = @_; |
| 159 |
return $self->sendJSONresponse( $req, { 'value' => '_Hidden_' } ); |
| 160 |
} |
| 161 |
|
| 162 |
sub viewKey { |
| 163 |
my ( $self, $req, @args ) = @_; |
| 164 |
$self->logger->debug("Viewer requested URI -> $req->{env}->{REQUEST_URI}"); |
| 165 |
|
| 166 |
# Check Browser activation rule |
| 167 |
if ( $self->brwRule->( $req, $req->{userData} ) ) { |
| 168 |
$self->logger->debug(" No restriction"); |
| 169 |
$self->SUPER::getKey( $req, @args ); |
| 170 |
} |
| 171 |
else { |
| 172 |
if ( $req->{env}->{REQUEST_URI} =~ m%/view/(?:latest|\d+/\w+)$% ) { |
| 173 |
$self->logger->debug(" $req->{env}->{REQUEST_URI} -> URI allowed"); |
| 174 |
$self->SUPER::getKey( $req, @args ); |
| 175 |
} |
| 176 |
else { |
| 177 |
$self->logger->debug( |
| 178 |
" $req->{env}->{REQUEST_URI} -> URI FORBIDDEN"); |
| 179 |
my $user = $req->{userData}->{_whatToTrace} || 'anonymous'; |
| 180 |
$self->auditLog( |
| 181 |
$req, |
| 182 |
code => "CONF_BROWSING_REFUSED", |
| 183 |
message => ("$user is not authorized to browse configurations"), |
| 184 |
user => $user, |
| 185 |
); |
| 186 |
$self->rejectKey( $req, @args ); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
1; |