Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUser;
Expand All @@ -39,6 +40,7 @@ public function __construct(
private IUserManager $userManager,
private IAccountManager $accountManager,
private Signer $signer,
private GlobalScaleConfig $globalScaleConfig,
) {
parent::__construct($time);

Expand Down Expand Up @@ -86,7 +88,7 @@ public function start(IJobList $jobList): void {
protected function shouldRemoveBackgroundJob(): bool {
// TODO: Remove global scale condition once lookup server is used for non-global scale federation
// return $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'no') !== 'yes'
return !$this->config->getSystemValueBool('gs.enabled', false)
return !$this->globalScaleConfig->isGlobalScaleEnabled()
|| $this->config->getSystemValueBool('has_internet_connection', true) === false
|| $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === ''
|| $this->retries >= 5;
Expand Down
23 changes: 9 additions & 14 deletions apps/lookup_server_connector/lib/UpdateLookupServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
use OCP\BackgroundJob\IJobList;
use OCP\Config\IUserConfig;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\IConfig;
use OCP\IUser;

Expand All @@ -20,26 +22,21 @@
* @package OCA\LookupServerConnector
*/
class UpdateLookupServer {
/**
* @param IJobList $jobList
* @param IConfig $config
*/
public function __construct(
private IJobList $jobList,
private IConfig $config,
private readonly IJobList $jobList,
private readonly IConfig $config,
private readonly IUserConfig $userConfig,
private readonly GlobalScaleConfig $globalScaleConfig,
) {
}

/**
* @param IUser $user
*/
public function userUpdated(IUser $user): void {
if (!$this->shouldUpdateLookupServer()) {
return;
}

// Reset retry counter
$this->config->deleteUserValue(
$this->userConfig->deleteUserConfig(
$user->getUID(),
'lookup_server_connector',
'update_retries'
Expand All @@ -48,17 +45,15 @@ public function userUpdated(IUser $user): void {
}

/**
* check if we should update the lookup server, we only do it if
* Check if we should update the lookup server, we only do it if
*
* + we have an internet connection
* + the lookup server update was not disabled by the admin
* + we have a valid lookup server URL
*
* @return bool
*/
private function shouldUpdateLookupServer(): bool {
// TODO: Consider reenable for non-global-scale setups by checking "'files_sharing', 'lookupServerUploadEnabled'" instead of "gs.enabled"
return $this->config->getSystemValueBool('gs.enabled', false)
return $this->globalScaleConfig->isGlobalScaleEnabled()
&& $this->config->getSystemValueBool('has_internet_connection', true)
&& $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') !== '';
}
Expand Down
4 changes: 3 additions & 1 deletion apps/settings/lib/BackgroundJobs/VerifyUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserManager;
Expand All @@ -38,6 +39,7 @@ public function __construct(
private LoggerInterface $logger,
ITimeFactory $timeFactory,
private IConfig $config,
private GlobalScaleConfig $globalScaleConfig,
) {
parent::__construct($timeFactory);

Expand Down Expand Up @@ -124,7 +126,7 @@ protected function verifyWebsite(array $argument) {

protected function verifyViaLookupServer(array $argument, string $dataType): bool {
// TODO: Consider to enable for non-global-scale setups by checking 'files_sharing', 'lookupServerUploadEnabled'
if (!$this->config->getSystemValueBool('gs.enabled', false)
if (!$this->globalScaleConfig->isGlobalScaleEnabled()
|| empty($this->lookupServerUrl)
|| $this->config->getSystemValue('has_internet_connection', true) === false
) {
Expand Down
5 changes: 0 additions & 5 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2209,11 +2209,6 @@
<code><![CDATA[setUserValue]]></code>
</DeprecatedMethod>
</file>
<file src="apps/lookup_server_connector/lib/UpdateLookupServer.php">
<DeprecatedMethod>
<code><![CDATA[deleteUserValue]]></code>
</DeprecatedMethod>
</file>
<file src="apps/oauth2/lib/Controller/LoginRedirectorController.php">
<DeprecatedMethod>
<code><![CDATA[generate]]></code>
Expand Down
14 changes: 8 additions & 6 deletions lib/private/Collaboration/Collaborators/LookupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Federation\ICloudIdManager;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserSession;
Expand All @@ -23,20 +24,21 @@ class LookupPlugin implements ISearchPlugin {
private string $currentUserRemote;

public function __construct(
private IConfig $config,
private IClientService $clientService,
private readonly IConfig $config,
private readonly IClientService $clientService,
IUserSession $userSession,
private ICloudIdManager $cloudIdManager,
private LoggerInterface $logger,
private ?TrustedServers $trustedServers,
private readonly ICloudIdManager $cloudIdManager,
private readonly LoggerInterface $logger,
private readonly ?TrustedServers $trustedServers,
private readonly GlobalScaleConfig $globalScaleConfig,
) {
$currentUserCloudId = $userSession->getUser()->getCloudId();
$this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
}

#[\Override]
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
$isGlobalScaleEnabled = $this->config->getSystemValueBool('gs.enabled', false);
$isGlobalScaleEnabled = $this->globalScaleConfig->isGlobalScaleEnabled();
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
$hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);

Expand Down
20 changes: 20 additions & 0 deletions lib/private/GlobalScale/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ public function onlyInternalFederation(): bool {

return $enabled === 'internal';
}

#[Override]
public function isPrimary(): bool {
return $this->isGlobalScaleEnabled()
&& ($this->config->getSystemValueString('gss.mode', 'slave') === 'master'
|| $this->config->getSystemValueString('gss.mode', 'slave') === 'primary');
}

#[Override]
public function isSecondary(): bool {
return $this->isGlobalScaleEnabled()
&& ($this->config->getSystemValueString('gss.mode', 'slave') === 'slave'
|| $this->config->getSystemValueString('gss.mode', 'slave') === 'secondary');
}

#[Override]
public function isPrimaryAdmin(string $userId): bool {
return in_array($userId, $this->config->getSystemValue('gss.master.admin', []))
|| in_array($userId, $this->config->getSystemValue('gss.primary.admin', []));
}
}
29 changes: 29 additions & 0 deletions lib/public/GlobalScale/IConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,33 @@ public function isGlobalScaleEnabled(): bool;
* @since 12.0.1
*/
public function onlyInternalFederation(): bool;

/**
* Check if the current instance is the primary instance.
*
* This instance is then only used to log in the users and then redirect them
* to their associated secondary instance.
*
* @since 34.0.3
*/
public function isPrimary(): bool;

/**
* Check if the current instance is one of the secondary instance.
*
* These instances are the actual instance of a user and hold all their data.
*
* @since 34.0.3
*/
public function isSecondary(): bool;

/**
* Check if the given user is one of the admin on the primary instance.
*
* These users won't be redirected to a secondary instance and instead will
* stay on the primary instance to manage the configuration of the instance.
*
* @since 34.0.3
*/
public function isPrimaryAdmin(string $userId): bool;
}
68 changes: 43 additions & 25 deletions tests/lib/Collaboration/Collaborators/LookupPluginTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -13,6 +15,7 @@
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
Expand All @@ -25,18 +28,13 @@
use Test\TestCase;

class LookupPluginTest extends TestCase {
/** @var IConfig|MockObject */
protected $config;
/** @var IClientService|MockObject */
protected $clientService;
/** @var IUserSession|MockObject */
protected $userSession;
/** @var ICloudIdManager|MockObject */
protected $cloudIdManager;
/** @var LookupPlugin */
protected $plugin;
/** @var LoggerInterface|MockObject */
protected $logger;
protected IConfig&MockObject $config;
protected GlobalScaleConfig&MockObject $globalScaleConfig;
protected IClientService&MockObject $clientService;
protected IUserSession&MockObject $userSession;
protected ICloudIdManager&MockObject $cloudIdManager;
protected LookupPlugin $plugin;
protected LoggerInterface&MockObject $logger;

#[\Override]
protected function setUp(): void {
Expand All @@ -45,6 +43,7 @@ protected function setUp(): void {
$this->userSession = $this->createMock(IUserSession::class);
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
$this->config = $this->createMock(IConfig::class);
$this->globalScaleConfig = $this->createMock(GlobalScaleConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->clientService = $this->createMock(IClientService::class);
$cloudId = $this->createMock(ICloudId::class);
Expand All @@ -71,7 +70,8 @@ protected function setUp(): void {
$this->userSession,
$this->cloudIdManager,
$this->logger,
null
null,
$this->globalScaleConfig,
);
}

Expand All @@ -80,13 +80,16 @@ public function testSearchNoLookupServerURI(): void {
->method('getAppValue')
->with('files_sharing', 'lookupServerEnabled', 'no')
->willReturn('yes');
$this->config->expects($this->exactly(2))
$this->config->expects($this->once())
->method('getSystemValueBool')
->willReturnMap([
['gs.enabled', false, true],
['has_internet_connection', true, true],
]);

$this->globalScaleConfig->expects($this->once())
->method('isGlobalScaleEnabled')
->willReturn(true);

$this->config->expects($this->once())
->method('getSystemValueString')
->with('lookup_server', 'https://lookup.nextcloud.com')
Expand All @@ -106,13 +109,16 @@ public function testSearchNoInternet(): void {
->method('getAppValue')
->with('files_sharing', 'lookupServerEnabled', 'no')
->willReturn('yes');
$this->config->expects($this->exactly(2))
$this->config->expects($this->exactly(1))
->method('getSystemValueBool')
->willReturnMap([
['gs.enabled', false, false],
['has_internet_connection', true, false],
]);

$this->globalScaleConfig->expects($this->exactly(1))
->method('isGlobalScaleEnabled')
->willReturn(false);

$this->clientService->expects($this->never())
->method('newClient');

Expand All @@ -139,13 +145,16 @@ public function testSearch(array $searchParams): void {
->method('getAppValue')
->with('files_sharing', 'lookupServerEnabled', 'no')
->willReturn('yes');
$this->config->expects($this->exactly(2))
$this->config->expects($this->once())
->method('getSystemValueBool')
->willReturnMap([
['gs.enabled', false, true],
['has_internet_connection', true, true],
]);

$this->globalScaleConfig->expects($this->once())
->method('isGlobalScaleEnabled')
->willReturn(true);

$this->config->expects($this->once())
->method('getSystemValueString')
->with('lookup_server', 'https://lookup.nextcloud.com')
Expand Down Expand Up @@ -200,12 +209,15 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
->method('addResultSet')
->with($type, $searchParams['expectedResult'], []);

$this->config->expects($this->exactly(2))
$this->config->expects($this->once())
->method('getSystemValueBool')
->willReturnMap([
['gs.enabled', false, $GSEnabled],
['has_internet_connection', true, true],
]);

$this->globalScaleConfig->expects($this->once())
->method('isGlobalScaleEnabled')
->willReturn($GSEnabled);
$this->config->expects($this->once())
->method('getSystemValueString')
->with('lookup_server', 'https://lookup.nextcloud.com')
Expand All @@ -230,12 +242,15 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
->willReturn($client);
} else {
$searchResult->expects($this->never())->method('addResultSet');
$this->config->expects($this->exactly(2))
$this->config->expects($this->once())
->method('getSystemValueBool')
->willReturnMap([
['gs.enabled', false, $GSEnabled],
['has_internet_connection', true, true],
]);

$this->globalScaleConfig->expects($this->once())
->method('isGlobalScaleEnabled')
->willReturn($GSEnabled);
}
$moreResults = $this->plugin->search(
$searchParams['search'],
Expand All @@ -248,13 +263,16 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
}

public function testSearchGSDisabled(): void {
$this->config->expects($this->atLeastOnce())
$this->config->expects($this->once())
->method('getSystemValueBool')
->willReturnMap([
['has_internet_connection', true, true],
['gs.enabled', false, false],
]);

$this->globalScaleConfig->expects($this->once())
->method('isGlobalScaleEnabled')
->willReturn(false);

/** @var ISearchResult|MockObject $searchResult */
$searchResult = $this->createMock(ISearchResult::class);
$searchResult->expects($this->never())
Expand Down
Loading