blib/lib/Clustericious/Plugin/PlugAuth.pm

filter: Statementstmt 27.6% 31 / 112 Branchbran 10.7% 3 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 27.1% 45 / 166 CC 17 SCAR 28.0
CC
17
Cov
23%
__ANON__
42.0
__ANON__
20.0
__ANON__
20.0
CRAP 16.4
« blib/lib/Clustericious/Command/nginx.pm ↑ Summary blib/lib/Clustericious/Command/apache.pm »
1 package Clustericious::Plugin::PlugAuth;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Clustericious::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Mojo::ByteStream qw/b/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use JSON::MaybeXS qw( encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Plugin for clustericious to use PlugAuth.
12 8 our $VERSION = '1.29'; # VERSION
13  
14  
15 8 has 'config_url';
16  
17 7 ▶sub register {
Subroutine register: called CC=3 SCAR=11.3
Pod documented
18 7 my ($self, $app, $conf) = @_;
19 7 my $url = eval { $conf->{plug_auth}->url(default => '') };
20 7 ▶ $url =~ s{/$}{} if defined $url;
Branch: 1 branch
BranchTrueFalseTotal
if defined $url 1 6 7
21 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
22 0 WARN "unable to determine PlugAuth URL: $@";
23 0 return $self;
24 }
25 7 $self->config_url($url);
26 7 $self;
27 }
28  
29  
30 0 ▶sub authenticate {
Subroutine authenticate: not called CC=2 SCAR=17.9
Pod documented
31 0 my $self = shift;
32 0 my $c = shift;
33 0 my $realm = shift;
34  
35 0 TRACE ("Authenticating for realm $realm");
36 # Everyone needs to send an authorization header
37 0 ▶ my $auth = $c->req->headers->authorization or do {
Branch: 1 branch
BranchTrueFalseTotal
my $auth = $c->req->headers->authorization or do { $c->res->headers->www_authenticate(qq[Basic realm="$realm"]); $c->render("text", "auth required", "layout", "", "status", 401); return } 0 0 0
38 0 $c->res->headers->www_authenticate(qq[Basic realm="$realm"]);
39 0 $c->render(text => "auth required", layout => "", status => 401);
40 0 return;
41 };
42  
43 0 my $config_url = $self->config_url;
44  
45 0 my ($method,$str) = split / /,$auth;
46 0 my $userinfo = b($str)->b64_decode;
47 0 my ($user,$pw) = split /:/, $userinfo;
48  
49 0 my $self_plug_auth = 0;
50  
51 # VIP treatment for some hosts
52 0 my $ip = $c->tx->remote_address;
53 0 ▶ $c->ua->get("$config_url/host/$ip/trusted", sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
54 0 my $ua = shift;
55
56 0 do {
57 0 my $tx = shift;
58 0 ▶ if ( my $res = $tx->success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $tx->success) { } 0 0 0
59 0 ▶ if ( $res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) { } 0 0 0
60 0 TRACE "Host $ip is trusted, not authenticating";
61 0 $c->stash( user => $user );
62 0 $c->continue;
63 0 return;
64 }
65 else {
66 0 WARN "PlugAuth returned code " . $res->code;
67 }
68 } else {
69 0 my $error = $tx->error;
70 0 ▶ if ($error->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{'code'}) { } 0 0 0
71 0 TRACE "Not VIP $config_url/host/$ip/trusted : @{[ $error->{code} ]} @{[ $error->{message} ]}";
72 } else {
73 0 WARN "Error connecting to PlugAuth at $config_url/host/$ip/trusted : @{[ encode_json $error ]}";
74 }
75 }
76 };
77  
78 # Everyone else get in line
79 0 my $auth_url = Mojo::URL->new("$config_url/auth");
80 0 $auth_url->userinfo($userinfo);
81  
82 0 my $check;
83 my $res;
84  
85 0 ▶ $ua->head($auth_url, sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
86
87 0 my($ua, $tx) = @_;
88  
89 0 $res = $tx->res;
90 0 $check = $res->code();
91  
92 0 ▶ if(!defined $check || $check == 503) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $check or $check == 503) 0 0 0
Condition0%0/3
not defined $check or $check == 503
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $check or $check == 503
not defined $check $check == 503
Truth table0%0/3
not(A) or B
A= not defined $checkB= $check == 503
ABresult
1X1
011
000
93 0 $c->res->headers->www_authenticate(qq[Basic realm="$realm"]);
94 0 my $error = $tx->error;
95 0 ▶ if ($error->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{'code'}) { } 0 0 0
96 0 WARN "Error connecting to PlugAuth at $auth_url : @{[ $error->{code} ]} @{[ $error->{message} ]}";
97 } else {
98 0 WARN "Error connecting to PlugAuth at $auth_url : @{[ encode_json $error ]}";
99 }
100 0 $c->render(text => "auth server down", status => 503); # "Service unavailable"
101 0 return;
102 }
103 0 ▶ if ($check == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check == 200) 0 0 0
104 0 $c->stash(user => $user);
105 0 $c->continue;
106 0 return;
107 }
108 0 INFO "Authentication denied for $user, status code : ".$check;
109 0 ▶ TRACE "Response was ".$res->to_string if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 0 0 0
110 0 $c->res->headers->www_authenticate(qq[Basic realm="$realm"]);
111 0 $c->render(text => "authentication failure", status => 401);
112 0 });
113
114 0 });
115  
116 0 return undef;
117 }
118  
119  
120 0 ▶sub authorize {
Subroutine authorize: not called CC=3 SCAR=24.8
Pod documented
121 0 my $self = shift;
122 0 my $c = shift;
123 0 my ($action,$resource) = @_;
124 0 ▶ my $user = $c->stash("user") or LOGDIE "missing user in authorize()";
Branch: 1 branch
BranchTrueFalseTotal
my $user = $c->stash("user") or LOGDIE("missing user in authorize()") 0 0 0
125 0 ▶ LOGDIE "missing action or resource in authorize()" unless @_==2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
126 0 TRACE "Authorizing user $user, action $action, resource $resource";
127  
128 0 $resource =~ s[^/][];
129  
130 0 my $url = Mojo::URL->new( join '/', $self->config_url, "authz/user", $user, $action, $resource );
131  
132 0 my $tx = $c->ua->build_tx(HEAD => $url);
133
134 0 ▶ $c->ua->start($tx, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
135 0 my($ua, $tx) = @_;
136
137 0 my $code = $tx->res->code;
138 0 ▶ if($code && $code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $code == 200) 0 0 0
Condition0%0/3
$code and $code == 200
!ll&&!rl&&r
000
MC/DC0%0/2
$code and $code == 200
$code $code == 200
Truth table0%0/3
A and B
A= $codeB= $code == 200
ABresult
0X0
100
111
139 0 $c->continue;
140 0 return;
141 }
142
143 0 INFO "Unauthorized access by $user to $action $resource";
144 0 ▶ if($code == 503) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 503) { } 0 0 0
145 0 $c->render(text => "auth server down", status => 503); # "Service unavailable"
146 } else {
147 0 $c->render(text => "unauthorized", status => 403);
148 }
149 0 });
150  
151 0 return undef;
152 }
153  
154  
155 8 1;
156  
157 __END__
« blib/lib/Clustericious/Command/nginx.pm ↑ Summary blib/lib/Clustericious/Command/apache.pm »