HTTP.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 33.3% 2 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 78.7% 52 / 66 CC 6 SCAR 15.4
CC
6
Cov
80%
startup
6.5
BEGIN
1.0
BEGIN
1.0
CRAP 4.7
« HTTP/Interface/System/Windows.pm ↑ Summary HTTP/Execute.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=3 sw=3 tw=0:
5 # vim: set expandtab:
6
7 =head1 NAME
8  
9 Rex::Endpoint::HTTP - Execute Rex over HTTP
10  
11 =head1 DESCRIPTION
12  
13 This is a replacement for the default SSH endpoint of Rex.
14  
15 =head1 DEPENDENCIES
16  
17 =over 4
18  
19 =item * Mojolicious
20  
21 =item * Digest::SHA
22  
23 =back
24  
25 =head1 CONFIGURATION
26  
27 =head2 Configuration File
28  
29 rex_endpoint_http search at multiple locations for the configuration file. In this file you can define a user database file and configure the build in webserver (hypnotoad from Mojolicious).
30  
31 =over 4
32  
33 =item * /etc/rex/httpd.conf
34  
35 =item * /usr/local/etc/rex/httpd.conf
36  
37 =item * ./httpd.conf
38  
39 =back
40  
41 The format of the file is a Perl Hash Reference. So you have to start and end the file with braces.
42  
43 {
44 key1 => "value1",
45 key2 => "value2",
46 };
47  
48 =head2 Configure Hypnotoad
49  
50 As a default hypnotoad listens on every device on port 8080 without encryption. This is a thing you want to change on production servers. To configure hypnotoad you have to add a new section. It is possible to use it without encryption but we advise you not to do that. If you have your own CA or want to create one, please read I<SSL Authentication>.
51  
52 hypnotoad => {
53 listen => ['https://127.0.0.1:8433'],
54 },
55  
56 The full file may look like this:
57  
58 {
59 hypnotoad => {
60 listen => ['https://127.0.0.1:8443'],
61 },
62 };
63  
64 =head2 User/Password Authentication
65  
66 If you want to use basic user/password authentication you have to add the location of the user file into the configuration. The key is I<user_file>.
67  
68 user_file => "/path/to/your/user.db",
69  
70 The full file may look like this:
71  
72 {
73 user_file => "/path/to/your/user.db",
74
75 # configure hypnotoad
76 hypnotoad => {
77 listen => ['https://127.0.0.1:8443'],
78 },
79 };
80  
81 You also have to create a user database for the authentication.
82  
83 This file exists of 2 columns seperated by ":".
84  
85 username:sha1crypted-password
86  
87 You can create the sha1 strings with the following command.
88  
89 perl -MDigest::SHA -le 'print Digest::SHA::sha1_hex("your-password")'
90  
91  
92 =head2 SSL Authentication
93  
94 If you want to use SSL Authentication (with client/server certificates) you have to configure it. Open the file I</etc/rex/httpd.conf> and modify the I<listen> line. And now you can remove the I<user_file> line.
95  
96 {
97 hypnotoad => {
98 listen => ["https://*:8443?cert=/path/to/your/cert-file.crt&key=/path/to/your/private.key&ca=/path/to/your/ca-file.crt"],
99 },
100 };
101  
102 It is also possible to put Apache or another Webserver in front of it. This is an example configuration:
103  
104 <VirtualHost _default_:8443>
105 ServerName your-server.your-domain.tld
106 ServerAlias your-server
107
108 SSLEngine on
109
110 SSLCertificateFile /etc/apache2/ssl/ssl.crt
111 SSLCertificateKeyFile /etc/apache2/ssl/ssl.key
112 SSLCACertificateFile /etc/apache2/ssl/ca.crt
113
114 SSLVerifyClient require
115 SSLVerifyDepth 10
116 SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
117
118 <Location />
119 ProxyPass http://localhost:8080/
120 ProxyPassReverse http://localhost:8080/
121 </Location>
122
123 </VirtualHost>
124  
125 For this to work you need to create your own Certificate Authority (CA). There is a Rex recipe that will help you. Just create a small Rex project.
126  
127 bash# rexify ca --use Rex::SSL::CA
128 bash# cd ca
129 bash ~/ca# cat >Rexfile<<EOF
130 require Rex::SSL::CA;
131 require Rex::SSL::CA::Server;
132 EOF
133  
134 Please don't use a servername for the I<cn> parameter here!
135  
136 bash# rex SSL:CA:create --password=pass [--country=cn --state=state --city=city --org=organization --unit=organizational-unit --cn=name-of-the-ca --email=email]
137  
138  
139 And to create a server/client certificate you can use the following task. Here you have to use the server name for the I<cn> parameter.
140  
141 bash# rex SSL:CA:Server:create --cn=name-of-the-server --password=password [--challenge-password=challenge-password --country=country --state=state --city=city --org=organization --unit=organizational-unit --email=email]
142  
143 After that you can copy the cert file from ca/certs/$cn.crt and the keyfile from ca/private/$cn.key. The CA Cert file is located at ca/certs/ca.crt.
144  
145 =head1 USAGE
146  
147 To use the HTTP/S endpoint in your rexfiles you just have to enable it.
148  
149 To use HTTP Transport use the following line inside your Rexfile:
150  
151 set connection => "http";
152  
153 To use HTTPS Transport use this one:
154  
155 set connection => "https";
156  
157 If you want to use HTTPS/SSL Authentication use this:
158  
159 set connection => "https";
160 set ca => "/path/to/ca-cert-file.crt";
161 set cert => "/path/to/client-cert-file.crt";
162 set key => "/path/to/client-key-file.key";
163  
164  
165 =head1 INIT
166  
167 You can download an init script from I<https://github.com/krimdomu/rex-endpoint-http/tree/master/doc>.
168  
169 =cut
170  
171  
172 package Rex::Endpoint::HTTP;
173 5 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
174  
175 5 our $VERSION = "0.35.0";
176  
177 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
178 5 $ENV{MOJO_MAX_MESSAGE_SIZE} = 2 * 1024 * 1024 * 1024; # 2 GB
179 };
180  
181 5 ▶use Rex::Endpoint::HTTP::Interface::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 # This method will run once at server start
184 5 ▶sub startup {
Subroutine startup: called CC=6 SCAR=18.7
Pod documented
185 5 my $self = shift;
186  
187 5 my @cfg = ("/etc/rex/httpd.conf", "/usr/local/etc/rex/httpd.conf", "httpd.conf");
188 5 my $cfg = "httpd.conf";
189 5 for my $file (@cfg) {
190 15 ▶ if(-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 15 15
191 0 $cfg = $file;
192 0 last;
193 }
194 }
195  
196 5 ▶ if(-f $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cfg) 0 5 5
197 # load config if available
198 0 $self->plugin('Config', file => $cfg);
199  
200 # do authentication, if needed
201 0 ▶ if(exists $self->{defaults}->{config} && exists $self->{defaults}->{config}->{user_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"defaults"}{"config"} and exists $self->{"defaults"}{"config"}{"user_file"}) 0 0 0
Condition0%0/3
exists $self->{"defaults"}{"config"} and exists $self->{"defaults"}{"config"}{"user_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"defaults"}{"config"} and exists $self->{"defaults"}{"config"}{"user_file"}
exists $self->{"defaults"}{"config"} exists $self->{"defaults"}{"config"}{"user_file"}
Truth table0%0/3
A and B
A= exists $self->{"defaults"}{"config"}B= exists $self->{"defaults"}{"config"}{"user_file"}
ABresult
0X0
100
111
202 0 $self->plugin("Rex::Endpoint::HTTP::Mojolicious::Plugin::Auth");
203 }
204 }
205  
206 # Router
207 5 my $r = $self->routes;
208  
209 # Normal route to controller
210 5 $r->get("/")->to("base#index");
211  
212 5 $r->post("/login")->to("login#index");
213 5 $r->post("/execute")->to("execute#index");
214  
215 5 $r->post("/fs/ls")->to("fs#ls");
216 5 $r->post("/fs/is_dir")->to("fs#is_dir");
217 5 $r->post("/fs/is_file")->to("fs#is_file");
218  
219 5 $r->post("/fs/unlink")->to("fs#unlink");
220 5 $r->post("/fs/mkdir")->to("fs#mkdir");
221  
222 5 $r->post("/fs/stat")->to("fs#stat");
223  
224 5 $r->post("/fs/is_readable")->to("fs#is_readable");
225 5 $r->post("/fs/is_writable")->to("fs#is_writable");
226  
227 5 $r->post("/fs/readlink")->to("fs#readlink");
228 5 $r->post("/fs/rename")->to("fs#rename");
229  
230 5 $r->post("/fs/glob")->to("fs#glob");
231  
232 5 $r->post("/fs/upload")->to("fs#upload");
233 5 $r->post("/fs/download")->to("fs#download");
234  
235 5 $r->post("/fs/rmdir")->to("fs#rmdir");
236 5 $r->post("/fs/ln")->to("fs#ln");
237  
238 5 $r->post("/fs/chown")->to("fs#chown");
239 5 $r->post("/fs/chgrp")->to("fs#chgrp");
240 5 $r->post("/fs/chmod")->to("fs#chmod");
241  
242 5 $r->post("/fs/cp")->to("fs#cp");
243  
244 5 $r->post("/file/open")->to("file#open");
245 5 $r->post("/file/close")->to("file#close");
246 5 $r->post("/file/read")->to("file#read");
247  
248 # this seems odd, but "write" is not allowed as an action
249 5 $r->post("/file/write_fh")->to("file#write_fh");
250  
251 5 $r->post("/file/seek")->to("file#seek");
252  
253 # load system specific routes
254 5 my $sys = Rex::Endpoint::HTTP::Interface::System->create;
255 5 $sys->set_routes($r);
256 }
257  
258 5 1;
« HTTP/Interface/System/Windows.pm ↑ Summary HTTP/Execute.pm »