blib/lib/Mojolicious/Plugin/ACME/Command/acme/automate.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 177 CC 22 SCAR 55.3
CC
22
Cov
0%
_automate
380.0
run
20.0
BEGIN
2.0
CRAP 252.1
1 package Mojolicious::Plugin::ACME::Command::acme::automate;
2 use Mojo::Base 'Mojolicious::Plugin::ACME::Command';
3  
4 our $VERSION = '0.02';
5  
6 use Mojo::Collection 'c';
7 use Mojo::File 'path';
8 use Mojo::Server::Daemon;
9 use Mojo::Template;
10 use Mojo::Util 'getopt';
11  
12 has description => 'For all specified domains, generate certificates signed by the ACME service';
13 has usage => sub {
14 my $c = shift;
15 $c->extract_usage . $c->common_usage;
16 };
17  
18 sub run {
19 my ($self, @args) = @_;
20  
21 my @daemon_args;
22 getopt \@args, ['pass_through'],
23 'l|listen=s' => sub { push @daemon_args, '-l', $_[1] },
24 'p|proxy' => sub { push @daemon_args, '-p', $_[1] };
25  
26 Mojo::IOLoop->next_tick(sub {
27 Mojo::IOLoop->subprocess(
28 sub {
29 my $subprocess = shift;
30 $self->_automate(@args);
31 },
32 sub {
33 my ($subprocess, $err, @results) = @_;
34 warn $err if $err;
35 say @results if @results;
36 Mojo::IOLoop->stop if Mojo::IOLoop->is_running;
37 }
38 );
39 });
40 $self->app->commands->run('daemon', @daemon_args);
41 }
42  
43 sub _automate {
44 my ($self, @args) = @_;
45  
46 my $t = 0;
47 my %options;
48 getopt \@args,
49 'intermediate=s' => \my $int_url,
50 'full!' => \(my $full = 1),
51 'n|name=s' => \my $name,
52 't|test' => \my $test,
53 'T|template=s' => \(my $template = 'nginx_default'),
54 'o|options=s' => \%options;
55 $name ||= $self->app->moniker;
56 $name .= 'test' if $test;
57  
58 die 'an ssl directory name is required' unless my $ssldir = $self->app->config('ssldir');
59  
60 my $host = $args[0] or die "no canonical host";
61 my $account = path($ssldir)->child("$name-account-$host.key")->to_string;
62 $name = path($ssldir)->child("$name-cert-$host")->to_string;
63 $self->app->log->info("generating $host certificate signed by the ACME service");
64  
65 push @args, '-a', $account, ($test?'-t':());
66 my $acme = $self->build_acme(\@args);
67  
68 sleep 3;
69  
70 # Register account
71  
72 unless ( -f $account ) {
73 my $response = eval { $acme->register };
74 print $@ if $@;
75 die "Account not registered" unless $response;
76  
77 say $response;
78 my $key = $acme->account_key;
79 if ($key->generated) {
80 my $key_path = $key->path;
81 $self->app->log->info("Writing $key_path");
82 Mojo::File->new($key_path)->spurt($key->string);
83 }
84 sleep 3;
85 }
86  
87 # Generate certificate
88  
89 $acme->server_url($self->app->config('acme')->{challenge_url});
90  
91 my $intermediate;
92 if ($full) {
93 my $msg = "No certificate generation was attempted. Use --no-full to proceed without it.\n";
94 $int_url ||= eval { $acme->ca->intermediate };
95 die "Intermediate certificate not specified. $msg"
96 unless $int_url;
97 my $tx = $acme->ua->get($int_url);
98 die "Failed to fetch intermediate cert. $msg"
99 unless $tx->result;
100 die "Intermediate cert was empty. $msg"
101 unless $intermediate = $tx->res->body;
102 }
103  
104 $acme->new_authz($_) for @args;
105  
106 my $cert;
107 Mojo::IOLoop->delay(
108 sub { $acme->check_all_challenges(shift->begin) },
109 sub {
110 my ($delay, $err) = @_;
111 die Mojo::Util::dumper($err) if $err;
112 my $bad = c(values %{ $acme->challenges })->grep(sub { $_->{status} ne 'valid' });
113 die 'The following challenges were not validated ' . Mojo::Util::dumper($bad->to_array) if $bad->size;
114 #TODO poll for cert when delayed
115 $cert = $acme->get_cert(@args);
116 },
117 )->catch(sub{ warn "$_[-1]\n" })->wait;
118  
119 die "No cert was generated for $host" unless $cert;
120  
121 my $key_path = "$name.key";
122 if ($acme->cert_key->generated) {
123 $self->app->log->info("Writing $key_path");
124 Mojo::File->new($key_path)->spurt($acme->cert_key->string);
125 }
126  
127 if ($intermediate) {
128 $cert = $cert . $intermediate;
129 }
130  
131 my $cert_path = "$name.crt";
132 $self->app->log->info("Writing $cert_path");
133 Mojo::File->new($cert_path)->spurt($cert);
134  
135 if ( my $webdir = $self->app->config('webdir') ) {
136 path($webdir)->child($host)->spurt(
137 Mojo::Template->new->vars(1)->render_file(
138 $self->app->home->child('templates', "$template.ep"),
139 {%options, hosts => \@args, cert => $cert_path, key => $key_path}
140 )
141 ) if $template && !$acme->ca->test_mode; # TODO: unless go+w
142 }
143 }
144  
145 1;
146  
147 =encoding utf8
148  
149 =head1 NAME
150  
151 Mojolicious::Plugin::ACME::Command::acme::automate - Automate ACME registration and signed certificate generation
152  
153 =head1 SYNOPSIS
154  
155 Usage: APPLICATION acme automate [OPTIONS]
156 myapp acme automate
157 myapp acme automate -t -a myaccount.key -T template -l http://*:8928 -o proxy_pass=http://127.0.0.1:3000 domain1.com {domain2.com, ...}
158  
159 Options:
160  
161 --full, --no-full Automatically chain the resulting
162 certificate with the intermediate
163 defaults to true, use --no-full to
164 disable
165 -h, --help Show this summary of available options
166 -i, --intermediate The url of the intermediate cert to
167 be chained if "full" is passed
168 -l, --listen <location> One or more locations you want to
169 listen on, defaults to the value of
170 MOJO_LISTEN or "http://*:3000"
171 -n, --name The name of the file(s) to be
172 generated, defaults to the app's
173 moniker
174 -o, --option <key=value> Options to pass as variables to the
175 template
176 -p, --proxy Activate reverse proxy support,
177 defaults to the value of
178 MOJO_REVERSE_PROXY
179 -T, --template <filename> Template for building a config file
180 for your reverse proxy server
181 (e.g. nginx)
182  
183 =head1 DESCRIPTION
184  
185 L<Mojolicious::Plugin::ACME::Command::acme::automate> automates ACME
186 registration and signed certificate generation for one or more domains.
187  
188 =head1 ATTRIBUTES
189  
190 L<Mojolicious::Plugin::ACME::Command::acme::automate> inherits all
191 attributes from L<Mojolicious::Plugin::ACME::Command> and implements the
192 following new ones.
193  
194 =head2 description
195  
196 my $description = $v->description;
197 $v = $v->description('Foo');
198  
199 Short description of this command, used for the command list.
200  
201 =head2 usage
202  
203 my $usage = $v->usage;
204 $v = $v->usage('Foo');
205  
206 Usage information for this command, used for the help screen.
207  
208 =head1 METHODS
209  
210 L<Mojolicious::Plugin::ACME::Command::acme::automate> inherits all methods from
211 L<Mojolicious::Command> and implements the following new ones.
212  
213 =head2 run
214  
215 $v->run(@ARGV);
216  
217 Run this command.
218  
219 =head1 SEE ALSO
220  
221 L<Mojolicious>, L<Mojo::ACME>.
222  
223 =head1 SOURCE REPOSITORY
224  
225 L<http://github.com/s1037989/Mojolicious-Plugin-ACME-Command-acme-automate>
226  
227 =head1 AUTHOR
228  
229 Stefan Adams, E<lt>sadams@cpan.org<gt>
230  
231 =head1 CONTRIBUTORS
232  
233 =over
234  
235 =item *
236  
237 Joel Berger (jberger)
238  
239 =back
240  
241 =head1 COPYRIGHT AND LICENSE
242  
243 Copyright (C) 2018 by Stefan Adams and L</CONTRIBUTORS>
244  
245 This library is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.
247  
248 =cut