Skip to content

Commit 40fda60

Browse files
Deprecate upload-rule
1 parent c2fd4de commit 40fda60

File tree

5 files changed

+135
-119
lines changed

5 files changed

+135
-119
lines changed

CLI.md

Lines changed: 94 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,6 @@ Commands which connect to Elasticsearch or Kibana are embedded under the subcomm
130130
These command groups will leverage their respective clients and will automatically use parsed config options if
131131
defined, otherwise arguments should be passed to the sub-command as:
132132

133-
`python -m detection-rules kibana -u <username> -p <password> upload-rule <...>`
134-
135-
136-
```console
137-
python -m detection_rules es -h
138-
139-
Usage: detection_rules es [OPTIONS] COMMAND [ARGS]...
140-
141-
Commands for integrating with Elasticsearch.
142-
143-
Options:
144-
-et, --timeout INTEGER Timeout for elasticsearch client
145-
-ep, --es-password TEXT
146-
-eu, --es-user TEXT
147-
--cloud-id TEXT
148-
-e, --elasticsearch-url TEXT
149-
-h, --help Show this message and exit.
150-
151-
Commands:
152-
collect-events Collect events from Elasticsearch.
153-
```
154-
155133
Providers are the name that Elastic Cloud uses to configure authentication in Kibana. When we create deployment, Elastic Cloud configures two providers by default: basic/cloud-basic and saml/cloud-saml (for SSO).
156134

157135
```console
@@ -168,26 +146,21 @@ Usage: detection_rules kibana [OPTIONS] COMMAND [ARGS]...
168146
Options:
169147
--ignore-ssl-errors TEXT
170148
--space TEXT Kibana space
171-
--provider-name TEXT For cloud deployments, Elastic Cloud configures
172-
two providers by default: cloud-basic and
173-
cloud-saml (for SSO)
174-
--provider-type TEXT For cloud deployments, Elastic Cloud configures
175-
two providers by default: basic and saml (for
176-
SSO)
149+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)
150+
--provider-type TEXT Elastic Cloud providers: basic and saml (for SSO)
177151
-ku, --kibana-user TEXT
178152
--kibana-url TEXT
179153
-kp, --kibana-password TEXT
180154
-kc, --kibana-cookie TEXT Cookie from an authed session
181-
--cloud-id TEXT ID of the cloud instance. Defaults the cloud
182-
provider to cloud-basic if this option is
183-
supplied
155+
--api-key TEXT
156+
--cloud-id TEXT ID of the cloud instance.
184157
-h, --help Show this message and exit.
185158

186159
Commands:
187160
export-rules Export custom rules from Kibana.
188161
import-rules Import custom rules into Kibana.
189162
search-alerts Search detection engine alerts with KQL.
190-
upload-rule Upload a list of rule .toml files to Kibana.
163+
upload-rule [Deprecated] Upload a list of rule .toml files to Kibana.
191164
```
192165

193166
## Searching Kibana for Alerts
@@ -198,23 +171,22 @@ Alerts stored in Kibana can be quickly be identified by searching with the `sear
198171
```console
199172
python -m detection_rules kibana search-alerts -h
200173

174+
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
175+
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
176+
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
177+
201178
Kibana client:
202179
Options:
203180
--ignore-ssl-errors TEXT
204181
--space TEXT Kibana space
205-
--provider-name TEXT For cloud deployments, Elastic Cloud configures
206-
two providers by default: cloud-basic and
207-
cloud-saml (for SSO)
208-
--provider-type TEXT For cloud deployments, Elastic Cloud configures
209-
two providers by default: basic and saml (for
210-
SSO)
182+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)
183+
--provider-type TEXT Elastic Cloud providers: basic and saml (for SSO)
211184
-ku, --kibana-user TEXT
212185
--kibana-url TEXT
213186
-kp, --kibana-password TEXT
214187
-kc, --kibana-cookie TEXT Cookie from an authed session
215-
--cloud-id TEXT ID of the cloud instance. Defaults the cloud
216-
provider to cloud-basic if this option is
217-
supplied
188+
--api-key TEXT
189+
--cloud-id TEXT ID of the cloud instance.
218190

