Skip to content

Commit

Permalink
multiple http_post_url
Browse files Browse the repository at this point in the history
  • Loading branch information
nsano-rururu committed May 22, 2023
1 parent ab57c95 commit 587519e
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 8 deletions.
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlJira.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlLineNotify.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlHttpPost.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlHttpPost002.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlHttpPostOpt.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlHttpPost2.spec.js &&
./node_modules/.bin/vue-cli-service test:unit tests/unit/specs/alert/ConfigYamlHttpPost2Opt.spec.js &&
Expand Down
112 changes: 108 additions & 4 deletions src/components/config/alert/ConfigAlertHttpPost.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
<template>
<div>
<el-form-item label="HTTP POST URL" prop="httpPostUrl" required>
<el-input v-model="httpPostUrl" :disabled="viewOnly" />
<label>JSON results will be POSTed to this URL</label>
</el-form-item>
<el-popover v-model="popHttpPostUrlVisible" :class="{ 'is-invalid': !popHttpPostUrlValid }">
<template v-slot:reference>
<span class="pop-trigger">
<el-tooltip v-if="httpPostUrl.length" :content="httpPostUrl.join(', ')" placement="top">
<span>Tags ({{ httpPostUrl.length }})</span>
</el-tooltip>
<span v-else>Tags ({{ httpPostUrl.length }})</span>
</span>
</template>
<template>
<el-form
ref="httpPostUrl"
:model="$store.state.config.alert"
label-position="top"
style="width: 360px"
@submit.native.prevent>
<el-form-item
v-for="(entry, index) in httpPostUrl"
:key="index"
:prop="'httpPostUrl.' + index"
:disabled="viewOnly"
class="el-form-item-list"
label=""
required>
<el-row :gutter="5" type="flex" justify="space-between">
<el-col :span="20">
<el-input
v-model="httpPostUrl[index]"
:disabled="viewOnly"
placeholder="Tags"
@input="(val) => updatehttpPostUrl(val, index)" />
</el-col>
<el-col :span="4">
<el-button
:disabled="viewOnly"
type="danger"
icon="el-icon-delete"
circle
plain
@click="removehttpPostUrlEntry(entry)" />
</el-col>
</el-row>
</el-form-item>
</el-form>

<el-button :disabled="viewOnly" class="m-n-sm" @click="addhttpPostUrlEntry">
Add httpPostUrls
</el-button>
</template>
</el-popover>

