| 1 | package Lemonldap::NG::Handler::Lib::DevOps; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Lemonldap::NG::Common::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Lemonldap::NG::Common::Util qw(isHiddenAttr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use JSON qw(from_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '2.23.0'; | |||||||||||||||||||||||||||
| 9 | 3 | our $_ua; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | sub ua { | ||||||||||||||||||||||||||
|
Subroutine
ua: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 4 | my ($class) = @_; | |||||||||||||||||||||||||||
| 13 | 4 | ▶ | return $_ua if $_ua; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | $_ua = Lemonldap::NG::Common::UserAgent->new( { | ||||||||||||||||||||||||||||
| 15 | lwpOpts => $class->tsv->{lwpOpts}, | ||||||||||||||||||||||||||||
| 16 | lwpSslOpts => $class->tsv->{lwpSslOpts} | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | 3 | ); | |||||||||||||||||||||||||||
| 19 | 3 | return $_ua; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | sub checkMaintenanceMode { | ||||||||||||||||||||||||||
|
Subroutine
checkMaintenanceMode: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 14 | my ( $class, $req ) = @_; | |||||||||||||||||||||||||||
| 24 | 14 | my $vhost = $class->resolveAlias($req); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 14 | ▶ | unless ($vhost) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | $class->logger->error('No VHost provided'); | |||||||||||||||||||||||||||
| 28 | 0 | return $class->Lemonldap::NG::Handler::Main::checkMaintenanceMode($req); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 14 | $class->logger->info("DevOps request from $vhost"); | |||||||||||||||||||||||||||
| 32 | 14 | ▶ | $class->tsv->{lastVhostUpdate} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class->tsv->{"lastVhostUpdate"} //= {}
MC/DC100%1/1
$class->tsv->{"lastVhostUpdate"} //= {}
$class->tsv->{"lastVhostUpdate"}
Truth table100%2/2
A
A= $class->tsv->{"lastVhostUpdate"}
| |||||||||||||||||||||||||||||
| 33 | $class->_loadVhostConfig( $req, $vhost ) | ||||||||||||||||||||||||||||
| 34 | unless ( | ||||||||||||||||||||||||||||
| 35 | $class->tsv->{defaultCondition}->{$vhost} | ||||||||||||||||||||||||||||
| 36 | and ( | ||||||||||||||||||||||||||||
| 37 | 14 | ▶ | time() - $class->tsv->{lastVhostUpdate}->{$vhost} < | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$class->tsv->{"defaultCondition"}{$vhost} and &CORE::GLOBAL::time() - $class->tsv->{"lastVhostUpdate"}{$vhost} < $class->checkTime
MC/DC100%2/2
$class->tsv->{"defaultCondition"}{$vhost} and &CORE::GLOBAL::time() - $class->tsv->{"lastVhostUpdate"}{$vhost} < $class->checkTime
$class->tsv->{"defaultCondition"}{$vhost}
&CORE::GLOBAL::time() - $class->tsv->{"lastVhostUpdate"}{$vhost} < $class->checkTime
Truth table100%3/3
A and B
A= $class->tsv->{"defaultCondition"}{$vhost}B= &CORE::GLOBAL::time() - $class->tsv->{"lastVhostUpdate"}{$vhost} < $class->checkTime
| |||||||||||||||||||||||||||||
| 38 | $class->checkTime ) | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 14 | return $class->Lemonldap::NG::Handler::Main::checkMaintenanceMode($req); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub _loadVhostConfig { | ||||||||||||||||||||||||||
|
Subroutine
_loadVhostConfig: called CC=14 SCAR=27.1
| |||||||||||||||||||||||||||||
| 45 | 4 | my ( $class, $req, $vhost ) = @_; | |||||||||||||||||||||||||||
| 46 | 4 | my ( $json, $rUrl, $rVhost ); | |||||||||||||||||||||||||||
| 47 | 4 | ▶ | if ( $class->tsv->{useSafeJail} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | if ( $req->env->{RULES_URL} || $class->tsv->{devOpsRulesUrl}->{$vhost} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$req->env->{"RULES_URL"} or $class->tsv->{"devOpsRulesUrl"}{$vhost}
MC/DC100%2/2
$req->env->{"RULES_URL"} or $class->tsv->{"devOpsRulesUrl"}{$vhost}
$req->env->{"RULES_URL"}
$class->tsv->{"devOpsRulesUrl"}{$vhost}
Truth table100%3/3
A or B
A= $req->env->{"RULES_URL"}B= $class->tsv->{"devOpsRulesUrl"}{$vhost}
| |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | $rUrl = $req->{env}->{RULES_URL} | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | || $class->tsv->{devOpsRulesUrl}->{$vhost}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$req->{'env'}{'RULES_URL'} || $class->tsv->{'devOpsRulesUrl'}{$vhost}
MC/DC0%0/2
$req->{'env'}{'RULES_URL'} || $class->tsv->{'devOpsRulesUrl'}{$vhost}
$req->{'env'}{'RULES_URL'}
$class->tsv->{'devOpsRulesUrl'}{$vhost}
Truth table66%2/3
A || B
A= $req->{'env'}{'RULES_URL'}B= $class->tsv->{'devOpsRulesUrl'}{$vhost}
| |||||||||||||||||||||||||||||
| 52 | 3 | $rVhost = ( $rUrl =~ m#^https?://([^/]*).*# )[0]; | |||||||||||||||||||||||||||
| 53 | 3 | $rVhost =~ s/:\d+$//; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | $rUrl = | ||||||||||||||||||||||||||||
| 57 | ( $class->localConfig->{loopBackUrl} | ||||||||||||||||||||||||||||
| 58 | || "http://127.0.0.1:" . $req->{env}->{SERVER_PORT} ) | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | . '/rules.json'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$class->localConfig->{'loopBackUrl'} || 'http://127.0.0.1:' . $req->{'env'}{'SERVER_PORT'}
MC/DC0%0/1
$class->localConfig->{'loopBackUrl'} || 'http://127.0.0.1:' . $req->{'env'}{'SERVER_PORT'}
$class->localConfig->{'loopBackUrl'}
Truth table50%1/2
A
A= $class->localConfig->{'loopBackUrl'}
| |||||||||||||||||||||||||||||
| 60 | 1 | $rVhost = $vhost; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | $class->logger->debug("Try to retrieve rules file from $rUrl"); | |||||||||||||||||||||||||||
| 64 | 4 | my $get = HTTP::Request->new( GET => $rUrl ); | |||||||||||||||||||||||||||
| 65 | 4 | $class->logger->debug("Set Host header with $rVhost"); | |||||||||||||||||||||||||||
| 66 | 4 | $get->header( Host => $rVhost ); | |||||||||||||||||||||||||||
| 67 | 4 | my $resp = $class->ua->request($get); | |||||||||||||||||||||||||||
| 68 | 4 | ▶ | if ( $resp->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 4 | $class->logger->debug('Response is success'); | |||||||||||||||||||||||||||
| 70 | 4 | eval { $json = from_json( $resp->content, { allow_nonref => 1 } ); }; | |||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $class->logger->debug('Bad json file received'); | |||||||||||||||||||||||||||
| 73 | 0 | $class->logger->error( | |||||||||||||||||||||||||||
| 74 | "Bad rules file retrieved from $rUrl for $vhost, skipping ($@)" | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 4 | $class->logger->debug('Good json file received'); | |||||||||||||||||||||||||||
| 79 | 4 | $class->logger->info( | |||||||||||||||||||||||||||
| 80 | "Compiling rules retrieved from $rUrl for $vhost"); | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 0 | $class->logger->error( | |||||||||||||||||||||||||||
| 85 | "Unable to retrieve rules file from $rUrl -> " | ||||||||||||||||||||||||||||
| 86 | . $resp->status_line ); | ||||||||||||||||||||||||||||
| 87 | 0 | $class->logger->info("Default rule and header are employed"); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 0 | $class->logger->error( | |||||||||||||||||||||||||||
| 92 | q"I refuse to compile 'rules.json' when useSafeJail isn't activated! Yes I know, I'm a coward..." | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | $json->{rules} ||= { default => 1 }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$json->{"rules"} ||= {"default", 1}
MC/DC0%0/1
$json->{"rules"} ||= {"default", 1}
$json->{"rules"}
Truth table50%1/2
A
A= $json->{"rules"}
| |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | $json->{headers} //= { 'Auth-User' => '$uid' }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$json->{"headers"} //= {"Auth-User", "\$uid"}
MC/DC0%0/1
$json->{"headers"} //= {"Auth-User", "\$uid"}
$json->{"headers"}
Truth table50%1/2
A
A= $json->{"headers"}
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Removed hidden session attributes | ||||||||||||||||||||||||||||
| 99 | 4 | foreach ( keys %{ $json->{headers} } ) { | |||||||||||||||||||||||||||
| 100 | 12 | my $header = $json->{headers}->{$_}; | |||||||||||||||||||||||||||
| 101 | 12 | $header =~ s/^\$//; | |||||||||||||||||||||||||||
| 102 | 12 | ▶ | if ( isHiddenAttr( $class->localConfig, $header ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 8 | delete $json->{headers}->{$_}; | |||||||||||||||||||||||||||
| 104 | 8 | $class->auditLog( | |||||||||||||||||||||||||||
| 105 | $req, | ||||||||||||||||||||||||||||
| 106 | message => ( | ||||||||||||||||||||||||||||
| 107 | "DevOps handler: $vhost tried to retrieve a hidden attribute -> $header" | ||||||||||||||||||||||||||||
| 108 | ), | ||||||||||||||||||||||||||||
| 109 | code => "HANDLER_DEVOPS_HIDDEN_ATTRIBUTE_REQUESTED", | ||||||||||||||||||||||||||||
| 110 | vhost => $vhost, | ||||||||||||||||||||||||||||
| 111 | attribute => $header, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | $class->logger->debug("DevOps handler called by $vhost"); | |||||||||||||||||||||||||||
| 117 | 4 | $class->locationRulesInit( undef, { $vhost => $json->{rules} } ); | |||||||||||||||||||||||||||
| 118 | 4 | $class->headersInit( undef, { $vhost => $json->{headers} } ); | |||||||||||||||||||||||||||
| 119 | 4 | $class->tsv->{lastVhostUpdate}->{$vhost} = time; | |||||||||||||||||||||||||||
| 120 | $class->tsv->{https}->{$vhost} = uc $req->env->{HTTPS_REDIRECT} eq 'ON' | ||||||||||||||||||||||||||||
| 121 | 4 | ▶ | if exists $req->env->{HTTPS_REDIRECT}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | $class->tsv->{port}->{$vhost} = $req->env->{PORT_REDIRECT} | ||||||||||||||||||||||||||||
| 123 | 4 | ▶ | if exists $req->env->{PORT_REDIRECT}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 4 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||||||||||