From 695a17804ed261980e9c3c20a588c1f3a3a3507f Mon Sep 17 00:00:00 2001 From: Philipp Schaffrath Date: Tue, 24 Jan 2017 12:18:29 +0100 Subject: [PATCH 1/3] Override config.php values through ENV variables (#26570) * added functionality to override config.php values with 'OC_' prefixed environment variables * use getenv to read environment variables since apache does not set $_ENV variables, fixed test Signed-off-by: Morris Jobke --- lib/private/Config.php | 14 ++++++++++++-- tests/lib/ConfigTest.php | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index e6a27a76b2..28d8c560c5 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -39,6 +39,9 @@ namespace OC; * configuration file of ownCloud. */ class Config { + + const ENV_PREFIX = 'OC_'; + /** @var array Associative array ($key => $value) */ protected $cache = array(); /** @var string */ @@ -71,15 +74,22 @@ class Config { } /** - * Gets a value from config.php + * Returns a config value * - * If it does not exist, $default will be returned. + * gets its value from an `OC_` prefixed environment variable + * if it doesn't exist from config.php + * if this doesn't exist either, it will return the given `$default` * * @param string $key key * @param mixed $default = null default value * @return mixed the value or $default */ public function getValue($key, $default = null) { + $envValue = getenv(self::ENV_PREFIX . $key); + if ($envValue) { + return $envValue; + } + if (isset($this->cache[$key])) { return $this->cache[$key]; } diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 74dcdc192c..f7add114aa 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -48,6 +48,12 @@ class ConfigTest extends TestCase { $this->assertSame(array('Appenzeller', 'Guinness', 'Kölsch'), $this->config->getValue('beers')); } + public function testGetValueReturnsEnvironmentValueIfSet() { + $this->assertEquals('bar', $this->config->getValue('foo')); + putenv('OC_foo=baz'); + $this->assertEquals('baz', $this->config->getValue('foo')); + } + public function testSetValue() { $this->config->setValue('foo', 'moo'); $expectedConfig = $this->initialConfig; From 0fcb37adcb08e8a74d712ff8a6146f91fa14d7a9 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 20 Mar 2017 21:29:55 -0600 Subject: [PATCH 2/3] OC_ -> NC_ Signed-off-by: Morris Jobke --- lib/private/Config.php | 4 ++-- tests/lib/ConfigTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index 28d8c560c5..7974202666 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -40,7 +40,7 @@ namespace OC; */ class Config { - const ENV_PREFIX = 'OC_'; + const ENV_PREFIX = 'NC_'; /** @var array Associative array ($key => $value) */ protected $cache = array(); @@ -76,7 +76,7 @@ class Config { /** * Returns a config value * - * gets its value from an `OC_` prefixed environment variable + * gets its value from an `NC_` prefixed environment variable * if it doesn't exist from config.php * if this doesn't exist either, it will return the given `$default` * diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index f7add114aa..418f7de0ae 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -50,7 +50,7 @@ class ConfigTest extends TestCase { public function testGetValueReturnsEnvironmentValueIfSet() { $this->assertEquals('bar', $this->config->getValue('foo')); - putenv('OC_foo=baz'); + putenv('NC_foo=baz'); $this->assertEquals('baz', $this->config->getValue('foo')); } From 95a21e2f2a7515be1d91d3f70db9cf821f872294 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 21 Mar 2017 12:46:17 -0600 Subject: [PATCH 3/3] Check for boolean false and add tests Signed-off-by: Morris Jobke --- lib/private/Config.php | 2 +- tests/lib/ConfigTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index 7974202666..f15854b611 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -86,7 +86,7 @@ class Config { */ public function getValue($key, $default = null) { $envValue = getenv(self::ENV_PREFIX . $key); - if ($envValue) { + if ($envValue !== false) { return $envValue; } diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 418f7de0ae..2a4c962034 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -52,6 +52,21 @@ class ConfigTest extends TestCase { $this->assertEquals('bar', $this->config->getValue('foo')); putenv('NC_foo=baz'); $this->assertEquals('baz', $this->config->getValue('foo')); + putenv('NC_foo'); // unset the env variable + } + + public function testGetValueReturnsEnvironmentValueIfSetToZero() { + $this->assertEquals('bar', $this->config->getValue('foo')); + putenv('NC_foo=0'); + $this->assertEquals('0', $this->config->getValue('foo')); + putenv('NC_foo'); // unset the env variable + } + + public function testGetValueReturnsEnvironmentValueIfSetToFalse() { + $this->assertEquals('bar', $this->config->getValue('foo')); + putenv('NC_foo=false'); + $this->assertEquals('false', $this->config->getValue('foo')); + putenv('NC_foo'); // unset the env variable } public function testSetValue() {