Skip to content

Commit 5bf69b7

Browse files
authored
Update package and install process (elastic#1948)
1 parent 7e459dd commit 5bf69b7

20 files changed

+167
-47
lines changed

.github/workflows/backport.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393
- name: Install dependencies
9494
run: |
9595
python -m pip install --upgrade pip
96-
pip install -r requirements.txt -r requirements-dev.txt
96+
pip install .[dev]
9797
9898
- name: Prune non-${{matrix.target_branch}} rules
9999
env:

.github/workflows/get-target-branches.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
pip install -r requirements.txt
27+
pip install .[dev]
2828
2929
- id: get-branch-list
3030
run: |

.github/workflows/lock-versions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Install dependencies
3535
run: |
3636
python -m pip install --upgrade pip
37-
pip install -r requirements.txt -r requirements-dev.txt
37+
pip install .[dev]
3838
3939
- name: Build release package
4040
run: |

.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
pip install -r requirements.txt
25+
pip install .[dev]
2626
2727
- name: Python Lint
2828
run: |

.github/workflows/release-fleet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
run: |
5050
cd detection-rules
5151
python -m pip install --upgrade pip
52-
pip install -r requirements.txt -r requirements-dev.txt
52+
pip install .[dev]
5353
5454
- name: Build release package
5555
run: |

.github/workflows/release-kibana.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
run: |
4242
cd detection-rules
4343
python -m pip install --upgrade pip
44-
pip install -r requirements.txt -r requirements-dev.txt
44+
pip install .[dev]
4545
4646
- name: Build release package
4747
run: |

.gitmodules

Whitespace-only changes.

.pre-commit-config.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/PyCQA/flake8
5+
rev: 5.0.4
6+
hooks:
7+
- id: flake8
8+
args: ['--ignore=D203,C901,E501,W503', '--max-line-length=120','--max-complexity=10', '--statistics']
9+
exclude: '^rta|^kql'
10+
- repo: https://github.com/PyCQA/bandit
11+
rev: 1.7.4
12+
hooks:
13+
- id: bandit
14+
args: ['-s', 'B101,B603,B404,B607']
15+
exclude: '^rta|^kql'
16+
# Potential future rigor
17+
# - repo: https://github.com/PyCQA/pylint
18+
# rev: v2.15.6
19+
# hooks:
20+
# - id: pylint
21+
# language: system
22+
# exclude: '^rta|^kql'
23+
# - repo: https://github.com/PyCQA/isort
24+
# rev: 5.10.1
25+
# hooks:
26+
# - id: isort

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ all: release
1313

1414

1515
$(VENV):
16-
pip install virtualenv
16+
pip3 install virtualenv
1717
virtualenv $(VENV) --python=python3.8
18-
$(PIP) install -r requirements.txt
18+
$(PIP) install .[dev]
1919
$(PIP) install setuptools -U
2020

2121

@@ -25,7 +25,7 @@ clean:
2525

2626
.PHONY: deps
2727
deps: $(VENV)
28-
$(PIP) install -r requirements.txt
28+
$(PIP) install .[dev]
2929

3030

3131
.PHONY: pytest

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Detection Rules contains more than just static rule files. This repository also
3939

4040
Although rules can be added by manually creating `.toml` files, we don't recommend it. This repository also consists of a python module that aids rule creation and unit testing. Assuming you have Python 3.8+, run the below command to install the dependencies:
4141
```console
42-
$ pip install -r requirements.txt
42+
$ pip3 install ".[dev]"
4343
Collecting jsl==0.2.4
4444
Downloading jsl-0.2.4.tar.gz (21 kB)
4545
Collecting jsonschema==3.2.0

detection_rules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# 2.0.
55

66
"""Detection rules."""
7+
78
import sys
89

10+
911
assert (3, 8) <= sys.version_info < (4, 0), "Only Python 3.8+ supported"
1012

1113
from . import ( # noqa: E402

detection_rules/attack.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ def load_techniques_redirect() -> dict:
3131
def get_attack_file_path() -> str:
3232
pattern = 'attack-v*.json.gz'
3333
attack_file = get_etc_glob_path(pattern)
34-
if len(attack_file) != 1:
34+
if len(attack_file) < 1:
3535
raise FileNotFoundError(f'Missing required {pattern} file')
36+
elif len(attack_file) != 1:
37+
raise FileExistsError(f'Multiple files found with {pattern} pattern. Only one is allowed')
3638
return attack_file[0]
3739

3840

detection_rules/devtools.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import urllib.parse
1818
from collections import defaultdict
1919
from pathlib import Path
20-
from typing import Dict, Optional, Tuple, List
20+
from typing import Dict, List, Optional, Tuple
2121

2222
import click
2323
import requests.exceptions
@@ -26,22 +26,26 @@
2626
from eql.table import Table
2727

2828
from kibana.connector import Kibana
29+
2930
from . import attack, rule_loader, utils
30-
from .cli_utils import single_collection, multi_collection
31+
from .cli_utils import multi_collection, single_collection
3132
from .docs import IntegrationSecurityDocs
3233
from .endgame import EndgameSchemaManager
3334
from .eswrap import CollectEvents, add_range_to_dsl
3435
from .ghwrap import GithubClient, update_gist
36+
from .integrations import build_integrations_manifest
3537
from .main import root
3638
from .misc import PYTHON_LICENSE, add_client, client_error
37-
from .packaging import PACKAGE_FILE, RELEASE_DIR, CURRENT_RELEASE_PATH, Package, current_stack_version
38-
from .version_lock import VersionLockFile, default_version_lock
39-
from .rule import AnyRuleData, BaseRuleData, DeprecatedRule, QueryRuleData, ThreatMapping, TOMLRule
39+
from .packaging import (CURRENT_RELEASE_PATH, PACKAGE_FILE, RELEASE_DIR,
40+
Package, current_stack_version)
41+
from .rule import (AnyRuleData, BaseRuleData, DeprecatedRule, QueryRuleData,
42+
ThreatMapping, TOMLRule)
4043
from .rule_loader import RuleCollection, production_filter
4144
from .schemas import definitions, get_stack_versions
4245
from .semver import Version
43-
from .utils import dict_hash, get_path, get_etc_path, load_dump
44-
from .integrations import build_integrations_manifest
46+
from .utils import (dict_hash, get_etc_path, get_path, load_dump, save_etc_dump,
47+
load_etc_dump)
48+
from .version_lock import VersionLockFile, default_version_lock
4549

4650
RULES_DIR = get_path('rules')
4751
GH_CONFIG = Path.home() / ".config" / "gh" / "hosts.yml"
@@ -147,6 +151,45 @@ def build_integration_docs(ctx: click.Context, registry_version: str, pre: str,
147151
return docs
148152

149153

154+
@dev_group.command("bump-versions")
155+
@click.option("--major", is_flag=True, help="bump the major version")
156+
@click.option("--minor", is_flag=True, help="bump the minor version")
157+
@click.option("--patch", is_flag=True, help="bump the patch version")
158+
@click.option("--package", is_flag=True, help="Update the package version in the packages.yml file")
159+
@click.option("--kibana", is_flag=True, help="Update the kibana version in the packages.yml file")
160+
@click.option("--registry", is_flag=True, help="Update the registry version in the packages.yml file")
161+
def bump_versions(major, minor, patch, package, kibana, registry):
162+
"""Bump the versions"""
163+
164+
package_data = load_etc_dump('packages.yml')['package']
165+
ver = package_data["name"]
166+
new_version = Version(ver).bump(major, minor, patch)
167+
168+
kibana_version = f"^{new_version}.0" if not patch else f"^{new_version}"
169+
registry_version = f"{new_version}.0-dev.0" if not patch else f"{new_version}-dev.0"
170+
171+
# print the new versions
172+
click.echo(f"New package version: {new_version}")
173+
click.echo(f"New registry data version: {registry_version}")
174+
click.echo(f"New Kibana version: {kibana_version}")
175+
176+
if package:
177+
# update package version
178+
package_data["name"] = str(new_version)
179+
180+
if kibana:
181+
# update kibana version
182+
package_data["registry_data"]["conditions"]["kibana.version"] = kibana_version
183+
184+
if registry:
185+
# update registry version
186+
package_data["registry_data"]["version"] = registry_version
187+
# update packages.yml
188+
189+
if package or kibana or registry:
190+
save_etc_dump({"package": package_data}, "packages.yml")
191+
192+
150193
@dataclasses.dataclass
151194
class GitChangeEntry:
152195
status: str
@@ -696,6 +739,7 @@ def package_stats(ctx, token, threads):
696739
def search_rule_prs(ctx, no_loop, query, columns, language, token, threads):
697740
"""Use KQL or EQL to find matching rules from active GitHub PRs."""
698741
from uuid import uuid4
742+
699743
from .main import search_rules
700744

701745
all_rules: Dict[Path, TOMLRule] = {}
@@ -1044,7 +1088,9 @@ def rule_survey(ctx: click.Context, query, date_range, dump_file, hide_zero_coun
10441088
elasticsearch_client: Elasticsearch = None, kibana_client: Kibana = None):
10451089
"""Survey rule counts."""
10461090
from kibana.resources import Signal
1091+
10471092
from .main import search_rules
1093+
10481094
# from .eswrap import parse_unique_field_results
10491095

10501096
survey_results = []

detection_rules/etc/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
# or more contributor license agreements. Licensed under the Elastic License
3+
# 2.0; you may not use this file except in compliance with the Elastic License
4+
# 2.0.

detection_rules/ghwrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(self, token: Optional[str] = None):
147147
@classmethod
148148
def assert_github(cls):
149149
if not Github:
150-
raise ModuleNotFoundError('Missing PyGithub - try running `pip install -r requirements-dev.txt`')
150+
raise ModuleNotFoundError('Missing PyGithub - try running `pip3 install .[dev]`')
151151

152152
@property
153153
def authenticated_client(self) -> Github:

detection_rules/semver.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ def __str__(self):
3030

3131
return recovered_str
3232

33+
def bump(self, major: bool = False, minor: bool = False, patch: bool = False) -> 'Version':
34+
"""Increment the version."""
35+
versions = list(self)
36+
if major:
37+
versions[0] += 1
38+
if minor:
39+
versions[1] += 1
40+
if patch and len(versions) > 2:
41+
versions[-1] += 1
42+
elif patch and len(versions) == 2:
43+
versions.append(1)
44+
return Version(versions)
45+
3346

3447
def max_versions(*versions: str) -> str:
3548
"""Return the max versioned string."""

pyproject.toml

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
[project]
2+
name = "detection_rules"
3+
version = "0.1.0"
4+
description = "Detection Rules is the home for rules used by Elastic Security. This repository is used for the development, maintenance, testing, validation, and release of rules for Elastic Security’s Detection Engine."
5+
readme = "README.md"
6+
requires-python = ">=3.8"
7+
license = {file = "LICENSE.txt"}
8+
keywords = ["Detection Rules", "Continuous Monitoring", "Data Protection", "Elastic", "Elastic Endgame", "Endpoint Security"]
9+
classifiers = [
10+
"Topic :: Software Development :: Build Tools",
11+
"Operating System :: OS Independent",
12+
"Programming Language :: Python :: 3.8",
13+
"Programming Language :: Python :: 3.9",
14+
"Programming Language :: Python :: 3.10",
15+
"Programming Language :: Python :: 3.11",
16+
"Programming Language :: Python",
17+
"Topic :: Security",
18+
"Topic :: Software Development :: Libraries :: Python Modules",
19+
"Topic :: Software Development :: Libraries",
20+
"Topic :: Software Development :: Testing",
21+
"Topic :: Software Development",
22+
"Topic :: Utilities"
23+
]
24+
dependencies = [
25+
"Click~=8.1.0",
26+
"elasticsearch~=8.1",
27+
"eql==0.9.15",
28+
"jsl==0.2.4",
29+
"jsonschema==3.2.0",
30+
"marko",
31+
"marshmallow-dataclass[union]~=8.5.6",
32+
"marshmallow-jsonschema~=0.12.0",
33+
"marshmallow-union~=0.1.15",
34+
"marshmallow~=3.13.0",
35+
"pytoml",
36+
"PyYAML~=5.3",
37+
"requests~=2.27",
38+
"toml==0.10.0",
39+
"typing-inspect==0.7.1",
40+
"XlsxWriter~=1.3.6"
41+
]
42+
[project.optional-dependencies]
43+
dev = ["pep8-naming==0.7.0", "PyGithub==1.55", "flake8==3.8.1", "pyflakes==2.2.0", "pytest>=3.6", "pre-commit==2.20.0"]
44+
45+
[project.urls]
46+
"Homepage" = "https://github.com/elastic/detection-rules"
47+
"Bug Reports" = "https://github.com/elastic/detection-rules/issues"
48+
"Research" = "https://www.elastic.co/security-labs"
49+
"Elastic" = "https://www.elastic.co"
50+
51+
[tool.setuptools]
52+
package-data = {"kql" = ["*.g"]}
53+
packages = ["detection_rules", "kql", "kibana", "rta"]
54+
155
[build-system]
2-
requires = ["setuptools"]
56+
requires = ["setuptools", "wheel", "setuptools_scm"]
357
build-backend = "setuptools.build_meta"

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

requirements.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

setup.cfg

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)