Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(lexicon): migrate config key/value
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Jun 24, 2025
commit e39ab652393841d9ad366eda6a8576f49e972bff
2 changes: 2 additions & 0 deletions core/Command/Config/App/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
*/
namespace OC\Core\Command\Config\App;

use OC\Config\ConfigManager;
use OCP\IAppConfig;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;

abstract class Base extends \OC\Core\Command\Base {
public function __construct(
protected IAppConfig $appConfig,
protected readonly ConfigManager $configManager,
) {
parent::__construct();
}
Expand Down
25 changes: 4 additions & 21 deletions core/Command/Config/App/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace OC\Core\Command\Config\App;

use OC\AppConfig;
use OCP\Exceptions\AppConfigIncorrectTypeException;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
use Symfony\Component\Console\Helper\QuestionHelper;
Expand Down Expand Up @@ -161,7 +160,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$value = (string)$input->getOption('value');

switch ($type) {
case IAppConfig::VALUE_MIXED:
$updated = $this->appConfig->setValueMixed($appName, $configName, $value, $lazy, $sensitive);
Expand All @@ -172,34 +170,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
break;

case IAppConfig::VALUE_INT:
if ($value !== ((string)((int)$value))) {
throw new AppConfigIncorrectTypeException('Value is not an integer');
}
$updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive);
$updated = $this->appConfig->setValueInt($appName, $configName, $this->configManager->convertToInt($value), $lazy, $sensitive);
break;

case IAppConfig::VALUE_FLOAT:
if ($value !== ((string)((float)$value))) {
throw new AppConfigIncorrectTypeException('Value is not a float');
}
$updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive);
$updated = $this->appConfig->setValueFloat($appName, $configName, $this->configManager->convertToFloat($value), $lazy, $sensitive);
break;

case IAppConfig::VALUE_BOOL:
if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) {
$valueBool = true;
} elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) {
$valueBool = false;
} else {
throw new AppConfigIncorrectTypeException('Value is not a boolean, please use \'true\' or \'false\'');
}
$updated = $this->appConfig->setValueBool($appName, $configName, $valueBool, $lazy);
$updated = $this->appConfig->setValueBool($appName, $configName, $this->configManager->convertToBool($value), $lazy);
break;

case IAppConfig::VALUE_ARRAY:
$valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
$valueArray = (is_array($valueArray)) ? $valueArray : throw new AppConfigIncorrectTypeException('Value is not an array');
$updated = $this->appConfig->setValueArray($appName, $configName, $valueArray, $lazy, $sensitive);
$updated = $this->appConfig->setValueArray($appName, $configName, $this->configManager->convertToArray($value), $lazy, $sensitive);
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/Command/Config/ListConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OC\Core\Command\Config;

