Skip to content

Commit 87c7140

Browse files
committed
fix(site): enhance error handling for SSL challenges
1 parent 19100f1 commit 87c7140

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
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

0 commit comments

Comments
 (0)