]> git.mxchange.org Git - friendica.git/commitdiff
Add the possibility to use a different configuration directory
authorPhilipp <admin@philipp.info>
Sat, 11 Sep 2021 12:33:26 +0000 (14:33 +0200)
committerPhilipp <admin@philipp.info>
Sat, 11 Sep 2021 12:33:26 +0000 (14:33 +0200)
12 files changed:
doc/Config.md
src/App.php
src/Module/Admin/Summary.php
src/Util/ConfigFileLoader.php
static/dependencies.config.php
tests/Util/VFSTrait.php
tests/src/Core/Cache/DatabaseCacheTest.php
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Core/StorageManagerTest.php
tests/src/Model/ProcessTest.php
tests/src/Model/Storage/DatabaseStorageTest.php
tests/src/Util/Config/ConfigFileLoaderTest.php

index f2224f5de7f2d573791e32dc642c441b8376ce09..8855099391d5dabd99fe4b4f9dd94fd0b0a0a8ef 100644 (file)
@@ -42,6 +42,9 @@ Some examples of common known configuration files:
 
 Addons can define their own default configuration values in `addon/[addon]/config/[addon].config.php` which is loaded when the addon is activated.
 
+If needed, an alternative `config` path can be used by using the `FRIENDICA_CONFIG_DIR` environment variable (full path required!).
+This is useful in case of hardening the system by separating configuration from program binaries. 
+
 ### Static Configuration location
 
 The `static` directory holds the codebase default configurations files.
index 6e9f7c3184389e9055f5369b4921544c2d947b76..67b2592bd3e841b340724e4cf6a5ddd562c743eb 100644 (file)
@@ -352,7 +352,7 @@ class App
                        $this->profiler->update($this->config);
 
                        Core\Hook::loadHooks();
-                       $loader = new ConfigFileLoader($this->getBasePath());
+                       $loader = new ConfigFileLoader($this->getBasePath(), $_SERVER);
                        Core\Hook::callAll('load_config', $loader);
                }
 
index 1c5785d1fec8ec4ca6c1d9af33003944a2944b21..9bd902b35429d5f5c11f4fbd03f86bc4f39667d3 100644 (file)
@@ -151,7 +151,7 @@ class Summary extends BaseAdmin
                }
 
                // check legacy basepath settings
-               $configLoader = new ConfigFileLoader($a->getBasePath());
+               $configLoader = new ConfigFileLoader($a->getBasePath(), $_SERVER);
                $configCache = new Cache();
                $configLoader->setupCache($configCache);
                $confBasepath = $configCache->get('system', 'basepath');
index 0c416a189f8fe51ce2527f822e23b7f17d71fab4..f2aa9124fc1da2de8613d0927a688f1f290a07a1 100644 (file)
@@ -35,6 +35,13 @@ use Friendica\Core\Config\Cache;
  */
 class ConfigFileLoader
 {
+       /**
+        * The key of the $_SERVER variable to override the config directory
+        *
+        * @var string
+        */
+       const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
+
        /**
         * The Sub directory of the config-files
         *
@@ -83,10 +90,14 @@ class ConfigFileLoader
         */
        private $staticDir;
 
-       public function __construct(string $basePath)
+       public function __construct(string $basePath, array $server)
        {
-               $this->baseDir   = $basePath;
-               $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
+               $this->baseDir = $basePath;
+               if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
+                       $this->configDir = $server[self::CONFIG_DIR_ENV];
+               } else {
+                       $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
+               }
                $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
        }
 
index 2068b6b1ad63d6f12bc0c1e220aeb7eca5972b6d..79b7f13648c7d0722c867868eea6b2feb95d5c38 100644 (file)
@@ -77,6 +77,7 @@ return [
                'shared'          => true,
                'constructParams' => [
                        [Dice::INSTANCE => '$basepath'],
+                       $_SERVER,
                ],
        ],
        Config\Cache::class          => [
index 7040f1fc00c8c9f4c6f84d58ce03673dc0c8945f..0b594814fa2b04ff22298de827318cbd0ac8e99d 100644 (file)
@@ -43,6 +43,7 @@ trait VFSTrait
                        'static' => [],
                        'test' => [],
                        'logs' => [],
+                       'config2' => [],
                ];
 
                // create a virtual directory and copy all needed files and folders to it
