1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
#!/usr/bin/perl
# (C) Dmitry Volyntsev
# (C) Nginx, Inc.
# Tests for stream njs module, fetch method, https support.
###############################################################################
use warnings;
use strict;
use Test::More;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
use Test::Nginx::Stream qw/ stream /;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()
->has(qw/http http_ssl rewrite stream stream_return socket_ssl/)
->has_daemon('openssl')
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
js_import test.js;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /njs {
js_content test.njs;
}
location /engine {
js_content test.engine;
}
}
server {
listen 127.0.0.1:8081 ssl default;
server_name default.example.com;
ssl_certificate default.example.com.chained.crt;
ssl_certificate_key default.example.com.key;
location /loc {
return 200 "You are at default.example.com.";
}
location /success {
return 200;
}
location /fail {
return 403;
}
location /backend {
return 200 "BACKEND OK";
}
}
server {
listen 127.0.0.1:8081 ssl;
server_name 1.example.com;
ssl_certificate 1.example.com.chained.crt;
ssl_certificate_key 1.example.com.key;
location /loc {
return 200 "You are at 1.example.com.";
}
}
}
stream {
%%TEST_GLOBALS_STREAM%%
js_import test.js;
js_var $message;
resolver 127.0.0.1:%%PORT_8981_UDP%%;
resolver_timeout 1s;
server {
listen 127.0.0.1:8082;
js_preread test.preread;
return "default CA $message";
}
server {
listen 127.0.0.1:8083;
js_preread test.preread;
return "my CA $message";
js_fetch_ciphers HIGH:!aNull:!MD5;
js_fetch_protocols TLSv1.1 TLSv1.2;
js_fetch_trusted_certificate myca.crt;
}
server {
listen 127.0.0.1:8084;
js_preread test.preread;
return "my CA with verify_depth=0 $message";
js_fetch_verify_depth 0;
js_fetch_trusted_certificate myca.crt;
}
server {
listen 127.0.0.1:8085;
js_access test.access_ok;
ssl_preread on;
js_fetch_ciphers HIGH:!aNull:!MD5;
js_fetch_protocols TLSv1.1 TLSv1.2;
js_fetch_trusted_certificate myca.crt;
proxy_pass 127.0.0.1:8081;
}
server {
listen 127.0.0.1:8086;
js_access test.access_nok;
ssl_preread on;
js_fetch_ciphers HIGH:!aNull:!MD5;
js_fetch_protocols TLSv1.1 TLSv1.2;
js_fetch_trusted_certificate myca.crt;
proxy_pass 127.0.0.1:8081;
}
}
EOF
my $p1 = port(8081);
my $p2 = port(8082);
my $p3 = port(8083);
my $p4 = port(8084);
$t->write_file('test.js', <<EOF);
function test_njs(r) {
r.return(200, njs.version);
}
function engine(r) {
r.return(200, njs.engine);
}
function preread(s) {
s.on('upload', function (data, flags) {
if (data.startsWith('GO')) {
s.off('upload');
ngx.fetch('https://' + data.substring(2) + ':$p1/loc')
.then(reply => {
s.variables.message = 'https OK - ' + reply.status;
s.done();
})
.catch(e => {
s.variables.message = 'https NOK - ' + e.message;
s.done();
})
} else if (data.length) {
s.deny();
}
});
}
async function access_ok(s) {
let r = await ngx.fetch('https://default.example.com:$p1/success',
{body: s.remoteAddress});
(r.status == 200) ? s.allow(): s.deny();
}
async function access_nok(s) {
let r = await ngx.fetch('https://default.example.com:$p1/fail',
{body: s.remoteAddress});
(r.status == 200) ? s.allow(): s.deny();
}
export default {njs: test_njs, engine, preread, access_ok, access_nok};
EOF
my $d = $t->testdir();
$t->write_file('openssl.conf', <<EOF);
[ req ]
default_bits = 2048
encrypt_key = no
distinguished_name = req_distinguished_name
x509_extensions = myca_extensions
[ req_distinguished_name ]
[ myca_extensions ]
basicConstraints = critical,CA:TRUE
EOF
$t->write_file('myca.conf', <<EOF);
[ ca ]
default_ca = myca
[ myca ]
new_certs_dir = $d
database = $d/certindex
default_md = sha256
policy = myca_policy
serial = $d/certserial
default_days = 1
x509_extensions = myca_extensions
[ myca_policy ]
commonName = supplied
[ myca_extensions ]
basicConstraints = critical,CA:TRUE
EOF
system('openssl req -x509 -new '
. "-config $d/openssl.conf -subj /CN=myca/ "
. "-out $d/myca.crt -keyout $d/myca.key "
. ">>$d/openssl.out 2>&1") == 0
or die "Can't create self-signed certificate for CA: $!\n";
foreach my $name ('intermediate', 'default.example.com', '1.example.com') {
system("openssl req -new "
. "-config $d/openssl.conf -subj /CN=$name/ "
. "-out $d/$name.csr -keyout $d/$name.key "
. ">>$d/openssl.out 2>&1") == 0
or die "Can't create certificate signing req for $name: $!\n";
}
$t->write_file('certserial', '1000');
$t->write_file('certindex', '');
system("openssl ca -batch -config $d/myca.conf "
. "-keyfile $d/myca.key -cert $d/myca.crt "
. "-subj /CN=intermediate/ -in $d/intermediate.csr "
. "-out $d/intermediate.crt "
. ">>$d/openssl.out 2>&1") == 0
or die "Can't sign certificate for intermediate: $!\n";
foreach my $name ('default.example.com', '1.example.com') {
system("openssl ca -batch -config $d/myca.conf "
. "-keyfile $d/intermediate.key -cert $d/intermediate.crt "
. "-subj /CN=$name/ -in $d/$name.csr -out $d/$name.crt "
. ">>$d/openssl.out 2>&1") == 0
or die "Can't sign certificate for $name $!\n";
$t->write_file("$name.chained.crt", $t->read_file("$name.crt")
. $t->read_file('intermediate.crt'));
}
$t->try_run('no njs.fetch');
plan(skip_all => 'not yet') if http_get('/engine') =~ /QuickJS$/m;
$t->plan(6);
$t->run_daemon(\&dns_daemon, port(8981), $t);
$t->waitforfile($t->testdir . '/' . port(8981));
###############################################################################
like(stream("127.0.0.1:$p2")->io('GOdefault.example.com'),
qr/connect failed/s, 'stream non trusted CA');
like(stream("127.0.0.1:$p3")->io('GOdefault.example.com'),
qr/https OK/s, 'stream trusted CA');
like(stream("127.0.0.1:$p3")->io('GOlocalhost'),
qr/connect failed/s, 'stream wrong CN');
like(stream("127.0.0.1:$p4")->io('GOdefaul.example.com'),
qr/connect failed/s, 'stream verify_depth too small');
like(https_get('default.example.com', port(8085), '/backend'),
qr!BACKEND OK!, 'access https fetch');
is(https_get('default.example.com', port(8086), '/backend'), '<conn failed>',
'access https fetch not');
###############################################################################
sub get_ssl_socket {
my ($host, $port) = @_;
my $s;
eval {
local $SIG{ALRM} = sub { die "timeout\n" };
local $SIG{PIPE} = sub { die "sigpipe\n" };
alarm(8);
$s = IO::Socket::SSL->new(
Proto => 'tcp',
PeerAddr => '127.0.0.1:' . $port,
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
SSL_error_trap => sub { die $_[1] }
);
alarm(0);
};
alarm(0);
if ($@) {
log_in("died: $@");
return undef;
}
return $s;
}
sub https_get {
my ($host, $port, $url) = @_;
my $s = get_ssl_socket($host, $port);
if (!$s) {
return '<conn failed>';
}
return http(<<EOF, socket => $s);
GET $url HTTP/1.0
Host: $host
EOF
}
###############################################################################
sub reply_handler {
my ($recv_data, $port, %extra) = @_;
my (@name, @rdata);
use constant NOERROR => 0;
use constant A => 1;
use constant IN => 1;
# default values
my ($hdr, $rcode, $ttl) = (0x8180, NOERROR, 3600);
# decode name
my ($len, $offset) = (undef, 12);
while (1) {
$len = unpack("\@$offset C", $recv_data);
last if $len == 0;
$offset++;
push @name, unpack("\@$offset A$len", $recv_data);
$offset += $len;
}
$offset -= 1;
my ($id, $type, $class) = unpack("n x$offset n2", $recv_data);
my $name = join('.', @name);
if ($type == A) {
push @rdata, rd_addr($ttl, '127.0.0.1');
}
$len = @name;
pack("n6 (C/a*)$len x n2", $id, $hdr | $rcode, 1, scalar @rdata,
0, 0, @name, $type, $class) . join('', @rdata);
}
sub rd_addr {
my ($ttl, $addr) = @_;
my $code = 'split(/\./, $addr)';
return pack 'n3N', 0xc00c, A, IN, $ttl if $addr eq '';
pack 'n3N nC4', 0xc00c, A, IN, $ttl, eval "scalar $code", eval($code);
}
sub dns_daemon {
my ($port, $t) = @_;
my ($data, $recv_data);
my $socket = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => $port,
Proto => 'udp',
)
or die "Can't create listening socket: $!\n";
local $SIG{PIPE} = 'IGNORE';
# signal we are ready
open my $fh, '>', $t->testdir() . '/' . $port;
close $fh;
while (1) {
$socket->recv($recv_data, 65536);
$data = reply_handler($recv_data, $port);
$socket->send($data);
}
}
###############################################################################
|