<el-form-item label="CA Certs" prop="httpPostCaCerts">
<el-switch
Expand Down Expand Up @@ -41,6 +87,13 @@
export default {
props: ['viewOnly'],
data() {
return {
popHttpPostUrlVisible: false,
popHttpPostUrlValid: true,
};
},
computed: {
httpPostUrl: {
get() {
Expand Down Expand Up @@ -101,6 +154,57 @@ export default {
},
methods: {
async validate() {
try {
if (this.$refs.httpPostUrl) {
await this.validatehttpPostUrl();
}
this.$emit('validate', true);
return true;
} catch (error) {
this.$emit('validate', false);
return false;
}
},
async validatehttpPostUrl() {
if (!this.httpPostUrl.length) {
this.popHttpPostUrlValid = false;
return;
}
try {
this.popHttpPostUrlValid = await this.$refs.httpPostUrl.validate();
} catch (error) {
this.popHttpPostUrlValid = false;
throw error;
}
},
updatehttpPostUrl(entry, index) {
if (Number.isNaN(entry)) return;
this.$store.commit('config/alert/UPDATE_HTTP_POST_URL_ENTRY', {
entry,
index
});
this.$nextTick(() => {
this.validate();
});
},
removehttpPostUrlEntry(entry) {
this.$store.commit('config/alert/REMOVE_HTTP_POST_URL_ENTRY', entry);
this.$nextTick(() => {
this.validate();
});
},
addhttpPostUrlEntry() {
this.$store.commit('config/alert/ADD_HTTP_POST_URL_ENTRY');
this.$nextTick(() => {
this.validate();
});
},
changeHttpPostIgnoreSslErrors(val) {
if (val) {
this.httpPostIgnoreSslErrors = true;
Expand Down
19 changes: 18 additions & 1 deletion src/store/config/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function initialState() {
googleChatProxy: '',

/* HTTP POST */
httpPostUrl: '',
httpPostUrl: [],
httpPostIgnoreSslErrors: false,
httpPostCaCerts: false,
httpPostTimeout: '',
Expand Down Expand Up @@ -1153,6 +1153,23 @@ export default {
state.httpPostUrl = httpPostUrl;
},

ADD_HTTP_POST_URL_ENTRY(state) {
state.httpPostUrl.push('');
},

ADD_HTTP_POST_URL_ENTRY_VALUE(state, value) {
state.httpPostUrl.push(value);
},

REMOVE_HTTP_POST_URL_ENTRY(state, entry) {
state.httpPostUrl = state.httpPostUrl.filter(b => b !== entry);
},

UPDATE_HTTP_POST_URL_ENTRY(state, { entry, index }) {
if (!state.httpPostUrl) return;
state.httpPostUrl[index] = entry;
},

UPDATE_HTTP_POST_IGNORE_SSL_ERRORS(state, httpPostIgnoreSslErrors) {
state.httpPostIgnoreSslErrors = httpPostIgnoreSslErrors;
},
Expand Down
12 changes: 10 additions & 2 deletions src/store/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,15 @@ export default {
commit('alert/UPDATE_OWNER', config.owner);

/* HTTP POST */
commit('alert/UPDATE_HTTP_POST_URL', config.http_post_url);
if (typeof (config.http_post_url) === 'string') {
let tmphttpPostUrl = [];
tmphttpPostUrl.push(config.http_post_url);
config.http_post_url = tmphttpPostUrl;
}

if (config.http_post_url) {
commit('alert/UPDATE_HTTP_POST_URL', config.http_post_url);
}

if (config.http_post_ignore_ssl_errors) {
commit('alert/UPDATE_HTTP_POST_IGNORE_SSL_ERRORS', config.http_post_ignore_ssl_errors);
Expand Down Expand Up @@ -1447,7 +1455,7 @@ export default {
http(state) {
let config = {};

if (state.alert.httpPostUrl) {
if (state.alert.httpPostUrl && state.alert.httpPostUrl.length) {
config.http_post_url = state.alert.httpPostUrl;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/unit/mockData/alert/ruleDataHttpPost002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const ruleYaml = `__praeco_query_builder: '{"query":{"logicalOperator":"all","children":[]}}'
alert:
- post
alert_subject: this is a test subject
alert_subject_args: []
alert_text: this is a test body
alert_text_args: []
alert_text_type: alert_text_only
doc_type: syslog
filter:
- query:
query_string:
query: '@timestamp:*'
http_post_url:
- http://localhost/webhook
- http://localhost/webhook2
import: BaseRule.config
index: hannibal-*
is_enabled: false
name: test123
num_events: 10000
realert:
minutes: 5
timeframe:
minutes: 5
timestamp_field: '@timestamp'
timestamp_type: iso
type: frequency
use_count_query: true
use_strftime_index: false
`;
3 changes: 2 additions & 1 deletion tests/unit/specs/alert/ConfigYamlHttpPost.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ filter:
query: "@timestamp:*"
generate_kibana_discover_url: false
http_post_timeout: 10
http_post_url: "http://localhost/webhook"
http_post_url:
- "http://localhost/webhook"
import: "BaseRule.config"
index: "hannibal-*"
is_enabled: false
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/specs/alert/ConfigYamlHttpPost002.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect } from 'chai';
import store from '@/store';
import { mockAxios } from '../../setup';
import { ruleYaml } from '../../mockData/alert/ruleDataHttpPost002.js';

mockAxios.onGet('/api/rules/test123').reply(200, { yaml: ruleYaml });

describe('HttpPost YAML parsing', () => {
it('renders the correct yaml', async () => {
await store.dispatch('config/load', { type: 'rules', path: 'test123' });

let yaml = store.getters['config/yaml']();

let expected = `__praeco_full_path: "test123"
__praeco_query_builder: "{\\"query\\":{\\"logicalOperator\\":\\"all\\",\\"children\\":[]}}"
alert:
- "post"
doc_type: "syslog"
filter:
- query:
query_string:
query: "@timestamp:*"
generate_kibana_discover_url: false
http_post_timeout: 10
http_post_url:
- "http://localhost/webhook"
- "http://localhost/webhook2"
import: "BaseRule.config"
index: "hannibal-*"
is_enabled: false
match_enhancements: []
name: "test123"
num_events: 10000
realert:
minutes: 5
terms_size: 50
timeframe:
minutes: 5
timestamp_field: "@timestamp"
timestamp_type: "iso"
type: "frequency"
use_count_query: true
use_strftime_index: false
`;

return expect(yaml).to.equal(expected);
});
});

0 comments on commit 587519e

Please sign in to comment.