Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch the errors related to untrusted self signed certificates for federation #7450

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/federation/lib/BackgroundJob/GetSharedSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Ring\Exception\RingException;
use OC\BackgroundJob\JobList;
use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler;
Expand Down Expand Up @@ -197,7 +199,10 @@ protected function run($argument) {
} else {
$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
}
} catch (ConnectException $e) {
} catch (RequestException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (RingException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (\Exception $e) {
Expand Down
7 changes: 6 additions & 1 deletion apps/federation/lib/BackgroundJob/RequestSharedSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Ring\Exception\RingException;
use OC\BackgroundJob\JobList;
use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler;
Expand Down Expand Up @@ -197,7 +199,10 @@ protected function run($argument) {
} else {
$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
}
} catch (ConnectException $e) {
} catch (RequestException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (RingException $e) {
$status = -1; // There is no status code if we could not connect
$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
} catch (\Exception $e) {
Expand Down
38 changes: 38 additions & 0 deletions apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Ring\Exception\RingException;
use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler;
Expand Down Expand Up @@ -315,4 +316,41 @@ public function testRunConnectionError() {

$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}

public function testRunRingException() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';

$argument = ['url' => $target, 'token' => $token];

$this->timeFactory->method('getTime')
->willReturn(42);

$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(RingException::class));

$this->dbHandler->expects($this->never())->method('addToken');
$this->trustedServers->expects($this->never())->method('addSharedSecret');

$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);

$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}
}
38 changes: 38 additions & 0 deletions apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Ring\Exception\RingException;
use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
Expand Down Expand Up @@ -300,4 +301,41 @@ public function testRunConnectionError() {
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
}

public function testRunRingException() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';

$argument = ['url' => $target, 'token' => $token];

$this->timeFactory->method('getTime')->willReturn(42);

$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);

$this->httpClient
->expects($this->once())
->method('post')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json',
[
'body' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(RingException::class));

$this->dbHandler->expects($this->never())->method('addToken');

$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
}
}