Skip to content

Commit e8edfb2

Browse files
authored
Merge pull request #447 from mrrobot47/fix/ssl-auto-cleanup
Fix site deletion when ssl is not issued
2 parents 275e435 + 87c7140 commit e8edfb2

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed

src/helper/Site_Letsencrypt.php

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,20 @@ public function revokeAuthorizationChallenge(AuthorizationChallenge $challenge)
7777
]]
7878
];
7979

80-
$client = $this->getHttpClient();
81-
$resourceUrl = $this->getResourceUrl(ResourcesDirectory::NEW_ORDER);
82-
$response = $client->request('POST', $resourceUrl, $client->signKidPayload($resourceUrl, $this->getResourceAccount(), $payload));
83-
if (!isset($response['authorizations']) || !$response['authorizations']) {
84-
throw new ChallengeNotSupportedException();
80+
$client = $this->getHttpClient();
81+
$resourceUrl = $this->getResourceUrl( ResourcesDirectory::NEW_ORDER );
82+
$response = $client->request( 'POST', $resourceUrl, $client->signKidPayload( $resourceUrl, $this->getResourceAccount(), $payload ) );
83+
if ( ! isset( $response['authorizations'] ) || ! $response['authorizations'] ) {
84+
\EE::warning( 'Challenge not supported for domain' );
85+
86+
return false;
8587
}
8688

8789
$orderEndpoint = $client->getLastLocation();
8890
foreach ($response['authorizations'] as $authorizationEndpoint) {
8991
$authorizationsResponse = $client->request('POST', $authorizationEndpoint, $client->signKidPayload($authorizationEndpoint, $this->getResourceAccount(), [ 'status' => 'deactivated' ]));
9092
}
91-
return;
93+
return true;
9294
}
9395
}
9496

@@ -207,9 +209,10 @@ public function authorize( Array $domains, $wildcard = false, $preferred_challen
207209
$order = $this->client->requestOrder( $domains );
208210
} catch ( \Exception $e ) {
209211
\EE::warning( 'It seems you\'re in local environment or using non-public domain, please check logs. Skipping letsencrypt.' );
210-
throw $e;
211-
}
212+
\EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domains[0] );
212213

214+
return false;
215+
}
213216
$authorizationChallengesToSolve = [];
214217
foreach ( $order->getAuthorizationsChallenges() as $domainKey => $authorizationChallenges ) {
215218
$authorizationChallenge = null;
@@ -223,12 +226,14 @@ public function authorize( Array $domains, $wildcard = false, $preferred_challen
223226
\EE::debug( 'Authorization challenge supported by solver. Solver: ' . $solverName . ' Challenge: ' . $candidate->getType() );
224227
break;
225228
}
226-
// Should not get here as we are handling it.
227229
\EE::debug( 'Authorization challenge not supported by solver. Solver: ' . $solverName . ' Challenge: ' . $candidate->getType() );
228230
\EE::debug( print_r( $candidate, true ) );
229231
}
230232
if ( null === $authorizationChallenge ) {
231-
throw new ChallengeNotSupportedException();
233+
\EE::warning( 'Challenge not supported for domain ' . $domainKey );
234+
\EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domainKey );
235+
236+
return false;
232237
}
233238
\EE::debug( 'Storing authorization challenge. Domain: ' . $domainKey . ' Challenge: ' . print_r( $authorizationChallenge->toArray(), true ) );
234239

@@ -367,29 +372,37 @@ public function check( Array $domains, $wildcard = false, $preferred_challenge =
367372
}
368373
}
369374
if ( null === $authorizationChallenge ) {
370-
throw new ChallengeNotSupportedException();
375+
\EE::warning( 'Challenge not supported for domain' );
376+
377+
return false;
371378
}
372379
} else {
373380
if ( ! $this->repository->hasDomainAuthorizationChallenge( $domain ) ) {
374381
\EE::error( "Domain: $domain not yet authorized/has not been started of with EasyEngine letsencrypt site creation." );
375382
}
376383
$authorizationChallenge = $this->repository->loadDomainAuthorizationChallenge( $domain );
377384
if ( ! $solver->supports( $authorizationChallenge ) ) {
378-
throw new ChallengeNotSupportedException();
385+
\EE::warning( 'Challenge not supported for domain' );
386+
387+
return false;
379388
}
380389
}
381390
\EE::debug( 'Challenge loaded.' );
382391

