Skip to content

Commit dfec8db

Browse files
authored
Merge pull request #60318 from nextcloud/fix/remove-mountconfig-skiptest
fix(files_external): Move MountConfig to non-static services
2 parents 63680bd + 7439b3d commit dfec8db

22 files changed

Lines changed: 228 additions & 259 deletions

.github/workflows/files-external-sftp.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ jobs:
9898
- name: PHPUnit
9999
run: composer run test:files_external -- \
100100
apps/files_external/tests/Storage/SftpTest.php \
101-
apps/files_external/tests/Storage/SFTP_KeyTest.php \
102101
--log-junit junit.xml \
103102
${{ matrix.coverage && '--coverage-clover ./clover.xml' || '' }}
104103

apps/files_external/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
'OCA\\Files_External\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
118118
'OCA\\Files_External\\Service\\BackendService' => $baseDir . '/../lib/Service/BackendService.php',
119119
'OCA\\Files_External\\Service\\DBConfigService' => $baseDir . '/../lib/Service/DBConfigService.php',
120+
'OCA\\Files_External\\Service\\EncryptionService' => $baseDir . '/../lib/Service/EncryptionService.php',
120121
'OCA\\Files_External\\Service\\GlobalStoragesService' => $baseDir . '/../lib/Service/GlobalStoragesService.php',
121122
'OCA\\Files_External\\Service\\ImportLegacyStoragesService' => $baseDir . '/../lib/Service/ImportLegacyStoragesService.php',
122123
'OCA\\Files_External\\Service\\LegacyStoragesService' => $baseDir . '/../lib/Service/LegacyStoragesService.php',

apps/files_external/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class ComposerStaticInitFiles_External
132132
'OCA\\Files_External\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
133133
'OCA\\Files_External\\Service\\BackendService' => __DIR__ . '/..' . '/../lib/Service/BackendService.php',
134134
'OCA\\Files_External\\Service\\DBConfigService' => __DIR__ . '/..' . '/../lib/Service/DBConfigService.php',
135+
'OCA\\Files_External\\Service\\EncryptionService' => __DIR__ . '/..' . '/../lib/Service/EncryptionService.php',
135136
'OCA\\Files_External\\Service\\GlobalStoragesService' => __DIR__ . '/..' . '/../lib/Service/GlobalStoragesService.php',
136137
'OCA\\Files_External\\Service\\ImportLegacyStoragesService' => __DIR__ . '/..' . '/../lib/Service/ImportLegacyStoragesService.php',
137138
'OCA\\Files_External\\Service\\LegacyStoragesService' => __DIR__ . '/..' . '/../lib/Service/LegacyStoragesService.php',