use OC\Config\ConfigManager;
use OC\Core\Command\Base;
use OC\SystemConfig;
use OCP\IAppConfig;
Expand All @@ -22,6 +23,7 @@ class ListConfigs extends Base {
public function __construct(
protected SystemConfig $systemConfig,
protected IAppConfig $appConfig,
protected ConfigManager $configManager,
) {
parent::__construct();
}
Expand All @@ -44,13 +46,18 @@ protected function configure() {
InputOption::VALUE_NONE,
'Use this option when you want to include sensitive configs like passwords, salts, ...'
)
->addOption('migrate', null, InputOption::VALUE_NONE, 'Rename config keys of all enabled apps, based on ConfigLexicon')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$app = $input->getArgument('app');
$noSensitiveValues = !$input->getOption('private');

if ($input->getOption('migrate')) {
$this->configManager->migrateConfigLexiconKeys(($app === 'all') ? null : $app);
}

if (!is_string($app)) {
$output->writeln('<error>Invalid app value given</error>');
return 1;
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => $baseDir . '/lib/private/Config.php',
'OC\\Config\\ConfigManager' => $baseDir . '/lib/private/Config/ConfigManager.php',
'OC\\Config\\Lexicon\\CoreConfigLexicon' => $baseDir . '/lib/private/Config/Lexicon/CoreConfigLexicon.php',
'OC\\Config\\UserConfig' => $baseDir . '/lib/private/Config/UserConfig.php',
'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php',
Expand Down Expand Up @@ -1901,6 +1902,7 @@
'OC\\Repair\\ClearGeneratedAvatarCache' => $baseDir . '/lib/private/Repair/ClearGeneratedAvatarCache.php',
'OC\\Repair\\ClearGeneratedAvatarCacheJob' => $baseDir . '/lib/private/Repair/ClearGeneratedAvatarCacheJob.php',
'OC\\Repair\\Collation' => $baseDir . '/lib/private/Repair/Collation.php',
'OC\\Repair\\ConfigKeyMigration' => $baseDir . '/lib/private/Repair/ConfigKeyMigration.php',
'OC\\Repair\\Events\\RepairAdvanceEvent' => $baseDir . '/lib/private/Repair/Events/RepairAdvanceEvent.php',
'OC\\Repair\\Events\\RepairErrorEvent' => $baseDir . '/lib/private/Repair/Events/RepairErrorEvent.php',
'OC\\Repair\\Events\\RepairFinishEvent' => $baseDir . '/lib/private/Repair/Events/RepairFinishEvent.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
'OC\\Config\\ConfigManager' => __DIR__ . '/../../..' . '/lib/private/Config/ConfigManager.php',
'OC\\Config\\Lexicon\\CoreConfigLexicon' => __DIR__ . '/../../..' . '/lib/private/Config/Lexicon/CoreConfigLexicon.php',
'OC\\Config\\UserConfig' => __DIR__ . '/../../..' . '/lib/private/Config/UserConfig.php',
'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php',
Expand Down Expand Up @@ -1942,6 +1943,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair\\ClearGeneratedAvatarCache' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearGeneratedAvatarCache.php',
'OC\\Repair\\ClearGeneratedAvatarCacheJob' => __DIR__ . '/../../..' . '/lib/private/Repair/ClearGeneratedAvatarCacheJob.php',
'OC\\Repair\\Collation' => __DIR__ . '/../../..' . '/lib/private/Repair/Collation.php',
'OC\\Repair\\ConfigKeyMigration' => __DIR__ . '/../../..' . '/lib/private/Repair/ConfigKeyMigration.php',
'OC\\Repair\\Events\\RepairAdvanceEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairAdvanceEvent.php',
'OC\\Repair\\Events\\RepairErrorEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairErrorEvent.php',
'OC\\Repair\\Events\\RepairFinishEvent' => __DIR__ . '/../../..' . '/lib/private/Repair/Events/RepairFinishEvent.php',
Expand Down
5 changes: 5 additions & 0 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OC\AppConfig;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Config\ConfigManager;
use OCP\Activity\IManager as IActivityManager;
use OCP\App\AppPathNotFoundException;
use OCP\App\Events\AppDisableEvent;
Expand Down Expand Up @@ -568,6 +569,8 @@ public function enableApp(string $appId, bool $forceEnable = false): void {
ManagerEvent::EVENT_APP_ENABLE, $appId
));
$this->clearAppsCache();

\OCP\Server::get(ConfigManager::class)->migrateConfigLexiconKeys($appId);
}

/**
Expand Down Expand Up @@ -626,6 +629,8 @@ public function enableAppForGroups(string $appId, array $groups, bool $forceEnab
ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
));
$this->clearAppsCache();

\OCP\Server::get(ConfigManager::class)->migrateConfigLexiconKeys($appId);
}

/**
Expand Down
79 changes: 64 additions & 15 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class AppConfig implements IAppConfig {
private array $valueTypes = []; // type for all config values
private bool $fastLoaded = false;
private bool $lazyLoaded = false;
/** @var array<array-key, array{entries: array<array-key, ConfigLexiconEntry>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */
/** @var array<array-key, array{entries: array<array-key, ConfigLexiconEntry>, aliases: array<array-key, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */
private array $configLexiconDetails = [];
private bool $ignoreLexiconAliases = false;

