-
Notifications
You must be signed in to change notification settings - Fork 35
feat: provide support for custom search command #1534
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
Closed
Closed
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
84c9d33
feat: custom seach command feature
hetangmodi-crest b5989f6
feat: template files for custom search command feature
hetangmodi-crest 039fe0d
tests(unit): add unit test cases
hetangmodi-crest 7432330
tests(smoke): add files for smoke test case
hetangmodi-crest 3802847
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest fda2c7f
doc: update docs, fix lint
hetangmodi-crest 85be57e
ci: fix app_inspect failure
hetangmodi-crest d73930c
test(smoke): update smoke tests, add better handling
hetangmodi-crest d4e3f3e
chore: resolve merge conflict
hetangmodi-crest 181da69
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 2e288e7
fix: update schema.json
hetangmodi-crest d90a475
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest f8d6ce3
chore: merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 1425f9b
feat: generate files using FileGenerator class
hetangmodi-crest 1ef0399
tests: add unit and smoke test cases
hetangmodi-crest b68d9a6
doc: update documentation
hetangmodi-crest 3951671
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest b3a42b6
docs: updated docs regarding generated conf, xml and html files
srv-rr-github-token 9af4085
ci: fix pipeline failures
hetangmodi-crest 2866cf9
docs: updated docs regarding generated conf, xml and html files
srv-rr-github-token 312ee3c
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 8119835
docs: resolve typos
hetangmodi-crest afc6bbb
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 5f44b3a
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 4f0a32f
chore: fix typos in source code
hetangmodi-crest 8823338
ci: fix globalConfig everything
hetangmodi-crest a7ec116
feat: added check for Splunk built-in commands
hetangmodi-crest 9986ec3
chore: merge branch 'develop' into feat/custom-search-command
hetangmodi-crest e47b2cd
chore: add license headers
hetangmodi-crest 1adadbb
refactor: removed version 1 support
hetangmodi-crest ff8da5b
tests: updated unit test cases
hetangmodi-crest 24d9860
Merge branch 'develop' into feat/custom-search-command
vtsvetkov-splunk b9a73e3
Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 80d43e6
chore: Merge branch 'develop' into feat/custom-search-command
hetangmodi-crest d9c8229
chore: merge branch 'develop' into feat/custom-search-command
hetangmodi-crest 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
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,212 @@ | ||
| # Custom Search Command | ||
|
|
||
| Custom search commands are user-defined [SPL](https://docs.splunk.com/Splexicon:SPL) (Splunk Search Processing Language) commands that enable users to add custom functionality to their Splunk searches. | ||
|
|
||
|
|
||
| ## Generation of custom search command | ||
|
|
||
| A new tag has been introduced in globalConfig (same indent level as of `meta` tag) named `customSearchCommand` where you need to define the configuration for the custom search command. | ||
|
|
||
| ### Minimal definition | ||
|
|
||
| ```json | ||
| "customSearchCommand": [ | ||
| { | ||
| "commandName": "mycommandname", | ||
| "fileName": "mycommandlogic.py", | ||
| "commandType": "generating", | ||
| "arguments": [ | ||
| { | ||
| "name": "argument_name", | ||
| "validate": { | ||
| "type": "Fieldname" | ||
| }, | ||
| "required": true | ||
| }, | ||
| { | ||
| "name": "argument_two" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| This configuration will generate a template Python file named `mycommandname.py`, which imports logic from the `mycommandlogic.py` file and automatically updates the `commands.conf` file as shown below: | ||
|
|
||
| ``` | ||
| [mycommandname] | ||
| filename = mycommandname.py | ||
| chunked = true | ||
| python.version = python3 | ||
| ``` | ||
|
|
||
| **NOTE:** | ||
| If the file specified in the `fileName` field does not exist in the `<YOUR_ADDON/bin>` directory, the build will fail. | ||
|
|
||
| ### Attributes for `customSearchCommand` tag | ||
|
|
||
| | Property | Type | Description | | ||
| | ------------------------ | ------ | ------------------------------------ | | ||
| | commandName<span class="required-asterisk">\*</span> | string | Name of the custom search command | | ||
| | fileName<span class="required-asterisk">\*</span> | string | Name of the Python file which contains logic of custom search command | | ||
| | commandType<span class="required-asterisk">\*</span> | string | Specify type of custom search command. Four types of commands are allowed, `streaming`,`generating`,`reporting` and `eventing`. | | ||
| | arguments<span class="required-asterisk">\*</span> | object | Arguments which can be passed to custom search command. | | ||
| | requiredSearchAssistant | boolean | Specifies whether search assistance is required for the custom search command. Default: false. | | ||
| | usage | string | Defines the usage of custom search command. It can be one of `public`, `private` and `deprecated`. | | ||
| | description | string | Provide description of the custom search command. | | ||
| | syntax | string | Provide syntax for custom search command | | ||
|
|
||
| To generate a custom search command, the following attributes must be defined in globalConfig: `commandName`, `commandType`, `fileName`, and `arguments`. Based on the provided commandType, UCC will generate a template Python file and integrate the user-defined logic into it. | ||
|
|
||
| If `requiredSearchAssistant` is set to True, the `syntax`, `description`, and `usage` attributes are mandatory, as they are essential for generating `searchbnf.conf` file. | ||
|
|
||
| **NOTE:** | ||
| The user-defined Python file must include specific functions based on the command type: | ||
|
|
||
| - For `Generating` command, the Python file must include a `generate` function. | ||
| - For `Streaming` command, the Python file must include a `stream` function. | ||
| - For `Eventing` command, the Python file must include a `transform` function. | ||
| - For `Reporting` command, the Python file must include a `reduce` function, and optionally a `map` function if a streaming pre-operation is required. | ||
|
|
||
| ## Arguments | ||
|
|
||
| | Property | Type | Description | | ||
| | --------------------------------------------------------------------- | ------ | ------------------------------------------------------- | | ||
| | name<span class="required-asterisk">\*</span> | string | Name of the argument | | ||
| | defaultValue | string/number | Default value of the argument. | | ||
| | required | string | Specify if the argument is required or not. | | ||
| | validate | object | Specify validation for the argument. It can be any of `Integer`, `Float`, `Boolean`, `RegularExpression` or `FieldName`. | | ||
|
|
||
| UCC currently supports five types of validations provided by `splunklib` library: | ||
|
|
||
| - IntegerValidator | ||
| + you can optionally define `minimum` and `maximum` properties. | ||
| - FloatValidator | ||
| + you can optionally define `minimum` and `maximum` properties. | ||
| - BooleanValidator | ||
| + no additional properties required. | ||
| - RegularExpressionValidator | ||
| + no additional properties required. | ||
| - FieldnameValidator | ||
| + no additional properties required. | ||
|
|
||
| For more information, refer [splunklib API docs](https://splunk-python-sdk.readthedocs.io/en/latest/searchcommands.html) | ||
|
|
||
| For example: | ||
|
|
||
| ```json | ||
| "arguments": [ | ||
| { | ||
| "name": "count", | ||
| "required": true, | ||
| "validate": { | ||
| "type": "Integer", | ||
| "minimum": 1, | ||
| "maximum": 10 | ||
| }, | ||
| "default": 5 | ||
| }, | ||
| { | ||
| "name": "test", | ||
| "required": true, | ||
| "validate": { | ||
| "type": "Fieldname" | ||
| } | ||
| }, | ||
| { | ||
| "name": "percent", | ||
| "validate": { | ||
| "type": "Float", | ||
| "minimum": "85.5" | ||
| } | ||
|
|
||
| } | ||
| ] | ||
|
|
||
| ``` | ||
|
|
||
| ## Example | ||
|
|
||
| ``` json | ||
| { | ||
| "meta": {...} | ||
| "customSearchCommand": [ | ||
| { | ||
| "commandName": "testcommand", | ||
| "fileName": "commandlogic.py", | ||
| "commandType": "streaming", | ||
| "requiredSearchAssistant": true, | ||
| "description": "This is a test command", | ||
| "syntax": "| testcommand fieldname=<Name of field> pattern=<Valid regex pattern>", | ||
| "usage": "public", | ||
| "arguments": [ | ||
| { | ||
| "name": "fieldname", | ||
| "validate": { | ||
| "type": "Fieldname" | ||
| } | ||
| }, | ||
| { | ||
| "name": "pattern", | ||
| "validate": { | ||
| "type": "RegularExpression" | ||
| }, | ||
| "required": true | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "pages": {...} | ||
| } | ||
| ``` | ||
|
|
||
| Generated python file named `testcommand.py`: | ||
|
|
||
| ``` python | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, StreamingCommand, Configuration, Option, validators | ||
| from commandlogic import stream | ||
|
|
||
| @Configuration() | ||
| class testcommandCommand(StreamingCommand): | ||
| """ | ||
|
|
||
| ##Syntax | ||
| This is a test command | ||
|
|
||
| ##Description | ||
| | testcommand fieldname=<Name of field> pattern=<Valid regex pattern> | ||
|
|
||
| """ | ||
|
|
||
| fieldname = Option(name = "fieldname",require = False, validate = validators.Fieldname(), default = "") | ||
| pattern = Option(name = "pattern",require = True, validate = validators.RegularExpression(), default = "") | ||
|
|
||
|
|
||
| def stream(self, events): | ||
| # Put your event transformation code here | ||
| return stream(self,events) | ||
|
|
||
| dispatch(testcommandCommand, sys.argv, sys.stdin, sys.stdout, __name__) | ||
| ``` | ||
|
|
||
| Generated stanza in `commands.conf` file | ||
|
|
||
| ``` | ||
| [testcommand] | ||
| filename = testcommand.py | ||
| chunked = true | ||
| python.version = python3 | ||
| ``` | ||
|
|
||
| Generated stanza in `searchbnf.conf` file | ||
|
|
||
| ``` | ||
| [testcommand] | ||
| syntax = | testcommand fieldname=<Name of field> pattern=<Valid regex pattern> | ||
| description = This is a test command. | ||
| usage = public | ||
| ``` | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.