Skip to content

Commit a88548b

Browse files
authored
Merge pull request magento#283 from magento/MQE-1354
MQE-1353 & 1354
2 parents 4522886 + 8efe995 commit a88548b

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

etc/config/command.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,51 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
if (isset($_POST['command'])) {
7+
require_once __DIR__ . '/../../../../app/bootstrap.php';
8+
9+
if (!empty($_POST['token']) && !empty($_POST['command'])) {
10+
$magentoObjectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, $_SERVER);
11+
$magentoObjectManager = $magentoObjectManagerFactory->create($_SERVER);
12+
$tokenModel = $magentoObjectManager->get(\Magento\Integration\Model\Oauth\Token::class);
13+
14+
$tokenPassedIn = urldecode($_POST['token']);
815
$command = urldecode($_POST['command']);
9-
if (array_key_exists("arguments", $_POST)) {
16+
17+
if (!empty($_POST['arguments'])) {
1018
$arguments = urldecode($_POST['arguments']);
1119
} else {
1220
$arguments = null;
1321
}
14-
$php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
15-
$valid = validateCommand($command);
16-
if ($valid) {
17-
exec(
18-
escapeCommand($php . ' -f ../../../../bin/magento ' . $command) . " $arguments" ." 2>&1",
19-
$output,
20-
$exitCode
21-
);
22-
if ($exitCode == 0) {
23-
http_response_code(202);
22+
23+
// Token returned will be null if the token we passed in is invalid
24+
$tokenFromMagento = $tokenModel->loadByToken($tokenPassedIn)->getToken();
25+
if (!empty($tokenFromMagento) && ($tokenFromMagento == $tokenPassedIn)) {
26+
$php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
27+
$magentoBinary = $php . ' -f ../../../../bin/magento';
28+
$valid = validateCommand($magentoBinary, $command);
29+
if ($valid) {
30+
exec(
31+
escapeCommand($magentoBinary . " $command" . " $arguments") . " 2>&1",
32+
$output,
33+
$exitCode
34+
);
35+
if ($exitCode == 0) {
36+
http_response_code(202);
37+
} else {
38+
http_response_code(500);
39+
}
40+
echo implode("\n", $output);
2441
} else {
25-
http_response_code(500);
42+
http_response_code(403);
43+
echo "Given command not found valid in Magento CLI Command list.";
2644
}
27-
echo implode("\n", $output);
2845
} else {
29-
http_response_code(403);
30-
echo "Given command not found valid in Magento CLI Command list.";
46+
http_response_code(401);
47+
echo("Command not unauthorized.");
3148
}
3249
} else {
3350
http_response_code(412);
34-
echo("Command parameter is not set.");
51+
echo("Required parameters are not set.");
3552
}
3653

3754
/**
@@ -55,13 +72,13 @@ function escapeCommand($command)
5572

5673
/**
5774
* Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
75+
* @param string $magentoBinary
5876
* @param string $command
5977
* @return bool
6078
*/
61-
function validateCommand($command)
79+
function validateCommand($magentoBinary, $command)
6280
{
63-
$php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
64-
exec($php . ' -f ../../../../bin/magento list', $commandList);
81+
exec($magentoBinary . ' list', $commandList);
6582
// Trim list of commands after first whitespace
6683
$commandList = array_map("trimAfterWhitespace", $commandList);
6784
return in_array(trimAfterWhitespace($command), $commandList);

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class WebapiExecutor extends AbstractExecutor implements CurlInterface
5151
*/
5252
private $storeCode;
5353

54+
/**
55+
* Admin user auth token.
56+
*
57+
* @var string
58+
*/
59+
private $authToken;
60+
5461
/**
5562
* WebapiExecutor Constructor.
5663
*
@@ -60,6 +67,7 @@ class WebapiExecutor extends AbstractExecutor implements CurlInterface
6067
public function __construct($storeCode = null)
6168
{
6269
$this->storeCode = $storeCode;
70+
$this->authToken = null;
6371
$this->transport = new CurlTransport();
6472
$this->authorize();
6573
}
@@ -88,7 +96,7 @@ public function getBaseUrl(): string
8896
}
8997

9098
/**
91-
* Returns the authorization token needed for some requests via REST call.
99+
* Acquire and store the authorization token needed for REST requests.
92100
*
93101
* @return void
94102
* @throws TestFrameworkException
@@ -102,10 +110,8 @@ protected function authorize()
102110
];
103111

104112
$this->transport->write($authUrl, json_encode($authCreds), CurlInterface::POST, $this->headers);
105-
$this->headers = array_merge(
106-
['Authorization: Bearer ' . str_replace('"', "", $this->read())],
107-
$this->headers
108-
);
113+
$this->authToken = str_replace('"', "", $this->read());
114+
$this->headers = array_merge(['Authorization: Bearer ' . $this->authToken], $this->headers);
109115
}
110116

111117
/**
@@ -178,4 +184,15 @@ public function getFormattedUrl($resource)
178184
$urlResult .= trim($resource, "/");
179185
return $urlResult;
180186
}
187+
188+
/**
189+
* Return admin auth token.
190+
*
191+
* @throws TestFrameworkException
192+
* @return string
193+
*/
194+
public function getAuthToken()
195+
{
196+
return $this->authToken;
197+
}
181198
}

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,20 @@ public function magentoCLI($command, $arguments = null)
482482
);
483483
$apiURL = $baseUrl . '/' . ltrim(getenv('MAGENTO_CLI_COMMAND_PATH'), '/');
484484

485+
$restExecutor = new WebapiExecutor();
485486
$executor = new CurlTransport();
486487
$executor->write(
487488
$apiURL,
488489
[
490+
'token' => $restExecutor->getAuthToken(),
489491
getenv('MAGENTO_CLI_COMMAND_PARAMETER') => $command,
490492
'arguments' => $arguments
491493
],
492494
CurlInterface::POST,
493495
[]
494496
);
495497
$response = $executor->read();
498+
$restExecutor->close();
496499
$executor->close();
497500
return $response;
498501
}

0 commit comments

Comments
 (0)