Do not try to contact lookup server without internet connection or URL

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2018-10-17 13:09:11 +02:00
parent 7c8b3c1056
commit 8e4ae23c48
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 108 additions and 27 deletions

View File

@ -50,9 +50,15 @@ class RetryJob extends Job {
$this->clientService = $clientService; $this->clientService = $clientService;
$this->jobList = $jobList; $this->jobList = $jobList;
if ($config->getSystemValue('has_internet_connection', true) === false) {
return;
}
$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
$this->lookupServer = rtrim($this->lookupServer, '/'); if (!empty($this->lookupServer)) {
$this->lookupServer .= '/users'; $this->lookupServer = rtrim($this->lookupServer, '/');
$this->lookupServer .= '/users';
}
} }
/** /**
@ -69,7 +75,7 @@ class RetryJob extends Job {
} }
protected function run($argument) { protected function run($argument) {
if($argument['retryNo'] === 5) { if ($argument['retryNo'] === 5 || empty($this->lookupServer)) {
return; return;
} }

View File

@ -64,15 +64,25 @@ class UpdateLookupServer {
$this->signer = $signer; $this->signer = $signer;
$this->jobList = $jobList; $this->jobList = $jobList;
if($config->getSystemValue('has_internet_connection', true) === false) {
return;
}
$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
$this->lookupServer = rtrim($this->lookupServer, '/'); if(!empty($this->lookupServer)) {
$this->lookupServer .= '/users'; $this->lookupServer = rtrim($this->lookupServer, '/');
$this->lookupServer .= '/users';
}
} }
/** /**
* @param IUser $user * @param IUser $user
*/ */
public function userUpdated(IUser $user) { public function userUpdated(IUser $user) {
if(empty($this->lookupServer)) {
return;
}
$userData = $this->accountManager->getUser($user); $userData = $this->accountManager->getUser($user);
$publicData = []; $publicData = [];

View File

@ -63,12 +63,17 @@ class LookupPlugin implements ISearchPlugin {
public function search($search, $limit, $offset, ISearchResult $searchResult) { public function search($search, $limit, $offset, ISearchResult $searchResult) {
$isGlobalScaleEnabled = $this->config->getSystemValue('gs.enabled', false); $isGlobalScaleEnabled = $this->config->getSystemValue('gs.enabled', false);
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes'; $isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
$hasInternetConnection = (bool)$this->config->getSystemValue('has_internet_connection', true);
// if case of Global Scale we always search the lookup server // if case of Global Scale we always search the lookup server
if (!$isLookupServerEnabled && !$isGlobalScaleEnabled) { if ((!$isLookupServerEnabled && !$isGlobalScaleEnabled) || !$hasInternetConnection) {
return false; return false;
} }
$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); $lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
if(empty($lookupServerUrl)) {
return false;
}
$lookupServerUrl = rtrim($lookupServerUrl, '/'); $lookupServerUrl = rtrim($lookupServerUrl, '/');
$result = []; $result = [];

View File

@ -182,6 +182,9 @@ class VerifyUserData extends Job {
* @return bool true if we could check the verification code, otherwise false * @return bool true if we could check the verification code, otherwise false
*/ */
protected function verifyViaLookupServer(array $argument, $dataType) { protected function verifyViaLookupServer(array $argument, $dataType) {
if(empty($this->lookupServerUrl) || $this->config->getSystemValue('has_internet_connection', true) === false) {
return false;
}
$user = $this->userManager->get($argument['uid']); $user = $this->userManager->get($argument['uid']);

View File

@ -70,7 +70,7 @@ class LookupPluginTest extends TestCase {
$this->userSession->expects($this->any())->method('getUser') $this->userSession->expects($this->any())->method('getUser')
->willReturn($user); ->willReturn($user);
$this->cloudIdManager->expects($this->any())->method('resolveCloudId') $this->cloudIdManager->expects($this->any())->method('resolveCloudId')
->willReturnCallback(function($cloudId) { ->willReturnCallback(function ($cloudId) {
if ($cloudId === 'user@myNextcloud.net') { if ($cloudId === 'user@myNextcloud.net') {
return new CloudId('user@myNextcloud.net', 'user', 'myNextcloud.net'); return new CloudId('user@myNextcloud.net', 'user', 'myNextcloud.net');
} }
@ -87,6 +87,58 @@ class LookupPluginTest extends TestCase {
); );
} }
public function testSearchNoLookupServerURI() {
$this->config->expects($this->once())
->method('getAppValue')
->with('files_sharing', 'lookupServerEnabled', 'no')
->willReturn('yes');
$this->config->expects($this->at(0))
->method('getSystemValue')
->with('gs.enabled', false)
->willReturn(false);
$this->config->expects($this->at(2))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config->expects($this->at(3))
->method('getSystemValue')
->with('lookup_server', 'https://lookup.nextcloud.com')
->willReturn('');
$this->clientService->expects($this->never())
->method('newClient');
/** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */
$searchResult = $this->createMock(ISearchResult::class);
$this->plugin->search('foobar', 10, 0, $searchResult);
}
public function testSearchNoInternet() {
$this->config->expects($this->once())
->method('getAppValue')
->with('files_sharing', 'lookupServerEnabled', 'no')
->willReturn('yes');
$this->config->expects($this->at(0))
->method('getSystemValue')
->with('gs.enabled', false)
->willReturn(false);
$this->config->expects($this->at(2))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(false);
$this->clientService->expects($this->never())
->method('newClient');
/** @var ISearchResult|\PHPUnit_Framework_MockObject_MockObject $searchResult */
$searchResult = $this->createMock(ISearchResult::class);
$this->plugin->search('foobar', 10, 0, $searchResult);
}
/** /**
* @dataProvider searchDataProvider * @dataProvider searchDataProvider
* @param array $searchParams * @param array $searchParams
@ -108,7 +160,12 @@ class LookupPluginTest extends TestCase {
->method('getSystemValue') ->method('getSystemValue')
->with('gs.enabled', false) ->with('gs.enabled', false)
->willReturn(false); ->willReturn(false);
$this->config->expects($this->at(2)) $this->config->expects($this->at(2))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config->expects($this->at(3))
->method('getSystemValue') ->method('getSystemValue')
->with('lookup_server', 'https://lookup.nextcloud.com') ->with('lookup_server', 'https://lookup.nextcloud.com')
->willReturn($searchParams['server']); ->willReturn($searchParams['server']);
@ -121,7 +178,7 @@ class LookupPluginTest extends TestCase {
$client = $this->createMock(IClient::class); $client = $this->createMock(IClient::class);
$client->expects($this->once()) $client->expects($this->once())
->method('get') ->method('get')
->willReturnCallback(function($url) use ($searchParams, $response) { ->willReturnCallback(function ($url) use ($searchParams, $response) {
$this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0); $this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0);
$this->assertNotFalse(strpos($url, urlencode($searchParams['search']))); $this->assertNotFalse(strpos($url, urlencode($searchParams['search'])));
return $response; return $response;
@ -138,8 +195,6 @@ class LookupPluginTest extends TestCase {
$searchResult $searchResult
); );
$this->assertFalse($moreResults); $this->assertFalse($moreResults);
} }
@ -170,6 +225,10 @@ class LookupPluginTest extends TestCase {
->with($type, $searchParams['expectedResult'], []); ->with($type, $searchParams['expectedResult'], []);
$this->config->expects($this->at(2)) $this->config->expects($this->at(2))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config->expects($this->at(3))
->method('getSystemValue') ->method('getSystemValue')
->with('lookup_server', 'https://lookup.nextcloud.com') ->with('lookup_server', 'https://lookup.nextcloud.com')
->willReturn($searchParams['server']); ->willReturn($searchParams['server']);
@ -201,8 +260,6 @@ class LookupPluginTest extends TestCase {
$searchResult $searchResult
); );
$this->assertFalse($moreResults); $this->assertFalse($moreResults);
} }
@ -237,9 +294,9 @@ class LookupPluginTest extends TestCase {
'offset' => 0, 'offset' => 0,
'server' => 'https://lookup.example.io', 'server' => 'https://lookup.example.io',
'resultBody' => [ 'resultBody' => [
[ 'federationId' => $fedIDs[0] ], ['federationId' => $fedIDs[0]],
[ 'federationId' => $fedIDs[1] ], ['federationId' => $fedIDs[1]],
[ 'federationId' => $fedIDs[2] ], ['federationId' => $fedIDs[2]],
], ],
'expectedResult' => [ 'expectedResult' => [
[ [
@ -276,9 +333,9 @@ class LookupPluginTest extends TestCase {
'offset' => 0, 'offset' => 0,
'server' => 'https://lookup.example.io', 'server' => 'https://lookup.example.io',
'resultBody' => [ 'resultBody' => [
[ 'federationId' => $fedIDs[0] ], ['federationId' => $fedIDs[0]],
[ 'federationId' => $fedIDs[1] ], ['federationId' => $fedIDs[1]],
[ 'federationId' => $fedIDs[2] ], ['federationId' => $fedIDs[2]],
], ],
'expectedResult' => [ 'expectedResult' => [
[ [
@ -315,9 +372,9 @@ class LookupPluginTest extends TestCase {
'offset' => 0, 'offset' => 0,
'server' => 'https://lookup.example.io', 'server' => 'https://lookup.example.io',
'resultBody' => [ 'resultBody' => [
[ 'federationId' => $fedIDs[0] ], ['federationId' => $fedIDs[0]],
[ 'federationId' => $fedIDs[1] ], ['federationId' => $fedIDs[1]],
[ 'federationId' => $fedIDs[2] ], ['federationId' => $fedIDs[2]],
], ],
'expectedResult' => [ 'expectedResult' => [
[ [
@ -354,9 +411,9 @@ class LookupPluginTest extends TestCase {
'offset' => 0, 'offset' => 0,
'server' => 'https://lookup.example.io', 'server' => 'https://lookup.example.io',
'resultBody' => [ 'resultBody' => [
[ 'federationId' => $fedIDs[0] ], ['federationId' => $fedIDs[0]],
[ 'federationId' => $fedIDs[1] ], ['federationId' => $fedIDs[1]],
[ 'federationId' => $fedIDs[2] ], ['federationId' => $fedIDs[2]],
], ],
'expectedResult' => [ 'expectedResult' => [
[ [
@ -405,9 +462,9 @@ class LookupPluginTest extends TestCase {
'offset' => 0, 'offset' => 0,
'server' => 'https://lookup.example.io', 'server' => 'https://lookup.example.io',
'resultBody' => [ 'resultBody' => [
[ 'federationId' => $fedIDs[0] ], ['federationId' => $fedIDs[0]],
[ 'federationId' => $fedIDs[1] ], ['federationId' => $fedIDs[1]],
[ 'federationId' => $fedIDs[2] ], ['federationId' => $fedIDs[2]],
], ],
'expectedResult' => [ 'expectedResult' => [
[ [