219191
Usage: detection_rules kibana search-alerts [OPTIONS] [QUERY]
220192

@@ -224,8 +196,8 @@ Options:
224196
-d, --date-range <TEXT TEXT>...
225197
Date range to scope search
226198
-c, --columns TEXT Columns to display in table
227-
-e, --extend If columns are specified, extend the
228-
original columns
199+
-e, --extend If columns are specified, extend the original columns
200+
-m, --max-count INTEGER The max number of alerts to return
229201
-h, --help Show this message and exit.
230202
```
231203

@@ -257,70 +229,9 @@ Running the following command will print out a table showing any alerts that hav
257229
```
258230
## Uploading rules to Kibana
259231

260-
Toml formatted rule files can be uploaded as custom rules using the `kibana upload-rule` command. To upload more than one
261-
file, specify multiple files at a time as individual args. This command is meant to support uploading and testing of
262-
rules and is not intended for production use in its current state.
232+
### Using `kibana import-rules`
263233

264-
This command is built on soon to be deprecated APIs and so should be phased off. For a better option, see below...
265-
266-
```console
267-
python -m detection_rules kibana upload-rule -h
268-
269-
Kibana client:
270-
Options:
271-
--space TEXT Kibana space
272-
-kp, --kibana-password TEXT
273-
-ku, --kibana-user TEXT
274-
--cloud-id TEXT
275-
-k, --kibana-url TEXT
276-
277-
Usage: detection_rules kibana upload-rule [OPTIONS]
278-
279-
Upload a list of rule .toml files to Kibana.
280-
281-
Options:
282-
-f, --rule-file FILE
283-
-d, --directory DIRECTORY Recursively export rules from a directory
284-
-id, --rule-id TEXT
285-
-r, --replace-id Replace rule IDs with new IDs before export
286-
-h, --help Show this message and exit.
287-
(detection-rules-build) (base) ➜ detection-rules git:(main) ✗
288-
```
289-
290-
Alternatively, rules can be exported into a consolidated ndjson file which can be imported in the Kibana security app
291-
directly.
292-
293-
```console
294-
Usage: detection_rules export-rules-from-repo [OPTIONS]
295-
296-
Export rule(s) and exception(s) into an importable ndjson file.
297-
298-
Options:
299-
-f, --rule-file FILE
300-
-d, --directory DIRECTORY Recursively load rules from a directory
301-
-id, --rule-id TEXT
302-
-o, --outfile PATH Name of file for exported rules
303-
-r, --replace-id Replace rule IDs with new IDs before export
304-
--stack-version [7.8|7.9|7.10|7.11|7.12|7.13|7.14|7.15|7.16|8.0|8.1|8.2|8.3|8.4|8.5|8.6|8.7|8.8|8.9|8.10|8.11|8.12|8.13|8.14]
305-
Downgrade a rule version to be compatible
306-
with older instances of Kibana
307-
-s, --skip-unsupported If `--stack-version` is passed, skip rule
308-
types which are unsupported (an error will
309-
be raised otherwise)
310-
--include-metadata Add metadata to the exported rules
311-
-ac, --include-action-connectors
312-
Include Action Connectors in export
313-
-e, --include-exceptions Include Exceptions Lists in export
314-
-h, --help Show this message and exit.
315-
```
316-
317-
_*To load a custom rule, the proper index must be setup first. The simplest way to do this is to click
318-
the `Load prebuilt detection rules and timeline templates` button on the `detections` page in the Kibana security app._
319-
320-
321-
### Using `import-rules`
322-
323-
This is a better option than `upload-rule` as it is built on refreshed APIs
234+
To directly load Toml formatted rule files into Kibana, one can use the `kibana import-rules` command as shown below.
324235

