-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathExAppConfigService.php
More file actions
211 lines (194 loc) · 7.3 KB
/
Copy pathExAppConfigService.php
File metadata and controls
211 lines (194 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AppAPI\Service;
use OCA\AppAPI\Db\ExAppConfig;
use OCP\IAppConfig;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
/**
* App configuration backed by the server's standard `IAppConfig` storage (`oc_appconfig`).
*
* AppAPI historically kept its own `appconfig_ex` table; since the server gained typed,
* lazy and sensitive (encrypted) app config, that duplication is gone. All ExApp config
* values are stored as lazy strings; sensitive values are encrypted by the server.
*/
readonly class ExAppConfigService {
public function __construct(
private IAppConfig $appConfig,
private IDBConnection $connection,
private LoggerInterface $logger,
) {
}
/**
* Return the values of the requested keys that actually exist, in the shape
* `[['configkey' => ..., 'configvalue' => ...], ...]`. Sensitive values are
* returned decrypted (the server handles decryption transparently).
*/
public function getAppConfigValues(string $appId, array $configKeys): ?array {
try {
$values = [];
// array_unique: a single SQL `IN (...)` used to dedupe; preserve that so duplicate
// request keys don't yield duplicate result rows.
foreach (array_unique(array_map('strval', $configKeys)) as $configKey) {
// `null` lazy matches keys regardless of their lazy flag.
if (!$this->appConfig->hasKey($appId, $configKey, null)) {
continue;
}
try {
$configValue = $this->buildConfigValue($appId, $configKey)->getConfigvalue();
} catch (\Throwable $e) {
$this->logger->warning(sprintf('Failed to read value for app %s, config key %s', $appId, $configKey), ['exception' => $e]);
$configValue = '';
}
$values[] = [
'configkey' => $configKey,
'configvalue' => $configValue,
];
}
return $values;
} catch (\Throwable $e) {
$this->logger->warning(sprintf('Failed to get app config values for app %s', $appId), ['exception' => $e]);
return [];
}
}
/**
* Create or update an ExApp config value.
*
* When `$sensitive` is null the current sensitivity is preserved (or defaults to
* non-sensitive for a new key). The returned object always carries the plaintext value.
*/
public function setAppConfigValue(string $appId, string $configKey, mixed $configValue, ?int $sensitive = null): ?ExAppConfig {
try {
$value = (string)($configValue ?? '');
$currentSensitive = $this->appConfig->hasKey($appId, $configKey, null)
&& $this->appConfig->isSensitive($appId, $configKey, null);
$targetSensitive = $sensitive !== null ? (bool)$sensitive : $currentSensitive;
if ($currentSensitive && !$targetSensitive) {
$this->downgradeToPlain($appId, $configKey, $value);
} else {
$this->appConfig->setValueString($appId, $configKey, $value, lazy: true, sensitive: $targetSensitive);
}
return new ExAppConfig([
'appid' => $appId,
'configkey' => $configKey,
'configvalue' => $value,
'sensitive' => $targetSensitive ? 1 : 0,
]);
} catch (\Throwable $e) {
$this->logger->error(sprintf('Failed to set app config value for app %s, config key %s. Error: %s', $appId, $configKey, $e->getMessage()), ['exception' => $e]);
return null;
}
}
/**
* Delete the requested keys that exist; returns the number deleted, or -1 on error.
*/
public function deleteAppConfigValues(array $configKeys, string $appId): int {
try {
$deleted = 0;
foreach ($configKeys as $configKey) {
$configKey = (string)$configKey;
if ($this->appConfig->hasKey($appId, $configKey, null)) {
$this->appConfig->deleteKey($appId, $configKey);
$deleted++;
}
}
return $deleted;
} catch (\Throwable $e) {
$this->logger->error(sprintf('Failed to delete app config values for app %s. Error: %s', $appId, $e->getMessage()), ['exception' => $e]);
return -1;
}
}
/**
* @return ExAppConfig[]
*/
public function getAllAppConfig(string $appId): array {
try {
$result = [];
foreach ($this->appConfig->getKeys($appId) as $configKey) {
try {
$result[] = $this->buildConfigValue($appId, $configKey);
} catch (\Throwable $e) {
// A single unreadable/type-conflicting key must not drop the whole listing.
$this->logger->warning(sprintf('Failed to read app config for app %s, config key %s — skipping', $appId, $configKey), ['exception' => $e]);
}
}
return $result;
} catch (\Throwable $e) {
$this->logger->warning(sprintf('Failed to list app config for app %s', $appId), ['exception' => $e]);
return [];
}
}
public function getAppConfig(string $appId, string $configKey): ?ExAppConfig {
try {
if (!$this->appConfig->hasKey($appId, $configKey, null)) {
return null;
}
return $this->buildConfigValue($appId, $configKey);
} catch (\Throwable $e) {
$this->logger->warning(sprintf('Failed to get app config for app %s, config key %s', $appId, $configKey), ['exception' => $e]);
return null;
}
}
public function updateAppConfigValue(ExAppConfig $exAppConfig): ?ExAppConfig {
return $this->setAppConfigValue(
$exAppConfig->getAppid(),
$exAppConfig->getConfigkey(),
$exAppConfig->getConfigvalue(),
$exAppConfig->getSensitive(),
);
}
public function deleteAppConfig(ExAppConfig $exAppConfig): ?int {
try {
$appId = $exAppConfig->getAppid();
$configKey = $exAppConfig->getConfigkey();
if (!$this->appConfig->hasKey($appId, $configKey, null)) {
return 0;
}
$this->appConfig->deleteKey($appId, $configKey);
return 1;
} catch (\Throwable $e) {
$this->logger->error(sprintf('Failed to delete app config for app %s, config key %s. Error: %s', $exAppConfig->getAppid(), $exAppConfig->getConfigkey(), $e->getMessage()), ['exception' => $e]);
return null;
}
}
/**
* Turn a currently-sensitive key into a plain value with the new content.
*
* IAppConfig won't unset sensitivity via setValueString() (it is sticky), and updateSensitive()
* refreshes the type cache but not the value cache. So we drop the key and re-create it as a
* plain value, which leaves both storage and the in-request cache correct. The two writes run
* in a transaction so a failure can never leave the key deleted (the legacy path was atomic).
*/
private function downgradeToPlain(string $appId, string $configKey, string $value): void {
$this->connection->beginTransaction();
try {
$this->appConfig->deleteKey($appId, $configKey);
$this->appConfig->setValueString($appId, $configKey, $value, lazy: true, sensitive: false);
$this->connection->commit();
} catch (\Throwable $e) {
try {
$this->connection->rollBack();
} catch (\Throwable) {
// rollBack on an already-aborted transaction is not actionable here.
}
throw $e;
}
}
/**
* Build the value object for an existing key, reading it with its actual lazy flag
* so values created outside AppAPI (e.g. `occ config:app:set`) are handled too.
*/
private function buildConfigValue(string $appId, string $configKey): ExAppConfig {
$lazy = $this->appConfig->isLazy($appId, $configKey);
return new ExAppConfig([
'appid' => $appId,
'configkey' => $configKey,
'configvalue' => $this->appConfig->getValueString($appId, $configKey, '', lazy: $lazy),
'sensitive' => $this->appConfig->isSensitive($appId, $configKey, $lazy) ? 1 : 0,
]);
}
}