lib/Metabase/Gateway.pm

filter: Statementstmt 34.2% 48 / 140 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 54.5% 12 / 22 Podpod 100% 3 / 3 totaltotal 26.0% 63 / 242 CC 33 SCAR 37.6
CC
33
Cov
23%
_validate_fact_struct
90.0
_validate_submitter
72.0
handle_registration
72.0
CRAP 42.9
↑ Summary lib/Metabase/Index.pm »
1 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Gateway;
6 # ABSTRACT: Manage Metabase fact submission
7 1 our $VERSION = '1.003'; # VERSION
8  
9 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Metabase::Fact 0.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Metabase::Librarian;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Metabase::User::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Metabase::User::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 requires '_build_public_librarian';
19 1 requires '_build_private_librarian';
20 1 requires '_build_fact_classes';
21  
22 1 has public_librarian => (
23 is => 'ro',
24 isa => 'Metabase::Librarian',
25 lazy => 1,
26 builder => '_build_public_librarian',
27 );
28  
29 1 has private_librarian => (
30 is => 'ro',
31 isa => 'Metabase::Librarian',
32 lazy => 1,
33 builder => '_build_private_librarian',
34 );
35  
36 # XXX life becomes a lot easier if we say that fact classes MUST have 1-to-1
37 # relationship with a .pm file. -- dagolden, 2009-03-31
38  
39 1 has fact_classes => (
40 is => 'ro',
41 isa => 'ArrayRef[Str]',
42 auto_deref => 1,
43 lazy => 1,
44 builder => '_build_fact_classes',
45 );
46  
47 1 has approved_types => (
48 is => 'ro',
49 isa => 'ArrayRef[Str]',
50 auto_deref => 1,
51 lazy => 1,
52 builder => '_build_approved_types',
53 init_arg => undef,
54 );
55  
56 1 has disable_security => (
57 is => 'ro',
58 isa => 'Bool',
59 default => 0,
60 );
61  
62 1 has allow_registration => (
63 is => 'ro',
64 isa => 'Bool',
65 default => 1,
66 );
67  
68 1 has authentication_timeout => (
69 is => 'ro',
70 isa => 'Int',
71 default => 600,
72 );
73  
74 has cache_options => (
75 is => 'ro',
76 isa => 'HashRef',
77 1 ▶ default => sub { my $hash = {}; return { driver => 'Memory', datastore => $hash } },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 );
79  
80 1 has _cache => (
81 is => 'ro',
82 isa => 'CHI::Driver',
83 lazy => 1,
84 builder => '_build_cache',
85 );
86  
87 # create a CHI cache
88 1 ▶sub _build_cache {
Subroutine _build_cache: called CC=1 SCAR=0.0
89 1 my ($self) = @_;
90 1 return CHI->new( %{ $self->cache_options } );
91 }
92  
93 # recurse report classes -- less to specify to new()
94 0 ▶sub _build_approved_types {
Subroutine _build_approved_types: not called CC=3 SCAR=24.8
95 0 my ($self) = @_;
96 0 my @queue = $self->fact_classes;
97 0 my @approved;
98 0 ▶ while ( my $class = shift @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = shift @queue) 0 0 0
99 0 push @approved, $class;
100 # XXX $class->can('fact_classes') ?? -- dagolden, 2009-03-31
101 0 ▶ push @queue, $class->fact_classes if $class->isa('Metabase::Report');
Branch: 1 branch
BranchTrueFalseTotal
if $class->isa("Metabase::Report") 0 0 0
102 }
103 0 return [ map { Class::Load::load_class($_); $_->type } @approved ];
104 }
105  
106 # for use in handle_XXXX methods
107 0 ▶sub _fatal {
Subroutine _fatal: not called CC=5 SCAR=34.0
108 0 my ($self, $code, $reason, $details) = @_;
109 0 ▶ $code ||= 500;
Condition0%0/2
$code ||= 500
l!l
00
MC/DC0%0/1
$code ||= 500
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
110 0 ▶ $reason ||= "unknown error";
Condition0%0/2
$reason ||= "unknown error"
l!l
00
MC/DC0%0/1
$reason ||= "unknown error"
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
111 0 ▶ $details ||= '';
Condition0%0/2
$details ||= ""
l!l
00
MC/DC0%0/1
$details ||= ""
$details
Truth table0%0/2
A
A= $details
Aresult
11
00
112 0 chomp $details;
113 0 my $message = "$code\: $reason";
114 0 ▶ $message .= ": $details" if $details;
Branch: 1 branch
BranchTrueFalseTotal
if $details 0 0 0
115 0 die "$message\n";
116 }
117  
118 0 ▶sub _validate_resource {
Subroutine _validate_resource: not called CC=1 SCAR=6.9
119 0 my ($self, $request) = @_;
120  
121 # XXX Well... yeah, eventually we'll want to reject reports for dists that
122 # don't, you know, exist. -- rjbs, 2008-04-06
123 0 1;
124 }
125  
126 0 ▶sub _validate_submitter {
Subroutine _validate_submitter: not called CC=8 SCAR=42.8
127 0 my ($self, $user_guid, $user_secret) = @_;
128  
129 0 ▶ return 1 if $self->disable_security;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disable_security 0 0 0
130  
131 # did we get arguments?
132 0 ▶ die "no user identity provided\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $user_guid 0 0 0
133 unless $user_guid;
134 0 ▶ die "no user secret provided\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $user_secret 0 0 0
135 unless $user_secret;
136  
137 0 my $user_resource = "metabase:user:$user_guid";
138  
139 0 my $secret = $self->_cache->get("secret/$user_guid");
140  
141 0 ▶ if ( ! defined $secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $secret) 0 0 0
142 # check if we have a secret on file
143 0 eval {
144 0 my $found = $self->private_librarian->search(
145 'core.type' => 'Metabase-User-Secret',
146 'core.resource' => $user_resource,
147 );
148 0 ▶ unless ( defined $found->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $found->[0]) 0 0 0
149 0 die "credentials for $user_resource not found\n";
150 }
151 0 my $obj = $self->private_librarian->extract($found->[0]);
152 # if we haven't died, we have it, so cache it
153 0 $secret = $obj->content;
154 0 $self->_cache->set(
155 "secret/$user_guid", $secret, $self->authentication_timeout
156 );
157 };
158 }
159  
160 # match against submitted secret
161 0 ▶ die "authentication failed for $user_resource\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $secret and $user_secret eq $secret 0 0 0
Condition0%0/3
defined $secret and $user_secret eq $secret
!ll&&!rl&&r
000
MC/DC0%0/2
defined $secret and $user_secret eq $secret
defined $secret $user_secret eq $secret
Truth table0%0/3
A and B
A= defined $secretB= $user_secret eq $secret
ABresult
0X0
100
111
162 unless defined $secret && $user_secret eq $secret;
163  
164 # submitter is good!
165 0 return 1;
166 }
167  
168 0 ▶sub _validate_fact_struct {
Subroutine _validate_fact_struct: not called CC=9 SCAR=45.0
169 0 my ($self, $struct, @approved) = @_;
170  
171 # exists and has type
172 0 ▶ die "no fact provide" unless defined $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $struct 0 0 0
173 0 ▶ die "fact type not provided" unless defined $struct->{metadata}{core}{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $struct->{"metadata"}{"core"}{"type"} 0 0 0
174  
175 # approved fact type
176 0 my $type = $struct->{metadata}{core}{type};
177 0 ▶ unless ( grep { $type eq $_ } @approved ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$type eq $_;} @approved) 0 0 0
178 0 die "$type is not an approved fact type\n";
179 }
180  
181 # has content
182 0 ▶ die "no content provided" unless defined $struct->{content};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $struct->{"content"} 0 0 0
183  
184 # required metadata
185 0 for my $key ( qw/resource type schema_version guid creator creation_time/ ) {
186 0 my $meta = $struct->{metadata}{core}{$key};
187 0 ▶ die "no '$key' provided in core metadata"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta 0 0 0
188 unless defined $meta;
189 # XXX really should check meta validity: [ //str => 'abc' ], but lets wait
190 # until we decide on sugar for metadata types -- dagolden, 2009-03-31
191 }
192  
193 die "submissions must not include resource or content metadata"
194 0 ▶ if $struct->{metadata}{content} or $struct->{metadata}{resource};
Branch: 1 branch
BranchTrueFalseTotal
if $struct->{"metadata"}{"content"} or $struct->{"metadata"}{"resource"} 0 0 0
Condition0%0/3
$struct->{"metadata"}{"content"} or $struct->{"metadata"}{"resource"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$struct->{"metadata"}{"content"} or $struct->{"metadata"}{"resource"}
$struct->{"metadata"}{"content"} $struct->{"metadata"}{"resource"}
Truth table0%0/3
A or B
A= $struct->{"metadata"}{"content"}B= $struct->{"metadata"}{"resource"}
ABresult
1X1
011
000
195  
196 0 return 1;
197 }
198  
199 0 ▶sub _check_permissions {
Subroutine _check_permissions: not called CC=1 SCAR=6.9
200 0 my ($self, $user_guid, $action, $fact) = @_;
201  
202 # The devil may care, but we don't. -- rjbs, 2009-03-30
203  
204 # E.g. do we let a user submit a fact they aren't listed as the creator for?
205 # -- dagolden, 2010-02-28
206  
207 0 return 1;
208 }
209  
210 0 ▶sub _thaw_fact {
Subroutine _thaw_fact: not called CC=1 SCAR=6.9
211 0 my ($self, $struct, @approved) = @_;
212  
213 0 $self->_validate_fact_struct($struct, @approved);
214 0 my $type = $struct->{metadata}{core}{type};
215 0 return Metabase::Fact->class_from_type($type)->from_struct($struct);
216 }
217  
218 # NOTE ON ERRORS: die with _fatal( XXX => reason => details )
219 0 ▶sub handle_submission {
Subroutine handle_submission: not called CC=5 SCAR=34.0
Pod documented
220 0 my ($self, $struct, $user_guid, $user_secret) = @_;
221  
222 # use Data::Dumper;
223 # local $SIG{__WARN__} = sub { warn "@_: " . Dumper($struct); };
224  
225 # authenticate
226 0 ▶ unless ( eval { $self->_validate_submitter( $user_guid, $user_secret ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_validate_submitter($user_guid, $user_secret); 1 } }) 0 0 0
227 0 $self->_fatal( 401 => "unauthorized" => $@ );
228 }
229  
230 # thaw
231 0 my $fact = eval { $self->_thaw_fact($struct, $self->approved_types) };
232 0 ▶ unless ( $fact ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fact) 0 0 0
233 0 $self->_fatal( 400 => "invalid submission data" => $@ );
234 }
235  
236 # action allowed for this submitter for this fact
237 0 ▶ unless ( eval { $self->_check_permissions($user_guid => submit => $fact) } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_check_permissions($user_guid, "submit", $fact) } }) 0 0 0
238 0 $self->_fatal( 400 => "cannot accept fact from current submitter" => $@ );
239 }
240  
241 # accepted by librarian
242 0 my $guid = eval { $self->enqueue($fact) };
243 0 ▶ unless ( $guid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($guid) 0 0 0
244 0 $self->_fatal( 500 => "submission was not stored" => $@ );
245 }
246  
247 0 return $guid;
248 }
249  
250 # NOTE ON ERRORS: die with _fatal( XXX => reason => details )
251 0 ▶sub handle_registration {
Subroutine handle_registration: not called CC=8 SCAR=42.8
Pod documented
252 0 my ($self, $profile_struct, $secret_struct) = @_;
253  
254 0 ▶ $self->_fatal( 400 => "new user registration disabled" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->allow_registration 0 0 0
255 unless $self->allow_registration;
256  
257 # thaw profile
258 0 my $profile = eval { $self->_thaw_fact($profile_struct, 'Metabase-User-Profile') };
259 0 ▶ unless ( $profile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($profile) 0 0 0
260 0 $self->_fatal( 400 => "invalid registration profile" => $@ );
261 }
262  
263 # thaw secret
264 0 my $secret = eval { $self->_thaw_fact($secret_struct, 'Metabase-User-Secret') };
265 0 ▶ unless ( $secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 0 0 0
266 0 $self->_fatal( 400 => "invalid registration credential" => $@ );
267 }
268  
269 # neither should exist
270 0 ▶ if ( $self->public_librarian->exists( $profile->guid )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->public_librarian->exists($profile->guid) or $self->private_librarian->exists($secret->guid)) 0 0 0
Condition0%0/3
$self->public_librarian->exists($profile->guid) or $self->private_librarian->exists($secret->guid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->public_librarian->exists($profile->guid) or $self->private_librarian->exists($secret->guid)
$self->public_librarian->exists($profile->guid) $self->private_librarian->exists($secret->guid)
Truth table0%0/3
A or B
A= $self->public_librarian->exists($profile->guid)B= $self->private_librarian->exists($secret->guid)
ABresult
1X1
011
000
271 || $self->private_librarian->exists( $secret->guid )
272 ) {
273 0 $self->_fatal( 400 => "already registered" );
274 }
275  
276 # store with respective librarians
277 0 my ($secret_guid, $profile_guid);
278 0 eval {
279 0 $secret_guid = $self->private_librarian->store( $secret );
280 0 $profile_guid = $self->public_librarian->store( $profile );
281 };
282 0 ▶ unless ( $secret_guid && $profile_guid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret_guid and $profile_guid) 0 0 0
Condition0%0/3
$secret_guid and $profile_guid
!ll&&!rl&&r
000
MC/DC0%0/2
$secret_guid and $profile_guid
$secret_guid $profile_guid
Truth table0%0/3
A and B
A= $secret_guidB= $profile_guid
ABresult
0X0
100
111
283 0 $self->_fatal( 500 => "registration failed" => $@ );
284 }
285  
286 # profile accepted by librarian
287 0 return $profile_guid;
288 }
289  
290 0 ▶sub enqueue {
Subroutine enqueue: not called CC=1 SCAR=6.9
Pod documented
291 0 my ($self, $fact) = @_;
292 0 return $self->public_librarian->store($fact);
293 }
294  
295  
296 1 1;
297  
298 __END__
↑ Summary lib/Metabase/Index.pm »