Skip to content

Commit 4e40bc1

Browse files
[AWS S3] MC-37463: Support by Magento Maintenance Mode (#6215)
* MC-37479: Support by Magento Content Design * MC-37463: Support by Magento Maintenance Mode
1 parent 9804ad9 commit 4e40bc1

30 files changed

+638
-135
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

+42-3
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,40 @@ public function readDirectory($path): array
195195
*/
196196
public function getRealPathSafety($path)
197197
{
198-
return $this->normalizeAbsolutePath(
199-
$this->normalizeRelativePath($path)
198+
if (strpos($path, '/.') === false) {
199+
return $path;
200+
}
201+
202+
$isAbsolute = strpos($path, $this->normalizeAbsolutePath()) === 0;
203+
$path = $this->normalizeRelativePath($path);
204+
205+
//Removing redundant directory separators.
206+
$path = preg_replace(
207+
'/\\/\\/+/',
208+
'/',
209+
$path
200210
);
211+
$pathParts = explode('/', $path);
212+
if (end($pathParts) === '.') {
213+
$pathParts[count($pathParts) - 1] = '';
214+
}
215+
$realPath = [];
216+
foreach ($pathParts as $pathPart) {
217+
if ($pathPart === '.') {
218+
continue;
219+
}
220+
if ($pathPart === '..') {
221+
array_pop($realPath);
222+
continue;
223+
}
224+
$realPath[] = $pathPart;
225+
}
226+
227+
if ($isAbsolute) {
228+
return $this->normalizeAbsolutePath(implode('/', $realPath));
229+
}
230+
231+
return implode('/', $realPath);
201232
}
202233

203234
/**
@@ -227,6 +258,14 @@ public function getAbsolutePath($basePath, $path, $scheme = null)
227258
private function normalizeAbsolutePath(string $path = '.'): string
228259
{
229260
$path = ltrim($path, '/');
261+
$path = str_replace(
262+
$this->adapter->getClient()->getObjectUrl(
263+
$this->adapter->getBucket(),
264+
$this->adapter->applyPathPrefix('.')
265+
),
266+
'',
267+
$path
268+
);
230269

231270
if (!$path) {
232271
$path = '.';
@@ -317,7 +356,7 @@ public function getRelativePath($basePath, $path = null): string
317356
public function getParentDirectory($path): string
318357
{
319358
//phpcs:ignore Magento2.Functions.DiscouragedFunction
320-
return dirname($this->normalizeAbsolutePath($path));
359+
return rtrim(dirname($this->normalizeAbsolutePath($path)), '/') . '/';
321360
}
322361

323362
/**

app/code/Magento/AwsS3/Driver/AwsS3Factory.php

+7-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Aws\S3\S3Client;
1111
use League\Flysystem\AwsS3v3\AwsS3Adapter;
12-
use Magento\AwsS3\Model\Config;
1312
use Magento\Framework\Filesystem\DriverInterface;
1413
use Magento\Framework\ObjectManagerInterface;
1514
use Magento\RemoteStorage\Driver\DriverFactoryInterface;
@@ -24,52 +23,36 @@ class AwsS3Factory implements DriverFactoryInterface
2423
*/
2524
private $objectManager;
2625

27-
/**
28-
* @var Config
29-
*/
30-
private $config;
31-
3226
/**
3327
* @param ObjectManagerInterface $objectManager
34-
* @param Config $config
3528
*/
36-
public function __construct(ObjectManagerInterface $objectManager, Config $config)
29+
public function __construct(ObjectManagerInterface $objectManager)
3730
{
3831
$this->objectManager = $objectManager;
39-
$this->config = $config;
4032
}
4133

4234
/**
4335
* Creates an instance of AWS S3 driver.
4436
*
37+
* @param array $config
38+
* @param string $prefix
4539
* @return DriverInterface
4640
*/
47-
public function create(): DriverInterface
41+
public function create(array $config, string $prefix): DriverInterface
4842
{
49-
$config = [
50-
'region' => $this->config->getRegion(),
43+
$config += [
5144
'version' => 'latest'
5245
];
5346

54-
$key = $this->config->getAccessKey();
55-
$secret = $this->config->getSecretKey();
56-
57-
if ($key && $secret) {
58-
$config['credentials'] = [
59-
'key' => $key,
60-
'secret' => $secret,
61-
];
62-
}
63-
6447
return $this->objectManager->create(
6548
AwsS3::class,
6649
[
6750
'adapter' => $this->objectManager->create(
6851
AwsS3Adapter::class,
6952
[
7053
'client' => $this->objectManager->create(S3Client::class, ['args' => $config]),
71-
'bucket' => $this->config->getBucket(),
72-
'prefix' => $this->config->getPrefix()
54+
'bucket' => $config['bucket'],
55+
'prefix' => $prefix
7356
]
7457
)
7558
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
9+
<entity name="RemoteStorageAwsS3ConfigData">
10+
<data key="driver">{{_ENV.REMOTE_STORAGE_AWSS3_DRIVER}}</data>
11+
<data key="region">{{_ENV.REMOTE_STORAGE_AWSS3_REGION}}</data>
12+
<data key="prefix">{{_ENV.REMOTE_STORAGE_AWSS3_PREFIX}}</data>
13+
<data key="bucket">{{_ENV.REMOTE_STORAGE_AWSS3_BUCKET}}</data>
14+
<data key="access_key">{{_ENV.REMOTE_STORAGE_AWSS3_ACCESS_KEY}}</data>
15+
<data key="secret_key">{{_ENV.REMOTE_STORAGE_AWSS3_SECRET_KEY}}</data>
16+
</entity>
17+
</entities>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="AwsS3AdminMarketingCreateSitemapEntityTest">
11+
<annotations>
12+
<features value="Sitemap"/>
13+
<stories value="AWS S3 Admin Creates Sitemap Entity"/>
14+
<title value="AWS S3 Sitemap Creation"/>
15+
<description value="Sitemap Entity Creation"/>
16+
<testCaseId value="MC-38319"/>
17+
<severity value="MAJOR"/>
18+
<group value="sitemap"/>
19+
<group value="mtf_migrated"/>
20+
<group value="remote_storage_aws_s3"/>
21+
</annotations>
22+
<before>
23+
<magentoCLI command="remote-storage:enable {{RemoteStorageAwsS3ConfigData.driver}} {{RemoteStorageAwsS3ConfigData.bucket}} {{RemoteStorageAwsS3ConfigData.region}} {{RemoteStorageAwsS3ConfigData.prefix}} {{RemoteStorageAwsS3ConfigData.access_key}} {{RemoteStorageAwsS3ConfigData.secret_key}}" stepKey="enableRemoteStorage"/>
24+
<actionGroup ref="AdminLoginActionGroup" stepKey="LoginAsAdmin"/>
25+
</before>
26+
<after>
27+
<actionGroup ref="AdminMarketingSiteDeleteByNameActionGroup" stepKey="deleteCreatedSitemap">
28+
<argument name="filename" value="sitemap.xml"/>
29+
</actionGroup>
30+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logoutFromAdmin"/>
31+
<magentoCLI command="remote-storage:disable" stepKey="disableRemoteStorage"/>
32+
</after>
33+
34+
<!--TEST BODY -->
35+
<!--Navigate to Marketing->Sitemap Page -->
36+
<actionGroup ref="AdminNavigateMenuActionGroup" stepKey="navigateToMarketingSiteMapPage">
37+
<argument name="menuUiId" value="{{AdminMenuMarketing.dataUiId}}"/>
38+
<argument name="submenuUiId" value="{{AdminMenuSEOAndSearchSiteMap.dataUiId}}"/>
39+
</actionGroup>
40+
<!-- Navigate to New Sitemap Creation Page -->
41+
<actionGroup ref="AdminMarketingNavigateToNewSitemapPageActionGroup" stepKey="navigateToAddNewSitemap"/>
42+
<!-- Create Sitemap Entity -->
43+
<actionGroup ref="AdminMarketingCreateSitemapEntityActionGroup" stepKey="createSitemap">
44+
<argument name="filename" value="sitemap.xml"/>
45+
<argument name="path" value="/"/>
46+
</actionGroup>
47+
<!-- Assert Success Message -->
48+
<actionGroup ref="AssertMessageInAdminPanelActionGroup" stepKey="seeSuccessMessage">
49+
<argument name="message" value="You saved the sitemap."/>
50+
<argument name="messageType" value="success"/>
51+
</actionGroup>
52+
<!-- Find Created Sitemap On Grid -->
53+
<actionGroup ref="AdminMarketingSearchSitemapActionGroup" stepKey="findCreatedSitemapInGrid">
54+
<argument name="name" value="sitemap.xml"/>
55+
</actionGroup>
56+
<actionGroup ref="AssertAdminSitemapInGridActionGroup" stepKey="assertSitemapInGrid">
57+
<argument name="name" value="sitemap.xml"/>
58+
</actionGroup>
59+
<!--END TEST BODY -->
60+
</test>
61+
</tests>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="AwsS3AdminMarketingSiteMapCreateNewTest">
11+
<annotations>
12+
<features value="Sitemap"/>
13+
<stories value="AWS S3 Create Site Map"/>
14+
<title value="AWS S3 Create New Site Map with valid data"/>
15+
<description value="Create New Site Map with valid data"/>
16+
<testCaseId value="MC-38320" />
17+
<severity value="CRITICAL"/>
18+
<group value="sitemap"/>
19+
<group value="remote_storage_aws_s3"/>
20+
</annotations>
21+
<before>
22+
<magentoCLI command="remote-storage:enable {{RemoteStorageAwsS3ConfigData.driver}} {{RemoteStorageAwsS3ConfigData.bucket}} {{RemoteStorageAwsS3ConfigData.region}} {{RemoteStorageAwsS3ConfigData.prefix}} {{RemoteStorageAwsS3ConfigData.access_key}} {{RemoteStorageAwsS3ConfigData.secret_key}}" stepKey="enableRemoteStorage"/>
23+
<actionGroup ref="AdminLoginActionGroup" stepKey="LoginAsAdmin"/>
24+
</before>
25+
<after>
26+
<actionGroup ref="AdminMarketingSiteDeleteByNameActionGroup" stepKey="deleteSiteMap">
27+
<argument name="filename" value="{{DefaultSiteMap.filename}}" />
28+
</actionGroup>
29+
<actionGroup ref="AssertSiteMapDeleteSuccessActionGroup" stepKey="assertDeleteSuccessMessage"/>
30+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
31+
<magentoCLI command="remote-storage:disable" stepKey="disableRemoteStorage"/>
32+
</after>
33+
<actionGroup ref="AdminMarketingSiteMapNavigateNewActionGroup" stepKey="navigateNewSiteMap"/>
34+
<actionGroup ref="AdminMarketingSiteMapFillFormActionGroup" stepKey="fillSiteMapForm">
35+
<argument name="sitemap" value="DefaultSiteMap" />
36+
</actionGroup>
37+
<actionGroup ref="AssertSiteMapCreateSuccessActionGroup" stepKey="seeSuccessMessage"/>
38+
</test>
39+
</tests>

app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php

+43-4
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ protected function setUp(): void
5959
return self::URL . $path;
6060
});
6161

62-
$this->driver = new AwsS3(
63-
$this->adapterMock
64-
);
62+
$this->driver = new AwsS3($this->adapterMock);
6563
}
6664

6765
/**
@@ -116,6 +114,11 @@ public function getAbsolutePathDataProvider(): array
116114
self::URL . 'media/',
117115
'/catalog/test.png',
118116
self::URL . 'media/catalog/test.png'
117+
],
118+
[
119+
'',
120+
self::URL . 'media/catalog/test.png',
121+
self::URL . 'media/catalog/test.png'
119122
]
120123
];
121124
}
@@ -137,7 +140,7 @@ public function testGetRelativePath(string $basePath, string $path, string $expe
137140
*/
138141
public function getRelativePathDataProvider(): array
139142
{
140-
return [
143+
return [
141144
[
142145
'',
143146
'test/test.txt',
@@ -324,4 +327,40 @@ public function isFileDataProvider(): array
324327
]
325328
];
326329
}
330+
331+
/**
332+
* @param string $path
333+
* @param string $expected
334+
*
335+
* @dataProvider getRealPathSafetyDataProvider
336+
*/
337+
public function testGetRealPathSafety(string $path, string $expected): void
338+
{
339+
self::assertSame($expected, $this->driver->getRealPathSafety($path));
340+
}
341+
342+
/**
343+
* @return array
344+
*/
345+
public function getRealPathSafetyDataProvider(): array
346+
{
347+
return [
348+
[
349+
self::URL,
350+
self::URL
351+
],
352+
[
353+
'test.txt',
354+
'test.txt'
355+
],
356+
[
357+
self::URL . 'test/test/../test.txt',
358+
self::URL . 'test/test.txt'
359+
],
360+
[
361+
'test/test/../test.txt',
362+
'test/test.txt'
363+
]
364+
];
365+
}
327366
}

app/code/Magento/AwsS3/etc/di.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<type name="Magento\RemoteStorage\Driver\DriverPool">
9+
<type name="Magento\RemoteStorage\Driver\DriverFactoryPool">
1010
<arguments>
11-
<argument name="remotePool" xsi:type="array">
11+
<argument name="pool" xsi:type="array">
1212
<item name="aws-s3" xsi:type="object">Magento\AwsS3\Driver\AwsS3Factory</item>
1313
</argument>
1414
</arguments>

0 commit comments

Comments
 (0)