new: Allow passing headers along with feeds

- add any arbitrary header to a feed
- can be used for authentication via basic auth for example
pull/2883/head
iglocska 2018-01-19 00:28:44 +01:00
parent 88d971ecc2
commit fd858d627b
7 changed files with 52 additions and 11 deletions

View File

@ -210,7 +210,7 @@ class FeedsController extends AppController {
$this->request->data['Feed']['settings']['delimiter'] = ',';
}
$this->request->data['Feed']['settings'] = json_encode($this->request->data['Feed']['settings']);
$fields = array('id', 'name', 'provider', 'enabled', 'rules', 'url', 'distribution', 'sharing_group_id', 'tag_id', 'fixed_event', 'event_id', 'publish', 'delta_merge', 'source_format', 'override_ids', 'settings', 'input_source', 'delete_local_file', 'lookup_visible');
$fields = array('id', 'name', 'provider', 'enabled', 'rules', 'url', 'distribution', 'sharing_group_id', 'tag_id', 'fixed_event', 'event_id', 'publish', 'delta_merge', 'source_format', 'override_ids', 'settings', 'input_source', 'delete_local_file', 'lookup_visible', 'headers');
$feed = array();
foreach ($fields as $field) {
if (isset($this->request->data['Feed'][$field])) {

View File

@ -53,7 +53,7 @@ class AppModel extends Model {
68 => false, 69 => false, 71 => false, 72 => false, 73 => false,
75 => false, 77 => false, 78 => false, 79 => false, 80 => false,
81 => false, 82 => false, 83 => false, 84 => false, 85 => false,
86 => false
86 => false, 87 => false
)
)
);
@ -858,6 +858,9 @@ class AppModel extends Model {
break;
case '2.4.86':
break;
case '2.4.87':
$sqlArray[] = "ALTER TABLE `feeds` ADD `headers` TEXT COLLATE utf8_bin;";
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';

View File

@ -84,7 +84,7 @@ class Feed extends AppModel {
$data = file_get_contents($feed['Feed']['url'] . '/manifest.json');
}
} else {
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
$uri = $feed['Feed']['url'] . '/manifest.json';
$response = $HttpSocket->get($uri, '', $request);
if ($response->code != 200) return 1;
@ -117,7 +117,7 @@ class Feed extends AppModel {
public function getCache($feed, $HttpSocket) {
$result = array();
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
if (isset($feed['Feed']['input_source']) && $feed['Feed']['input_source'] == 'local') {
if (file_exists($feed['Feed']['url'] . '/hashes.csv')) {
$data = file_get_contents($feed['Feed']['url'] . '/hashes.csv');
@ -153,7 +153,7 @@ class Feed extends AppModel {
public function getManifest($feed, $HttpSocket) {
$result = array();
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
if (isset($feed['Feed']['input_source']) && $feed['Feed']['input_source'] == 'local') {
if (file_exists($feed['Feed']['url'] . '/manifest.json')) {
$data = file_get_contents($feed['Feed']['url'] . '/manifest.json');
@ -458,7 +458,7 @@ class Feed extends AppModel {
return $results;
}
private function __createFeedRequest() {
private function __createFeedRequest($headers = false) {
$version = $this->checkMISPVersion();
$version = implode('.', $version);
try {
@ -466,7 +466,6 @@ class Feed extends AppModel {
} catch (Exception $e) {
$commit = false;
}
$result = array(
'header' => array(
'Accept' => 'application/json',
@ -478,6 +477,19 @@ class Feed extends AppModel {
if ($commit) {
$result['header']['commit'] = $commit;
}
if (!empty($headers)) {
$lines = explode("\n", $headers);
foreach ($lines as $line) {
if (!empty($line)) {
$kv = explode(':', $line);
if (!empty($kv[0]) && !empty($kv[1])) {
if (!in_array($kv[0], array('commit', 'MISP-version', 'MISP-uuid'))) {
$result['header'][trim($kv[0])] = trim($kv[1]);
}
}
}
}
}
return $result;
}
@ -561,7 +573,7 @@ class Feed extends AppModel {
}
} else {
$HttpSocket = $this->__setupHttpSocket($feed);
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
$response = $HttpSocket->get($path, '', $request);
if ($response->code != 200) {
return false;
@ -655,7 +667,7 @@ class Feed extends AppModel {
$data = file_get_contents($path);
}
} else {
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
$response = $HttpSocket->get($path, '', $request);
if ($response->code != 200) {
return false;
@ -680,7 +692,7 @@ class Feed extends AppModel {
$data = file_get_contents($path);
}
} else {
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
$response = $HttpSocket->get($path, '', $request);
if ($response->code != 200) {
return false;
@ -1007,7 +1019,7 @@ class Feed extends AppModel {
}
} else {
$HttpSocket = $this->__setupHttpSocket($feed);
$request = $this->__createFeedRequest();
$request = $this->__createFeedRequest($feed['Feed']['headers']);
$fetchIssue = false;
try {
$response = $HttpSocket->get($path, '', $request);

View File

@ -48,6 +48,17 @@
'class' => 'form-control span6'
));
?>
<div id="HeadersDiv">
<?php
echo $this->Form->input('headers', array(
'label' => __('Any headers to be passed with requests (for example: Authorization)'),
'div' => 'clear',
'class' => 'input-xxlarge',
'type' => 'textarea',
'placeholder' => __('Line break separated list of headers in the "headername: value" format')
));
?>
</div>
<div id="TargetDiv" class="optionalField">
<?php
echo $this->Form->input('fixed_event', array(

View File

@ -26,6 +26,17 @@
'class' => 'form-control span6'
));
?>
<div id="HeadersDiv">
<?php
echo $this->Form->input('headers', array(
'label' => __('Any headers to be passed with requests (for example: Authorization)'),
'div' => 'clear',
'class' => 'input-xxlarge',
'type' => 'textarea',
'placeholder' => __('Line break separated list of headers in the "headername: value" format')
));
?>
</div>
<div class="input clear"></div>
<div id="DeleteLocalFileDiv" class="optionalField">
<?php

View File

@ -47,6 +47,7 @@
<th><?php echo $this->Paginator->sort('provider');?></th>
<th><?php echo $this->Paginator->sort('input_source', __('Input'));?></th>
<th><?php echo $this->Paginator->sort('url');?></th>
<th><?php echo $this->Paginator->sort('headers');?></th>
<th><?php echo __('Target');?></th>
<th><?php echo __('Publish');?></th>
<th><?php echo __('Delta Merge');?></th>
@ -118,6 +119,7 @@ foreach ($feeds as $item):
<td><?php echo h($item['Feed']['provider']); ?>&nbsp;</td>
<td><?php echo h($item['Feed']['input_source']); ?>&nbsp;</td>
<td><?php echo h($item['Feed']['url']); ?>&nbsp;</td>
<td class="short"><?php echo nl2br(h($item['Feed']['headers'])); ?>&nbsp;</td>
<td class="shortish">
<?php
if (in_array($item['Feed']['source_format'], array('freetext', 'csv'))):

View File

@ -2814,8 +2814,10 @@ function feedFormUpdate() {
}
if ($('#FeedInputSource').val() == 'local') {
$('#DeleteLocalFileDiv').show();
$('#HeadersDiv').hide();
} else {
$('#DeleteLocalFileDiv').hide();
$('#HeadersDiv').show();
}
}