-
Notifications
You must be signed in to change notification settings - Fork 311
Add new bot: cut string from string #1965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0176ace
Add new bot: cut string from string
mariuskarotkis 5833da1
Add documentation
mariuskarotkis 99fb552
Change int to bool
mariuskarotkis ed0299d
change field name, remove init function
mariuskarotkis 9754359
Small fix
mariuskarotkis 968f245
Update documentation
mariuskarotkis 0c313e4
Upda te bot and documentation
mariuskarotkis 37ac3b6
Add license
mariuskarotkis 9e1c60a
Fix space
mariuskarotkis a3f6d9b
Fix for python 3.6
mariuskarotkis 14549f5
Rename bot
mariuskarotkis 4f804c7
Rename bot
mariuskarotkis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Cut from string | ||
""" | ||
from intelmq.lib.bot import Bot | ||
|
||
|
||
class CutFromStringExpertBot(Bot): | ||
string_from_start: int = 1 # 1 - from start, 0 - from end | ||
string_for_cut: str = 'www.' | ||
field_for_cut: str = 'source.fqdn' | ||
mariuskarotkis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
def init(self): | ||
pass | ||
|
||
mariuskarotkis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
def process(self): | ||
event = self.receive_message() | ||
|
||
if self.field_for_cut in event: | ||
field_string = event[self.field_for_cut] | ||
if self.string_from_start == 1 and field_string.startswith(self.string_for_cut): | ||
field_string = field_string[len(self.string_for_cut):] | ||
event.change(self.field_for_cut, field_string) | ||
|
||
if self.string_from_start == 0 and field_string.endswith(self.string_for_cut): | ||
field_string = field_string[:-len(self.string_for_cut)] | ||
event.change(self.field_for_cut, field_string) | ||
|
||
self.send_message(event) | ||
self.acknowledge_message() | ||
|
||
mariuskarotkis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
BOT = CutFromStringExpertBot |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Testing cut from string | ||
""" | ||
import unittest | ||
import intelmq.lib.test as test | ||
from intelmq.bots.experts.cut_from_string.expert import CutFromStringExpertBot | ||
|
||
EXAMPLE_INPUT = { | ||
'__type': 'Event', | ||
'feed.accuracy': 100.0, | ||
'feed.name': 'MISP events', | ||
'feed.provider': 'MISP BAE', | ||
'time.observation': '2020-10-20T12:57:33+00:00', | ||
'feed.url': 'https://sig01.threatreveal.com', | ||
'source.fqdn': 'www.google.lt', | ||
'extra.elastic_index': 'cti-2020-10', | ||
'extra.elastic_id': 'VwVnSnUBXjJtaqsUSw8T'} | ||
|
||
EXAMPLE_OUTPUT = { | ||
'__type': 'Event', | ||
'feed.accuracy': 100.0, | ||
'feed.name': 'MISP events', | ||
'feed.provider': 'MISP BAE', | ||
'time.observation': '2020-10-20T12:57:33+00:00', | ||
'feed.url': 'https://sig01.threatreveal.com', | ||
'source.fqdn': 'google.lt', | ||
'extra.elastic_index': 'cti-2020-10', | ||
'extra.elastic_id': 'VwVnSnUBXjJtaqsUSw8T'} | ||
|
||
EXAMPLE_OUTPUT1 = { | ||
'__type': 'Event', | ||
'feed.accuracy': 100.0, | ||
'feed.name': 'MISP events', | ||
'feed.provider': 'MISP BAE', | ||
'time.observation': '2020-10-20T12:57:33+00:00', | ||
'feed.url': 'https://sig01.threatreveal.com', | ||
'source.fqdn': 'www.google', | ||
'extra.elastic_index': 'cti-2020-10', | ||
'extra.elastic_id': 'VwVnSnUBXjJtaqsUSw8T'} | ||
|
||
EXAMPLE_INPUT_2 = { | ||
'__type': 'Event', | ||
'feed.accuracy': 100.0, | ||
'feed.name': 'MISP events', | ||
'feed.provider': 'MISP BAE', | ||
'time.observation': '2020-10-20T12:57:33+00:00', | ||
'feed.url': 'https://sig01.threatreveal.com', | ||
'extra.elastic_index': 'cti-2020-10', | ||
'extra.elastic_id': 'VwVnSnUBXjJtaqsUSw8T'} | ||
|
||
EXAMPLE_OUTPUT_2 = { | ||
'__type': 'Event', | ||
'feed.accuracy': 100.0, | ||
'feed.name': 'MISP events', | ||
'feed.provider': 'MISP BAE', | ||
'time.observation': '2020-10-20T12:57:33+00:00', | ||
'feed.url': 'https://sig01.threatreveal.com', | ||
'extra.elastic_index': 'cti-2020-10', | ||
'extra.elastic_id': 'VwVnSnUBXjJtaqsUSw8T'} | ||
|
||
|
||
class TestCutFromStringExpertBot(test.BotTestCase, unittest.TestCase): | ||
""" | ||
A TestCase for TestCutFromStringExpertBot. | ||
""" | ||
|
||
@classmethod | ||
def set_bot(cls): | ||
cls.bot_reference = CutFromStringExpertBot | ||
|
||
def test_event_cut_start(self): | ||
self.input_message = EXAMPLE_INPUT | ||
self.run_bot() | ||
self.assertMessageEqual(0, EXAMPLE_OUTPUT) | ||
|
||
def test_event_cut_without_field(self): | ||
self.input_message = EXAMPLE_INPUT_2 | ||
self.run_bot() | ||
self.assertMessageEqual(0, EXAMPLE_OUTPUT_2) | ||
|
||
def test_event_cut_end(self): | ||
self.input_message = EXAMPLE_INPUT | ||
self.run_bot(parameters={"string_from_start": 0, "string_for_cut": ".lt"}) | ||
self.assertMessageEqual(0, EXAMPLE_OUTPUT1) | ||
|
||
|
||
if __name__ == '__main__': # pragma: no cover | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.