apps/files_external/lib/Command/Verify.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use OC\Core\Command\Base;
1111
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
1212
use OCA\Files_External\Lib\StorageConfig;
13-
use OCA\Files_External\MountConfig;
1413
use OCA\Files_External\NotFoundException;
14+
use OCA\Files_External\Service\BackendService;
1515
use OCA\Files_External\Service\GlobalStoragesService;
1616
use OCP\AppFramework\Http;
1717
use OCP\Files\StorageNotAvailableException;
@@ -23,6 +23,7 @@
2323
class Verify extends Base {
2424
public function __construct(
2525
protected GlobalStoragesService $globalService,
26+
protected BackendService $backendService,
2627
) {
2728
parent::__construct();
2829
}
@@ -96,7 +97,7 @@ private function updateStorageStatus(StorageConfig &$storage, $configInput, Outp
9697
$backend = $storage->getBackend();
9798
// update status (can be time-consuming)
9899
$storage->setStatus(
99-
MountConfig::getBackendStatus(
100+
$this->backendService->getBackendStatus(
100101
$backend->getStorageClass(),
101102
$storage->getBackendOptions(),
102103
)

apps/files_external/lib/Config/ConfigAdapter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OC\Files\Storage\Wrapper\KnownMtime;
1414
use OCA\Files_External\Lib\PersonalMount;
1515
use OCA\Files_External\Lib\StorageConfig;
16-
use OCA\Files_External\MountConfig;
16+
use OCA\Files_External\Service\BackendService;
1717
use OCA\Files_External\Service\UserGlobalStoragesService;
1818
use OCA\Files_External\Service\UserStoragesService;
1919
use OCP\Files\Config\IAuthoritativeMountProvider;
@@ -39,6 +39,7 @@ class ConfigAdapter implements IMountProvider, IAuthoritativeMountProvider, IPar
3939
public function __construct(
4040
private UserStoragesService $userStoragesService,
4141
private UserGlobalStoragesService $userGlobalStoragesService,
42+
private BackendService $backendService,
4243
private ClockInterface $clock,
4344
) {
4445
}
@@ -63,7 +64,7 @@ private function validateObjectStoreClassString(string $class): string {
6364
*/
6465
private function prepareStorageConfig(StorageConfig &$storage, IUser $user): void {
6566
foreach ($storage->getBackendOptions() as $option => $value) {
66-
$storage->setBackendOption($option, MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
67+
$storage->setBackendOption($option, $this->backendService->applyConfigHandlers($value, $user->getUID()));
6768
}
6869

6970
$objectStore = $storage->getBackendOption('objectstore');

apps/files_external/lib/Controller/GlobalStoragesController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OCA\Files_External\Controller;
99

1010
use OCA\Files_External\NotFoundException;
11+
use OCA\Files_External\Service\BackendService;
1112
use OCA\Files_External\Service\GlobalStoragesService;
1213
use OCA\Files_External\Settings\Admin;
1314
use OCP\AppFramework\Http;
@@ -37,6 +38,7 @@ public function __construct(
3738
IUserSession $userSession,
3839
IGroupManager $groupManager,
3940
IConfig $config,
41+
BackendService $backendService,
4042
) {
4143
parent::__construct(
4244
$appName,
@@ -46,7 +48,8 @@ public function __construct(
4648
$logger,
4749
$userSession,
4850
$groupManager,
49-
$config
51+
$config,
52+
$backendService,
5053
);
5154
}
5255

apps/files_external/lib/Controller/StoragesController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use OCA\Files_External\Lib\Backend\Backend;
1212
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
1313
use OCA\Files_External\Lib\StorageConfig;
14-
use OCA\Files_External\MountConfig;
1514
use OCA\Files_External\NotFoundException;
15+
use OCA\Files_External\Service\BackendService;
1616
use OCA\Files_External\Service\StoragesService;
1717
use OCP\AppFramework\Controller;
1818
use OCP\AppFramework\Http;
@@ -48,6 +48,7 @@ public function __construct(
4848
protected IUserSession $userSession,
4949
protected IGroupManager $groupManager,
5050
protected IConfig $config,
51+
protected BackendService $backendService,
5152
) {
5253
parent::__construct($appName, $request);
5354
}
@@ -222,7 +223,7 @@ protected function updateStorageStatus(StorageConfig &$storage) {
222223
$backend = $storage->getBackend();
223224
// update status (can be time-consuming)
224225
$storage->setStatus(
225-
MountConfig::getBackendStatus(
226+
$this->backendService->getBackendStatus(
226227
$backend->getStorageClass(),
227228
$storage->getBackendOptions(),
228229
)

apps/files_external/lib/Controller/UserGlobalStoragesController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
1515
use OCA\Files_External\Lib\StorageConfig;
1616
use OCA\Files_External\NotFoundException;
17+
use OCA\Files_External\Service\BackendService;
1718
use OCA\Files_External\Service\UserGlobalStoragesService;
1819
use OCP\AppFramework\Http;
1920
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -50,6 +51,7 @@ public function __construct(
5051
IUserSession $userSession,
5152
IGroupManager $groupManager,
5253
IConfig $config,
54+
BackendService $backendService,
5355
) {
5456
parent::__construct(
5557
$appName,
@@ -59,7 +61,8 @@ public function __construct(
5961
$logger,
6062
$userSession,
6163
$groupManager,
62-
$config
64+
$config,
65+
$backendService,
6366
);
6467
}
6568

apps/files_external/lib/Controller/UserStoragesController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\Files_External\Lib\Backend\Backend;
1212
use OCA\Files_External\Lib\StorageConfig;
1313
use OCA\Files_External\NotFoundException;
14+
use OCA\Files_External\Service\BackendService;
1415
use OCA\Files_External\Service\UserStoragesService;
1516
use OCP\AppFramework\Http;
1617
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -40,6 +41,7 @@ public function __construct(
4041
IUserSession $userSession,
4142
IGroupManager $groupManager,
4243
IConfig $config,
44+
BackendService $backendService,
4345
) {
4446
parent::__construct(
4547
$appName,
@@ -49,7 +51,8 @@ public function __construct(
4951
$logger,
5052
$userSession,
5153
$groupManager,
52-
$config
54+
$config,
55+
$backendService,
5356
);
5457
}
5558

apps/files_external/lib/MountConfig.php

Lines changed: 14 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,34 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OCA\Files_External;
910

10-
use OC\Files\Storage\Common;
11-
use OCA\Files_External\Config\IConfigHandler;
12-
use OCA\Files_External\Config\UserContext;
1311
use OCA\Files_External\Lib\Backend\Backend;
1412
use OCA\Files_External\Service\BackendService;
15-
use OCA\Files_External\Service\GlobalStoragesService;
16-
use OCA\Files_External\Service\UserGlobalStoragesService;
17-
use OCA\Files_External\Service\UserStoragesService;
18-
use OCP\Files\StorageNotAvailableException;
19-
use OCP\IConfig;
20-
use OCP\Security\ISecureRandom;
13+
use OCA\Files_External\Service\EncryptionService;
2114
use OCP\Server;
22-
use phpseclib\Crypt\AES;
2315
use Psr\Container\ContainerExceptionInterface;
24-
use Psr\Log\LoggerInterface;
2516

2617
/**
2718
* Class to configure mount.json globally and for users
2819
*/
2920
class MountConfig {
30-
// TODO: make this class non-static and give it a proper namespace
31-
3221
public const MOUNT_TYPE_GLOBAL = 'global';
3322
public const MOUNT_TYPE_GROUP = 'group';
3423
public const MOUNT_TYPE_USER = 'user';
3524
public const MOUNT_TYPE_PERSONAL = 'personal';
3625

37-
// whether to skip backend test (for unit tests, as this static class is not mockable)
38-
public static $skipTest = false;
39-
40-
public function __construct(
41-
private UserGlobalStoragesService $userGlobalStorageService,
42-
private UserStoragesService $userStorageService,
43-
private GlobalStoragesService $globalStorageService,
44-
) {
45-
}
46-
4726
/**
4827
* @param mixed $input
4928
* @param string|null $userId
5029
* @return mixed
5130
* @throws ContainerExceptionInterface
5231
* @since 16.0.0
32+
* @deprecated 34.0.0 use BackendService instead
5333
*/
5434
public static function substitutePlaceholdersInConfig($input, ?string $userId = null) {
55-
/** @var BackendService $backendService */
56-
$backendService = Server::get(BackendService::class);
57-
/** @var IConfigHandler[] $handlers */
58-
$handlers = $backendService->getConfigHandlers();
59-
foreach ($handlers as $handler) {
60-
if ($handler instanceof UserContext && $userId !== null) {
61-
$handler->setUserId($userId);
62-
}
63-
$input = $handler->handle($input);
64-
}
65-
return $input;
35+
return Server::get(BackendService::class)->applyConfigHandlers($input, $userId);
6636
}
6737

6838
/**
@@ -73,120 +43,41 @@ public static function substitutePlaceholdersInConfig($input, ?string $userId =
7343
* @param boolean $isPersonal
7444
* @return int see self::STATUS_*
7545
* @throws \Exception
46+
* @deprecated 34.0.0 use BackendService instead
7647
*/
7748
public static function getBackendStatus($class, $options) {
78-
if (self::$skipTest) {
79-
return StorageNotAvailableException::STATUS_SUCCESS;
80-
}
81-
foreach ($options as $key => &$option) {
82-
if ($key === 'password') {
83-
// no replacements in passwords
84-
continue;
85-
}
86-
$option = self::substitutePlaceholdersInConfig($option);
87-
}
88-
if (class_exists($class)) {
89-
try {
90-
/** @var Common $storage */
91-
$storage = new $class($options);
92-
93-
try {
94-
$result = $storage->test();
95-
$storage->setAvailability($result);
96-
if ($result) {
97-
return StorageNotAvailableException::STATUS_SUCCESS;
98-
}
99-
} catch (\Exception $e) {
100-
$storage->setAvailability(false);
101-
throw $e;
102-
}
103-
} catch (\Exception $exception) {
104-
Server::get(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'app' => 'files_external']);
105-
throw $exception;
106-
}
107-
}
108-
return StorageNotAvailableException::STATUS_ERROR;
49+
return Server::get(BackendService::class)->getBackendStatus($class, $options);
10950
}
11051

11152
/**
11253
* Encrypt passwords in the given config options
11354
*
11455
* @param array $options mount options
11556
* @return array updated options
57+
* @deprecated 34.0.0 use EncryptionService instead
11658
*/
11759
public static function encryptPasswords($options) {
118-
if (isset($options['password'])) {
119-
$options['password_encrypted'] = self::encryptPassword($options['password']);
120-
// do not unset the password, we want to keep the keys order
121-
// on load... because that's how the UI currently works
122-
$options['password'] = '';
123-
}
124-
return $options;
60+
return Server::get(EncryptionService::class)->encryptPasswords($options);
12561
}
12662

12763
/**
12864
* Decrypt passwords in the given config options
12965
*
13066
* @param array $options mount options
13167
* @return array updated options
68+
* @deprecated 34.0.0 use EncryptionService instead
13269
*/
13370
public static function decryptPasswords($options) {
134-
// note: legacy options might still have the unencrypted password in the "password" field
135-
if (isset($options['password_encrypted'])) {
136-
$options['password'] = self::decryptPassword($options['password_encrypted']);
137-
unset($options['password_encrypted']);
138-
}
139-
return $options;
140-
}
141-
142-
/**
143-
* Encrypt a single password
144-
*
145-
* @param string $password plain text password
146-
* @return string encrypted password
147-
*/
148-
private static function encryptPassword($password) {
149-
$cipher = self::getCipher();
150-
$iv = Server::get(ISecureRandom::class)->generate(16);
151-
$cipher->setIV($iv);
152-
return base64_encode($iv . $cipher->encrypt($password));
153-
}
154-
155-
/**
156-
* Decrypts a single password
157-
*
158-
* @param string $encryptedPassword encrypted password
159-
* @return string plain text password
160-
*/
161-
private static function decryptPassword($encryptedPassword) {
162-
$cipher = self::getCipher();
163-
$binaryPassword = base64_decode($encryptedPassword);
164-
$iv = substr($binaryPassword, 0, 16);
165-
$cipher->setIV($iv);
166-
$binaryPassword = substr($binaryPassword, 16);
167-
return $cipher->decrypt($binaryPassword);
168-
}
169-
170-
/**
171-
* Returns the encryption cipher
172-
*
173-
* @return AES
174-
*/
175-
private static function getCipher() {
176-
$cipher = new AES(AES::MODE_CBC);
177-
$cipher->setKey(Server::get(IConfig::class)->getSystemValue('passwordsalt', null));
178-
return $cipher;
71+
return Server::get(EncryptionService::class)->decryptPasswords($options);
17972
}
18073

18174
/**
18275
* Computes a hash based on the given configuration.
18376
* This is mostly used to find out whether configurations
18477
* are the same.
185-
*
186-
* @param array $config
187-
* @return string
78+
* @throws \JsonException
18879
*/
189-
public static function makeConfigHash($config) {
80+
public static function makeConfigHash(array $config): string {
19081
$data = json_encode(
19182
[
19283
'c' => $config['backend'],
@@ -195,7 +86,8 @@ public static function makeConfigHash($config) {
19586
'o' => $config['options'],
19687
'p' => $config['priority'] ?? -1,
19788
'mo' => $config['mountOptions'] ?? [],
198-
]
89+
],
90+
JSON_THROW_ON_ERROR
19991
);
20092
return hash('md5', $data);
20193
}

0 commit comments

Comments
 (0)