Connector/Proxy/Net/SFTP.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 196 CC 12 SCAR 23.4
CC
12
Cov
0%
_sanitize_path
30.0
set
20.0
get
6.0
CRAP 10.4
1 package Connector::Proxy::Net::SFTP;
2  
3 use strict;
4 use warnings;
5 use English;
6 use File::Spec;
7 use File::Temp qw(tempfile tempdir);
8 use File::Basename;
9 use Net::SFTP;
10 use Template;
11 use Data::Dumper;
12 use Feature::Compat::Try;
13  
14 use Moose;
15 extends 'Connector::Proxy';
16 with 'Connector::Role::LocalPath';
17  
18 has port => (
19 is => 'rw',
20 isa => 'Int',
21 default => 22,
22 );
23  
24 has basedir => (
25 is => 'rw',
26 isa => 'Str',
27 );
28  
29 has content => (
30 is => 'rw',
31 isa => 'Str',
32 );
33  
34 has username => (
35 is => 'rw',
36 isa => 'Str',
37 required => 1,
38 );
39  
40 has password => (
41 is => 'rw',
42 isa => 'Str',
43 required => 1,
44 );
45  
46 has timeout => (
47 is => 'rw',
48 isa => 'Int',
49 default => 30
50 );
51  
52 has debug => (
53 is => 'rw',
54 isa => 'Bool',
55 default => 0,
56 );
57  
58 has ssh_args => (
59 is => 'rw',
60 isa => 'HashRef',
61 lazy => 1,
62 default => sub { return {} }
63 );
64  
65 has _client => (
66 is => 'ro',
67 isa => 'Net::SFTP',
68 lazy => 1,
69 builder => '_init_client',
70 );
71  
72 # return the content of the file
73 sub get {
74  
75 my $self = shift;
76 my $path = shift;
77  
78 my $sftp = $self->_client();
79  
80 my $filename = $self->_sanitize_path( $path );
81 $self->log()->debug('Fetch '. $filename );
82  
83 my $content = $sftp->get( $filename );
84 return $content if (defined $content);
85  
86 $self->log()->info("Cannot read file $filename");
87 return $self->_node_not_exists();
88 }
89  
90 sub get_keys {
91  
92 my $self = shift;
93 my $path = shift;
94  
95 my $dirname = $self->_sanitize_path( $path );
96  
97 $self->log()->debug('Fetch directory ' . $dirname );
98 my $ftp = $self->_client();
99  
100 my @files = $ftp->ls($dirname);
101 my @names = map { ($_->{filename} !~ /\A\.\.?\z/) ? $_->{filename} : () } @files;
102 $self->log()->debug('List content of directory ' . (join "|", @names));
103 return @names;
104  
105 }
106  
107 sub get_meta {
108 my $self = shift;
109 return {TYPE => "scalar" };
110 }
111  
112  
113 sub exists {
114  
115 my $self = shift;
116  
117 # No path = connector root which always exists
118 my @path = $self->_build_path_with_prefix( shift );
119 if (scalar @path == 0) {
120 return 1;
121 }
122  
123 return 1;
124  
125 }
126  
127  
128 # return the content of the file
129 sub set {
130  
131 my $self = shift;
132 my $file = shift;
133 my $data = shift;
134  
135 my $content;
136 if ($self->content()) {
137 $self->log()->debug('Process template for content ' . $self->content());
138 my $template = Template->new({});
139  
140 $data = { DATA => $data } if (ref $data eq '');
141  
142 $template->process( \$self->content(), $data, \$content) || $self->_log_and_die("Error processing content template.");
143 } else {
144 if (ref $data ne '') {
145 $self->_log_and_die("You need to define a content template if data is not a scalar");
146 }
147 $content = $data;
148 }
149  
150 my $tmpdir = tempdir( CLEANUP => 1 );
151 my ($fh, $source) = tempfile( DIR => $tmpdir );
152  
153 open FILE, ">$source" || $self->_log_and_die("Unable to open file for writing");
154 print FILE $content;
155 close FILE;
156  
157 my $sftp = $self->_client();
158  
159 my $filename = $self->_sanitize_path( $file, $data );
160  
161 $self->log()->debug('Send put '. $source . ' => ' . $filename );
162 $sftp->put( $source, $filename)
163 or $self->_log_and_die('put failed: ' . $sftp->status);
164  
165 return 1;
166 }
167  
168 sub _sanitize_path {
169  
170 my $self = shift;
171 my $inargs = shift;
172 my $data = shift;
173  
174 my @args = $self->_build_path_with_prefix( $inargs );
175  
176 my $file;
177 my $template = Template->new({});
178  
179 if ($self->path() || $self->file()) {
180 $file = $self->_render_local_path( \@args, $data );
181 } else {
182 $self->log()->debug('Neither target pattern nor file set, join arguments');
183 map {
184 if ($_ =~ /\.\.|\//) {
185 $self->_log_and_die("args contains invalid characters (double dot or slash)");
186 }
187 } @args;
188 $file = join("/", @args);
189 $file =~ s/[^\s\w\.\-\\\/]//g;
190 }
191  
192 $self->log()->debug('Filename evaluated to ' . $file);
193  
194 if (my $basedir = $self->basedir()) {
195 $file = File::Spec->catfile($basedir, $file);
196 $self->log()->debug('Added basedir: ' . $file);
197 }
198 if (wantarray) {
199 return (dirname($file), basename($file));
200 } else {
201 return $file;
202 }
203  
204 }
205  
206 sub _init_client {
207  
208 my $self = shift;
209 my $sftp;
210 try {
211 my %args = (
212 debug => $self->debug() ? 1 : 0,
213 user => $self->username(),
214 ssh_args => { %{$self->ssh_args()}, port => $self->port() },
215 );
216 $self->log()->trace(Dumper \%args);
217 $sftp = Net::SFTP->new( $self->LOCATION(),
218 %args,
219 password => $self->password(),
220 'warn' => sub { shift; $self->log()->warn('SFTP: ' . join(",", @_)) }
221 );
222 } catch ($error) {
223 $self->_log_and_die(sprintf("Cannot connect to %s (%s)", $self->LOCATION(), $error));
224 }
225 return $sftp;
226 }
227  
228 1;
229 __END__