blib/lib/HealthCheck/Diagnostic/SFTP.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 95.0% 19 / 20 Conditioncond 63.6% 7 / 11 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 92.7% 102 / 110 CC 17 SCAR 19.1
CC
17
Cov
94%
run
11.0
new
6.6
check
4.0
CRAP 6.8
↑ Summary
1 package HealthCheck::Diagnostic::SFTP;
2 2 ▶use parent 'HealthCheck::Diagnostic';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Check for SFTP access and operations in a HealthCheck
5 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = 'v1.4.0'; # VERSION
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::SFTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # For some reason, without this in the cpanfile
15 # Net::SFTP wouldn't install, so leave this note.
16 2 require Net::SSH::Perl::Buffer;
17  
18 sub new {
19 6 ▶ my ($class, @params) = @_;
Subroutine new: called CC=4 SCAR=18.9
Pod documented
20  
21 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
22 6 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 6 6
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
600
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
23  
24 6 return $class->SUPER::new(
25 id => 'sftp',
26 label => 'sftp',
27 %params,
28 );
29 }
30  
31 sub check {
32 22 ▶ my ($self, %params) = @_;
Subroutine check: called CC=4 SCAR=13.9
Pod documented
33  
34 # Allow the diagnostic to be called as a class as well.
35 22 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 6 16 22
36 $params{$_} = $self->{$_}
37 6 foreach grep { ! defined $params{$_} } keys %$self;
38 }
39  
40 # The host is the only required parameter.
41 22 ▶ croak "No host" unless $params{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"host"} 1 21 22
42  
43 21 return $self->SUPER::check(%params);
44 }
45  
46 sub run {
47 21 ▶ my ($self, %params) = @_;
Subroutine run: called CC=11 SCAR=24.0
Pod documented
48 21 my $host = $params{host};
49 21 my $callback = $params{callback};
50  
51 # Get our description of the connection.
52 21 my $user = $params{user};
53 21 my $name = $params{name};
54 21 ▶ my $timeout = $params{timeout} // 3;
Condition100%2/2
$params{"timeout"} // 3
l!l
219
MC/DC100%1/1
$params{"timeout"} // 3
$params{"timeout"}
Truth table100%2/2
A
A= $params{"timeout"}
Aresult
11
00
55 21 ▶ my $target = ( $user ? $user.'@' : '' ).$host;
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 3 18 21
56 21 ▶ my $description = $name ? "$name ($target) SFTP" : "$target SFTP";
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 2 19 21
57  
58 21 ▶ my $ssh_args = $params{ssh_args} // {};
Condition100%2/2
$params{"ssh_args"} // {}
l!l
318
MC/DC100%1/1
$params{"ssh_args"} // {}
$params{"ssh_args"}
Truth table100%2/2
A
A= $params{"ssh_args"}
Aresult
11
00
59 21 ▶ my $options = $ssh_args->{options} // [];
Condition100%2/2
$ssh_args->{"options"} // []
l!l
219
MC/DC100%1/1
$ssh_args->{"options"} // []
$ssh_args->{"options"}
Truth table100%2/2
A
A= $ssh_args->{"options"}
Aresult
11
00
60  
61 21 ▶ unless ( grep { $_ =~ /^ConnectTimeout / } @$options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ =~ /^ConnectTimeout /;} @$options) 20 1 21
62 20 push @$options, "ConnectTimeout $timeout";
63 20 $ssh_args->{options} = $options;
64 }
65  
66 # Try to connect to the host.
67 21 my $sftp;
68 6 my %args = map { $_ => $params{$_} }
69 21 grep { exists $params{$_} }
70 qw( user password debug warn );
71 21 $args{ssh_args} = $ssh_args;
72  
73 21 local $@;
74 21 eval {
75 21 local $SIG{__DIE__};
76 21 $sftp = Net::SFTP->new( $host, %args );
77 };
78 return {
79 21 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 20 21
80 info => "Error for $description: $@",
81 } if $@;
82  
83 # No errors were returned so it must be a successful result,
84 # unless we want to run a callback.
85 return {
86 20 ▶ status => 'OK',
Branch: 1 branch
BranchTrueFalseTotal
unless $callback 17 3 20
87 info => "Successful connection for $description",
88 } unless $callback;
89  
90 # Try to run a callback on the instance if one is provided.
91 3 my $result;
92 3 eval {
93 3 local $SIG{__DIE__};
94 3 $result = $callback->( $sftp );
95 };
96 return {
97 3 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
98 info => "Error in running callback for $description: $@",
99 } if $@;
100  
101 # Return the callback result hash, or a generic success message.
102 2 ▶ return ref $result eq 'HASH' ? $result : {
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 1 1 2
103 status => 'OK',
104 info => "Successful connection and callback for $description",
105 };
106 }
107  
108 2 1;
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 HealthCheck::Diagnostic::SFTP - Check for SFTP access and operations in a HealthCheck
119  
120 =head1 VERSION
121  
122 version v1.4.0
123  
124 =head1 SYNOPSIS
125  
126 use HealthCheck::Diagnostic::SFTP;
127  
128 # Just check that we can connect to a host.
129 HealthCheck::Diagnostic::SFTP->check(
130 host => 'sftp.example.com',
131 user => 'auser',
132 timeout => 3, # default
133 );
134  
135 # Check that the './history' file exists on the host.
136 HealthCheck::Diagnostic::SFTP->check(
137 host => 'sftp.example.com',
138 callback => sub {
139 my ($sftp) = @_;
140 my ($directory) = @{ $sftp->ls('history') || [] };
141 return {
142 info => 'Looking for "history" file.',
143 status => $directory ? 'OK' : 'CRITCAL',
144 };
145 },
146 );
147  
148 =head1 DESCRIPTION
149  
150 This diagnostic allows a process to test SFTP connectivity to a server.
151 You can specify the host and additional parameters and the rest is
152 handled by the diagnostic.
153 Additionally, you can send in a callback to run after connecting for more
154 checks.
155  
156 =head1 ATTRIBUTES
157  
158 =head2 name
159  
160 A descriptive name for the connection test.
161 This gets populated in the resulting C<info> tag.
162  
163 =head2 host
164  
165 The server name to connect to for the test.
166 This is required.
167  
168 =head2 callback
169  
170 An anonymous sub that can get run after a conneciton is made to the
171 host. This sub receives one argument, the L<Net::SFTP> instance that
172 was recently created.
173  
174 =head2 user
175  
176 Optional argument that can get passed into the L<Net::SFTP> constructor.
177 Represents the authentication user credential for the host.
178  
179 =head2 password
180  
181 Optional argument that can get passed into the L<Net::SFTP> constructor.
182 Represents the authentication password credential for the host.
183  
184 =head2 debug
185  
186 Optional argument that can get passed into the L<Net::SFTP> constructor.
187 Represents whether to print debug information or not.
188  
189 =head2 warn
190  
191 Optional argument that can get passed into the L<Net::SFTP> constructor.
192 An anonymous sub that gets called when warnings are generated.
193  
194 =head2 ssh_args
195  
196 Optional argument that can get passed into the L<Net::SFTP> constructor.
197 Additional SSH connection arguments.
198  
199 =head2 timeout
200  
201 Set for the C<ConnectTimeout> value passed to the C<ssh_args> C<options> setting
202 in L<Net::SSH::Perl>.
203 Will not be set if an existing C<ConnectTimeout> value has been set.
204 Defaults to 3.
205  
206 =head1 DEPENDENCIES
207  
208 L<Net::SFTP>
209 L<HealthCheck::Diagnostic>
210  
211 =head1 CONFIGURATION AND ENVIRONMENT
212  
213 None
214  
215 =head1 AUTHOR
216  
217 Grant Street Group <developers@grantstreet.com>
218  
219 =head1 COPYRIGHT AND LICENSE
220  
221 This software is Copyright (c) 2018 - 2022 by Grant Street Group.
222  
223 This is free software, licensed under:
224  
225 The Artistic License 2.0 (GPL Compatible)
226  
227 =cut
↑ Summary