| 
 | 1 | +diff --git a/vendor/magento/module-aws-s3/Driver/AwsS3Factory.php b/vendor/magento/module-aws-s3/Driver/AwsS3Factory.php  | 
 | 2 | +index 66c95e97ace0..b1cec93ed8f7 100644  | 
 | 3 | +--- a/vendor/magento/module-aws-s3/Driver/AwsS3Factory.php  | 
 | 4 | ++++ b/vendor/magento/module-aws-s3/Driver/AwsS3Factory.php  | 
 | 5 | +@@ -54,6 +54,11 @@ class AwsS3Factory implements DriverFactoryInterface  | 
 | 6 | +      */  | 
 | 7 | +     private $cachePrefix;  | 
 | 8 | +   | 
 | 9 | ++    /**  | 
 | 10 | ++     * @var CachedCredentialsProvider  | 
 | 11 | ++     */  | 
 | 12 | ++    private $cachedCredentialsProvider;  | 
 | 13 | ++  | 
 | 14 | +     /**  | 
 | 15 | +      * @param ObjectManagerInterface $objectManager  | 
 | 16 | +      * @param Config $config  | 
 | 17 | +@@ -61,6 +66,7 @@ class AwsS3Factory implements DriverFactoryInterface  | 
 | 18 | +      * @param CacheInterfaceFactory $cacheInterfaceFactory  | 
 | 19 | +      * @param CachedAdapterInterfaceFactory $cachedAdapterInterfaceFactory  | 
 | 20 | +      * @param string|null $cachePrefix  | 
 | 21 | ++     * @param CachedCredentialsProvider|null $cachedCredentialsProvider  | 
 | 22 | +      */  | 
 | 23 | +     public function __construct(  | 
 | 24 | +         ObjectManagerInterface $objectManager,  | 
 | 25 | +@@ -68,7 +74,8 @@ public function __construct(  | 
 | 26 | +         MetadataProviderInterfaceFactory $metadataProviderFactory,  | 
 | 27 | +         CacheInterfaceFactory $cacheInterfaceFactory,  | 
 | 28 | +         CachedAdapterInterfaceFactory $cachedAdapterInterfaceFactory,  | 
 | 29 | +-        string $cachePrefix = null  | 
 | 30 | ++        string $cachePrefix = null,  | 
 | 31 | ++        ?CachedCredentialsProvider $cachedCredentialsProvider = null,  | 
 | 32 | +     ) {  | 
 | 33 | +         $this->objectManager = $objectManager;  | 
 | 34 | +         $this->config = $config;  | 
 | 35 | +@@ -76,6 +83,8 @@ public function __construct(  | 
 | 36 | +         $this->cacheInterfaceFactory = $cacheInterfaceFactory;  | 
 | 37 | +         $this->cachedAdapterInterfaceFactory = $cachedAdapterInterfaceFactory;  | 
 | 38 | +         $this->cachePrefix = $cachePrefix;  | 
 | 39 | ++        $this->cachedCredentialsProvider = $cachedCredentialsProvider ??  | 
 | 40 | ++            $this->objectManager->get(CachedCredentialsProvider::class);  | 
 | 41 | +     }  | 
 | 42 | +   | 
 | 43 | +     /**  | 
 | 44 | +@@ -94,18 +103,19 @@ public function create(): RemoteDriverInterface  | 
 | 45 | +     }  | 
 | 46 | +   | 
 | 47 | +     /**  | 
 | 48 | +-     * @inheritDoc  | 
 | 49 | ++     * Prepare config for S3Client  | 
 | 50 | ++     *  | 
 | 51 | ++     * @param array $config  | 
 | 52 | ++     * @return array  | 
 | 53 | ++     * @throws DriverException  | 
 | 54 | +      */  | 
 | 55 | +-    public function createConfigured(  | 
 | 56 | +-        array $config,  | 
 | 57 | +-        string $prefix,  | 
 | 58 | +-        string $cacheAdapter = '',  | 
 | 59 | +-        array $cacheConfig = []  | 
 | 60 | +-    ): RemoteDriverInterface {  | 
 | 61 | ++    private function prepareConfig(array $config)  | 
 | 62 | ++    {  | 
 | 63 | +         $config['version'] = 'latest';  | 
 | 64 | +   | 
 | 65 | +         if (empty($config['credentials']['key']) || empty($config['credentials']['secret'])) {  | 
 | 66 | +-            unset($config['credentials']);  | 
 | 67 | ++            //Access keys were not provided; request token from AWS config (local or EC2) and cache result  | 
 | 68 | ++            $config['credentials'] = $this->cachedCredentialsProvider->get();  | 
 | 69 | +         }  | 
 | 70 | +   | 
 | 71 | +         if (empty($config['bucket']) || empty($config['region'])) {  | 
 | 72 | +@@ -120,6 +130,19 @@ public function createConfigured(  | 
 | 73 | +             $config['use_path_style_endpoint'] = boolval($config['path_style']);  | 
 | 74 | +         }  | 
 | 75 | +   | 
 | 76 | ++        return $config;  | 
 | 77 | ++    }  | 
 | 78 | ++  | 
 | 79 | ++    /**  | 
 | 80 | ++     * @inheritDoc  | 
 | 81 | ++     */  | 
 | 82 | ++    public function createConfigured(  | 
 | 83 | ++        array $config,  | 
 | 84 | ++        string $prefix,  | 
 | 85 | ++        string $cacheAdapter = '',  | 
 | 86 | ++        array $cacheConfig = []  | 
 | 87 | ++    ): RemoteDriverInterface {  | 
 | 88 | ++        $config = $this->prepareConfig($config);  | 
 | 89 | +         $client = new S3Client($config);  | 
 | 90 | +         $adapter = new AwsS3V3Adapter($client, $config['bucket'], $prefix);  | 
 | 91 | +         $cache = $this->cacheInterfaceFactory->create(  | 
 | 92 | +diff --git a/vendor/magento/module-aws-s3/Driver/CachedCredentialsProvider.php b/vendor/magento/module-aws-s3/Driver/CachedCredentialsProvider.php  | 
 | 93 | +new file mode 100644  | 
 | 94 | +index 000000000000..0137284358bd  | 
 | 95 | +--- /dev/null  | 
 | 96 | ++++ b/vendor/magento/module-aws-s3/Driver/CachedCredentialsProvider.php  | 
 | 97 | +@@ -0,0 +1,42 @@  | 
 | 98 | ++<?php  | 
 | 99 | ++/**  | 
 | 100 | ++ * Copyright © Magento, Inc. All rights reserved.  | 
 | 101 | ++ * See COPYING.txt for license details.  | 
 | 102 | ++ */  | 
 | 103 | ++declare(strict_types=1);  | 
 | 104 | ++  | 
 | 105 | ++namespace Magento\AwsS3\Driver;  | 
 | 106 | ++  | 
 | 107 | ++use Aws\Credentials\CredentialProvider;  | 
 | 108 | ++  | 
 | 109 | ++class CachedCredentialsProvider  | 
 | 110 | ++{  | 
 | 111 | ++    /**  | 
 | 112 | ++     * @var CredentialsCache  | 
 | 113 | ++     */  | 
 | 114 | ++    private $magentoCacheAdapter;  | 
 | 115 | ++  | 
 | 116 | ++    /**  | 
 | 117 | ++     * @param CredentialsCache $magentoCacheAdapter  | 
 | 118 | ++     */  | 
 | 119 | ++    public function __construct(CredentialsCache $magentoCacheAdapter)  | 
 | 120 | ++    {  | 
 | 121 | ++        $this->magentoCacheAdapter = $magentoCacheAdapter;  | 
 | 122 | ++    }  | 
 | 123 | ++  | 
 | 124 | ++    /**  | 
 | 125 | ++     * Provides cache mechanism to retrieve and store AWS credentials  | 
 | 126 | ++     *  | 
 | 127 | ++     * @return callable  | 
 | 128 | ++     */  | 
 | 129 | ++    public function get()  | 
 | 130 | ++    {  | 
 | 131 | ++        //phpcs:ignore Magento2.Functions.DiscouragedFunction  | 
 | 132 | ++        return call_user_func(  | 
 | 133 | ++            [CredentialProvider::class, 'cache'],  | 
 | 134 | ++            //phpcs:ignore Magento2.Functions.DiscouragedFunction  | 
 | 135 | ++            call_user_func([CredentialProvider::class, 'defaultProvider']),  | 
 | 136 | ++            $this->magentoCacheAdapter  | 
 | 137 | ++        );  | 
 | 138 | ++    }  | 
 | 139 | ++}  | 
 | 140 | +diff --git a/vendor/magento/module-aws-s3/Driver/CredentialsCache.php b/vendor/magento/module-aws-s3/Driver/CredentialsCache.php  | 
 | 141 | +new file mode 100644  | 
 | 142 | +index 000000000000..337e9d2a2acf  | 
 | 143 | +--- /dev/null  | 
 | 144 | ++++ b/vendor/magento/module-aws-s3/Driver/CredentialsCache.php  | 
 | 145 | +@@ -0,0 +1,82 @@  | 
 | 146 | ++<?php  | 
 | 147 | ++/**  | 
 | 148 | ++ * Copyright © Magento, Inc. All rights reserved.  | 
 | 149 | ++ * See COPYING.txt for license details.  | 
 | 150 | ++ */  | 
 | 151 | ++declare(strict_types=1);  | 
 | 152 | ++  | 
 | 153 | ++namespace Magento\AwsS3\Driver;  | 
 | 154 | ++  | 
 | 155 | ++use Aws\CacheInterface;  | 
 | 156 | ++use Aws\Credentials\CredentialsFactory;  | 
 | 157 | ++use Magento\Framework\App\CacheInterface as MagentoCacheInterface;  | 
 | 158 | ++use Magento\Framework\Serialize\Serializer\Json;  | 
 | 159 | ++  | 
 | 160 | ++/** Cache Adapter for AWS credentials */  | 
 | 161 | ++class CredentialsCache implements CacheInterface  | 
 | 162 | ++{  | 
 | 163 | ++    /**  | 
 | 164 | ++     * @var MagentoCacheInterface  | 
 | 165 | ++     */  | 
 | 166 | ++    private $magentoCache;  | 
 | 167 | ++  | 
 | 168 | ++    /**  | 
 | 169 | ++     * @var Json  | 
 | 170 | ++     */  | 
 | 171 | ++    private $json;  | 
 | 172 | ++  | 
 | 173 | ++    /**  | 
 | 174 | ++     * @var CredentialsFactory  | 
 | 175 | ++     */  | 
 | 176 | ++    private $credentialsFactory;  | 
 | 177 | ++  | 
 | 178 | ++    /**  | 
 | 179 | ++     * @param MagentoCacheInterface $magentoCache  | 
 | 180 | ++     * @param CredentialsFactory $credentialsFactory  | 
 | 181 | ++     * @param Json $json  | 
 | 182 | ++     */  | 
 | 183 | ++    public function __construct(MagentoCacheInterface $magentoCache, CredentialsFactory $credentialsFactory, Json $json)  | 
 | 184 | ++    {  | 
 | 185 | ++        $this->magentoCache = $magentoCache;  | 
 | 186 | ++        $this->credentialsFactory = $credentialsFactory;  | 
 | 187 | ++        $this->json = $json;  | 
 | 188 | ++    }  | 
 | 189 | ++  | 
 | 190 | ++    /**  | 
 | 191 | ++     * @inheritdoc  | 
 | 192 | ++     */  | 
 | 193 | ++    public function get($key)  | 
 | 194 | ++    {  | 
 | 195 | ++        $value = $this->magentoCache->load($key);  | 
 | 196 | ++  | 
 | 197 | ++        if (!is_string($value)) {  | 
 | 198 | ++            return null;  | 
 | 199 | ++        }  | 
 | 200 | ++  | 
 | 201 | ++        $result = $this->json->unserialize($value);  | 
 | 202 | ++        try {  | 
 | 203 | ++            return $this->credentialsFactory->create($result);  | 
 | 204 | ++        } catch (\Exception $e) {  | 
 | 205 | ++            return $result;  | 
 | 206 | ++        }  | 
 | 207 | ++    }  | 
 | 208 | ++  | 
 | 209 | ++    /**  | 
 | 210 | ++     * @inheritdoc  | 
 | 211 | ++     */  | 
 | 212 | ++    public function set($key, $value, $ttl = 0)  | 
 | 213 | ++    {  | 
 | 214 | ++        if (method_exists($value, 'toArray')) {  | 
 | 215 | ++            $value = $value->toArray();  | 
 | 216 | ++        }  | 
 | 217 | ++        $this->magentoCache->save($this->json->serialize($value), $key, [], $ttl);  | 
 | 218 | ++    }  | 
 | 219 | ++  | 
 | 220 | ++    /**  | 
 | 221 | ++     * @inheritdoc  | 
 | 222 | ++     */  | 
 | 223 | ++    public function remove($key)  | 
 | 224 | ++    {  | 
 | 225 | ++        $this->magentoCache->remove($key);  | 
 | 226 | ++    }  | 
 | 227 | ++}  | 
0 commit comments