Skip to content

Commit 399aa4d

Browse files
committed
Merge branch 'mrrobot47-fix/letsencrypt-in-global-auth' into develop
2 parents 655c869 + 2833ff3 commit 399aa4d

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

src/helper/Site_Letsencrypt.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use League\Flysystem\Adapter\Local;
3535
use League\Flysystem\Adapter\NullAdapter;
3636
use GuzzleHttp\Client;
37+
use function \EE\Site\Utils\reload_global_nginx_proxy;
3738

3839

3940
class Site_Letsencrypt {
@@ -109,13 +110,19 @@ private function getSecureHttpClient() {
109110
);
110111
}
111112

112-
113+
/**
114+
* Function to register mail to letsencrypt.
115+
*
116+
* @param string $email Mail id to be registered.
117+
*
118+
* @return bool Success.
119+
*/
113120
public function register( $email ) {
114121
try {
115122
$this->client->registerAccount( null, $email );
116123
} catch ( \Exception $e ) {
117124
\EE::warning( $e->getMessage() );
118-
\EE::warning( 'It seems you\'re in local environment or there is some issue with network, please check logs. Skipping letsencrypt.' );
125+
\EE::warning( 'It seems you\'re in local environment or used invalid email or there is some issue with network, please check logs. Skipping letsencrypt.' );
119126

120127
return false;
121128
}
@@ -124,7 +131,15 @@ public function register( $email ) {
124131
return true;
125132
}
126133

127-
public function authorize( Array $domains, $site_root, $wildcard = false ) {
134+
/**
135+
* Function to authorize the letsencrypt request and get the token for challenge.
136+
*
137+
* @param array $domains Domains to be authorized.
138+
* @param bool $wildcard Is the authorization for wildcard or not.
139+
*
140+
* @return bool Success.
141+
*/
142+
public function authorize( Array $domains, $wildcard = false ) {
128143
$solver = $wildcard ? new SimpleDnsSolver( null, new ConsoleOutput() ) : new SimpleHttpSolver();
129144
$solverName = $wildcard ? 'dns-01' : 'http-01';
130145
try {
@@ -165,10 +180,17 @@ public function authorize( Array $domains, $site_root, $wildcard = false ) {
165180
if ( ! $wildcard ) {
166181
$token = $authorizationChallenge->toArray()['token'];
167182
$payload = $authorizationChallenge->toArray()['payload'];
168-
\EE::launch( "mkdir -p $site_root/app/src/.well-known/acme-challenge/" );
169-
\EE::debug( "Creating challange file $site_root/app/src/.well-known/acme-challenge/$token" );
170-
file_put_contents( "$site_root/app/src/.well-known/acme-challenge/$token", $payload );
171-
\EE::launch( "chown www-data: $site_root/app/src/.well-known/acme-challenge/$token" );
183+
184+
$fs = new \Symfony\Component\Filesystem\Filesystem();
185+
$fs->copy( SITE_TEMPLATE_ROOT . '/vhost.d_default_letsencrypt.mustache', EE_CONF_ROOT . '/nginx/vhost.d/default' );
186+
$challange_dir = EE_CONF_ROOT . '/nginx/html/.well-known/acme-challenge';
187+
if ( ! $fs->exists( $challange_dir ) ) {
188+
$fs->mkdir( $challange_dir );
189+
}
190+
$challange_file = $challange_dir . '/' . $token;
191+
\EE::debug( 'Creating challange file ' . $challange_file );
192+
$fs->dumpFile( $challange_file, $payload );
193+
reload_global_nginx_proxy();
172194
}
173195
}
174196

@@ -519,11 +541,20 @@ public function status() {
519541
$table->render();
520542
}
521543

522-
public function cleanup( $site_root ) {
523-
$challange_dir = "$site_root/app/src/.well-known";
524-
if ( file_exists( "$site_root/app/src/.well-known" ) ) {
525-
\EE::debug( 'Cleaning up webroot files.' );
526-
\EE\Utils\delete_dir( $challange_dir );
544+
/**
545+
* Cleanup created challenge files and specific rule sets for it.
546+
*/
547+
public function cleanup() {
548+
549+
$fs = new \Symfony\Component\Filesystem\Filesystem();
550+
551+
$challange_dir = EE_CONF_ROOT . '/nginx/html/.well-known';
552+
$challange_rule_file = EE_CONF_ROOT . '/nginx/vhost.d/default';
553+
if ( $fs->exists( $challange_rule_file ) ) {
554+
$fs->remove( $challange_rule_file );
555+
}
556+
if ( $fs->exists( $challange_dir ) ) {
557+
$fs->remove( $challange_dir );
527558
}
528559
}
529560
}

src/helper/class-ee-site.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ protected function init_ssl( $site_url, $site_fs_path, $ssl_type, $wildcard = fa
422422
*/
423423
protected function init_le( $site_url, $site_fs_path, $wildcard = false ) {
424424

425-
\EE::debug( "Wildcard in init_le: $wildcard" );
425+
\EE::debug( 'Wildcard in init_le: ' . ( bool ) $wildcard );
426426

427427
$this->site_data['site_url'] = $site_url;
428428
$this->site_data['site_fs_path'] = $site_fs_path;
@@ -438,7 +438,7 @@ protected function init_le( $site_url, $site_fs_path, $wildcard = false ) {
438438

439439
$domains = $this->get_cert_domains( $site_url, $wildcard );
440440

441-
if ( ! $client->authorize( $domains, $this->site_data['site_fs_path'], $wildcard ) ) {
441+
if ( ! $client->authorize( $domains, $wildcard ) ) {
442442
return;
443443
}
444444
if ( $wildcard ) {
@@ -523,7 +523,7 @@ public function le( $args = [], $assoc_args = [] ) {
523523
$client->request( $this->site_data['site_url'], $san, $this->le_mail, $force );
524524

525525
if ( ! $this->site_data['site_ssl_wildcard'] ) {
526-
$client->cleanup( $this->site_data['site_fs_path'] );
526+
$client->cleanup();
527527
}
528528
reload_global_nginx_proxy();
529529
}

src/helper/site-utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function generate_global_docker_compose_yml( Filesystem $fs ) {
171171
EE_CONF_ROOT . '/nginx/conf.d:/etc/nginx/conf.d',
172172
EE_CONF_ROOT . '/nginx/htpasswd:/etc/nginx/htpasswd',
173173
EE_CONF_ROOT . '/nginx/vhost.d:/etc/nginx/vhost.d',
174-
'/usr/share/nginx/html',
174+
EE_CONF_ROOT . '/nginx/html:/usr/share/nginx/html',
175175
'/var/run/docker.sock:/tmp/docker.sock:ro',
176176
],
177177
'networks' => [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
location ^~ /.well-known/acme-challenge/ {
2+
auth_basic off;
3+
allow all;
4+
root /usr/share/nginx/html;
5+
try_files $uri =404;
6+
break;
7+
}

0 commit comments

Comments
 (0)