mirror of https://github.com/MISP/MISP
chg: [sync] Simplify server post test code
parent
c04c009567
commit
157dbed32b
|
@ -17,7 +17,6 @@ class ServerShell extends AppShell
|
|||
public function list()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$res = ['servers'=>[]];
|
||||
$servers = $this->Server->find('all', [
|
||||
'fields' => ['Server.id', 'Server.name', 'Server.url'],
|
||||
'recursive' => 0
|
||||
|
@ -64,7 +63,7 @@ class ServerShell extends AppShell
|
|||
die("Server with ID $serverId doesn't exists.");
|
||||
}
|
||||
|
||||
$res = @$this->Server->runConnectionTest($server);
|
||||
$res = $this->Server->runConnectionTest($server, false);
|
||||
echo json_encode($res) . PHP_EOL;
|
||||
}
|
||||
|
||||
|
|
|
@ -1662,9 +1662,6 @@ class ServersController extends AppController
|
|||
$mismatch = false;
|
||||
$newer = false;
|
||||
$parts = array('major', 'minor', 'hotfix');
|
||||
if ($version[0] == 2 && $version[1] == 4 && $version[2] > 68) {
|
||||
$post = $this->Server->runPOSTTest($server);
|
||||
}
|
||||
foreach ($parts as $k => $v) {
|
||||
if (!$mismatch) {
|
||||
if ($version[$k] > $local_version[$v]) {
|
||||
|
@ -1693,8 +1690,8 @@ class ServersController extends AppController
|
|||
'version' => implode('.', $version),
|
||||
'mismatch' => $mismatch,
|
||||
'newer' => $newer,
|
||||
'post' => isset($post) ? $post['status'] : 'too old',
|
||||
'response_encoding' => isset($post['content-encoding']) ? $post['content-encoding'] : null,
|
||||
'post' => isset($result['post']) ? $result['post']['status'] : 'too old',
|
||||
'response_encoding' => isset($result['post']['content-encoding']) ? $result['post']['content-encoding'] : null,
|
||||
'request_encoding' => isset($result['info']['request_encoding']) ? $result['info']['request_encoding'] : null,
|
||||
'client_certificate' => $result['client_certificate'],
|
||||
], 'json');
|
||||
|
|
|
@ -25,7 +25,7 @@ class MispAdminSyncTestWidget
|
|||
}
|
||||
$syncTestErrorCodes = $this->Server->syncTestErrorCodes;
|
||||
foreach ($servers as $server) {
|
||||
$result = $this->Server->runConnectionTest($server);
|
||||
$result = $this->Server->runConnectionTest($server, false);
|
||||
if ($result['status'] === 1) {
|
||||
$message = __('Connected.');
|
||||
$colour = 'green';
|
||||
|
|
|
@ -7,7 +7,8 @@ class ServerSyncTool
|
|||
FEATURE_GZIP = 'gzip',
|
||||
FEATURE_ORG_RULE = 'org_rule',
|
||||
FEATURE_FILTER_SIGHTINGS = 'filter_sightings',
|
||||
FEATURE_PROPOSALS = 'proposals';
|
||||
FEATURE_PROPOSALS = 'proposals',
|
||||
FEATURE_POST_TEST = 'post_test';
|
||||
|
||||
/** @var array */
|
||||
private $server;
|
||||
|
@ -178,6 +179,14 @@ class ServerSyncTool
|
|||
return $this->server;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function serverId()
|
||||
{
|
||||
return $this->server['Server']['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $flag
|
||||
* @return bool
|
||||
|
@ -201,6 +210,9 @@ class ServerSyncTool
|
|||
case self::FEATURE_PROPOSALS:
|
||||
$version = explode('.', $info['version']);
|
||||
return $version[0] == 2 && $version[1] == 4 && $version[2] >= 111;
|
||||
case self::FEATURE_POST_TEST:
|
||||
$version = explode('.', $info['version']);
|
||||
return $version[0] == 2 && $version[1] == 4 && $version[2] > 68;
|
||||
default:
|
||||
throw new InvalidArgumentException("Invalid flag `$flag` provided");
|
||||
}
|
||||
|
|
|
@ -2449,10 +2449,11 @@ class Server extends AppModel
|
|||
|
||||
/**
|
||||
* @param array $server
|
||||
* @param bool $withPostTest
|
||||
* @return array
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function runConnectionTest(array $server)
|
||||
public function runConnectionTest(array $server, $withPostTest = true)
|
||||
{
|
||||
App::uses('SyncTool', 'Tools');
|
||||
try {
|
||||
|
@ -2460,8 +2461,8 @@ class Server extends AppModel
|
|||
if ($clientCertificate) {
|
||||
$clientCertificate['valid_from'] = $clientCertificate['valid_from'] ? $clientCertificate['valid_from']->format('c') : __('Not defined');
|
||||
$clientCertificate['valid_to'] = $clientCertificate['valid_to'] ? $clientCertificate['valid_to']->format('c') : __('Not defined');
|
||||
$clientCertificate['public_key_size'] = $clientCertificate['public_key_size'] ?: __('Unknwon');
|
||||
$clientCertificate['public_key_type'] = $clientCertificate['public_key_type'] ?: __('Unknwon');
|
||||
$clientCertificate['public_key_size'] = $clientCertificate['public_key_size'] ?: __('Unknown');
|
||||
$clientCertificate['public_key_type'] = $clientCertificate['public_key_type'] ?: __('Unknown');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$clientCertificate = ['error' => $e->getMessage()];
|
||||
|
@ -2471,7 +2472,17 @@ class Server extends AppModel
|
|||
|
||||
try {
|
||||
$info = $serverSync->info();
|
||||
return ['status' => 1, 'info' => $info, 'client_certificate' => $clientCertificate];
|
||||
$response = [
|
||||
'status' => 1,
|
||||
'info' => $info,
|
||||
'client_certificate' => $clientCertificate,
|
||||
];
|
||||
|
||||
if ($withPostTest) {
|
||||
$response['post'] = $serverSync->isSupported(ServerSyncTool::FEATURE_POST_TEST) ? $this->runPOSTtest($serverSync) : null;
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (HttpSocketHttpException $e) {
|
||||
if ($e->getCode() === 403) {
|
||||
|
@ -2506,28 +2517,26 @@ class Server extends AppModel
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $server
|
||||
* @param ServerSyncTool $serverSync
|
||||
* @return array
|
||||
* @throws JsonException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function runPOSTtest(array $server)
|
||||
private function runPOSTtest(ServerSyncTool $serverSync)
|
||||
{
|
||||
$testFile = file_get_contents(APP . 'files/scripts/test_payload.txt');
|
||||
if (!$testFile) {
|
||||
throw new Exception("Could not load payload for POST test.");
|
||||
}
|
||||
|
||||
$serverSync = new ServerSyncTool($server, $this->setupSyncRequest($server));
|
||||
|
||||
try {
|
||||
$response = $serverSync->postTest($testFile);
|
||||
$contentEncoding = $response->getHeader('Content-Encoding');
|
||||
$rawBody = $response->body;
|
||||
$response = $response->json();
|
||||
} catch (Exception $e) {
|
||||
$this->logException("Invalid response for remote server {$server['Server']['name']} POST test.", $e);
|
||||
$this->logException("Invalid response for remote server {$serverSync->server()['Server']['name']} POST test.", $e);
|
||||
$title = 'Error: POST connection test failed. Reason: ' . $e->getMessage();
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $server['Server']['id'], $title);
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $serverSync->serverId(), $title);
|
||||
return ['status' => 8];
|
||||
}
|
||||
if (!isset($response['body']['testString']) || $response['body']['testString'] !== $testFile) {
|
||||
|
@ -2540,7 +2549,7 @@ class Server extends AppModel
|
|||
}
|
||||
|
||||
$title = 'Error: POST connection test failed due to the message body not containing the expected data. Response: ' . PHP_EOL . PHP_EOL . $responseString;
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $server['Server']['id'], $title);
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $serverSync->serverId(), $title);
|
||||
return ['status' => 9, 'content-encoding' => $contentEncoding];
|
||||
}
|
||||
$headers = array('Accept', 'Content-type');
|
||||
|
@ -2548,7 +2557,7 @@ class Server extends AppModel
|
|||
if (!isset($response['headers'][$header]) || $response['headers'][$header] !== 'application/json') {
|
||||
$responseHeader = isset($response['headers'][$header]) ? $response['headers'][$header] : 'Header was not set.';
|
||||
$title = 'Error: POST connection test failed due to a header ' . $header . ' not matching the expected value. Expected: "application/json", received "' . $responseHeader . '"';
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $server['Server']['id'], $title);
|
||||
$this->loadLog()->createLogEntry('SYSTEM', 'error', 'Server', $serverSync->serverId(), $title);
|
||||
return ['status' => 10, 'content-encoding' => $contentEncoding];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue