Merge pull request #10693 from nupplaphil/feat/config_dir

Add the possibility to use a different configuration directory
This commit is contained in:
Hypolite Petovan 2021-09-13 07:02:52 -04:00 committed by GitHub
commit d78661817e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 157 additions and 42 deletions

View 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. 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 ### Static Configuration location
The `static` directory holds the codebase default configurations files. The `static` directory holds the codebase default configurations files.

View File

@ -25,6 +25,7 @@ use Exception;
use Friendica\App\Arguments; use Friendica\App\Arguments;
use Friendica\App\BaseURL; use Friendica\App\BaseURL;
use Friendica\App\Module; use Friendica\App\Module;
use Friendica\Factory\ConfigFactory;
use Friendica\Module\Maintenance; use Friendica\Module\Maintenance;
use Friendica\Security\Authentication; use Friendica\Security\Authentication;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
@ -352,7 +353,7 @@ class App
$this->profiler->update($this->config); $this->profiler->update($this->config);
Core\Hook::loadHooks(); Core\Hook::loadHooks();
$loader = new ConfigFileLoader($this->getBasePath()); $loader = (new ConfigFactory())->createConfigFileLoader($this->getBasePath(), $_SERVER);
Core\Hook::callAll('load_config', $loader); Core\Hook::callAll('load_config', $loader);
} }

View File

