Skip to content

Commit d0ca7f7

Browse files
committed
Tests: caching SSL certificates with variables to upstream.
1 parent e2a6840 commit d0ca7f7

File tree

2 files changed

+524
-0
lines changed

2 files changed

+524
-0
lines changed

proxy_ssl_certificate_cache.t

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for proxy to ssl backend, proxy_ssl_certificate_cache directive.
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()
26+
->has(qw/http http_ssl proxy openssl:1.0.2/)
27+
->has_daemon('openssl');
28+
29+
$t->write_file_expand('nginx.conf', <<'EOF');
30+
31+
%%TEST_GLOBALS%%
32+
33+
daemon off;
34+
35+
events {
36+
}
37+
38+
http {
39+
%%TEST_GLOBALS_HTTP%%
40+
41+
server {
42+
listen 127.0.0.1:8080;
43+
server_name localhost;
44+
45+
proxy_ssl_session_reuse off;
46+
proxy_ssl_certificate $arg_cert.example.com.crt;
47+
proxy_ssl_certificate_key $arg_cert.example.com.key;
48+
49+
proxy_ssl_certificate_cache max=4 valid=2s;
50+
51+
location / {
52+
proxy_pass https://127.0.0.1:8081/;
53+
}
54+
55+
location /enc {
56+
proxy_pass https://127.0.0.1:8081/;
57+
proxy_ssl_password_file password;
58+
}
59+
60+
location /nocache {
61+
proxy_pass https://127.0.0.1:8081/;
62+
proxy_ssl_certificate_cache off;
63+
}
64+
65+
location /inactive {
66+
proxy_pass https://127.0.0.1:8081/;
67+
proxy_ssl_certificate_cache max=4 inactive=1s;
68+
}
69+
}
70+
71+
server {
72+
listen 127.0.0.1:8081 ssl;
73+
server_name localhost;
74+
75+
ssl_certificate localhost.crt;
76+
ssl_certificate_key localhost.key;
77+
78+
ssl_verify_client optional_no_ca;
79+
ssl_trusted_certificate root.crt;
80+
81+
location / {
82+
add_header X-Name $ssl_client_s_dn;
83+
}
84+
}
85+
}
86+
87+
EOF
88+
89+
my $d = $t->testdir();
90+
91+
$t->write_file('openssl.conf', <<EOF);
92+
[ req ]
93+
default_bits = 2048
94+
encrypt_key = no
95+
distinguished_name = req_distinguished_name
96+
x509_extensions = myca_extensions
97+
[ req_distinguished_name ]
98+
[ myca_extensions ]
99+
basicConstraints = critical,CA:TRUE
100+
EOF
101+
102+
$t->write_file('ca.conf', <<EOF);
103+
[ ca ]
104+
default_ca = myca
105+
106+
[ myca ]
107+
new_certs_dir = $d
108+
database = $d/certindex
109+
default_md = sha256
110+
policy = myca_policy
111+
serial = $d/certserial
112+
default_days = 1
113+
114+
[ myca_policy ]
115+
commonName = supplied
116+
EOF
117+
118+
foreach my $name ('root', 'localhost') {
119+
system('openssl req -x509 -new '
120+
. "-config $d/openssl.conf -subj /CN=$name/ "
121+
. "-out $d/$name.crt -keyout $d/$name.key "
122+
. ">>$d/openssl.out 2>&1") == 0
123+
or die "Can't create certificate for $name: $!\n";
124+
}
125+
126+
$t->write_file('certserial', '1000');
127+
$t->write_file('certindex', '');
128+
129+
foreach my $name ('1.example.com', '2.example.com', '3.example.com',
130+
'4.example.com', '5.example.com', 'dummy')
131+
{
132+
system('openssl req -new '
133+
. "-config $d/openssl.conf -subj /CN=$name/ "
134+
. "-out $d/$name.csr -keyout $d/$name.key "
135+
. ">>$d/openssl.out 2>&1") == 0
136+
or die "Can't create certificate for $name: $!\n";
137+
system("openssl ca -batch -config $d/ca.conf "
138+
. "-keyfile $d/root.key -cert $d/root.crt "
139+
. "-subj /CN=$name/ -in $d/$name.csr -out $d/$name.crt "
140+
. ">>$d/openssl.out 2>&1") == 0
141+
or die "Can't sign certificate for $name: $!\n";
142+
}
143+
144+
foreach my $name ('e.example.com') {
145+
system("openssl genrsa -out $d/$name.key -passout pass:$name "
146+
. "-aes128 2048 >>$d/openssl.out 2>&1") == 0
147+
or die "Can't create private key: $!\n";
148+
system('openssl req -new '
149+
. "-config $d/openssl.conf -subj /CN=$name/ "
150+
. "-out $d/$name.csr "
151+
. "-key $d/$name.key -passin pass:$name"
152+
. ">>$d/openssl.out 2>&1") == 0
153+
or die "Can't create certificate for $name: $!\n";
154+
system("openssl ca -batch -config $d/ca.conf "
155+
. "-keyfile $d/root.key -cert $d/root.crt "
156+
. "-subj /CN=$name/ -in $d/$name.csr -out $d/$name.crt "
157+
. ">>$d/openssl.out 2>&1") == 0
158+
or die "Can't sign certificate for $name: $!\n";
159+
}
160+
161+
update($t, '4b.example.com', '4.example.com');
162+
update($t, 'eb.example.com', 'e.example.com');
163+
164+
$t->write_file('password', 'e.example.com');
165+
$t->write_file('index.html', '');
166+
167+
$t->try_run('no proxy_ssl_certificate_cache')->plan(20);
168+
169+
###############################################################################
170+
171+
like(http_get('/?cert=1'), qr/CN=1.example.com/, 'certificate 1');
172+
173+
update($t, '1.example.com');
174+
like(http_get('/?cert=1'), qr/CN=1.example.com/, 'certificate 1 cached');
175+
176+
like(http_get('/?cert=2'), qr/CN=2.example.com/, 'certificate 2');
177+
like(http_get('/?cert=3'), qr/CN=3.example.com/, 'certificate 3');
178+
179+
# eviction after inserting 4 new items
180+
181+
like(http_get('/?cert=1'), qr/500 Internal/, 'certificate 1 evicted');
182+
183+
update($t, '2.example.com', 'dummy');
184+
update($t, '3.example.com');
185+
186+
# replaced or removed certificates do not affect caching
187+
188+
like(http_get('/?cert=2'), qr/CN=2.example.com/, 'certificate 2 cached');
189+
like(http_get('/?cert=3'), qr/CN=3.example.com/, 'certificate 3 cached');
190+
191+
# encrypted certificates are exempt from caching
192+
193+
like(http_get('/enc/?cert=e'), qr/CN=e.example.com/, 'encrypted');
194+
195+
like(http_get('/?cert=e'), qr/500 Internal/, 'encrypted no password');
196+
197+
update($t, 'e.example.com', 'dummy');
198+
like(http_get('/enc/?cert=e'), qr/CN=dummy/, 'encrypted not cached');
199+
200+
# replacing non-cacheable item with cacheable doesn't affect cacheability
201+
202+
update($t, 'e.example.com', 'eb.example.com');
203+
like(http_get('/enc/?cert=e'), qr/CN=dummy/, 'cached after encrypted');
204+
205+
like(http_get('/nocache/?cert=4'), qr/CN=4.example.com/, 'no cache');
206+
207+
update($t, '4.example.com', 'dummy');
208+
like(http_get('/nocache/?cert=4'), qr/CN=dummy/, 'no cache updated');
209+
210+
like(http_get('/inactive/?cert=5'), qr/CN=5.example.com/, 'inactive');
211+
212+
select undef, undef, undef, 3.1;
213+
214+
like(http_get('/?cert=2'), qr/CN=dummy/, 'certificate 2 expired');
215+
like(http_get('/?cert=3'), qr/500 Internal/, 'certificate 3 expired');
216+
217+
# replacing cacheable item with non-cacheable doesn't affect cacheability
218+
219+
like(http_get('/enc/?cert=e'), qr/CN=e.example.com/, 'encrypted after cached');
220+
221+
update($t, 'e.example.com', 'dummy');
222+
like(http_get('/enc/?cert=e'), qr/CN=dummy/,
223+
'encrypted not cached after cached');
224+
225+
# eviction after inactive time
226+
227+
update($t, '5.example.com', 'dummy');
228+
229+
like(http_get('/inactive/?cert=4b'), qr/CN=4.example.com/, 'inactive expire');
230+
like(http_get('/inactive/?cert=5'), qr/CN=dummy/, 'inactive expired');
231+
232+
###############################################################################
233+
234+
sub update {
235+
my ($t, $old, $new) = @_;
236+
237+
for my $ext ("crt", "key") {
238+
if (defined $new) {
239+
$t->write_file("$old.$ext.tmp",
240+
$t->read_file("$new.$ext"));
241+
rename("$d/$old.$ext.tmp", "$d/$old.$ext");
242+
243+
} else {
244+
unlink "$d/$old.$ext";
245+
}
246+
}
247+
}
248+
249+
###############################################################################

0 commit comments

Comments
 (0)