Introspector/Probe/Users.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 1 totaltotal 89.1% 74 / 83 CC 8 SCAR 8.9
CC
8
Cov
90%
_gather_ssh_keys
5.0
_gather_crontab
4.1
__ANON__
2.0
CRAP 2.4
« Introspector/Probe/Groups.pm ↑ Summary Introspector.pm »
1 package System::Introspector::Probe::Users;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use System::Introspector::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
5 handle_from_command
6 transform_exceptions
7 output_from_file
8 output_from_command
9 files_from_dir
10 handle_from_file
11 1 );
12  
13 0 ▶has passwd_file => (is => 'ro', default => sub { '/etc/passwd' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 1 ▶sub gather {
Subroutine gather: called CC=1 SCAR=0.0
Pod undocumented
16 1 my ($self) = @_;
17 1 ▶ return transform_exceptions {
Subroutine __ANON__: called CC=2 SCAR=6.9
18 1 my $fh = $self->_open_passwd_fh;
19 1 my %user;
20 1 ▶ while (defined( my $line = <$fh> )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 1 3
21 2 my $data = $self->_deparse_htpasswd_line($line);
22 2 my $user = $data->{username};
23 2 my $home = $data->{home};
24 2 ▶ $data->{groups} = transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 $self->_gather_user_groups($user);
26 2 };
27 2 ▶ $data->{ssh}{keys} = transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 $self->_gather_ssh_keys($user, $home);
29 2 };
30 2 ▶ $data->{crontab} = transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 $self->_gather_crontab($user);
32 2 };
33 2 $user{ $data->{username} } = $data;
34 }
35 1 return { users => \%user };
36 1 };
37 }
38  
39 2 ▶sub _gather_crontab {
Subroutine _gather_crontab: called CC=3 SCAR=14.2
40 2 my ($self, $user) = @_;
41 2 my ($out, $err, $ok) = output_from_command
42 ['crontab', '-u', $user, '-l'];
43 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 2 2
44 return {}
45 0 ▶ if $err =~ m{^no crontab}i;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /^no crontab/i 0 0 0
46 0 return { __error__ => $err };
47 }
48 2 return { body => $out };
49 }
50  
51 2 ▶sub _gather_ssh_keys {
Subroutine _gather_ssh_keys: called CC=5 SCAR=16.1
52 2 my ($self, $user, $home) = @_;
53 2 my $ssh_dir = "$home/.ssh";
54 2 my $ssh_authkeys = "$ssh_dir/authorized_keys";
55 return {
56 2 ▶ files => {},
Branch: 1 branch
BranchTrueFalseTotal
unless -d $ssh_dir 1 1 2
57 authorized => { file_name => $ssh_authkeys, body => '' }
58 } unless -d $ssh_dir;
59 1 my %key;
60 1 for my $item (files_from_dir $ssh_dir) {
61 2 ▶ next unless $item =~ m{\.pub$};
Branch: 1 branch
BranchTrueFalseTotal
unless $item =~ /\.pub$/ 1 1 2
62 1 ▶ $key{ $item } = transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 return {
64 1 file_name => "$ssh_dir/$item",
65 body => scalar output_from_file "$ssh_dir/$item",
66 };
67 1 };
68 }
69 1 ▶ my $auth_keys = (-e $ssh_authkeys) ? (transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 return {
71 1 file_name => $ssh_authkeys,
72 body => scalar output_from_file $ssh_authkeys,
73 };
74 1 ▶ }) : { file_name => $ssh_authkeys, body => '' };
Branch: 1 branch
BranchTrueFalseTotal
-e $ssh_authkeys ? : 1 0 1
75 1 return { files => \%key, authorized => $auth_keys };
76 }
77  
78 2 ▶sub _gather_user_groups {
Subroutine _gather_user_groups: called CC=1 SCAR=0.0
79 2 my ($self, $user) = @_;
80 2 my $groups = output_from_command [groups => $user];
81 2 chomp $groups;
82 2 $groups =~ s{^ .* : \s* }{}x;
83 2 return [split m{\s+}, $groups];
84 }
85  
86 2 ▶sub _deparse_htpasswd_line {
Subroutine _deparse_htpasswd_line: called CC=1 SCAR=0.0
87 2 my ($self, $line) = @_;
88 2 chomp $line;
89 2 my %value;
90 2 @value{qw( username uid gid comment home shell )}
91 = (split m{:}, $line)[0, 2..6];
92 2 return \%value;
93 }
94  
95 1 ▶sub _open_passwd_fh {
Subroutine _open_passwd_fh: called CC=1 SCAR=0.0
96 1 my ($self) = @_;
97 1 return handle_from_file $self->passwd_file;
98 }
99  
100 1 1;
101  
102 __END__
« Introspector/Probe/Groups.pm ↑ Summary Introspector.pm »