@ -30,6 +30,45 @@ use Friendica\Util\ConfigFileLoader;
class ConfigFactory class ConfigFactory
{ {
/**
* 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
*
* @var string
*/
const CONFIG_DIR = 'config';
/**
* The Sub directory of the static config-files
*
* @var string
*/
const STATIC_DIR = 'static';
/**
* @param string $basePath The basepath of FRIENDICA
* @param array $serer the $_SERVER array
*
* @return ConfigFileLoader
*/
public function createConfigFileLoader(string $basePath, array $server = [])
{
if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
$configDir = $server[self::CONFIG_DIR_ENV];
} else {
$configDir = $basePath . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
}
$staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
return new ConfigFileLoader($basePath, $configDir, $staticDir);
}
/** /**
* @param ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig) * @param ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
* *

View File

@ -29,6 +29,7 @@ use Friendica\Core\Update;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
use Friendica\DI; use Friendica\DI;
use Friendica\Factory\ConfigFactory;
use Friendica\Model\Register; use Friendica\Model\Register;
use Friendica\Module\BaseAdmin; use Friendica\Module\BaseAdmin;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
@ -151,7 +152,7 @@ class Summary extends BaseAdmin
} }
// check legacy basepath settings // check legacy basepath settings
$configLoader = new ConfigFileLoader($a->getBasePath()); $configLoader = (new ConfigFactory())->createConfigFileLoader($a->getBasePath(), $_SERVER);
$configCache = new Cache(); $configCache = new Cache();
$configLoader->setupCache($configCache); $configLoader->setupCache($configCache);
$confBasepath = $configCache->get('system', 'basepath'); $confBasepath = $configCache->get('system', 'basepath');

View File

@ -35,20 +35,6 @@ use Friendica\Core\Config\Cache;
*/ */
class ConfigFileLoader class ConfigFileLoader
{ {
/**
* The Sub directory of the config-files
*
* @var string
*/
const CONFIG_DIR = 'config';
/**
* The Sub directory of the static config-files
*
* @var string
*/
const STATIC_DIR = 'static';
/** /**
* The default name of the user defined ini file * The default name of the user defined ini file
* *
@ -83,11 +69,16 @@ class ConfigFileLoader
*/ */
private $staticDir; private $staticDir;
public function __construct(string $basePath) /**
* @param string $baseDir The base
* @param string $configDir
* @param string $staticDir
*/
public function __construct(string $baseDir, string $configDir, string $staticDir)
{ {
$this->baseDir = $basePath; $this->baseDir = $baseDir;
$this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR; $this->configDir = $configDir;
$this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR; $this->staticDir = $staticDir;
} }
/** /**
@ -102,7 +93,7 @@ class ConfigFileLoader
* *
* @throws Exception * @throws Exception
*/ */
public function setupCache(Cache $config, array $server = [], $raw = false) public function setupCache(Cache $config, array $server = [], bool $raw = false)
{ {
// Load static config files first, the order is important // Load static config files first, the order is important
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE); $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
@ -185,7 +176,7 @@ class ConfigFileLoader
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/ $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/ Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
$name . DIRECTORY_SEPARATOR . // openstreetmap/ $name . DIRECTORY_SEPARATOR . // openstreetmap/
self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/ 'config'. DIRECTORY_SEPARATOR . // config/
$name . ".config.php"; // openstreetmap.config.php $name . ".config.php"; // openstreetmap.config.php
if (file_exists($filepath)) { if (file_exists($filepath)) {
@ -206,8 +197,7 @@ class ConfigFileLoader
*/ */
public function loadEnvConfig(array $server) public function loadEnvConfig(array $server)
{ {
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/ $filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
self::STATIC_DIR . DIRECTORY_SEPARATOR . // static/
"env.config.php"; // env.config.php "env.config.php"; // env.config.php
if (!file_exists($filepath)) { if (!file_exists($filepath)) {

View File

@ -74,9 +74,12 @@ return [
] ]
], ],
Util\ConfigFileLoader::class => [ Util\ConfigFileLoader::class => [
'shared' => true, 'instanceOf' => Factory\ConfigFactory::class,
'constructParams' => [ 'call' => [
['createConfigFileLoader', [
[Dice::INSTANCE => '$basepath'], [Dice::INSTANCE => '$basepath'],
$_SERVER,
], Dice::CHAIN_CALL],
], ],
], ],
Config\Cache::class => [ Config\Cache::class => [

View File

@ -43,6 +43,7 @@ trait VFSTrait
'static' => [], 'static' => [],
'test' => [], 'test' => [],
'logs' => [], 'logs' => [],
'config2' => [],
]; ];
// create a virtual directory and copy all needed files and folders to it // create a virtual directory and copy all needed files and folders to it

View 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 // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory(); $configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url()); $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader); $configCache = $configFactory->createCache($loader);
$dba = new StaticDatabase($configCache, $profiler, $logger); $dba = new StaticDatabase($configCache, $profiler, $logger);

View 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 // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory(); $configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url()); $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader); $configCache = $configFactory->createCache($loader);
$dba = new StaticDatabase($configCache, $profiler, $logger); $dba = new StaticDatabase($configCache, $profiler, $logger);

View 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 // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory(); $configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url()); $loader = $configFactory->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader); $configCache = $configFactory->createCache($loader);
$this->dba = new StaticDatabase($configCache, $profiler, $this->logger); $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);

View 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 // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory(); $configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url()); $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader); $configCache = $configFactory->createCache($loader);
$this->dba = new StaticDatabase($configCache, $profiler, $logger); $this->dba = new StaticDatabase($configCache, $profiler, $logger);

View 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 // load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory(); $configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url()); $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader); $configCache = $configFactory->createCache($loader);
$dba = new StaticDatabase($configCache, $profiler, $logger); $dba = new StaticDatabase($configCache, $profiler, $logger);

View File

@ -22,6 +22,7 @@
namespace Friendica\Test\src\Util\Config; namespace Friendica\Test\src\Util\Config;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
use Friendica\Factory\ConfigFactory;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait; use Friendica\Test\Util\VFSTrait;
use Friendica\Util\ConfigFileLoader; use Friendica\Util\ConfigFileLoader;
@ -45,7 +46,11 @@ class ConfigFileLoaderTest extends MockedTest
{ {
$this->delConfigFile('local.config.php'); $this->delConfigFile('local.config.php');
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -67,7 +72,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent('<?php return true;'); ->setContent('<?php return true;');
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -91,7 +100,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent(file_get_contents($file)); ->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -123,7 +136,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent(file_get_contents($file)); ->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -154,7 +171,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root) ->at($this->root)
->setContent(file_get_contents($file)); ->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -203,7 +224,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('addon')->getChild('test')->getChild('config')) ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
->setContent(file_get_contents($file)); ->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$conf = $configFileLoader->loadAddonConfig('test'); $conf = $configFileLoader->loadAddonConfig('test');
@ -235,7 +260,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.config.php')); ->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -264,7 +293,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php')); ->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -293,7 +326,11 @@ class ConfigFileLoaderTest extends MockedTest
->at($this->root->getChild('config')) ->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php')); ->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader($this->root->url()); $configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache(); $configCache = new Cache();
$configFileLoader->setupCache($configCache); $configFileLoader->setupCache($configCache);
@ -301,4 +338,44 @@ class ConfigFileLoaderTest extends MockedTest
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email')); self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEmpty($configCache->get('system', 'NewKey')); 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 ConfigFactory())->createConfigFileLoader($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 ConfigFactory())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
} }