Concierge/Desk/Setup.pm

filter: Statementstmt 94.8% 164 / 173 Branchbran 70.1% 80 / 114 Conditioncond 39.0% 25 / 64 MC/DCmcdc 18.4% 7 / 38 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 72.1% 293 / 406 CC 89 SCAR 40.6
CC
89
Cov
77%
build_desk
92.3
validate_setup_config
38.7
build_quick_desk
17.1
CRAP 58.0
↑ Summary Concierge.pm »
1 package Concierge::Desk::Setup v0.13.0;
2 9 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 our $VERSION = 'v0.13.0';
5  
6 # ABSTRACT: Setup and configuration for Concierge desk initialization
7  
8 9 ▶use Carp qw<carp croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Path qw/make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON::PP qw< encode_json decode_json >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Concierge;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Concierge::Desk::Component;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # === COMPONENT MODULES ===
16 9 ▶use Concierge::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Concierge::Sessions;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Concierge::Users;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # === AUTH BACKEND CATALOG ===
21 # Maps a friendly auth.backend name (as given in setup config) to its
22 # fully-qualified class name and the settings it requires. Adding a
23 # new backend (e.g. an LDAP-backed one) is a one-entry addition here;
24 # Concierge::Auth itself never guesses or validates any of this --
25 # see its POD. 'class' need not live under the Concierge:: namespace
26 # -- Concierge::Auth's factory just require()s it and calls its new()
27 # (see its POD); any installed module implementing the 5-verb contract
28 # works, e.g. a company-internal SSO wrapper.
29 9 my %AUTH_BACKENDS = (
30 pwd => {
31 class => 'Concierge::Auth::Pwd',
32 required => [], # 'file' always resolves via
33 # default_file below -- never
34 # actually missing
35 default_file => 'auth.pwd',
36 },
37 # ldap => {
38 # class => 'Concierge::Auth::LDAP',
39 # required => [qw/host bind_dn password/],
40 # },
41 # oauth => {
42 # class => 'Concierge::Auth::OAuth',
43 # required => [qw/client_id client_secret redirect_uri/],
44 # },
45 # saml => {
46 # class => 'Concierge::Auth::SAML',
47 # required => [qw/idp_metadata_url entity_id/],
48 # },
49 # sso => {
50 # class => 'MyApp::Auth::SSO', # outside Concierge:: -- fine, see note above
51 # required => [qw/provider_url/],
52 # },
53 );
54  
55 # === SESSIONS BACKEND CATALOG ===
56 # Maps a friendly sessions.backend name to its fully-qualified class
57 # name and the settings it requires, same shape as %AUTH_BACKENDS.
58 # Concierge::Sessions itself never guesses a class from a friendly
59 # name -- it requires an already-resolved backend_class (see its POD).
60 9 my %SESSIONS_BACKENDS = (
61 database => { class => 'Concierge::Sessions::SQLite', required => [] },
62 file => { class => 'Concierge::Sessions::File', required => [] },
63 # redis => {
64 # class => 'Concierge::Sessions::Redis',
65 # required => [qw/host port/],
66 # },
67 );
68  
69 # === USERS BACKEND CATALOG ===
70 # Maps a friendly users.backend name to its fully-qualified class name
71 # and the settings it requires, same shape as %AUTH_BACKENDS.
72 # Concierge::Users itself never guesses a class from a friendly name --
73 # setup() requires an already-resolved backend_class (see its POD).
74 9 my %USERS_BACKENDS = (
75 database => { class => 'Concierge::Users::SQLite', required => [] },
76 file => { class => 'Concierge::Users::File', required => [] },
77 yaml => { class => 'Concierge::Users::YAML', required => [] },
78 # postgres => {
79 # class => 'Concierge::Users::Postgres',
80 # required => [qw/host dbname user password/],
81 # },
82 );
83  
84 # Resolve a component's storage directory against base_dir: a relative
85 # $dir is joined onto $base_dir (so it moves if base_dir does); an
86 # absolute $dir is used as-is (letting a component's storage live
87 # anywhere, including outside the desk entirely); an undefined $dir
88 # falls back to $base_dir itself.
89 91 ▶sub _resolve_dir ($dir, $base_dir) {
Subroutine _resolve_dir: called CC=3 SCAR=11.0
90 91 ▶ return $base_dir unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 72 19 91
91 19 ▶ return $dir if File::Spec->file_name_is_absolute($dir);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($dir) 3 16 19
92 16 return File::Spec->catdir($base_dir, $dir);
93 }
94  
95 # =============================================================================
96 # SIMPLE SETUP - Opinionated defaults for quick start
97 # =============================================================================
98  
99 15 ▶sub build_quick_desk ($storage_dir, $app_fields=[]) {
Subroutine build_quick_desk: called CC=13 SCAR=28.4
Pod documented
100 # Simple, opinionated setup with reasonable defaults:
101 # - Database sessions backend (SQLite via Concierge::Sessions)
102 # - Database users backend (SQLite via Concierge::Users)
103 # - All standard user fields included
104 # - All storage co-located in $storage_dir
105  
106 15 ▶ return { success => 0, message => 'desk_location is required' }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $storage_dir 1 14 15
107 unless defined $storage_dir;
108  
109 # Safety: Convert '.' or empty string to './desk' to avoid cluttering app root
110 14 ▶ if (!$storage_dir || $storage_dir eq '.' || $storage_dir eq './') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $storage_dir or $storage_dir eq "." or $storage_dir eq "./") 0 14 14
Condition33%2/6
not $storage_dir or $storage_dir eq "." or $storage_dir eq "./"
l!l&&r!l&&!r
0014
not $storage_dir or $storage_dir eq "."
l!l&&r!l&&!r
0014
MC/DC0%0/3
not $storage_dir or $storage_dir eq "." or $storage_dir eq "./"
not $storage_dir $storage_dir eq "." $storage_dir eq "./"
Truth table25%1/4
not(A) or B or C
A= not $storage_dirB= $storage_dir eq "."C= $storage_dir eq "./"
ABCresult
1XX1
01X1
0011
0000
111 0 $storage_dir = './desk';
112 }
113  
114 # Ensure storage directory exists
115 14 ▶ unless (-d $storage_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $storage_dir) 1 13 14
116 1 eval { make_path($storage_dir) };
117 1 ▶ croak "Cannot create storage directory '$storage_dir': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
118 }
119  
120 # Create minimal Concierge object for internal operations
121 14 my $concierge = bless {}, 'Concierge';
122  
123 # Initialize Sessions component -- build_quick_desk is fully
124 # opinionated (like its fixed 'database' choice for Users), so the
125 # resolved class name is hardcoded here rather than going through
126 # the catalog/validation path used by build_desk().
127 14 my $sessions_backend = 'Concierge::Sessions::SQLite';
128 14 $concierge->{sessions} = Concierge::Sessions->new(
129 backend_class => $sessions_backend,
130 storage_dir => $storage_dir,
131 );
132  
133 # Initialize Auth component -- same rationale as Sessions above.
134 14 my $auth_file = File::Spec->catfile($storage_dir, 'auth.pwd');
135 14 my $auth_backend = 'Concierge::Auth::Pwd';
136 14 my $auth_args = { file => $auth_file };
137 14 ▶ unlink $auth_file if -f $auth_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $auth_file 0 14 14
138 14 $concierge->{auth} = Concierge::Auth->new(
139 backend_class => $auth_backend,
140 %$auth_args,
141 );
142  
143 # Setup Users component -- same rationale as Sessions above.
144 14 my $users_backend = 'Concierge::Users::SQLite';
145 14 my $users_setup = Concierge::Users->setup({
146 storage_dir => $storage_dir,
147 backend_class => $users_backend,
148 include_standard_fields => 'all', # All standard fields
149 field_overrides => [], # No overrides
150 app_fields => $app_fields,
151 });
152 14 ▶ unless ($users_setup->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($users_setup->{"success"}) 0 14 14
153 return {
154 success => 0,
155 message => "Failed to set up Users: " . $users_setup->{message}
156 0 };
157 }
158  
159 # Build configuration to store in concierge session
160 my $full_config = {
161 users_config_file => $users_setup->{config_file},
162 14 storage_dir => $storage_dir,
163 sessions_dir => $storage_dir,
164 users_dir => $storage_dir,
165 auth_backend => $auth_backend,
166 auth_args => $auth_args,
167 sessions_backend => $sessions_backend,
168 users_backend => $users_backend,
169 };
170 # Encode to JSON with pretty formatting and write with trailing newline
171 14 my $json = JSON::PP->new->utf8->pretty->encode($full_config) . "\n";
172
173 14 my $concierge_conf_file = File::Spec->catfile($storage_dir, 'concierge.conf');
174
175 14 my $fh;
176 14 ▶ open $fh, ">", $concierge_conf_file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh or return {"success", 0, "message", "Cannot write to concierge config file: $!"} 14 0 14
Condition33%2/6
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh
!ll&&!rl&&r
0014
open $fh, ">", $concierge_conf_file and print $fh $json
!ll&&!rl&&r
0014
MC/DC0%0/3
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh
open $fh, ">", $concierge_conf_file print $fh $json close $fh
Truth table25%1/4
A and B and C
A= open $fh, ">", $concierge_conf_fileB= print $fh $jsonC= close $fh
ABCresult
0XX0
10X0
1100
1111
177 and
178 print $fh $json
179 and
180 close $fh
181 or return { success => 0, message => "Cannot write to concierge config file: $!" };
182  
183 return {
184 14 success => 1,
185 message => "Concierge desk built successfully",
186 desk => $storage_dir,
187 };
188 }
189  
190 # =============================================================================
191 # ADVANCED SETUP - Full control with custom configuration
192 # =============================================================================
193  
194 29 ▶sub build_desk ($config) {
Subroutine build_desk: called CC=51 SCAR=45.3
Pod documented
195 # Advanced setup with full configuration options:
196 # - Separate storage directories per component
197 # - Full Users.pm field configuration (include_standard_fields, field_overrides, etc.)
198 # - Custom backend selection for Authentication, Sessions, and Users
199 # - No assumptions or defaults
200  
201 # Validate required top-level config
202 29 ▶ return { success => 0, message => 'Configuration must be a hash reference' }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 1 28 29
203 unless ref $config eq 'HASH';
204  
205 return { success => 0, message => 'Missing base_dir' }
206 28 ▶ unless $config->{base_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"base_dir"} 2 26 28
207  
208 # Safety: Convert '.' or empty string to './desk' to avoid cluttering app root
209 26 my $base_dir = $config->{base_dir};
210 26 ▶ if (!$base_dir || $base_dir eq '.' || $base_dir eq './') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $base_dir or $base_dir eq "." or $base_dir eq "./") 1 25 26
Condition66%4/6
not $base_dir or $base_dir eq "." or $base_dir eq "./"
l!l&&r!l&&!r
1025
not $base_dir or $base_dir eq "."
l!l&&r!l&&!r
0125
MC/DC33%1/3
not $base_dir or $base_dir eq "." or $base_dir eq "./"
not $base_dir $base_dir eq "." $base_dir eq "./"
Truth table50%2/4
not(A) or B or C
A= not $base_dirB= $base_dir eq "."C= $base_dir eq "./"
ABCresult
1XX1
01X1
0011
0000
211 1 $base_dir = './desk';
212 1 $config->{base_dir} = $base_dir;
213 }
214  
215 # Determine storage locations. Each component may specify its own
216 # 'dir' (falls back to base_dir if omitted). A relative dir is
217 # resolved against base_dir, so it moves with it; an absolute dir
218 # is used as-is, letting a component's storage live entirely
219 # outside the desk (e.g. for security) without tying that
220 # placement to how the rest of the desk is laid out.
221 26 my $sessions_dir = _resolve_dir($config->{sessions}{dir}, $base_dir);
222 26 my $users_dir = _resolve_dir($config->{users}{dir}, $base_dir);
223 26 my $auth_dir = _resolve_dir($config->{auth}{dir}, $base_dir);
224  
225 # Resolve the auth backend: config gives a friendly name (e.g.
226 # 'pwd'); the catalog maps it to a fully-qualified class name and
227 # the settings that backend requires. No default -- unlike
228 # build_quick_desk(), this path is not opinionated about backend
229 # choice.
230 my $auth_backend_name = $config->{auth}{backend}
231 26 ▶ or return { success => 0, message => 'Missing auth.backend' };
Branch: 1 branch
BranchTrueFalseTotal
my $auth_backend_name = $config->{"auth"}{"backend"} or return {"success", 0, "message", "Missing auth.backend"} 26 0 26
232 26 ▶ my $auth_spec = $AUTH_BACKENDS{lc $auth_backend_name}
Branch: 1 branch
BranchTrueFalseTotal
my $auth_spec = $AUTH_BACKENDS{lc $auth_backend_name} or return {"success", 0, "message", "Unknown auth.backend: $auth_backend_name"} 26 0 26
233 or return { success => 0, message => "Unknown auth.backend: $auth_backend_name" };
234  
235 # auth.file is a filename only (not a path) -- where it lives is a
236 # separate concern controlled by auth.dir (falls back to base_dir,
237 # same as sessions.dir/users.dir). This lets the auth store be
238 # located anywhere, including outside the rest of the desk's
239 # storage for security, without tying that placement to
240 # what the file happens to be named. The default filename is
241 # backend-specific (only 'pwd' has one, since it's the only
242 # backend that needs a file at all); this does not mean
243 # Concierge::Auth itself has a fallback -- it never guesses at
244 # runtime (see Concierge::Auth POD).
245 26 ▶ my $auth_filename = $config->{auth}{file} || $auth_spec->{default_file};
Condition66%2/3
$config->{'auth'}{'file'} || $auth_spec->{'default_file'}
l!l&&r!l&&!r
1250
MC/DC0%0/2
$config->{'auth'}{'file'} || $auth_spec->{'default_file'}
$config->{'auth'}{'file'} $auth_spec->{'default_file'}
Truth table66%2/3
A || B
A= $config->{'auth'}{'file'}B= $auth_spec->{'default_file'}
ABresult
1X1
011
000
246 26 ▶ my %auth_args = defined $auth_filename
Branch: 1 branch
BranchTrueFalseTotal
defined $auth_filename ? : 26 0 26
247 ? ( file => File::Spec->catfile($auth_dir, $auth_filename) )
248 : (); # today's only 'pwd' setting; a future backend's required
249 # keys would be read from $config->{auth}{$key} here instead
250  
251 # Validate every setting the chosen backend requires, failing the
252 # build now rather than deferring to a runtime error later.
253 26 for my $key (@{ $auth_spec->{required} }) {
254 return { success => 0, message => "Missing auth.$key required for backend '$auth_backend_name'" }
255 0 ▶ unless defined $auth_args{$key} && length $auth_args{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth_args{$key} and length $auth_args{$key} 0 0 0
Condition0%0/3
defined $auth_args{$key} and length $auth_args{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $auth_args{$key} and length $auth_args{$key}
defined $auth_args{$key} length $auth_args{$key}
Truth table0%0/3
A and B
A= defined $auth_args{$key}B= length $auth_args{$key}
ABresult
0X0
100
111
256 }
257  
258 # Resolve the sessions backend: same friendly-name-to-class
259 # resolution as auth, via %SESSIONS_BACKENDS.
260 my $sessions_backend_name = $config->{sessions}{backend}
261 26 ▶ or return { success => 0, message => 'Missing sessions.backend' };
Branch: 1 branch
BranchTrueFalseTotal
my $sessions_backend_name = $config->{"sessions"}{"backend"} or return {"success", 0, "message", "Missing sessions.backend"} 26 0 26
262 26 ▶ my $sessions_spec = $SESSIONS_BACKENDS{lc $sessions_backend_name}
Branch: 1 branch
BranchTrueFalseTotal
my $sessions_spec = $SESSIONS_BACKENDS{lc $sessions_backend_name} or return {"success", 0, "message", "Unknown sessions.backend: $sessions_backend_name"} 26 0 26
263 or return { success => 0, message => "Unknown sessions.backend: $sessions_backend_name" };
264 26 for my $key (@{ $sessions_spec->{required} }) {
265 return { success => 0, message => "Missing sessions.$key required for backend '$sessions_backend_name'" }
266 0 ▶ unless defined $config->{sessions}{$key} && length $config->{sessions}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key} 0 0 0
Condition0%0/3
defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key}
defined $config->{"sessions"}{$key} length $config->{"sessions"}{$key}
Truth table0%0/3
A and B
A= defined $config->{"sessions"}{$key}B= length $config->{"sessions"}{$key}
ABresult
0X0
100
111
267 }
268  
269 # Resolve the users backend: same friendly-name-to-class
270 # resolution as auth, via %USERS_BACKENDS.
271 my $users_backend_name = $config->{users}{backend}
272 26 ▶ or return { success => 0, message => 'Missing users.backend' };
Branch: 1 branch
BranchTrueFalseTotal
my $users_backend_name = $config->{"users"}{"backend"} or return {"success", 0, "message", "Missing users.backend"} 26 0 26
273 26 ▶ my $users_spec = $USERS_BACKENDS{lc $users_backend_name}
Branch: 1 branch
BranchTrueFalseTotal
my $users_spec = $USERS_BACKENDS{lc $users_backend_name} or return {"success", 0, "message", "Unknown users.backend: $users_backend_name"} 26 0 26
274 or return { success => 0, message => "Unknown users.backend: $users_backend_name" };
275 26 for my $key (@{ $users_spec->{required} }) {
276 return { success => 0, message => "Missing users.$key required for backend '$users_backend_name'" }
277 0 ▶ unless defined $config->{users}{$key} && length $config->{users}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"users"}{$key} and length $config->{"users"}{$key} 0 0 0
Condition0%0/3
defined $config->{"users"}{$key} and length $config->{"users"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"users"}{$key} and length $config->{"users"}{$key}
defined $config->{"users"}{$key} length $config->{"users"}{$key}
Truth table0%0/3
A and B
A= defined $config->{"users"}{$key}B= length $config->{"users"}{$key}
ABresult
0X0
100
111
278 }
279  
280 # Create directories if needed
281 26 for my $dir ($base_dir, $sessions_dir, $users_dir, $auth_dir) {
282 104 ▶ next if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 97 7 104
283 7 eval { make_path($dir) };
284 return {
285 7 ▶ success => 0,
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
286 message => "Failed to create directory '$dir': $@"
287 } if $@;
288 }
289  
290 # Create minimal Concierge object for internal operations
291 26 my $concierge = bless {}, 'Concierge';
292  
293 # Initialize Sessions component with the resolved backend class
294 $concierge->{sessions} = Concierge::Sessions->new(
295 backend_class => $sessions_spec->{class},
296 26 storage_dir => $sessions_dir,
297 );
298  
299 # Initialize Auth component
300 26 ▶ unlink $auth_args{file} if $auth_args{file} && -f $auth_args{file};
Branch: 1 branch
BranchTrueFalseTotal
if $auth_args{"file"} and -f $auth_args{"file"} 0 26 26
Condition33%1/3
$auth_args{"file"} and -f $auth_args{"file"}
!ll&&!rl&&r
0260
MC/DC0%0/2
$auth_args{"file"} and -f $auth_args{"file"}
$auth_args{"file"} -f $auth_args{"file"}
Truth table33%1/3
A and B
A= $auth_args{"file"}B= -f $auth_args{"file"}
ABresult
0X0
100
111
301 $concierge->{auth} = Concierge::Auth->new(
302 backend_class => $auth_spec->{class},
303 26 %auth_args,
304 );
305  
306 # Build Users setup configuration
307 my $users_config = {
308 storage_dir => $users_dir,
309 backend_class => $users_spec->{class},
310 26 };
311  
312 # Add Users-specific configuration options
313 $users_config->{include_standard_fields} = $config->{users}{include_standard_fields}
314 26 ▶ if exists $config->{users}{include_standard_fields};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"users"}{"include_standard_fields"} 26 0 26
315  
316 $users_config->{app_fields} = $config->{users}{app_fields}
317 26 ▶ if exists $config->{users}{app_fields};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"users"}{"app_fields"} 1 25 26
318  
319 $users_config->{field_overrides} = $config->{users}{field_overrides}
320 26 ▶ if exists $config->{users}{field_overrides};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"users"}{"field_overrides"} 1 25 26
321  
322 # Setup Users component
323 26 my $users_setup = Concierge::Users->setup($users_config);
324 26 ▶ unless ($users_setup->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($users_setup->{"success"}) 0 26 26
325 return {
326 success => 0,
327 message => "Failed to setup Users: " . $users_setup->{message}
328 0 };
329 }
330  
331 # Initialize any additional components declared in config->{components}.
332 # Unlike sessions/auth/users (hardcoded core affordances), these are
333 # resolved once, here, at build time -- never recomputed at
334 # open_desk()/runtime. Each entry's class is require()d and constructed
335 # with a no-arg new() (build time is not the same call path as
336 # open_desk()'s runtime new($payload)), then setup() is called with
337 # its config block; the result is persisted verbatim as that
338 # component's 'payload'. A setup() failure always fails the whole
339 # build, regardless of 'optional' -- 'optional' only affects behavior
340 # at open_desk() time.
341 26 my %components;
342 26 ▶ for my $name (keys %{ $config->{components} // {} }) {
Condition100%2/2
$config->{"components"} // {}
l!l
1412
MC/DC100%1/1
$config->{"components"} // {}
$config->{"components"}
Truth table100%2/2
A
A= $config->{"components"}
Aresult
11
00
343 14 my $entry = $config->{components}{$name};
344 14 ▶ return { success => 0, message => "components.$name must be a hash reference" }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 14 14
345 unless ref $entry eq 'HASH';
346  
347 my $class = $entry->{class}
348 14 ▶ or return { success => 0, message => "components.$name is missing required 'class'" };
Branch: 1 branch
BranchTrueFalseTotal
my $class = $entry->{"class"} or return {"success", 0, "message", "components.$name is missing required 'class'"} 14 0 14
349 14 ▶ my $optional = $entry->{optional} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'optional'} ? : 11 3 14
350 14 ▶ my $defer = $entry->{defer} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'defer'} ? : 10 4 14
351  
352 # 'defer' forces 'optional': a required-and-deferred component's
353 # eventual first-use failure would happen mid-process, potentially
354 # while other users are already being served -- violating
355 # Concierge's guarantee that its API methods are never fatal once
356 # a desk is open. See Concierge::Desk::Component/PROBING for the
357 # full tier model.
358 14 ▶ if ($defer && !$optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defer and not $optional) 1 13 14
Condition100%3/3
$defer and not $optional
!ll&&!rl&&r
491
MC/DC100%2/2
$defer and not $optional
$defer not $optional
Truth table100%3/3
A and not(B)
A= $deferB= not $optional
ABresult
0X0
100
111
359 return {
360 1 success => 0,
361 message => "components.$name cannot use 'defer' without also being 'optional'",
362 };
363 }
364  
365 13 my $comp_dir = _resolve_dir($entry->{dir}, $base_dir);
366 13 ▶ unless (-d $comp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $comp_dir) 13 0 13
367 13 eval { make_path($comp_dir) };
368 return {
369 13 ▶ success => 0,
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
370 message => "Failed to create directory '$comp_dir' for component '$name': $@"
371 } if $@;
372 }
373  
374 13 my $comp;
375 13 eval {
376 13 (my $file = $class) =~ s{::}{/}g;
377 13 require "$file.pm";
378 13 $comp = $class->new();
379 };
380 return {
381 13 ▶ success => 0,
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
382 message => "Failed to build component '$name' ($class): $@"
383 } if $@;
384  
385 13 my %setup_args = %$entry;
386 13 delete $setup_args{class};
387 13 delete $setup_args{optional};
388 13 delete $setup_args{defer};
389 13 $setup_args{dir} = $comp_dir;
390 13 $setup_args{name} = $name;
391  
392 13 my $setup_result = $comp->setup(\%setup_args);
393 13 ▶ return $setup_result unless $setup_result->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $setup_result->{"success"} 1 12 13
394  
395 # Tier 1b (build-time probe), 'defer' entries only: run the same
396 # probe-or-default check that open_desk() will later run at tier
397 # 2, via the shared helper, at a human-attended moment. Failure
398 # here unconditionally fails the whole build -- deliberately
399 # *not* gated by 'optional', even though 'defer' always implies
400 # 'optional' -- to catch a broken probe or an unrequireable class
401 # now, rather than only discovering it later, unattended, as a
402 # silent UnavailableComponent substitution. See
403 # Concierge::Desk::Component/PROBING for the full tier model.
404 12 ▶ if ($defer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defer) 9 3 12
405 9 my $probe = Concierge::Desk::Component::probe_component($class, $setup_result);
406 9 ▶ unless ($probe->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($probe->{"success"}) 1 8 9
407 return {
408 success => 0,
409 message => "Build-time probe failed for deferred component '$name' ($class): "
410 1 ▶ . ($probe->{message} // 'unknown reason'),
Condition50%1/2
$probe->{"message"} // "unknown reason"
l!l
10
MC/DC0%0/1
$probe->{"message"} // "unknown reason"
$probe->{"message"}
Truth table50%1/2
A
A= $probe->{"message"}
Aresult
11
00
411 };
412 }
413 }
414  
415 11 $components{$name} = {
416 class => $class,
417 optional => $optional,
418 defer => $defer,
419 payload => $setup_result,
420 };
421 }
422  
423 # Build configuration to store in concierge session
424 my $full_config = {
425 users_config_file => $users_setup->{config_file},
426 storage_dir => $base_dir,
427 sessions_dir => $sessions_dir,
428 users_dir => $users_dir,
429 auth_dir => $auth_dir,
430 auth_backend => $auth_spec->{class},
431 auth_args => \%auth_args,
432 sessions_backend => $sessions_spec->{class},
433 users_backend => $users_config->{backend_class},
434 23 ▶ (%components ? (components => \%components) : ()),
Branch: 1 branch
BranchTrueFalseTotal
%components ? : 11 12 23
435 };
436 23 my $json = JSON::PP->new->utf8->pretty->encode($full_config) . "\n";
437  
438 23 my $config_location = $full_config->{storage_dir};
439 23 my $concierge_conf_file = File::Spec->catfile($config_location, 'concierge.conf');
440 23 my $fh;
441 23 ▶ open $fh, ">", $concierge_conf_file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh or return {"success", 0, "message", "Cannot write to concierge config file: $!"} 23 0 23
Condition33%2/6
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh
!ll&&!rl&&r
0023
open $fh, ">", $concierge_conf_file and print $fh $json
!ll&&!rl&&r
0023
MC/DC0%0/3
open $fh, ">", $concierge_conf_file and print $fh $json and close $fh
open $fh, ">", $concierge_conf_file print $fh $json close $fh
Truth table25%1/4
A and B and C
A= open $fh, ">", $concierge_conf_fileB= print $fh $jsonC= close $fh
ABCresult
0XX0
10X0
1100
1111
442 and
443 print $fh $json
444 and
445 close $fh
446 or return { success => 0, message => "Cannot write to concierge config file: $!" };
447  
448 return {
449 23 success => 1,
450 message => "Custom Concierge desk built successfully",
451 desk => $base_dir,
452 config => $full_config,
453 };
454 }
455  
456  
457 # =============================================================================
458 # HELPER METHODS
459 # =============================================================================
460  
461 # Validate setup configuration before executing
462 12 ▶sub validate_setup_config ($config) {
Subroutine validate_setup_config: called CC=25 SCAR=36.6
Pod documented
463 12 my @errors;
464  
465 # Check required fields
466 push @errors, "Missing base_dir"
467 12 ▶ unless $config->{base_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"base_dir"} 2 10 12
468  
469 push @errors, "Missing auth.backend"
470 12 ▶ unless $config->{auth} && $config->{auth}{backend};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"auth"} and $config->{"auth"}{"backend"} 2 10 12
Condition66%2/3
$config->{"auth"} and $config->{"auth"}{"backend"}
!ll&&!rl&&r
2010
MC/DC50%1/2
$config->{"auth"} and $config->{"auth"}{"backend"}
$config->{"auth"} $config->{"auth"}{"backend"}
Truth table66%2/3
A and B
A= $config->{"auth"}B= $config->{"auth"}{"backend"}
ABresult
0X0
100
111
471  
472 push @errors, "Missing sessions.backend"
473 12 ▶ unless $config->{sessions} && $config->{sessions}{backend};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"sessions"} and $config->{"sessions"}{"backend"} 2 10 12
Condition66%2/3
$config->{"sessions"} and $config->{"sessions"}{"backend"}
!ll&&!rl&&r
2010
MC/DC50%1/2
$config->{"sessions"} and $config->{"sessions"}{"backend"}
$config->{"sessions"} $config->{"sessions"}{"backend"}
Truth table66%2/3
A and B
A= $config->{"sessions"}B= $config->{"sessions"}{"backend"}
ABresult
0X0
100
111
474  
475 push @errors, "Missing users.backend"
476 12 ▶ unless $config->{users} && $config->{users}{backend};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"users"} and $config->{"users"}{"backend"} 2 10 12
Condition66%2/3
$config->{"users"} and $config->{"users"}{"backend"}
!ll&&!rl&&r
2010
MC/DC50%1/2
$config->{"users"} and $config->{"users"}{"backend"}
$config->{"users"} $config->{"users"}{"backend"}
Truth table66%2/3
A and B
A= $config->{"users"}B= $config->{"users"}{"backend"}
ABresult
0X0
100
111
477  
478 # Validate backend values
479 12 ▶ if ($config->{auth}{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"auth"}{"backend"}) 10 2 12
480 10 my $spec = $AUTH_BACKENDS{lc $config->{auth}{backend}};
481 10 ▶ if (!$spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec) { } 1 9 10
482 1 push @errors, "Invalid auth.backend: '$config->{auth}{backend}'";
483 } else {
484 9 for my $key (@{ $spec->{required} }) {
485 push @errors, "Missing auth.$key required for backend '$config->{auth}{backend}'"
486 0 ▶ unless defined $config->{auth}{$key} && length $config->{auth}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"auth"}{$key} and length $config->{"auth"}{$key} 0 0 0
Condition0%0/3
defined $config->{"auth"}{$key} and length $config->{"auth"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"auth"}{$key} and length $config->{"auth"}{$key}
defined $config->{"auth"}{$key} length $config->{"auth"}{$key}
Truth table0%0/3
A and B
A= defined $config->{"auth"}{$key}B= length $config->{"auth"}{$key}
ABresult
0X0
100
111
487 }
488 }
489 }
490  
491 12 ▶ if ($config->{sessions}{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"sessions"}{"backend"}) 10 2 12
492 10 my $spec = $SESSIONS_BACKENDS{lc $config->{sessions}{backend}};
493 10 ▶ if (!$spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec) { } 2 8 10
494 2 push @errors, "Invalid sessions.backend: '$config->{sessions}{backend}'";
495 } else {
496 8 for my $key (@{ $spec->{required} }) {
497 push @errors, "Missing sessions.$key required for backend '$config->{sessions}{backend}'"
498 0 ▶ unless defined $config->{sessions}{$key} && length $config->{sessions}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key} 0 0 0
Condition0%0/3
defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"sessions"}{$key} and length $config->{"sessions"}{$key}
defined $config->{"sessions"}{$key} length $config->{"sessions"}{$key}
Truth table0%0/3
A and B
A= defined $config->{"sessions"}{$key}B= length $config->{"sessions"}{$key}
ABresult
0X0
100
111
499 }
500 }
501 }
502  
503 12 ▶ if ($config->{users}{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"users"}{"backend"}) 10 2 12
504 10 my $spec = $USERS_BACKENDS{lc $config->{users}{backend}};
505 10 ▶ if (!$spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec) { } 2 8 10
506 2 push @errors, "Invalid users.backend: '$config->{users}{backend}'";
507 } else {
508 8 for my $key (@{ $spec->{required} }) {
509 push @errors, "Missing users.$key required for backend '$config->{users}{backend}'"
510 0 ▶ unless defined $config->{users}{$key} && length $config->{users}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"users"}{$key} and length $config->{"users"}{$key} 0 0 0
Condition0%0/3
defined $config->{"users"}{$key} and length $config->{"users"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"users"}{$key} and length $config->{"users"}{$key}
defined $config->{"users"}{$key} length $config->{"users"}{$key}
Truth table0%0/3
A and B
A= defined $config->{"users"}{$key}B= length $config->{"users"}{$key}
ABresult
0X0
100
111
511 }
512 }
513 }
514  
515 return {
516 12 ▶ success => @errors ? 0 : 1,
Branch: 2 branches
BranchTrueFalseTotal
@errors ? : 9 3 12
@errors ? : 9 3 12
517 (@errors ? (errors => \@errors) : ()),
518 };
519 }
520  
521 9 1;
522  
523 __END__
↑ Summary Concierge.pm »