325236
```
326237
python -m detection_rules kibana import-rules -h
@@ -333,10 +244,8 @@ Kibana client:
333244
Options:
334245
--ignore-ssl-errors TEXT
335246
--space TEXT Kibana space
336-
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-
337-
saml (for SSO)
338-
--provider-type TEXT Elastic Cloud providers: basic and saml (for
339-
SSO)
247+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)
248+
--provider-type TEXT Elastic Cloud providers: basic and saml (for SSO)
340249
-ku, --kibana-user TEXT
341250
--kibana-url TEXT
342251
-kp, --kibana-password TEXT
@@ -355,8 +264,7 @@ Options:
355264
-o, --overwrite Overwrite existing rules
356265
-e, --overwrite-exceptions Overwrite exceptions in existing rules
357266
-ac, --overwrite-action-connectors
358-
Overwrite action connectors in existing
359-
rules
267+
Overwrite action connectors in existing rules
360268
-h, --help Show this message and exit.
361269
```
362270

@@ -499,6 +407,78 @@ python -m detection_rules kibana import-rules -d test-export-rules -o
499407

500408
</details>
501409

410+
### Using `export-rules-from-repo`
411+
412+
Toml formatted rule files can also be imported into Kibana through Kibana security app via a consolidated ndjson file
413+
which is exported from detection rules.
414+
415+
```console
416+
Usage: detection_rules export-rules-from-repo [OPTIONS]
417+
418+
Export rule(s) and exception(s) into an importable ndjson file.
419+
420+
Options:
421+
-f, --rule-file FILE
422+
-d, --directory DIRECTORY Recursively load rules from a directory
423+
-id, --rule-id TEXT
424+
-o, --outfile PATH Name of file for exported rules
425+
-r, --replace-id Replace rule IDs with new IDs before export
426+
--stack-version [7.8|7.9|7.10|7.11|7.12|7.13|7.14|7.15|7.16|8.0|8.1|8.2|8.3|8.4|8.5|8.6|8.7|8.8|8.9|8.10|8.11|8.12|8.13|8.14]
427+
Downgrade a rule version to be compatible
428+
with older instances of Kibana
429+
-s, --skip-unsupported If `--stack-version` is passed, skip rule
430+
types which are unsupported (an error will
431+
be raised otherwise)
432+
--include-metadata Add metadata to the exported rules
433+
-ac, --include-action-connectors
434+
Include Action Connectors in export
435+
-e, --include-exceptions Include Exceptions Lists in export
436+
-h, --help Show this message and exit.
437+
```
438+
439+
_*To load a custom rule, the proper index must be setup first. The simplest way to do this is to click
440+
the `Load prebuilt detection rules and timeline templates` button on the `detections` page in the Kibana security app._
441+
442+
443+
### Deprecated Methods
444+
445+
Toml formatted rule files can also be uploaded as custom rules using the `kibana upload-rule` command. This command is
446+
deprecated as of Elastic Stack version 9.0, but is included for compatibility with older stacks. To upload more than one
447+
file, specify multiple files at a time as individual args. This command is meant to support uploading and testing of
448+
rules and is not intended for production use in its current state.
449+
450+
```console
451+
python -m detection_rules kibana upload-rule -h
452+
453+
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
454+
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
455+
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
456+
457+
Kibana client:
458+
Options:
459+
--ignore-ssl-errors TEXT
460+
--space TEXT Kibana space
461+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)
462+
--provider-type TEXT Elastic Cloud providers: basic and saml (for SSO)
463+
-ku, --kibana-user TEXT
464+
--kibana-url TEXT
465+
-kp, --kibana-password TEXT
466+
-kc, --kibana-cookie TEXT Cookie from an authed session
467+
--api-key TEXT
468+
--cloud-id TEXT ID of the cloud instance.
469+
470+
Usage: detection_rules kibana upload-rule [OPTIONS]
471+
472+
[Deprecated] Upload a list of rule .toml files to Kibana.
473+
474+
Options:
475+
-f, --rule-file FILE
476+
-d, --directory DIRECTORY Recursively load rules from a directory
477+
-id, --rule-id TEXT
478+
-r, --replace-id Replace rule IDs with new IDs before export
479+
-h, --help Show this message and exit.
480+
```
481+
502482
### Exporting rules
503483

