Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Dec 7, 2023
1 parent e5e50ec commit 7e6d8a9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 58 deletions.
7 changes: 1 addition & 6 deletions core/Command/Config/ListConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,7 @@ protected function getSystemConfigs(bool $noSensitiveValues): array {
* @return array
*/
protected function getAppConfigs(string $app, bool $noSensitiveValues) {
// return $this->appConfig->getAllValues($app, filtered: $noSensitiveValues);
if ($noSensitiveValues) {
return $this->appConfig->getFilteredValues($app);
} else {
return $this->appConfig->getValues($app, false);
}
return $this->appConfig->getAllValues($app, filtered: $noSensitiveValues);
}

/**
Expand Down
1 change: 0 additions & 1 deletion core/Command/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

use OC\Console\TimestampFormatter;
use OC\DB\MigratorExecuteSqlEvent;
use OC\Installer;
use OC\Repair\Events\RepairAdvanceEvent;
use OC\Repair\Events\RepairErrorEvent;
use OC\Repair\Events\RepairFinishEvent;
Expand Down
144 changes: 103 additions & 41 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use InvalidArgumentException;
use JsonException;
use OCP\DB\Exception as DBException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
use OCP\ICache;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -159,6 +161,23 @@ public function hasKey(string $app, string $key, string $lazyGroup = ''): bool {
return isset($cache[$app][$key]);
}

/**
* @param string $app id of the app
* @param string $key config key
* @param string $lazyGroup lazy group
*
* @throws AppConfigUnknownKeyException if config key is not known
* @return bool
* @since 29.0.0
*/
public function isSensitiveKey(string $app, string $key, string $lazyGroup = ''): bool {
$this->assertParams($app, $key, $lazyGroup);
$this->loadConfig($lazyGroup);
($lazyGroup === '') ? $cache = &$this->fastCache : $cache = &$this->lazyCache;
/** @psalm-suppress UndefinedVariable */
return $cache[$app][$key]['sensitive'] ?? throw new AppConfigUnknownKeyException();
}

/**
* @inheritDoc
* @param string $app if of the app
Expand All @@ -167,6 +186,7 @@ public function hasKey(string $app, string $key, string $lazyGroup = ''): bool {
* @return string|null lazy group or NULL if key is not found
*/
public function getLazyGroup(string $app, string $key): ?string {
$this->assertParams($app, $key);
$qb = $this->connection->getQueryBuilder();
$qb->select('lazy_group')
->from('appconfig')
Expand All @@ -192,8 +212,22 @@ public function getLazyGroup(string $app, string $key): ?string {
public function getAllValues(string $app, string $key = '', bool $filtered = false): array {
$this->assertParams($app, $key);
$this->loadConfig(ignoreLazyGroup: true);
// TODO: filtered=true
return array_merge($this->fastCache[$app], $this->lazyCache[$app]);

$values = array_merge($this->fastCache[$app], $this->lazyCache[$app] ?? []);

/**
* Using the old (deprecated) list of sensitive values.
*/
foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) {
$sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
foreach ($sensitiveKeys as $sensitiveKey) {
$values[$sensitiveKey]['sensitive'] = true;
}
}

return array_map(function (array $entry) use ($app): mixed {
return ($entry['sensitive']) ? IConfig::SENSITIVE_VALUE : $entry['value'];
}, $values);
}

/**
Expand All @@ -205,13 +239,14 @@ public function getAllValues(string $app, string $key = '', bool $filtered = fal
* @since 29.0.0
*/
public function searchValues(string $key, string $lazyGroup = ''): array {
$this->assertParams('', $key, $lazyGroup);
$this->loadConfig($lazyGroup);
$values = [];
/** @var array<array-key, array<array-key, mixed>> $cache */
($lazyGroup === '') ? $cache = &$this->fastCache : $cache = &$this->lazyCache;
foreach (array_keys($cache) as $app) {
if (isset($cache[$app][$key])) {
$values[$app] = $cache[$app][$key];
$values[$app] = $cache[$app][$key]['value'];
}
}

Expand Down Expand Up @@ -240,7 +275,7 @@ public function getValueString(string $app, string $key, string $default = '', s
$this->loadConfig($lazyGroup);
($lazyGroup === '') ? $cache = &$this->fastCache : $cache = &$this->lazyCache;
/** @psalm-suppress UndefinedVariable */
return $cache[$app][$key] ?? $default;
return $cache[$app][$key]['value'] ?? $default;
}

/**
Expand Down Expand Up @@ -331,7 +366,7 @@ public function getValueArray(string $app, string $key, array $default = [], str
* @param string $key config key
* @param string $value config value
* @param string $lazyGroup name of the lazy group
* @param bool $sensitive value should be hidden when needed
* @param bool|null $sensitive value should be hidden when needed. if NULL sensitive flag is not changed in database
*
* @return bool TRUE if value was different, therefor updated in database
* @since 29.0.0
Expand All @@ -345,23 +380,32 @@ public function setValueString(
string $app,
string $key,
string $value,
bool $sensitive = false,
?bool $sensitive = null,
string $lazyGroup = ''
): bool {
$this->assertParams($app, $key, $lazyGroup);
$this->loadConfig($lazyGroup);
$updated = !$this->hasKey($app, $key, $lazyGroup) || $value !== $this->getValueString($app, $key, $value, $lazyGroup);
// store value if not known yet, or value is different, or sensitivity changed
$updated = !$this->hasKey($app, $key, $lazyGroup)
|| $value !== $this->getValueString($app, $key, $value, $lazyGroup)
|| ($sensitive !== null && $sensitive !== $this->isSensitiveKey($app, $key, $lazyGroup));
if (!$updated) {
return false;
}

// update local cache, do not touch sensitive if null or set it to false if new key
($lazyGroup === '') ? $cache = &$this->fastCache : $cache = &$this->lazyCache;
$cache[$app][$key] = $value;
$cache[$app][$key] = [
'value' => $value,
'sensitive' => $sensitive ?? $cache[$app][$key]['sensitive'] ?? false,
'lazyGroup' => $lazyGroup
];

$insert = $this->connection->getQueryBuilder();
$insert->insert('appconfig')
->setValue('appid', $insert->createNamedParameter($app))
->setValue('lazy_group', $insert->createNamedParameter($lazyGroup))
->setValue('sensitive', $insert->createNamedParameter(($sensitive ?? false) ? 1 : 0, IQueryBuilder::PARAM_INT))
->setValue('configkey', $insert->createNamedParameter($key))
->setValue('configvalue', $insert->createNamedParameter($value));
try {
Expand All @@ -377,6 +421,10 @@ public function setValueString(
->set('lazy_group', $update->createNamedParameter($lazyGroup))
->where($update->expr()->eq('appid', $update->createNamedParameter($app)))
->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
if ($sensitive !== null) {
$update->set('sensitive', $update->createNamedParameter($sensitive ? 1 : 0, IQueryBuilder::PARAM_INT));
}

$update->executeStatement();
}

Expand All @@ -389,7 +437,7 @@ public function setValueString(
* @param string $key config key
* @param int $value config value
* @param string $lazyGroup name of the lazy group
* @param bool $sensitive value should be hidden when needed
* @param bool|null $sensitive value should be hidden when needed. if NULL sensitive flag is not changed in database
*
* @return bool TRUE if value was different, therefor updated in database
* @since 29.0.0
Expand All @@ -399,8 +447,8 @@ public function setValueString(
* @see self::setValueBool()
* @see self::setValueArray()
*/
public function setValueInt(string $app, string $key, int $value, bool $sensitive = false, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, (string) $value);
public function setValueInt(string $app, string $key, int $value, ?bool $sensitive = null, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, (string) $value, $sensitive, $lazyGroup);
}

/**
Expand All @@ -409,7 +457,7 @@ public function setValueInt(string $app, string $key, int $value, bool $sensitiv
* @param string $key config key
* @param float $value config value
* @param string $lazyGroup name of the lazy group
* @param bool $sensitive value should be hidden when needed
* @param bool|null $sensitive value should be hidden when needed. if NULL sensitive flag is not changed in database
*
* @return bool TRUE if value was different, therefor updated in database
* @since 29.0.0
Expand All @@ -419,8 +467,8 @@ public function setValueInt(string $app, string $key, int $value, bool $sensitiv
* @see self::setValueBool()
* @see self::setValueArray()
*/
public function setValueFloat(string $app, string $key, float $value, bool $sensitive = false, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, (string) $value);
public function setValueFloat(string $app, string $key, float $value, ?bool $sensitive = null, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, (string) $value, $sensitive, $lazyGroup);
}

/**
Expand All @@ -429,7 +477,7 @@ public function setValueFloat(string $app, string $key, float $value, bool $sens
* @param string $key config key
* @param bool $value config value
* @param string $lazyGroup name of the lazy group
* @param bool $sensitive value should be hidden when needed
* @param bool|null $sensitive value should be hidden when needed. if NULL sensitive flag is not changed in database
*
* @return bool TRUE if value was different, therefor updated in database
* @since 29.0.0
Expand All @@ -439,8 +487,8 @@ public function setValueFloat(string $app, string $key, float $value, bool $sens
* @see self::setValueFloat()
* @see self::setValueArray()
*/
public function setValueBool(string $app, string $key, bool $value, bool $sensitive = false, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, $value ? 'true' : 'false');
public function setValueBool(string $app, string $key, bool $value, ?bool $sensitive = null, string $lazyGroup = ''): bool {
return $this->setValueString($app, $key, $value ? 'true' : 'false', $sensitive, $lazyGroup);
}

/**
Expand All @@ -449,7 +497,7 @@ public function setValueBool(string $app, string $key, bool $value, bool $sensit
* @param string $key config key
* @param array $value config value
* @param string $lazyGroup name of the lazy group
* @param bool $sensitive value should be hidden when needed
* @param bool|null $sensitive value should be hidden when needed. if NULL sensitive flag is not changed in database
*
* @return bool TRUE if value was different, therefor updated in database
* @since 29.0.0
Expand All @@ -459,9 +507,9 @@ public function setValueBool(string $app, string $key, bool $value, bool $sensit
* @see self::setValueFloat()
* @see self::setValueBool()
*/
public function setValueArray(string $app, string $key, array $value, bool $sensitive = false, string $lazyGroup = ''): bool {
public function setValueArray(string $app, string $key, array $value, ?bool $sensitive = null, string $lazyGroup = ''): bool {
try {
return $this->setValueString($app, $key, json_encode($value, JSON_THROW_ON_ERROR));
return $this->setValueString($app, $key, json_encode($value, JSON_THROW_ON_ERROR), $sensitive, $lazyGroup);
} catch (JsonException $e) {
$this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]);
}
Expand All @@ -478,6 +526,7 @@ public function setValueArray(string $app, string $key, array $value, bool $sens
* @since 29.0.0
*/
public function unsetKey(string $app, string $key): void {
$this->assertParams($app, $key);
$qb = $this->connection->getQueryBuilder();
$qb->delete('appconfig')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
Expand All @@ -499,6 +548,7 @@ public function unsetKey(string $app, string $key): void {
* @since 29.0.0
*/
public function unsetAppKeys(string $app): void {
$this->assertParams($app);
$qb = $this->connection->getQueryBuilder();
$qb->delete('appconfig')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)));
Expand All @@ -515,6 +565,7 @@ public function unsetAppKeys(string $app): void {
* @see IAppConfig for explanation about lazy grouping
*/
public function deleteLazyGroup(string $lazyGroup): void {
$this->assertParams(lazyGroup: $lazyGroup);
$qb = $this->connection->getQueryBuilder();
$qb->delete('appconfig')
->where($qb->expr()->eq('lazy_group', $qb->createNamedParameter($lazyGroup)));
Expand Down Expand Up @@ -564,13 +615,13 @@ private function assertParams(string $app = '', string $configKey = '', string $
throw new InvalidArgumentException('app cannot be an empty string');
}
if (strlen($app) > self::APP_MAX_LENGTH) {
throw new InvalidArgumentException('value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')');
throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')');
}
if (strlen($configKey) > self::KEY_MAX_LENGTH) {
throw new InvalidArgumentException('value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')');
throw new InvalidArgumentException('Value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')');
}
if (strlen($lazyGroup) > self::LAZY_MAX_LENGTH) {
throw new InvalidArgumentException('value (' . $lazyGroup . ') for lazyGroup is too long (' . self::LAZY_MAX_LENGTH . ')');
throw new InvalidArgumentException('Value (' . $lazyGroup . ') for lazyGroup is too long (' . self::LAZY_MAX_LENGTH . ')');
}
}

Expand All @@ -591,6 +642,7 @@ private function getDistributedCache(string $item): array {
}

private function loadConfigAll(): void {
// TODO: why use of __ALL__ ? still needed ?
$this->loadConfig(self::ALL_APPS_CONFIG, ignoreLazyGroup: true);
}

Expand All @@ -615,7 +667,7 @@ private function loadConfig(string $lazyGroup = '', bool $ignoreLazyGroup = fals
if (!$this->migrationCompleted) {
$qb->select('appid', 'configkey', 'configvalue');
} else {
$qb->select('appid', 'configkey', 'configvalue', 'lazy_group');
$qb->select('appid', 'configkey', 'configvalue', 'sensitive', 'lazy_group');
if (!$ignoreLazyGroup) {
$qb->where($qb->expr()->eq('lazy_group', $qb->createNamedParameter($lazyGroup)));
}
Expand Down Expand Up @@ -646,7 +698,12 @@ private function loadConfig(string $lazyGroup = '', bool $ignoreLazyGroup = fals
}
// if migration is not completed, 'lazy_group' does not exist in $row
(($row['lazy_group'] ?? '') === '') ? $cache = &$this->fastCache : $cache = &$this->lazyCache;
$cache[$row['appid']][$row['configkey']] = $row['configvalue'];
$cache[$row['appid']][$row['configkey']] =
[
'value' => $row['configvalue'],
'lazyGroup' => $row['lazy_group'],
'sensitive' => ($row['sensitive'] === 1)
];
}
$result->closeCursor();
if ($lazyGroup === '' && !$ignoreLazyGroup) {
Expand Down Expand Up @@ -712,12 +769,9 @@ private function storeDistributedCache(): void {
}
}

//
//
//
// -- DEPRECATED
//
//
/**
* All methods below this line are set as deprecated.
*/

/**
* Gets the config value
Expand Down Expand Up @@ -813,6 +867,23 @@ public function getValues($app, $key) {
*/
public function getFilteredValues($app) {
$values = $this->getAllValues($app, filtered: true);
foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) {
$sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
foreach ($sensitiveKeys as $sensitiveKey) {
$values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
}
}

return $values;
}

/**
* @param string $app
*
* @return string[]
* @deprecated data sensitivity should be set when calling setValue*()
*/
private function getSensitiveKeys(string $app): array {
$sensitiveValues = [
'circles' => [
'/^key_pairs$/',
Expand Down Expand Up @@ -911,16 +982,7 @@ public function getFilteredValues($app) {
],
];

if (isset($sensitiveValues[$app])) {
foreach ($sensitiveValues[$app] as $sensitiveKeyExp) {
$sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
foreach ($sensitiveKeys as $sensitiveKey) {
$values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
}
}
}

return $values;
return $sensitiveValues[$app] ?? [];
}

/**
Expand Down
Loading

0 comments on commit 7e6d8a9

Please sign in to comment.