SystemConfig.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Johannes Schlichenmaier <johannes@schlichenmaier.info>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use OCP\IConfig;
  29. /**
  30. * Class which provides access to the system config values stored in config.php
  31. * Internal class for bootstrap only.
  32. * fixes cyclic DI: AllConfig needs AppConfig needs Database needs AllConfig
  33. */
  34. class SystemConfig {
  35. /** @var array */
  36. protected $sensitiveValues = [
  37. 'instanceid' => true,
  38. 'datadirectory' => true,
  39. 'dbname' => true,
  40. 'dbhost' => true,
  41. 'dbpassword' => true,
  42. 'dbuser' => true,
  43. 'activity_dbname' => true,
  44. 'activity_dbhost' => true,
  45. 'activity_dbpassword' => true,
  46. 'activity_dbuser' => true,
  47. 'mail_from_address' => true,
  48. 'mail_domain' => true,
  49. 'mail_smtphost' => true,
  50. 'mail_smtpname' => true,
  51. 'mail_smtppassword' => true,
  52. 'passwordsalt' => true,
  53. 'secret' => true,
  54. 'updater.secret' => true,
  55. 'updater.server.url' => true,
  56. 'trusted_proxies' => true,
  57. 'preview_imaginary_url' => true,
  58. 'proxyuserpwd' => true,
  59. 'sentry.dsn' => true,
  60. 'sentry.public-dsn' => true,
  61. 'zammad.download.secret' => true,
  62. 'zammad.portal.secret' => true,
  63. 'zammad.secret' => true,
  64. 'github.client_id' => true,
  65. 'github.client_secret' => true,
  66. 'log.condition' => [
  67. 'shared_secret' => true,
  68. ],
  69. 'license-key' => true,
  70. 'redis' => [
  71. 'host' => true,
  72. 'password' => true,
  73. ],
  74. 'redis.cluster' => [
  75. 'seeds' => true,
  76. 'password' => true,
  77. ],
  78. 'objectstore' => [
  79. 'arguments' => [
  80. // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207)
  81. 'options' => [
  82. 'credentials' => [
  83. 'key' => true,
  84. 'secret' => true,
  85. ]
  86. ],
  87. // S3
  88. 'key' => true,
  89. 'secret' => true,
  90. // Swift v2
  91. 'username' => true,
  92. 'password' => true,
  93. // Swift v3
  94. 'user' => [
  95. 'name' => true,
  96. 'password' => true,
  97. ],
  98. ],
  99. ],
  100. 'objectstore_multibucket' => [
  101. 'arguments' => [
  102. 'options' => [
  103. 'credentials' => [
  104. 'key' => true,
  105. 'secret' => true,
  106. ]
  107. ],
  108. // S3
  109. 'key' => true,
  110. 'secret' => true,
  111. // Swift v2
  112. 'username' => true,
  113. 'password' => true,
  114. // Swift v3
  115. 'user' => [
  116. 'name' => true,
  117. 'password' => true,
  118. ],
  119. ],
  120. ],
  121. 'onlyoffice' => [
  122. 'jwt_secret' => true,
  123. ],
  124. ];
  125. public function __construct(
  126. private Config $config,
  127. ) {
  128. }
  129. /**
  130. * Lists all available config keys
  131. * @return array an array of key names
  132. */
  133. public function getKeys() {
  134. return $this->config->getKeys();
  135. }
  136. /**
  137. * Sets a new system wide value
  138. *
  139. * @param string $key the key of the value, under which will be saved
  140. * @param mixed $value the value that should be stored
  141. */
  142. public function setValue($key, $value) {
  143. $this->config->setValue($key, $value);
  144. }
  145. /**
  146. * Sets and deletes values and writes the config.php
  147. *
  148. * @param array $configs Associative array with `key => value` pairs
  149. * If value is null, the config key will be deleted
  150. */
  151. public function setValues(array $configs) {
  152. $this->config->setValues($configs);
  153. }
  154. /**
  155. * Looks up a system wide defined value
  156. *
  157. * @param string $key the key of the value, under which it was saved
  158. * @param mixed $default the default value to be returned if the value isn't set
  159. * @return mixed the value or $default
  160. */
  161. public function getValue($key, $default = '') {
  162. return $this->config->getValue($key, $default);
  163. }
  164. /**
  165. * Looks up a system wide defined value and filters out sensitive data
  166. *
  167. * @param string $key the key of the value, under which it was saved
  168. * @param mixed $default the default value to be returned if the value isn't set
  169. * @return mixed the value or $default
  170. */
  171. public function getFilteredValue($key, $default = '') {
  172. $value = $this->getValue($key, $default);
  173. if (isset($this->sensitiveValues[$key])) {
  174. $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value);
  175. }
  176. return $value;
  177. }
  178. /**
  179. * Delete a system wide defined value
  180. *
  181. * @param string $key the key of the value, under which it was saved
  182. */
  183. public function deleteValue($key) {
  184. $this->config->deleteKey($key);
  185. }
  186. /**
  187. * @param bool|array $keysToRemove
  188. * @param mixed $value
  189. * @return mixed
  190. */
  191. protected function removeSensitiveValue($keysToRemove, $value) {
  192. if ($keysToRemove === true) {
  193. return IConfig::SENSITIVE_VALUE;
  194. }
  195. if (is_array($value)) {
  196. foreach ($keysToRemove as $keyToRemove => $valueToRemove) {
  197. if (isset($value[$keyToRemove])) {
  198. $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]);
  199. }
  200. }
  201. }
  202. return $value;
  203. }
  204. }