/** @var ?array<string, string> */
private ?array $appVersionsCache = null;
Expand Down Expand Up @@ -117,6 +118,7 @@ public function getKeys(string $app): array {
public function hasKey(string $app, string $key, ?bool $lazy = false): bool {
$this->assertParams($app, $key);
$this->loadConfig($app, $lazy);
$this->matchAndApplyLexiconDefinition($app, $key);

if ($lazy === null) {
$appCache = $this->getAllValues($app);
Expand All @@ -142,6 +144,7 @@ public function hasKey(string $app, string $key, ?bool $lazy = false): bool {
public function isSensitive(string $app, string $key, ?bool $lazy = false): bool {
$this->assertParams($app, $key);
$this->loadConfig(null, $lazy);
$this->matchAndApplyLexiconDefinition($app, $key);

if (!isset($this->valueTypes[$app][$key])) {
throw new AppConfigUnknownKeyException('unknown config key');
Expand All @@ -162,6 +165,9 @@ public function isSensitive(string $app, string $key, ?bool $lazy = false): bool
* @since 29.0.0
*/
public function isLazy(string $app, string $key): bool {
$this->assertParams($app, $key);
$this->matchAndApplyLexiconDefinition($app, $key);

// there is a huge probability the non-lazy config are already loaded
if ($this->hasKey($app, $key, false)) {
return false;
Expand Down Expand Up @@ -284,7 +290,7 @@ public function getValueMixed(
): string {
try {
$lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy;
} catch (AppConfigUnknownKeyException $e) {
} catch (AppConfigUnknownKeyException) {
return $default;
}

Expand Down Expand Up @@ -429,6 +435,7 @@ private function getTypedValue(
int $type,
): string {
$this->assertParams($app, $key, valueType: $type);
$origKey = $key;
if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default)) {
return $default; // returns default if strictness of lexicon is set to WARNING (block and report)
}
Expand Down Expand Up @@ -469,6 +476,15 @@ private function getTypedValue(
$value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH));
}

// in case the key was modified while running matchAndApplyLexiconDefinition() we are
// interested to check options in case a modification of the value is needed
if ($origKey !== $key) {
$lexiconEntry = $this->getLexiconEntry($app, $key);
if ($type === self::VALUE_BOOL && $lexiconEntry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN)) {
$value = (in_array(strtolower($value), ['1', 'true', 'yes', 'on'])) ? '0' : '1';
}
}

return $value;
}

Expand Down Expand Up @@ -863,7 +879,8 @@ private function setTypedValue(
public function updateType(string $app, string $key, int $type = self::VALUE_MIXED): bool {
$this->assertParams($app, $key);
$this->loadConfigAll();
$lazy = $this->isLazy($app, $key);
$this->matchAndApplyLexiconDefinition($app, $key);
$this->isLazy($app, $key); // confirm key exists

// type can only be one type
if (!in_array($type, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) {
Expand Down Expand Up @@ -905,6 +922,7 @@ public function updateType(string $app, string $key, int $type = self::VALUE_MIX
public function updateSensitive(string $app, string $key, bool $sensitive): bool {
$this->assertParams($app, $key);
$this->loadConfigAll();
$this->matchAndApplyLexiconDefinition($app, $key);

try {
if ($sensitive === $this->isSensitive($app, $key, null)) {
Expand Down Expand Up @@ -964,6 +982,7 @@ public function updateSensitive(string $app, string $key, bool $sensitive): bool
public function updateLazy(string $app, string $key, bool $lazy): bool {
$this->assertParams($app, $key);
$this->loadConfigAll();
$this->matchAndApplyLexiconDefinition($app, $key);

try {
if ($lazy === $this->isLazy($app, $key)) {
Expand Down Expand Up @@ -999,6 +1018,7 @@ public function updateLazy(string $app, string $key, bool $lazy): bool {
public function getDetails(string $app, string $key): array {
$this->assertParams($app, $key);
$this->loadConfigAll();
$this->matchAndApplyLexiconDefinition($app, $key);
$lazy = $this->isLazy($app, $key);

if ($lazy) {
Expand Down Expand Up @@ -1086,6 +1106,8 @@ public function convertTypeToString(int $type): string {
*/
public function deleteKey(string $app, string $key): void {
$this->assertParams($app, $key);
$this->matchAndApplyLexiconDefinition($app, $key);

$qb = $this->connection->getQueryBuilder();
$qb->delete('appconfig')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
Expand Down Expand Up @@ -1293,6 +1315,7 @@ private function setAsLoaded(?bool $lazy): void {
*/
public function getValue($app, $key, $default = null) {
$this->loadConfig($app);
$this->matchAndApplyLexiconDefinition($app, $key);

return $this->fastCache[$app][$key] ?? $default;
}
Expand Down Expand Up @@ -1372,7 +1395,7 @@ private function formatAppValues(string $app, array $values, ?bool $lazy = null)
foreach ($values as $key => $value) {
try {
$type = $this->getValueType($app, $key, $lazy);
} catch (AppConfigUnknownKeyException $e) {
} catch (AppConfigUnknownKeyException) {
continue;
}

Expand Down Expand Up @@ -1556,17 +1579,18 @@ public function clearCachedConfig(): void {
}

/**
* match and apply current use of config values with defined lexicon
* Match and apply current use of config values with defined lexicon.
* Set $lazy to NULL only if only interested into checking that $key is alias.
*
* @throws AppConfigUnknownKeyException
* @throws AppConfigTypeConflictException
* @return bool TRUE if everything is fine compared to lexicon or lexicon does not exist
*/
private function matchAndApplyLexiconDefinition(
string $app,
string $key,
bool &$lazy,
int &$type,
string &$key,
?bool &$lazy = null,
int &$type = self::VALUE_MIXED,
string &$default = '',
): bool {
if (in_array($key,
Expand All @@ -1578,11 +1602,18 @@ private function matchAndApplyLexiconDefinition(
return true; // we don't break stuff for this list of config keys.
}
$configDetails = $this->getConfigDetailsFromLexicon($app);
if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) {
// in case '$rename' is set in ConfigLexiconEntry, we use the new config key
$key = $configDetails['aliases'][$key];
}

if (!array_key_exists($key, $configDetails['entries'])) {
return $this->applyLexiconStrictness(
$configDetails['strictness'],
'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon'
);
return $this->applyLexiconStrictness($configDetails['strictness'], 'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon');
}

// if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon
if ($lazy === null) {
return true;
}

/** @var ConfigLexiconEntry $configValue */
Expand Down Expand Up @@ -1644,27 +1675,45 @@ private function applyLexiconStrictness(
* extract details from registered $appId's config lexicon
*
* @param string $appId
* @internal
*
* @return array{entries: array<array-key, ConfigLexiconEntry>, strictness: ConfigLexiconStrictness}
* @return array{entries: array<array-key, ConfigLexiconEntry>, aliases: array<array-key, string>, strictness: ConfigLexiconStrictness}
*/
private function getConfigDetailsFromLexicon(string $appId): array {
public function getConfigDetailsFromLexicon(string $appId): array {
if (!array_key_exists($appId, $this->configLexiconDetails)) {
$entries = [];
$entries = $aliases = [];
$bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
$configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);
foreach ($configLexicon?->getAppConfigs() ?? [] as $configEntry) {
$entries[$configEntry->getKey()] = $configEntry;
if ($configEntry->getRename() !== null) {
$aliases[$configEntry->getRename()] = $configEntry->getKey();
}
}

$this->configLexiconDetails[$appId] = [
'entries' => $entries,
'aliases' => $aliases,
'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE
];
}

return $this->configLexiconDetails[$appId];
}

private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry {
return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null;
}

/**
* if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class
*
* @internal
*/
public function ignoreLexiconAliases(bool $ignore): void {
$this->ignoreLexiconAliases = $ignore;
}

/**
* Returns the installed versions of all apps
*
Expand Down
Loading