Skip to content

Commit 2c17f5b

Browse files
authored
MCLOUD-6485: Add validator for braintree variable existence (magento#759)
1 parent 077d045 commit 2c17f5b

File tree

17 files changed

+555
-20
lines changed

17 files changed

+555
-20
lines changed

config/schema.error.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@
135135
suggestion: 'If you installed the Magento application version directly from the Magento git repository, verify that the `DEPLOYED_MAGENTO_VERSION_FROM_GIT` environment variable is configured.'
136136
stage: build
137137
type: critical
138+
!php/const Magento\MagentoCloud\App\Error::BUILD_WRONG_BRAINTREE_VARIABLE:
139+
step: validate-config
140+
title: 'Remove Magento Braintree module configuration which is no longer supported in Magento 2.4 and later versions.'
141+
suggestion: 'Support for the Braintree module is no longer included with Magento 2.4.0 and later. Remove the CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL variable from the variables section of the .magento.app.yaml file. For Braintree payment support, use an official extension from the Magento Marketplace instead.'
142+
stage: build
143+
type: critical
138144
!php/const Magento\MagentoCloud\App\Error::DEPLOY_WRONG_CACHE_CONFIGURATION:
139145
step: 'pre-deploy: cache'
140146
title: 'Incorrect cache configuration (missing port or host)'
@@ -319,6 +325,12 @@
319325
suggestion: 'Check that credentials for elasticsearch are correct and service is running'
320326
stage: deploy
321327
type: critical
328+
!php/const Magento\MagentoCloud\App\Error::DEPLOY_WRONG_BRAINTREE_VARIABLE:
329+
step: validate-config
330+
title: 'Remove Magento Braintree module configuration which is no longer supported in Magento 2.4 and later versions.'
331+
suggestion: 'Support for the Braintree module is no longer included with Magento 2.4.0 and later. Remove the CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL variable from the variables section of the .magento.app.yaml file. For Braintree support, use an official Braintree Payments extension from the Magento Marketplace instead.'
332+
stage: deploy
333+
type: critical
322334
!php/const Magento\MagentoCloud\App\Error::PD_DEPLOY_IS_FAILED:
323335
step: is-deploy-failed
324336
title: 'Deploy stage failed'

dist/error-codes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
| 23 | | Unable to create a logger object | |
3333
| 24 | backup-data: static-content | Failed to clean the `./init/pub/static/` directory | Failed to clean `./init/pub/static` folder. Check your filesystem permissions. |
3434
| 25 | | Cannot find the Composer package | If you installed the Magento application version directly from the Magento git repository, verify that the `DEPLOYED_MAGENTO_VERSION_FROM_GIT` environment variable is configured. |
35+
| 26 | validate-config | Remove Magento Braintree module configuration which is no longer supported in Magento 2.4 and later versions. | Support for the Braintree module is no longer included with Magento 2.4.0 and later. Remove the CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL variable from the variables section of the .magento.app.yaml file. For Braintree payment support, use an official extension from the Magento Marketplace instead. |
3536

3637
### Deploy stage
3738

@@ -70,6 +71,7 @@
7071
| 130 | install-update: cache_type | Command failed: `php ./bin/magento cache:enable` | Command `php ./bin/magento cache:enable` runs only when Magento was installed but `./app/etc/env.php` file was absent or empty at the beginning of the deployment. Check the `cloud.log` for more information. Add `VERBOSE_COMMANDS: '-vvv'` into `.magento.env.yaml` for more detailed command output. |
7172
| 131 | install-update | The `crypt/key` key value does not exist in the `./app/etc/env.php` file or the `CRYPT_KEY` cloud environment variable | This error occurs if the `./app/etc/env.php` file is not present when Magento deployment begins, or if the `crypt/key` value is undefined. If you migrated the database from another environment, retrieve the crypt key value from that environment. Then, add the value to the [CRYPT_KEY](https://devdocs.magento.com/cloud/env/variables-deploy.html#crypt_key) cloud environment variable in your current environment. See [Add the Magento encryption key](https://devdocs.magento.com/cloud/setup/first-time-setup-import-import.html#encryption-key). If you accidentally removed the `./app/etc/env.php` file, use the following command to restore it from the backup files created from a previous deployment: `./vendor/bin/ece-tools backup:restore` CLI command ." |
7273
| 132 | | Can not connect to the Elasticsearch service | Check that credentials for elasticsearch are correct and service is running |
74+
| 133 | validate-config | Remove Magento Braintree module configuration which is no longer supported in Magento 2.4 and later versions. | Support for the Braintree module is no longer included with Magento 2.4.0 and later. Remove the CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL variable from the variables section of the .magento.app.yaml file. For Braintree support, use an official Braintree Payments extension from the Magento Marketplace instead. |
7375

7476
### Post-deploy stage
7577

scenario/build/generate.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<item name="critical" xsi:type="array">
1111
<item name="composer" xsi:type="object" priority="100">Magento\MagentoCloud\Config\Validator\Build\ComposerFile</item>
1212
<item name="stage-config" xsi:type="object" priority="200">Magento\MagentoCloud\Config\Validator\Build\StageConfig</item>
13+
<item name="magento-app-yaml" xsi:type="object" priority="300">Magento\MagentoCloud\Config\Validator\Build\MagentoAppYaml</item>
1314
</item>
1415
<item name="warning" xsi:type="array">
1516
<item name="config-file-exists" xsi:type="object" priority="100">Magento\MagentoCloud\Config\Validator\Build\ConfigFileExists</item>

scenario/deploy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
2525
<argument name="validators" xsi:type="array">
2626
<item name="critical" xsi:type="array">
27+
<item name="environment-variables" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\EnvironmentVariables</item>
2728
<item name="database-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\DatabaseConfiguration</item>
2829
<item name="search-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\SearchConfiguration</item>
2930
<item name="resource-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\ResourceConfiguration</item>

src/App/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Error
3636
public const BUILD_UNABLE_TO_CREATE_LOGGER = 23;
3737
public const BUILD_CLEAN_INIT_PUB_STATIC_FAILED = 24;
3838
public const BUILD_COMPOSER_PACKAGE_NOT_FOUND = 25;
39+
public const BUILD_WRONG_BRAINTREE_VARIABLE = 26;
3940

4041
public const DEPLOY_WRONG_CACHE_CONFIGURATION = 101;
4142
public const DEPLOY_ENV_PHP_IS_NOT_WRITABLE = 102;
@@ -69,6 +70,7 @@ class Error
6970
public const DEPLOY_CACHE_ENABLE_FAILED = 130;
7071
public const DEPLOY_CRYPT_KEY_IS_ABSENT = 131;
7172
public const DEPLOY_ES_CANNOT_CONNECT = 132;
73+
public const DEPLOY_WRONG_BRAINTREE_VARIABLE = 133;
7274

7375
public const PD_DEPLOY_IS_FAILED = 201;
7476
public const PD_ENV_PHP_IS_NOT_WRITABLE = 202;

src/App/ErrorInfo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\MagentoCloud\Filesystem\Driver\File;
1111
use Magento\MagentoCloud\Filesystem\FileList;
12+
use Magento\MagentoCloud\Filesystem\FileSystemException;
1213
use Symfony\Component\Yaml\Yaml;
1314

1415
/**
@@ -46,7 +47,7 @@ public function __construct(File $file, FileList $fileList)
4647
*
4748
* @param int $errorCode
4849
* @return array
49-
* @throws \Magento\MagentoCloud\Filesystem\FileSystemException
50+
* @throws FileSystemException
5051
*/
5152
public function get(int $errorCode): array
5253
{
@@ -58,7 +59,7 @@ public function get(int $errorCode): array
5859
/**
5960
* Fetches all errors from schema.error.yaml file and caches them
6061
*
61-
* @throws \Magento\MagentoCloud\Filesystem\FileSystemException
62+
* @throws FileSystemException
6263
*/
6364
private function loadErrors(): void
6465
{

src/App/Logger/LineFormatterFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77

88
namespace Magento\MagentoCloud\App\Logger;
99

10+
use Magento\MagentoCloud\App\ContainerInterface;
1011
use Magento\MagentoCloud\App\Logger\Formatter\LineFormatter;
1112

1213
/**
1314
* The factory for LineFormatter.
1415
*/
1516
class LineFormatterFactory
1617
{
18+
/**
19+
* @var ContainerInterface
20+
*/
21+
private $container;
22+
23+
/**
24+
* @param ContainerInterface $container
25+
*/
26+
public function __construct(ContainerInterface $container)
27+
{
28+
$this->container = $container;
29+
}
30+
1731
/**
1832
* @return LineFormatter
1933
*/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Config\Validator\Build;
9+
10+
use Magento\MagentoCloud\App\Error as AppError;
11+
use Magento\MagentoCloud\Config\Environment;
12+
use Magento\MagentoCloud\Config\Validator;
13+
use Magento\MagentoCloud\Config\ValidatorException;
14+
use Magento\MagentoCloud\Config\ValidatorInterface;
15+
use Magento\MagentoCloud\Package\MagentoVersion;
16+
17+
/**
18+
* Validates that .magento.app.yaml contains correct configuration:
19+
* - CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL must be absent in .magento.app.yaml env variables
20+
* for Magento >= 2.4.0
21+
*
22+
*/
23+
class MagentoAppYaml implements ValidatorInterface
24+
{
25+
/**
26+
* @var Environment
27+
*/
28+
private $environment;
29+
30+
/**
31+
* @var MagentoVersion
32+
*/
33+
private $magentoVersion;
34+
35+
/**
36+
* @var Validator\ResultFactory
37+
*/
38+
private $resultFactory;
39+
40+
/**
41+
*
42+
* @param Environment $environment
43+
* @param MagentoVersion $magentoVersion
44+
* @param Validator\ResultFactory $resultFactory
45+
*/
46+
public function __construct(
47+
Environment $environment,
48+
MagentoVersion $magentoVersion,
49+
Validator\ResultFactory $resultFactory
50+
) {
51+
$this->environment = $environment;
52+
$this->magentoVersion = $magentoVersion;
53+
$this->resultFactory = $resultFactory;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function validate(): Validator\ResultInterface
60+
{
61+
try {
62+
if ($this->magentoVersion->satisfies('>= 2.4.0')) {
63+
$appData = $this->environment->getApplication();
64+
if (isset($appData['variables']['env']['CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL'])) {
65+
return $this->resultFactory->errorByCode(AppError::BUILD_WRONG_BRAINTREE_VARIABLE);
66+
}
67+
}
68+
69+
return $this->resultFactory->success();
70+
} catch (\Exception $e) {
71+
throw new ValidatorException($e->getMessage(), $e->getCode(), $e);
72+
}
73+
}
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Config\Validator\Deploy;
9+
10+
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\Config\Environment;
12+
use Magento\MagentoCloud\Config\Validator;
13+
use Magento\MagentoCloud\Config\ValidatorException;
14+
use Magento\MagentoCloud\Config\ValidatorInterface;
15+
use Magento\MagentoCloud\Package\MagentoVersion;
16+
17+
/**
18+
* Validates that environment variables do not contain redundant variables.
19+
* - CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL must be absent in env variables for Magento >= 2.4.0
20+
*/
21+
class EnvironmentVariables implements ValidatorInterface
22+
{
23+
/**
24+
* @var Environment
25+
*/
26+
private $environment;
27+
28+
/**
29+
* @var Validator\ResultFactory
30+
*/
31+
private $resultFactory;
32+
33+
/**
34+
* @var MagentoVersion
35+
*/
36+
private $magentoVersion;
37+
38+
/**
39+
* @param Environment $environment
40+
* @param MagentoVersion $magentoVersion
41+
* @param Validator\ResultFactory $resultFactory
42+
*/
43+
public function __construct(
44+
Environment $environment,
45+
MagentoVersion $magentoVersion,
46+
Validator\ResultFactory $resultFactory
47+
) {
48+
$this->environment = $environment;
49+
$this->magentoVersion = $magentoVersion;
50+
$this->resultFactory = $resultFactory;
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function validate(): Validator\ResultInterface
57+
{
58+
try {
59+
if ($this->magentoVersion->satisfies('>= 2.4.0')
60+
&& $this->environment->getEnv('CONFIG__STORES__DEFAULT__PAYMENT__BRAINTREE__CHANNEL')
61+
) {
62+
return $this->resultFactory->errorByCode(Error::DEPLOY_WRONG_BRAINTREE_VARIABLE);
63+
}
64+
65+
return $this->resultFactory->success();
66+
} catch (\Exception $e) {
67+
throw new ValidatorException($e->getMessage(), $e->getCode(), $e);
68+
}
69+
}
70+
}

src/Config/Validator/ResultFactory.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@
77

88
namespace Magento\MagentoCloud\Config\Validator;
99

10+
use Magento\MagentoCloud\App\ErrorInfo;
1011
use Magento\MagentoCloud\Config\Validator\Result\Error;
1112
use Magento\MagentoCloud\Config\Validator\Result\Success;
13+
use Magento\MagentoCloud\Filesystem\FileSystemException;
1214

1315
/**
1416
* Creates instance of ResultInterface object
1517
*/
1618
class ResultFactory
1719
{
20+
/**
21+
* @var ErrorInfo
22+
*/
23+
private $errorInfo;
24+
25+
/**
26+
* @param ErrorInfo $errorInfo
27+
*/
28+
public function __construct(ErrorInfo $errorInfo)
29+
{
30+
$this->errorInfo = $errorInfo;
31+
}
32+
1833
/**
1934
* Creates instance of ResultInterface object
2035
*
@@ -53,4 +68,23 @@ public function error(string $message, string $suggestion = '', int $code = null
5368
{
5469
return new Error($message, $suggestion, $code);
5570
}
71+
72+
/**
73+
* Creates Error object, fetches info about error from .schema.error.yaml file by code.
74+
* Uses for errors with static error message and suggestion.
75+
*
76+
* @param int $code
77+
* @return Error
78+
* @throws FileSystemException
79+
*/
80+
public function errorByCode(int $code): Error
81+
{
82+
$errorData = $this->errorInfo->get($code);
83+
84+
return new Error(
85+
$errorData['title'] ?? '',
86+
$errorData['suggestion'] ?? '',
87+
$code
88+
);
89+
}
5690
}

0 commit comments

Comments
 (0)