|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Copyright (c) F5, Inc. |
| 4 | +# |
| 5 | +# This source code is licensed under the Apache License, Version 2.0 license |
| 6 | +# found in the LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# Tests for ACME client: redirect and pathless URI handling. |
| 9 | + |
| 10 | +############################################################################### |
| 11 | + |
| 12 | +use warnings; |
| 13 | +use strict; |
| 14 | + |
| 15 | +use Test::More; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx; |
| 21 | +use Test::Nginx::ACME; |
| 22 | +use Test::Nginx::DNS; |
| 23 | + |
| 24 | +############################################################################### |
| 25 | + |
| 26 | +select STDERR; $| = 1; |
| 27 | +select STDOUT; $| = 1; |
| 28 | + |
| 29 | +my $t = Test::Nginx->new()->has(qw/http http_ssl rewrite socket_ssl/) |
| 30 | + ->has_daemon('openssl'); |
| 31 | + |
| 32 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 33 | +
|
| 34 | +%%TEST_GLOBALS%% |
| 35 | +
|
| 36 | +daemon off; |
| 37 | +
|
| 38 | +events { |
| 39 | +} |
| 40 | +
|
| 41 | +http { |
| 42 | + %%TEST_GLOBALS_HTTP%% |
| 43 | +
|
| 44 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 45 | +
|
| 46 | + acme_issuer default { |
| 47 | + uri https://acme.test:%%PORT_8999%%; |
| 48 | + ssl_trusted_certificate acme.test.crt; |
| 49 | + state_path %%TESTDIR%%; |
| 50 | + accept_terms_of_service; |
| 51 | + } |
| 52 | +
|
| 53 | + server { |
| 54 | + listen 127.0.0.1:8080; |
| 55 | + server_name example.test; |
| 56 | + } |
| 57 | +
|
| 58 | + server { |
| 59 | + listen 127.0.0.1:8443 ssl; |
| 60 | + server_name example.test; |
| 61 | +
|
| 62 | + acme_certificate default; |
| 63 | +
|
| 64 | + ssl_certificate $acme_certificate; |
| 65 | + ssl_certificate_key $acme_certificate_key; |
| 66 | + } |
| 67 | +
|
| 68 | + server { |
| 69 | + listen 127.0.0.1:8999 ssl; |
| 70 | + server_name acme.test; |
| 71 | +
|
| 72 | + absolute_redirect off; |
| 73 | +
|
| 74 | + ssl_certificate acme.test.crt; |
| 75 | + ssl_certificate_key acme.test.key; |
| 76 | +
|
| 77 | + location / { |
| 78 | + return 301 /dir; |
| 79 | + } |
| 80 | +
|
| 81 | + location /dir { |
| 82 | + return 302 https://acme.test:%%PORT_9000%%/dir; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +EOF |
| 88 | + |
| 89 | +$t->write_file('openssl.conf', <<EOF); |
| 90 | +[ req ] |
| 91 | +default_bits = 2048 |
| 92 | +encrypt_key = no |
| 93 | +distinguished_name = req_distinguished_name |
| 94 | +[ req_distinguished_name ] |
| 95 | +EOF |
| 96 | + |
| 97 | +my $d = $t->testdir(); |
| 98 | + |
| 99 | +foreach my $name ('acme.test') { |
| 100 | + system('openssl req -x509 -new ' |
| 101 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 102 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 103 | + . ">>$d/openssl.out 2>&1") == 0 |
| 104 | + or die "Can't create certificate for $name: $!\n"; |
| 105 | +} |
| 106 | + |
| 107 | +my $dp = port(8980, udp=>1); |
| 108 | +my @dc = ( |
| 109 | + { name => 'acme.test', A => '127.0.0.1' }, |
| 110 | + { name => 'example.test', A => '127.0.0.1' } |
| 111 | +); |
| 112 | + |
| 113 | +my $acme = Test::Nginx::ACME->new($t, port(9000), port(9001), |
| 114 | + $t->testdir . '/acme.test.crt', |
| 115 | + $t->testdir . '/acme.test.key', |
| 116 | + http_port => port(8080), |
| 117 | + dns_port => $dp, |
| 118 | + nosleep => 1, |
| 119 | +); |
| 120 | + |
| 121 | +$t->run_daemon(\&Test::Nginx::DNS::dns_test_daemon, $t, $dp, \@dc); |
| 122 | +$t->waitforfile($t->testdir . '/' . $dp); |
| 123 | + |
| 124 | +$t->run_daemon(\&Test::Nginx::ACME::acme_test_daemon, $t, $acme); |
| 125 | +$t->waitforsocket('127.0.0.1:' . $acme->port()); |
| 126 | +$t->write_file('acme-root.crt', $acme->trusted_ca()); |
| 127 | + |
| 128 | +$t->write_file('index.html', 'SUCCESS'); |
| 129 | +$t->plan(1)->run(); |
| 130 | + |
| 131 | +############################################################################### |
| 132 | + |
| 133 | +$acme->wait_certificate('example.test') or die "no certificate"; |
| 134 | + |
| 135 | +like(get(8443, 'example.test', 'acme-root'), qr/SUCCESS/, 'tls request'); |
| 136 | + |
| 137 | +############################################################################### |
| 138 | + |
| 139 | +sub get { |
| 140 | + my ($port, $host, $ca) = @_; |
| 141 | + |
| 142 | + $ca = undef if $IO::Socket::SSL::VERSION < 2.062 |
| 143 | + || !eval { Net::SSLeay::X509_V_FLAG_PARTIAL_CHAIN() }; |
| 144 | + |
| 145 | + http_get('/', |
| 146 | + PeerAddr => '127.0.0.1:' . port($port), |
| 147 | + SSL => 1, |
| 148 | + $ca ? ( |
| 149 | + SSL_ca_file => "$d/$ca.crt", |
| 150 | + SSL_verifycn_name => $host, |
| 151 | + SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(), |
| 152 | + ) : () |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +############################################################################### |
0 commit comments