383392
$authorizationChallenge = $this->client->reloadAuthorization( $authorizationChallenge );
384393
if ( ! $authorizationChallenge->isValid() ) {
385-
\EE::debug( sprintf( 'Testing the challenge for domain %s', $domain ) );
386-
if ( ! $validator->isValid( $authorizationChallenge ) ) {
387-
throw new \Exception( sprintf( 'Can not validate challenge for domain %s', $domain ) );
388-
}
389-
390-
\EE::debug( sprintf( 'Requesting authorization check for domain %s', $domain ) );
391394
try {
395+
\EE::debug( sprintf( 'Testing the challenge for domain %s', $domain ) );
396+
if ( ! $validator->isValid( $authorizationChallenge ) ) {
397+
\EE::warning( 'Can not validate challenge for domain ' . $domain );
398+
\EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domain );
399+
400+
return false;
401+
}
402+
403+
\EE::debug( sprintf( 'Requesting authorization check for domain %s', $domain ) );
392404
$this->client->challengeAuthorization( $authorizationChallenge );
405+
$authorizationChallengeToCleanup[] = $authorizationChallenge;
393406
} catch ( \Exception $e ) {
394407
\EE::debug( $e->getMessage() );
395408
\EE::warning( 'Challenge Authorization failed. Check logs and check if your domain is pointed correctly to this server.' );
@@ -398,9 +411,9 @@ public function check( Array $domains, $wildcard = false, $preferred_challenge =
398411
$site_name = str_replace( '*.', '', $site_name );
399412

400413
\EE::log( "Re-run `ee site ssl-verify $site_name` after fixing the issue." );
401-
throw $e;
414+
415+
return false;
402416
}
403-
$authorizationChallengeToCleanup[] = $authorizationChallenge;
404417
}
405418
}
406419

@@ -581,7 +594,7 @@ private function executeRenewal( $domain, array $alternativeNames, $force = fals
581594
)
582595
);
583596

584-
return;
597+
return true;
585598
}
586599

587600
\EE::log(
@@ -627,12 +640,18 @@ private function executeRenewal( $domain, array $alternativeNames, $force = fals
627640
\EE::warning( 'A critical error occured during certificate renewal' );
628641
\EE::debug( print_r( $e, true ) );
629642

630-
throw $e;
643+
\EE::warning( 'Challenge Authorization failed. Check logs and check if your domain is pointed correctly to this server.' );
644+
\EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domains[0] );
645+
646+
return false;
631647
} catch ( \Throwable $e ) {
632648
\EE::warning( 'A critical error occured during certificate renewal' );
633649
\EE::debug( print_r( $e, true ) );
634650

635-
throw $e;
651+
\EE::warning( 'Challenge Authorization failed. Check logs and check if your domain is pointed correctly to this server.' );
652+
\EE::log( 'You can fix the issue and re-run: ee site ssl-verify ' . $domains[0] );
653+
654+
return false;
636655
}
637656
}
638657

src/helper/class-ee-site.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ protected function init_le( $site_url, $site_fs_path, $wildcard = false, $www_or
15431543
\EE::get_runner()->ensure_present_in_config( 'le-mail', $this->le_mail );
15441544
if ( ! $client->register( $this->le_mail ) ) {
15451545
$this->site_data['site_ssl'] = null;
1546+
\EE::warning( 'SSL registration failed.' );
15461547

15471548
return;
15481549
}
@@ -1553,6 +1554,9 @@ protected function init_le( $site_url, $site_fs_path, $wildcard = false, $www_or
15531554
$client->revokeAuthorizationChallenges( $domains );
15541555

15551556
if ( ! $client->authorize( $domains, $wildcard, $preferred_challenge ) ) {
1557+
$this->site_data['site_ssl'] = null;
1558+
\EE::warning( 'SSL authorization failed. Site will be created without SSL. You can fix the issue and re-run: ee site ssl-verify ' . $site_url );
1559+
15561560
return;
15571561
}
15581562
$api_key_absent = empty( get_config_value( 'cloudflare-api-key' ) );
@@ -1563,7 +1567,12 @@ protected function init_le( $site_url, $site_fs_path, $wildcard = false, $www_or
15631567
EE::log( 'Waiting for DNS entry propagation.' );
15641568
sleep( 10 );
15651569
}
1566-
$this->ssl_verify( [], [ 'force' => $force ], $www_or_non_www );
1570+
if ( ! $this->ssl_verify( [], [ 'force' => $force ], $www_or_non_www ) ) {
1571+
$this->site_data['site_ssl'] = null;
1572+
\EE::warning( 'SSL verification failed. You can fix the issue and re-run: ee site ssl-verify ' . $site_url );
1573+
1574+
return;
1575+
}
15671576
}
15681577
}
15691578

