| 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.2.2'; # 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 | 2 | ▶ | 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 | 2 | ▶ | ? %{ $params[0] } : @params; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
ref $params[0] || ''
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]
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 2 | return $class->SUPER::new( | |||||||||||||||||||||||||||||||
| 25 | id => 'sftp', | ||||||||||||||||||||||||||||||||
| 26 | label => 'sftp', | ||||||||||||||||||||||||||||||||
| 27 | %params, | ||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | sub check { | ||||||||||||||||||||||||||||||||
| 32 | 18 | ▶ | 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 | 18 | ▶ | if ( ref $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | $params{$_} = $self->{$_} | ||||||||||||||||||||||||||||||||
| 37 | 2 | foreach grep { ! defined $params{$_} } keys %$self; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | # The host is the only required parameter. | ||||||||||||||||||||||||||||||||
| 41 | 18 | ▶ | croak "No host" unless $params{host}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 17 | return $self->SUPER::check(%params); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | sub run { | ||||||||||||||||||||||||||||||||
| 47 | 17 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 48 | 17 | my $host = $params{host}; | |||||||||||||||||||||||||||||||
| 49 | 17 | my $callback = $params{callback}; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | # Get our description of the connection. | ||||||||||||||||||||||||||||||||
| 52 | 17 | my $user = $params{user}; | |||||||||||||||||||||||||||||||
| 53 | 17 | my $name = $params{name}; | |||||||||||||||||||||||||||||||
| 54 | 17 | ▶ | my $target = ( $user ? $user.'@' : '' ).$host; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 17 | ▶ | my $description = $name ? "$name ($target) SFTP" : "$target SFTP"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # Try to connect to the host. | ||||||||||||||||||||||||||||||||
| 58 | 17 | my $sftp; | |||||||||||||||||||||||||||||||
| 59 | 7 | my %args = map { $_ => $params{$_} } | |||||||||||||||||||||||||||||||
| 60 | 17 | grep { exists $params{$_} } | |||||||||||||||||||||||||||||||
| 61 | qw( user password debug warn ssh_args ); | ||||||||||||||||||||||||||||||||
| 62 | 17 | local $@; | |||||||||||||||||||||||||||||||
| 63 | 17 | eval { | |||||||||||||||||||||||||||||||
| 64 | 17 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||
| 65 | 17 | $sftp = Net::SFTP->new( $host, %args ); | |||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||||
| 67 | return { | ||||||||||||||||||||||||||||||||
| 68 | 17 | ▶ | status => 'CRITICAL', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | info => "Error for $description: $@", | ||||||||||||||||||||||||||||||||
| 70 | } if $@; | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | # No errors were returned so it must be a successful result, | ||||||||||||||||||||||||||||||||
| 73 | # unless we want to run a callback. | ||||||||||||||||||||||||||||||||
| 74 | return { | ||||||||||||||||||||||||||||||||
| 75 | 16 | ▶ | status => 'OK', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | info => "Successful connection for $description", | ||||||||||||||||||||||||||||||||
| 77 | } unless $callback; | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | # Try to run a callback on the instance if one is provided. | ||||||||||||||||||||||||||||||||
| 80 | 3 | my $result; | |||||||||||||||||||||||||||||||
| 81 | 3 | eval { | |||||||||||||||||||||||||||||||
| 82 | 3 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||
| 83 | 3 | $result = $callback->( $sftp ); | |||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||
| 85 | return { | ||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | status => 'CRITICAL', | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | info => "Error in running callback for $description: $@", | ||||||||||||||||||||||||||||||||
| 88 | } if $@; | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | # Return the callback result hash, or a generic success message. | ||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | return ref $result eq 'HASH' ? $result : { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | status => 'OK', | ||||||||||||||||||||||||||||||||
| 93 | info => "Successful connection and callback for $description", | ||||||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 2 | 1; | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | =pod | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | HealthCheck::Diagnostic::SFTP - Check for SFTP access and operations in a HealthCheck | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | version v1.2.2 | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | use HealthCheck::Diagnostic::SFTP; | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # Just check that we can connect to a host. | ||||||||||||||||||||||||||||||||
| 118 | HealthCheck::Diagnostic::SFTP->check( | ||||||||||||||||||||||||||||||||
| 119 | host => 'sftp.example.com', | ||||||||||||||||||||||||||||||||
| 120 | user => 'auser', | ||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # Check that the './history' file exists on the host. | ||||||||||||||||||||||||||||||||
| 124 | HealthCheck::Diagnostic::SFTP->check( | ||||||||||||||||||||||||||||||||
| 125 | host => 'sftp.example.com', | ||||||||||||||||||||||||||||||||
| 126 | callback => sub { | ||||||||||||||||||||||||||||||||
| 127 | my ($sftp) = @_; | ||||||||||||||||||||||||||||||||
| 128 | my ($directory) = @{ $sftp->ls('history') || [] }; | ||||||||||||||||||||||||||||||||
| 129 | return { | ||||||||||||||||||||||||||||||||
| 130 | info => 'Looking for "history" file.', | ||||||||||||||||||||||||||||||||
| 131 | status => $directory ? 'OK' : 'CRITCAL', | ||||||||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | This diagnostic allows a process to test SFTP connectivity to a server. | ||||||||||||||||||||||||||||||||
| 139 | You can specify the host and additional parameters and the rest is | ||||||||||||||||||||||||||||||||
| 140 | handled by the diagnostic. | ||||||||||||||||||||||||||||||||
| 141 | Additionally, you can send in a callback to run after connecting for more | ||||||||||||||||||||||||||||||||
| 142 | checks. | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =head2 name | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | A descriptive name for the connection test. | ||||||||||||||||||||||||||||||||
| 149 | This gets populated in the resulting C<info> tag. | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | =head2 host | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | The server name to connect to for the test. | ||||||||||||||||||||||||||||||||
| 154 | This is required. | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | =head2 callback | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | An anonymous sub that can get run after a conneciton is made to the | ||||||||||||||||||||||||||||||||
| 159 | host. This sub receives one argument, the L<Net::SFTP> instance that | ||||||||||||||||||||||||||||||||
| 160 | was recently created. | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | =head2 user | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | Optional argument that can get passed into the L<Net::SFTP> constructor. | ||||||||||||||||||||||||||||||||
| 165 | Represents the authentication user credential for the host. | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | =head2 password | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | Optional argument that can get passed into the L<Net::SFTP> constructor. | ||||||||||||||||||||||||||||||||
| 170 | Represents the authentication password credential for the host. | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | =head2 debug | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | Optional argument that can get passed into the L<Net::SFTP> constructor. | ||||||||||||||||||||||||||||||||
| 175 | Represents whether to print debug information or not. | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | =head2 warn | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | Optional argument that can get passed into the L<Net::SFTP> constructor. | ||||||||||||||||||||||||||||||||
| 180 | An anonymous sub that gets called when warnings are generated. | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | =head2 ssh_args | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | Optional argument that can get passed into the L<Net::SFTP> constructor. | ||||||||||||||||||||||||||||||||
| 185 | Additional SSH connection arguments. | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | L<Net::SFTP> | ||||||||||||||||||||||||||||||||
| 190 | L<HealthCheck::Diagnostic> | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | =head1 CONFIGURATION AND ENVIRONMENT | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | None | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | Grant Street Group <developers@grantstreet.com> | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | This software is Copyright (c) 2018 - 2020 by Grant Street Group. | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||||||