From 75905b02b75a5a88a2629249f01445c7adcab384 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 5 Jan 2024 15:04:00 +0100 Subject: [PATCH] [FEATURE] Allow storage configuration override in config/system/additional.php Record-based storage configuration can be overridden by defining $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['aus_driver_amazon_s3']['storage'] or $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['aus_driver_amazon_s3']['storage_X'] (where X is the UID of the storage record) in config/system/additional.php. This makes it possible to use environment variable based storage configuration, and no secret keys need to be stored in the database anymore. Storage configuration in the database record is merged with the generic 'storage' configuration, which then with the uid-specific storage config. Example for defining the credentials in config/system/additional.php: $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['aus_driver_amazon_s3']['storage_23'] = [ 'key' => $_ENV['S3_KEY'], 'secretKey' => $_ENV['S3_SECRET'], ]; --- Classes/Driver/AmazonS3Driver.php | 44 ++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/Classes/Driver/AmazonS3Driver.php b/Classes/Driver/AmazonS3Driver.php index dd64c757..0fa35790 100644 --- a/Classes/Driver/AmazonS3Driver.php +++ b/Classes/Driver/AmazonS3Driver.php @@ -226,7 +226,8 @@ public function processConfiguration() */ public function initialize() { - $this->initializeBaseUrl() + $this->loadSettings() + ->initializeBaseUrl() ->initializeBaseFolder() ->initializeSettings() ->initializeClient(); @@ -1060,6 +1061,32 @@ public function streamFile(string $identifier, array $properties): ResponseInter ******************** Protected Helpers ********************** *************************************************************/ + /** + * Load extension configuration and load additional storage configuration + * + * Record-based storage configuration can be overridden by defining + * $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['aus_driver_amazon_s3']['storage'] + * or + * $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['aus_driver_amazon_s3']['storage_X'] + * (where X is the UID of the storage record) + * in config/system/additional.php. + * This makes it possible to use environment variable based storage configuration. + */ + protected function loadSettings(): self + { + self::$settings = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::EXTENSION_KEY); + + //allow overriding storage configuration from AdditionalConfiguration.php + if (isset(self::$settings['storage'])) { + $this->configuration = array_merge($this->configuration, self::$settings['storage']); + } + if (isset(self::$settings['storage_' . $this->storageUid])) { + $this->configuration = array_merge($this->configuration, self::$settings['storage_' . $this->storageUid]); + } + + return $this; + } + /** * initializeBaseUrl * @@ -1113,16 +1140,13 @@ protected function initializeBaseFolder(): self */ protected function initializeSettings() { - if (self::$settings === null) { - self::$settings = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::EXTENSION_KEY); - - if (!isset(self::$settings['doNotLoadAmazonLib']) || !self::$settings['doNotLoadAmazonLib']) { - self::loadExternalClasses(); - } - if ($this->compatibilityService->isFrontend() && (!isset(self::$settings['dnsPrefetch']) || self::$settings['dnsPrefetch'])) { - $GLOBALS['TSFE']->additionalHeaderData['ausDriverAmazonS3_dnsPrefetch'] = ''; - } + if (!isset(self::$settings['doNotLoadAmazonLib']) || !self::$settings['doNotLoadAmazonLib']) { + self::loadExternalClasses(); } + if ($this->compatibilityService->isFrontend() && (!isset(self::$settings['dnsPrefetch']) || self::$settings['dnsPrefetch'])) { + $GLOBALS['TSFE']->additionalHeaderData['ausDriverAmazonS3_dnsPrefetch'] = ''; + } + return $this; }