SystemConfig.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OCP\IConfig;
  9. /**
  10. * Class which provides access to the system config values stored in config.php
  11. * Internal class for bootstrap only.
  12. * fixes cyclic DI: AllConfig needs AppConfig needs Database needs AllConfig
  13. */
  14. class SystemConfig {
  15. /** @var array */
  16. protected $sensitiveValues = [
  17. 'instanceid' => true,
  18. 'datadirectory' => true,
  19. 'dbname' => true,
  20. 'dbhost' => true,
  21. 'dbpassword' => true,
  22. 'dbuser' => true,
  23. 'dbreplica' => true,
  24. 'activity_dbname' => true,
  25. 'activity_dbhost' => true,
  26. 'activity_dbpassword' => true,
  27. 'activity_dbuser' => true,
  28. 'mail_from_address' => true,
  29. 'mail_domain' => true,
  30. 'mail_smtphost' => true,
  31. 'mail_smtpname' => true,
  32. 'mail_smtppassword' => true,
  33. 'passwordsalt' => true,
  34. 'secret' => true,
  35. 'updater.secret' => true,
  36. 'updater.server.url' => true,
  37. 'trusted_proxies' => true,
  38. 'preview_imaginary_url' => true,
  39. 'preview_imaginary_key' => true,
  40. 'proxyuserpwd' => true,
  41. 'sentry.dsn' => true,
  42. 'sentry.public-dsn' => true,
  43. 'zammad.download.secret' => true,
  44. 'zammad.portal.secret' => true,
  45. 'zammad.secret' => true,
  46. 'github.client_id' => true,
  47. 'github.client_secret' => true,
  48. 'log.condition' => [
  49. 'shared_secret' => true,
  50. ],
  51. 'license-key' => true,
  52. 'redis' => [
  53. 'host' => true,
  54. 'password' => true,
  55. ],
  56. 'redis.cluster' => [
  57. 'seeds' => true,
  58. 'password' => true,
  59. ],
  60. 'objectstore' => [
  61. 'arguments' => [
  62. // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207)
  63. 'options' => [
  64. 'credentials' => [
  65. 'key' => true,
  66. 'secret' => true,
  67. ]
  68. ],
  69. // S3
  70. 'key' => true,
  71. 'secret' => true,
  72. // Swift v2
  73. 'username' => true,
  74. 'password' => true,
  75. // Swift v3
  76. 'user' => [
  77. 'name' => true,
  78. 'password' => true,
  79. ],
  80. ],
  81. ],
  82. 'objectstore_multibucket' => [
  83. 'arguments' => [
  84. 'options' => [
  85. 'credentials' => [
  86. 'key' => true,
  87. 'secret' => true,
  88. ]
  89. ],
  90. // S3
  91. 'key' => true,
  92. 'secret' => true,
  93. // Swift v2
  94. 'username' => true,
  95. 'password' => true,
  96. // Swift v3
  97. 'user' => [
  98. 'name' => true,
  99. 'password' => true,
  100. ],
  101. ],
  102. ],
  103. 'onlyoffice' => [
  104. 'jwt_secret' => true,
  105. ],
  106. ];
  107. public function __construct(
  108. private Config $config,
  109. ) {
  110. }
  111. /**
  112. * Lists all available config keys
  113. * @return array an array of key names
  114. */
  115. public function getKeys() {
  116. return $this->config->getKeys();
  117. }
  118. /**
  119. * Sets a new system wide value
  120. *
  121. * @param string $key the key of the value, under which will be saved
  122. * @param mixed $value the value that should be stored
  123. */
  124. public function setValue($key, $value) {
  125. $this->config->setValue($key, $value);
  126. }
  127. /**
  128. * Sets and deletes values and writes the config.php
  129. *
  130. * @param array $configs Associative array with `key => value` pairs
  131. * If value is null, the config key will be deleted
  132. */
  133. public function setValues(array $configs) {
  134. $this->config->setValues($configs);
  135. }
  136. /**
  137. * Looks up a system wide defined value
  138. *
  139. * @param string $key the key of the value, under which it was saved
  140. * @param mixed $default the default value to be returned if the value isn't set
  141. * @return mixed the value or $default
  142. */
  143. public function getValue($key, $default = '') {
  144. return $this->config->getValue($key, $default);
  145. }
  146. /**
  147. * Looks up a system wide defined value and filters out sensitive data
  148. *
  149. * @param string $key the key of the value, under which it was saved
  150. * @param mixed $default the default value to be returned if the value isn't set
  151. * @return mixed the value or $default
  152. */
  153. public function getFilteredValue($key, $default = '') {
  154. $value = $this->getValue($key, $default);
  155. if (isset($this->sensitiveValues[$key])) {
  156. $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value);
  157. }
  158. return $value;
  159. }
  160. /**
  161. * Delete a system wide defined value
  162. *
  163. * @param string $key the key of the value, under which it was saved
  164. */
  165. public function deleteValue($key) {
  166. $this->config->deleteKey($key);
  167. }
  168. /**
  169. * @param bool|array $keysToRemove
  170. * @param mixed $value
  171. * @return mixed
  172. */
  173. protected function removeSensitiveValue($keysToRemove, $value) {
  174. if ($keysToRemove === true) {
  175. return IConfig::SENSITIVE_VALUE;
  176. }
  177. if (is_array($value)) {
  178. foreach ($keysToRemove as $keyToRemove => $valueToRemove) {
  179. if (isset($value[$keyToRemove])) {
  180. $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]);
  181. }
  182. }
  183. }
  184. return $value;
  185. }
  186. }