504484
This command should be run with the `CUSTOM_RULES_DIR` envvar set, that way proper validation is applied to versioning when the rules are downloaded. See the [custom rules docs](docs/custom-rules.md) for more information.
@@ -514,10 +494,8 @@ Kibana client:
514494
Options:
515495
--ignore-ssl-errors TEXT
516496
--space TEXT Kibana space
517-
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-
518-
saml (for SSO)
519-
--provider-type TEXT Elastic Cloud providers: basic and saml (for
520-
SSO)
497+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)
498+
--provider-type TEXT Elastic Cloud providers: basic and saml (for SSO)
521499
-ku, --kibana-user TEXT
522500
--kibana-url TEXT
523501
-kp, --kibana-password TEXT

detection_rules/kbwrap.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ def kibana_group(ctx: click.Context, **kibana_kwargs):
5151
@click.option('--replace-id', '-r', is_flag=True, help='Replace rule IDs with new IDs before export')
5252
@click.pass_context
5353
def upload_rule(ctx, rules: RuleCollection, replace_id):
54-
"""Upload a list of rule .toml files to Kibana."""
54+
"""[Deprecated] Upload a list of rule .toml files to Kibana."""
5555
kibana = ctx.obj['kibana']
5656
api_payloads = []
5757

58+
click.secho(
59+
"WARNING: This command is deprecated as of Elastic Stack version 9.0. Please use `kibana import-rules`.",
60+
fg="yellow",
61+
)
62+
5863
for rule in rules:
5964
try:
6065
payload = downgrade_contents_from_rule(rule, kibana.version, replace_id=replace_id)

detection_rules/ml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def setup_bundle(ctx, model_tag, repo, model_dir):
383383
click.echo(table)
384384

385385
click.echo('Associated rules and jobs can be found under ML-experimental-detections releases in the repo')
386-
click.echo('To upload rules, run: kibana upload-rule <ml-rule.toml>')
386+
click.echo('To upload rules, run: kibana import-rules -f <ml-rule.toml>')
387387
click.echo('To upload ML jobs, run: es experimental upload-ml-job <ml-job.json>')
388388

389389

docs/experimental-machine-learning/experimental-detections.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Rules are now stored in ndjson format and can be imported into Kibana via the se
1414

1515
Earlier releases stored the rules in toml format. These can be uploaded using the
1616
[7.12 branch](https://github.com/elastic/detection-rules/tree/7.12) CLI using the
17-
[kibana upload-rule](../../CLI.md#uploading-rules-to-kibana) command
17+
[kibana import-rules](../../CLI.md#uploading-rules-to-kibana) command
1818

1919
### Uploading ML Jobs and Datafeeds
2020

generate_rules.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import glob
2+
import os
3+
import pytoml as toml
4+
import pandas as pd
5+
6+
# List to store file paths and rule IDs
7+
data = []
8+
9+
# Iterate over all TOML files in the directory and its subdirectories
10+
for file in glob.glob('rules/**/*.toml', recursive=True):
11+
# Load the TOML file
12+
print(f"Processing {file}")
13+
try:
14+
with open(file, 'r') as f:
15+
toml_data = toml.load(f)
16+
17+
# Extract the rule ID
18+
rule_id = toml_data.get('rule', {}).get('rule_id', None)
19+
20+
# Append the file path and rule ID to the list
21+
if rule_id:
22+
data.append({'file_path': file, 'rule_id': rule_id})
23+
except (toml.TomlError, IndexError) as e:
24+
print(f"Error processing {file}: {e}")
25+
26+
# Create a pandas DataFrame from the list
27+
df = pd.DataFrame(data)
28+
29+
# Write the DataFrame to a CSV file
30+
df.to_csv('rules_with_ids.csv', index=False)
31+
32+
print("CSV file 'rules_with_ids.csv' has been created.")
33+

0 commit comments

Comments
 (0)