index 0d57f7128a9968ece419f547e2293211fd81a285..a89714a8538258b2e33c04ebb25953b20cf0f40a 100644 (file)
@@ -55,7 +55,7 @@ class DatabaseCacheTest extends CacheTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader = new ConfigFileLoader($this->root->url());
+               $loader = new ConfigFileLoader($this->root->url(), []);
                $configCache = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index f7db9d8b46b13a7021ed68d47a076222e8e136e1..9e995c123e616583b390d9f7ec70d118c88e4eb3 100644 (file)
@@ -57,7 +57,7 @@ class DatabaseLockDriverTest extends LockTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader        = new ConfigFileLoader($this->root->url());
+               $loader        = new ConfigFileLoader($this->root->url(), []);
                $configCache   = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index 93fc0b6648faa9459f4f8d8ed81d4c12c96c828c..f68bf027b6bf2b32ff23cdf6b91583009eab9fd6 100644 (file)
@@ -73,7 +73,7 @@ class StorageManagerTest extends DatabaseTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader        = new ConfigFileLoader($this->root->url());
+               $loader        = new ConfigFileLoader($this->root->url(), []);
                $configCache   = $configFactory->createCache($loader);
 
                $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
index 0410ff2dedd8155064d8f414270a9d3fd3f3c643..66a02eced3e445f08004cd352bac0551eac9942f 100644 (file)
@@ -33,7 +33,7 @@ class ProcessTest extends DatabaseTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader        = new ConfigFileLoader($this->root->url());
+               $loader        = new ConfigFileLoader($this->root->url(), []);
                $configCache   = $configFactory->createCache($loader);
 
                $this->dba = new StaticDatabase($configCache, $profiler, $logger);
index d7b810c1f887f579f85de1cc844635bd7db28b20..8fd8c8c42c8ee5678258aceccd51cb150c7c525d 100644 (file)
@@ -55,7 +55,7 @@ class DatabaseStorageTest extends StorageTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader = new ConfigFileLoader($this->root->url());
+               $loader = new ConfigFileLoader($this->root->url(), []);
                $configCache = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index b54ae1ec2d2d353bc35984f91a91b7d24287b64c..c69d94679d2bfbf2f95eadff6a587769aa914d95 100644 (file)
@@ -45,7 +45,7 @@ class ConfigFileLoaderTest extends MockedTest
        {
                $this->delConfigFile('local.config.php');
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -67,7 +67,7 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent('<?php return true;');
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -91,7 +91,7 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -123,7 +123,7 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -154,7 +154,7 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root)
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -203,7 +203,7 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
 
                $conf = $configFileLoader->loadAddonConfig('test');
 
@@ -235,7 +235,7 @@ class ConfigFileLoaderTest extends MockedTest
                                ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.config.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -264,7 +264,7 @@ class ConfigFileLoaderTest extends MockedTest
                         ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.ini.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -293,7 +293,7 @@ class ConfigFileLoaderTest extends MockedTest
                         ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.ini.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url());
+               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -301,4 +301,44 @@ class ConfigFileLoaderTest extends MockedTest
                self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
                self::assertEmpty($configCache->get('system', 'NewKey'));
        }
+
+       /**
+        * Test that using a wrong configuration directory leads to the "normal" config path
+        */
+       public function testWrongEnvDir()
+       {
+               $this->delConfigFile('local.config.php');
+
+               $configFileLoader = new ConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
+               $configCache = new Cache();
+
+               $configFileLoader->setupCache($configCache);
+
+               self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
+       }
+
+       /**
+        * Test that a different location of the configuration directory produces the expected output
+        */
+       public function testRightEnvDir()
+       {
+               $this->delConfigFile('local.config.php');
+
+               $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                                  '..' . DIRECTORY_SEPARATOR .
+                                  '..' . DIRECTORY_SEPARATOR .
+                                  'datasets' . DIRECTORY_SEPARATOR .
+                                  'config' . DIRECTORY_SEPARATOR;
+
+               vfsStream::newFile('B.config.php')
+                                ->at($this->root->getChild('config2'))
+                                ->setContent(file_get_contents($fileDir . 'B.config.php'));
+
+               $configFileLoader = new ConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
+               $configCache = new Cache();
+
+               $configFileLoader->setupCache($configCache);
+
+               self::assertEquals('newValue', $configCache->get('system', 'newKey'));
+       }
 }