@@ -1603,6 +1612,7 @@ private function get_cert_domains( string $site_url, $wildcard, $www_or_non_www
16031612
* @param string Absolute path of site.
16041613
* @param string $site_container_path
16051614
*
1615+
*
16061616
* @return bool
16071617
*/
16081618
protected function check_www_or_non_www_domain( $site_url, $site_path, $site_container_path ): bool {
@@ -1679,29 +1689,24 @@ public function ssl_verify( $args = [], $assoc_args = [], $www_or_non_www = fals
16791689

16801690
$preferred_challenge = get_preferred_ssl_challenge( $domains );
16811691

1682-
try {
1683-
$client->check( $domains, $this->site_data['site_ssl_wildcard'], $preferred_challenge );
1684-
} catch ( \Exception $e ) {
1685-
if ( $called_by_ee && $api_key_absent ) {
1686-
throw $e;
1687-
}
1692+
if ( ! $client->check( $domains, $this->site_data['site_ssl_wildcard'], $preferred_challenge ) ) {
16881693
$is_solver_dns = ( $this->site_data['site_ssl_wildcard'] || 'dns' === $preferred_challenge ) ? true : false;
16891694
$api_key_present = ! empty( get_config_value( 'cloudflare-api-key' ) );
16901695

1691-
if ( $called_by_ee && ! $is_solver_dns && $api_key_present ) {
1692-
throw $e;
1693-
}
1694-
1695-
$warning = ( $is_solver_dns && $api_key_present ) ? "The dns entries have not yet propogated. Manually check: \nhost -t TXT _acme-challenge." . $this->site_data['site_url'] . "\nBefore retrying `ee site ssl " . $this->site_data['site_url'] . "`" : 'Failed to verify SSL: ' . $e->getMessage();
1696+
$warning = ( $is_solver_dns && $api_key_present )
1697+
? "The dns entries have not yet propogated. Manually check: \nhost -t TXT _acme-challenge." . $this->site_data['site_url'] . "\nBefore retrying `ee site ssl " . $this->site_data['site_url'] . "`"
1698+
: 'Failed to verify SSL.';
16961699

16971700
EE::warning( $warning );
16981701
EE::warning( sprintf( 'Check logs and retry `ee site ssl-verify %s` once the issue is resolved.', $this->site_data['site_url'] ) );
16991702

1700-
return;
1703+
return false;
17011704
}
17021705

17031706
$san = array_values( array_diff( $domains, [ $this->site_data['site_url'] ] ) );
1704-
$client->request( $this->site_data['site_url'], $san, $this->le_mail, $force );
1707+
if ( ! $client->request( $this->site_data['site_url'], $san, $this->le_mail, $force ) ) {
1708+
return false;
1709+
}
17051710

17061711
if ( ! $this->site_data['site_ssl_wildcard'] ) {
17071712
$client->cleanup();
@@ -1710,6 +1715,8 @@ public function ssl_verify( $args = [], $assoc_args = [], $www_or_non_www = fals
17101715
reload_global_nginx_proxy();
17111716

17121717
EE::success( 'SSL verification completed.' );
1718+
1719+
return true;
17131720
}
17141721

17151722
/**
@@ -2284,7 +2291,7 @@ public function sync( $args, $assoc_args ) {
22842291
*
22852292
* [--list]
22862293
* : List all available backups on remote.
2287-
*
2294+
*
22882295
* ## EXAMPLES
22892296
*
22902297
* # Backup a site
@@ -2296,7 +2303,7 @@ public function sync( $args, $assoc_args ) {
22962303
public function backup( $args, $assoc_args ) {
22972304
$args = auto_site_name( $args, 'site', __FUNCTION__ );
22982305
$this->site_data = get_site_info( $args, true, true, true );
2299-
$backup_restore = new Site_Backup_Restore();
2306+
$backup_restore = new Site_Backup_Restore();
23002307
$backup_restore->backup( $args, $assoc_args );
23012308
}
23022309

@@ -2315,15 +2322,15 @@ public function backup( $args, $assoc_args ) {
23152322
*
23162323
* # Restore latest backup of site.
23172324
* $ ee site restore example.com
2318-
*
2325+
*
23192326
* # Restore specific backup of site.
23202327
* $ ee site restore example.com --id=1737560626_2025-01-22-15-43-46
23212328
*
23222329
*/
23232330
public function restore( $args, $assoc_args ) {
23242331
$args = auto_site_name( $args, 'site', __FUNCTION__ );
23252332
$this->site_data = get_site_info( $args, true, true, true );
2326-
$backup_restore = new Site_Backup_Restore();
2333+
$backup_restore = new Site_Backup_Restore();
23272334
$backup_restore->restore( $args, $assoc_args );
23282335
}
23292336

0 commit comments

Comments
 (0)