Skip to content

Commit fe2feae

Browse files
committed
MQE-1354: bug fix in command.php
1 parent bf2143d commit fe2feae

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

etc/config/command.php

+37-20
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 (isset($_POST['token']) && isset($_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']);
16+
917
if (array_key_exists("arguments", $_POST)) {
10-
$arguments = urldecode($_POST['arguments']);
18+
$arguments = escapeshellarg(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

+22-5
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
*
@@ -64,12 +71,13 @@ public function __construct($storeCode = null)
6471
}
6572

6673
$this->storeCode = $storeCode;
74+
$this->authToken = null;
6775
$this->transport = new CurlTransport();
6876
$this->authorize();
6977
}
7078

7179
/**
72-
* Returns the authorization token needed for some requests via REST call.
80+
* Acquire and store the authorization token needed for REST requests.
7381
*
7482
* @return void
7583
* @throws TestFrameworkException
@@ -83,10 +91,8 @@ protected function authorize()
8391
];
8492

8593
$this->transport->write($authUrl, json_encode($authCreds), CurlInterface::POST, $this->headers);
86-
$this->headers = array_merge(
87-
['Authorization: Bearer ' . str_replace('"', "", $this->read())],
88-
$this->headers
89-
);
94+
$this->authToken = str_replace('"', "", $this->read());
95+
$this->headers = array_merge(['Authorization: Bearer ' . $this->authToken], $this->headers);
9096
}
9197

9298
/**
@@ -159,4 +165,15 @@ public function getFormattedUrl($resource)
159165
$urlResult.= trim($resource, "/");
160166
return $urlResult;
161167
}
168+
169+
/**
170+
* Return admin auth token.
171+
*
172+
* @throws TestFrameworkException
173+
* @return string
174+
*/
175+
public function getAuthToken()
176+
{
177+
return $this->authToken;
178+
}
162179
}

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

+3
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)