|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for SSL object cache inheritance on configuration reload. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | + |
| 17 | +use lib 'lib'; |
| 18 | +use Test::Nginx; |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +select STDERR; $| = 1; |
| 23 | +select STDOUT; $| = 1; |
| 24 | + |
| 25 | +my $t = Test::Nginx->new()->has(qw/http http_ssl socket_ssl/) |
| 26 | + ->has_daemon('openssl'); |
| 27 | + |
| 28 | +$t->write_file_expand('nginx.conf', << 'EOF'); |
| 29 | +
|
| 30 | +%%TEST_GLOBALS%% |
| 31 | +
|
| 32 | +daemon off; |
| 33 | +
|
| 34 | +ssl_object_cache_inheritable on; |
| 35 | +
|
| 36 | +events { |
| 37 | +} |
| 38 | +
|
| 39 | +http { |
| 40 | + %%TEST_GLOBALS_HTTP%% |
| 41 | +
|
| 42 | + server { |
| 43 | + listen 127.0.0.1:8443 ssl; |
| 44 | + server_name localhost; |
| 45 | +
|
| 46 | + ssl_certificate 1.example.com.crt; |
| 47 | + ssl_certificate_key 1.example.com.key; |
| 48 | + } |
| 49 | +
|
| 50 | + server { |
| 51 | + listen 127.0.0.1:8444 ssl; |
| 52 | + server_name localhost; |
| 53 | +
|
| 54 | + ssl_certificate 2.example.com.crt; |
| 55 | + ssl_certificate_key 2.example.com.key; |
| 56 | + } |
| 57 | +} |
| 58 | +
|
| 59 | +EOF |
| 60 | + |
| 61 | +my $d = $t->testdir(); |
| 62 | + |
| 63 | +$t->write_file('openssl.conf', <<EOF); |
| 64 | +[ req ] |
| 65 | +default_bits = 2048 |
| 66 | +encrypt_key = no |
| 67 | +distinguished_name = req_distinguished_name |
| 68 | +[ req_distinguished_name ] |
| 69 | +EOF |
| 70 | + |
| 71 | +foreach my $name ('1.example.com', '2.example.com', '3.example.com') { |
| 72 | + system('openssl req -x509 -new ' |
| 73 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 74 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 75 | + . ">>$d/openssl.out 2>&1") == 0 |
| 76 | + or die "Can't create certificate for $name: $!\n"; |
| 77 | +} |
| 78 | + |
| 79 | +$t->try_run('no ssl_object_cache_inheritable')->plan(5); |
| 80 | + |
| 81 | +############################################################################### |
| 82 | + |
| 83 | +# make sure SSL certificates are properly cached on configuration reload by: |
| 84 | +# |
| 85 | +# - updating backing storage |
| 86 | +# - keeping inode and mtime metadata |
| 87 | +# (on win32, File ID appears to be modified by in-place rewrite) |
| 88 | + |
| 89 | +like(get_cert_cn(8443), qr!/CN=1.example.com!, 'certificate 1'); |
| 90 | +like(get_cert_cn(8444), qr!/CN=2.example.com!, 'certificate 2'); |
| 91 | + |
| 92 | +update($t, "1.example.com", "3.example.com", update_metadata => 1); |
| 93 | +update($t, "2.example.com", "3.example.com") unless $^O eq 'MSWin32'; |
| 94 | + |
| 95 | +ok(reload($t), 'reload'); |
| 96 | + |
| 97 | +like(get_cert_cn(8443), qr!/CN=3.example.com!, 'certificate updated'); |
| 98 | +like(get_cert_cn(8444), qr!/CN=2.example.com!, 'certificate cached'); |
| 99 | + |
| 100 | +############################################################################### |
| 101 | + |
| 102 | +sub get_cert_cn { |
| 103 | + my ($port) = @_; |
| 104 | + my $s = http('', |
| 105 | + start => 1, |
| 106 | + PeerAddr => '127.0.0.1:' . port($port), |
| 107 | + SSL => 1); |
| 108 | + return $s->dump_peer_certificate(); |
| 109 | +} |
| 110 | + |
| 111 | +sub update { |
| 112 | + my ($t, $old, $new, %extra) = @_; |
| 113 | + |
| 114 | + for my $ext ("crt", "key") { |
| 115 | + if ($extra{update_metadata}) { |
| 116 | + $t->write_file("$old.$ext.tmp", |
| 117 | + $t->read_file("$new.$ext")); |
| 118 | + rename("$d/$old.$ext.tmp", "$d/$old.$ext"); |
| 119 | + |
| 120 | + } else { |
| 121 | + my $mtime = -e "$d/$old.$ext" && (stat(_))[9]; |
| 122 | + $t->write_file("$old.$ext", $t->read_file("$new.$ext")); |
| 123 | + utime(time(), $mtime, "$d/$old.$ext"); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +sub reload { |
| 129 | + my ($t) = @_; |
| 130 | + |
| 131 | + $t->reload(); |
| 132 | + |
| 133 | + for (1 .. 30) { |
| 134 | + return 1 if $t->read_file('error.log') =~ /exited with code/; |
| 135 | + select undef, undef, undef, 0.2; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +############################################################################### |
0 commit comments