|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\EncryptionKey\Setup\Patch\Data; |
| 10 | + |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\App\DeploymentConfig; |
| 13 | +use Magento\Framework\Encryption\Encryptor; |
| 14 | + |
| 15 | +class SodiumChachaPatchTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + const PATH_KEY = 'crypt/key'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ObjectManagerInterface |
| 21 | + */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var DeploymentConfig |
| 26 | + */ |
| 27 | + private $deployConfig; |
| 28 | + |
| 29 | + protected function setUp() |
| 30 | + { |
| 31 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 32 | + $this->deployConfig = $this->objectManager->get(DeploymentConfig::class); |
| 33 | + } |
| 34 | + |
| 35 | + public function testChangeEncryptionKey() |
| 36 | + { |
| 37 | + $testPath = 'test/config'; |
| 38 | + $testValue = 'test'; |
| 39 | + |
| 40 | + $structureMock = $this->createMock(\Magento\Config\Model\Config\Structure\Proxy::class); |
| 41 | + $structureMock->expects($this->once()) |
| 42 | + ->method('getFieldPathsByAttribute') |
| 43 | + ->will($this->returnValue([$testPath])); |
| 44 | + |
| 45 | + /** @var \Magento\Config\Model\ResourceModel\Config $configModel */ |
| 46 | + $configModel = $this->objectManager->create(\Magento\Config\Model\ResourceModel\Config::class); |
| 47 | + $configModel->saveConfig($testPath, $this->legacyEncrypt($testValue), 'default', 0); |
| 48 | + |
| 49 | + /** @var \Magento\EncryptionKey\Setup\Patch\Data\SodiumChachaPatch $patch */ |
| 50 | + $patch = $this->objectManager->create( |
| 51 | + \Magento\EncryptionKey\Setup\Patch\Data\SodiumChachaPatch::class, |
| 52 | + [ |
| 53 | + 'structure' => $structureMock, |
| 54 | + ] |
| 55 | + ); |
| 56 | + $patch->apply(); |
| 57 | + |
| 58 | + $connection = $configModel->getConnection(); |
| 59 | + $values = $connection->fetchPairs( |
| 60 | + $connection->select()->from( |
| 61 | + $configModel->getMainTable(), |
| 62 | + ['config_id', 'value'] |
| 63 | + )->where( |
| 64 | + 'path IN (?)', |
| 65 | + [$testPath] |
| 66 | + )->where( |
| 67 | + 'value NOT LIKE ?', |
| 68 | + '' |
| 69 | + ) |
| 70 | + ); |
| 71 | + |
| 72 | + /** @var \Magento\Framework\Encryption\EncryptorInterface $encyptor */ |
| 73 | + $encyptor = $this->objectManager->get(\Magento\Framework\Encryption\EncryptorInterface::class); |
| 74 | + |
| 75 | + $rawConfigValue = array_pop($values); |
| 76 | + |
| 77 | + $this->assertNotEquals($testValue, $rawConfigValue); |
| 78 | + $this->assertStringStartsWith('0:' . Encryptor::CIPHER_LATEST . ':', $rawConfigValue); |
| 79 | + $this->assertEquals($testValue, $encyptor->decrypt($rawConfigValue)); |
| 80 | + } |
| 81 | + |
| 82 | + private function legacyEncrypt(string $data): string |
| 83 | + { |
| 84 | + // @codingStandardIgnoreStart |
| 85 | + $handle = @mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); |
| 86 | + $initVectorSize = @mcrypt_enc_get_iv_size($handle); |
| 87 | + $initVector = str_repeat("\0", $initVectorSize); |
| 88 | + @mcrypt_generic_init($handle, $this->deployConfig->get(static::PATH_KEY), $initVector); |
| 89 | + |
| 90 | + $encrpted = @mcrypt_generic($handle, $data); |
| 91 | + |
| 92 | + @mcrypt_generic_deinit($handle); |
| 93 | + @mcrypt_module_close($handle); |
| 94 | + // @codingStandardIgnoreEnd |
| 95 | + |
| 96 | + return '0:' . Encryptor::CIPHER_RIJNDAEL_256 . ':' . base64_encode($encrpted); |
| 97 | + } |
| 98 | +} |
0 commit comments