SystemConfig.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. 'trusted_proxies' => true,
  56. 'proxyuserpwd' => true,
  57. 'sentry.dsn' => true,
  58. 'sentry.public-dsn' => true,
  59. 'zammad.download.secret' => true,
  60. 'zammad.portal.secret' => true,
  61. 'zammad.secret' => true,
  62. 'github.client_id' => true,
  63. 'github.client_secret' => true,
  64. 'log.condition' => [
  65. 'shared_secret' => true,
  66. ],
  67. 'license-key' => true,
  68. 'redis' => [
  69. 'host' => true,
  70. 'password' => true,
  71. ],
  72. 'redis.cluster' => [
  73. 'seeds' => true,
  74. 'password' => true,
  75. ],
  76. 'objectstore' => [
  77. 'arguments' => [
  78. // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207)
  79. 'options' => [
  80. 'credentials' => [
  81. 'key' => true,
  82. 'secret' => true,
  83. ]
  84. ],
  85. // S3
  86. 'key' => true,
  87. 'secret' => true,
  88. // Swift v2
  89. 'username' => true,
  90. 'password' => true,
  91. // Swift v3
  92. 'user' => [
  93. 'name' => true,
  94. 'password' => true,
  95. ],
  96. ],
  97. ],
  98. 'objectstore_multibucket' => [
  99. 'arguments' => [
  100. 'options' => [
  101. 'credentials' => [
  102. 'key' => true,
  103. 'secret' => true,
  104. ]
  105. ],
  106. // S3
  107. 'key' => true,
  108. 'secret' => true,
  109. // Swift v2
  110. 'username' => true,
  111. 'password' => true,
  112. // Swift v3
  113. 'user' => [
  114. 'name' => true,
  115. 'password' => true,
  116. ],
  117. ],
  118. ],
  119. ];
  120. /** @var Config */
  121. private $config;
  122. public function __construct(Config $config) {
  123. $this->config = $config;
  124. }
  125. /**
  126. * Lists all available config keys
  127. * @return array an array of key names
  128. */
  129. public function getKeys() {
  130. return $this->config->getKeys();
  131. }
  132. /**
  133. * Sets a new system wide value
  134. *
  135. * @param string $key the key of the value, under which will be saved
  136. * @param mixed $value the value that should be stored
  137. */
  138. public function setValue($key, $value) {
  139. $this->config->setValue($key, $value);
  140. }
  141. /**
  142. * Sets and deletes values and writes the config.php
  143. *
  144. * @param array $configs Associative array with `key => value` pairs
  145. * If value is null, the config key will be deleted
  146. */
  147. public function setValues(array $configs) {
  148. $this->config->setValues($configs);
  149. }
  150. /**
  151. * Looks up a system wide defined value
  152. *
  153. * @param string $key the key of the value, under which it was saved
  154. * @param mixed $default the default value to be returned if the value isn't set
  155. * @return mixed the value or $default
  156. */
  157. public function getValue($key, $default = '') {
  158. return $this->config->getValue($key, $default);
  159. }
  160. /**
  161. * Looks up a system wide defined value and filters out sensitive data
  162. *
  163. * @param string $key the key of the value, under which it was saved
  164. * @param mixed $default the default value to be returned if the value isn't set
  165. * @return mixed the value or $default
  166. */
  167. public function getFilteredValue($key, $default = '') {
  168. $value = $this->getValue($key, $default);
  169. if (isset($this->sensitiveValues[$key])) {
  170. $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value);
  171. }
  172. return $value;
  173. }
  174. /**
  175. * Delete a system wide defined value
  176. *
  177. * @param string $key the key of the value, under which it was saved
  178. */
  179. public function deleteValue($key) {
  180. $this->config->deleteKey($key);
  181. }
  182. /**
  183. * @param bool|array $keysToRemove
  184. * @param mixed $value
  185. * @return mixed
  186. */
  187. protected function removeSensitiveValue($keysToRemove, $value) {
  188. if ($keysToRemove === true) {
  189. return IConfig::SENSITIVE_VALUE;
  190. }
  191. if (is_array($value)) {
  192. foreach ($keysToRemove as $keyToRemove => $valueToRemove) {
  193. if (isset($value[$keyToRemove])) {
  194. $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]);
  195. }
  196. }
  197. }
  198. return $value;
  199. }
  200. }