Skip to content

fix: type hinting fixes and additional code checks #4790

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

Conversation

traut
Copy link
Contributor

@traut traut commented Jun 11, 2025

Pull Request

Issue link(s):

Summary - What I changed

  • adding ruff and pyright checks in CI workflow
  • making sure pyright has no complains

How To Test

Checklist

  • Added a label for the type of pr: bug, enhancement, schema, maintenance, Rule: New, Rule: Deprecation, Rule: Tuning, Hunt: New, or Hunt: Tuning so guidelines can be generated
  • Added the meta:rapid-merge label if planning to merge within 24 hours
  • Secret and sensitive material has been managed correctly
  • Automated testing was updated or added to match the most common scenarios
  • Documentation and comments were added for features that require explanation

Contributor checklist

@traut traut changed the title [WIP] Type hint fixes and adding code checks [WIP] fix: type hint fixes and adding code checks Jun 11, 2025
@traut traut added python Internal python for the repository ci/cd maintenance Internal changes minor labels Jun 17, 2025
@traut traut marked this pull request as ready for review June 17, 2025 16:32
Copy link
Contributor

Enhancement - Guidelines

These guidelines serve as a reminder set of considerations when addressing adding a new schema feature to the code.

Documentation and Context

  • Describe the feature enhancement in detail (alternative solutions, description of the solution, etc.) if not already documented in an issue.
  • Include additional context or screenshots.
  • Ensure the enhancement includes necessary updates to the documentation and versioning.

Code Standards and Practices

  • Code follows established design patterns within the repo and avoids duplication.
  • Code changes do not introduce new warnings or errors.
  • Variables and functions are well-named and descriptive.
  • Any unnecessary / commented-out code is removed.
  • Ensure that the code is modular and reusable where applicable.
  • Check for proper exception handling and messaging.

Testing

  • New unit tests have been added to cover the enhancement.
  • Existing unit tests have been updated to reflect the changes.
  • Provide evidence of testing and validating the enhancement (e.g., test logs, screenshots).
  • Validate that any rules affected by the enhancement are correctly updated.
  • Ensure that performance is not negatively impacted by the changes.
  • Verify that any release artifacts are properly generated and tested.

Additional Schema Related Checks

  • Ensure that the enhancement does not break existing functionality. (e.g., run make test-cli)
  • Review the enhancement with a peer or team member for additional insights.
  • Verify that the enhancement works across all relevant environments (e.g., different OS versions).
  • Confirm that all dependencies are up-to-date and compatible with the changes.
  • Link to the relevant Kibana PR or issue provided
  • Exported detection rule(s) from Kibana to showcase the feature(s)
  • Converted the exported ndjson file(s) to toml in the detection-rules repo
  • Re-exported the toml rule(s) to ndjson and re-imported into Kibana
  • Updated necessary unit tests to accommodate the feature
  • Applied min_compat restrictions to limit the feature to a specified minimum stack version
  • Executed all unit tests locally with a test toml rule to confirm passing
  • Included Kibana PR implementer as an optional reviewer for insights on the feature
  • Implemented requisite downgrade functionality
  • Cross-referenced the feature with product documentation for consistency
  • Incorporated a comprehensive test rule in unit tests for full schema coverage
  • Conducted system testing, including fleet, import, and create APIs (e.g., run make test-remote-cli)
  • Confirm that the proper version label is applied to the PR patch, minor, major.

@traut traut changed the title [WIP] fix: type hint fixes and adding code checks fix: type hinting fixes and additional code checks Jun 17, 2025
detection_rules/etc/*/* @mikaayenson @eric-forte-elastic @terrancedejesus
detection_rules/etc/packages.yaml @mikaayenson @eric-forte-elastic @traut
detection_rules/etc/*.json @mikaayenson @eric-forte-elastic @traut
detection_rules/etc/*/* @mikaayenson @eric-forte-elastic @traut
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
detection_rules/etc/*/* @mikaayenson @eric-forte-elastic @traut
detection_rules/etc/*/* @mikaayenson @eric-forte-elastic @traut
# exclude files from code owners
detection_rules/etc/non-ecs-schema.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per our team discussion in the team sync today.

query += " | LIMIT 10"
click.echo("No LIMIT detected in query. Added LIMIT 10 to truncate output.")
return query

def run_individual_query(self, query: str, wait_timeout: int):
def run_individual_query(self, query: str, _: int):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥂

@@ -65,13 +66,14 @@ def setUpClass(cls):
except Exception as e:
RULE_LOADER_FAIL = True
RULE_LOADER_FAIL_MSG = str(e)
raise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the message supposed to be rolled up instead of failing here?

config = '## Setup\n\n'
beats_integration_pattern = config + 'The {} Fleet integration, Filebeat module, or similarly ' \
'structured data is required to be compatible with this rule.'
config = "## Setup\n\n"
Copy link
Contributor

@Mikaayenson Mikaayenson Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just delete this test or is it a bug?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR I think we can delete this test.

I think the intent for this unittest.skip was similar to / the inverse of

    @unittest.skipIf(PACKAGE_STACK_VERSION < Version("8.3.0"),
                     "Test only applicable to 8.3+ stacks regarding related integrations build time field.")

Which were both added in 2429 to address

In 8.3, we added new build-time fields to our rules, specifically required_fields,related_integrations,setup. This feature request focuses solely on the related_integrations field.

At this time to determine which integrations to build the integrations manifest file, we rely on the integrations folder to determine this and then reference these names in package-storage. For matching, we rely solely on event.dataset fields in these integration queries

The issue:
We do not include the endpoint integration to this integrations manifest. In addition, we cannot rely on event.dataset, we need to look for the logs-endpoint* index for this.

Comment on lines +1418 to +1429
osquery_note_pattern = (
"> **Note**:\n> This investigation guide uses the [Osquery Markdown Plugin]"
"(https://www.elastic.co/guide/en/security/current/invest-guide-run-osquery.html) "
"introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display "
"unrendered Markdown in this guide."
)
invest_note_pattern = (
'> This investigation guide uses the [Investigate Markdown Plugin]'
'(https://www.elastic.co/guide/en/security/current/interactive-investigation-guides.html)'
' introduced in Elastic Stack version 8.8.0. Older Elastic Stack versions will display '
'unrendered Markdown in this guide.')
"> This investigation guide uses the [Investigate Markdown Plugin]"
"(https://www.elastic.co/guide/en/security/current/interactive-investigation-guides.html)"
" introduced in Elastic Stack version 8.8.0. Older Elastic Stack versions will display "
"unrendered Markdown in this guide."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should double check that when the transform occurs, its still formatted correctly.

print(f"Downloading beats {release_name}")
response = requests.get(url)

print(f"Downloaded {len(response.content) / 1024.0 / 1024.0:.2f} MB release.")

fs = {}
fs: dict[str, Any] = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we type hint the parsed field below

Comment on lines +282 to +285
# def get_schema_from_eql(tree: eql.ast.BaseNode, beats: list, version: str = None) -> dict:
# """Get a schema based on datasets and modules in an EQL AST."""
# datasets, modules = get_datasets_and_modules(tree)
# return get_schema_from_datasets(beats, modules, datasets, version=version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

Comment on lines +250 to +251
suggested_path: Path = Path(DEFAULT_PREBUILT_RULES_DIRS[0]) / contents["name"]
path = Path(path or input(f"File path for rule [{suggested_path}]: ") or suggested_path).resolve()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit odd to type hint as Path when we explicitly set as a Path object. We also dont type hint the next field path.

"""Format unit test names into expected format for direct calling."""
raw = [t.rsplit('.', maxsplit=2) for t in tests]
formatted = []
raw = [t.rsplit(".", maxsplit=2) for t in tests]
Copy link
Contributor

@Mikaayenson Mikaayenson Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raw = [t.rsplit(".", maxsplit=2) for t in tests]
raw: list[list[str]] = [t.rsplit(".", maxsplit=2) for t in tests]

?

Comment on lines +439 to +440
paths = [Path(c) for c in columns[1:]]
return cls(columns[0], *paths)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to clean this up?

worksheet.freeze_panes(1, 0)
worksheet.set_column(0, 0, 25)
worksheet.set_column(1, 1, 10)
worksheet = self.add_worksheet("Summary") # type: ignore[reportUnknownMemberType]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this to the function # type: ignore[reportUnknownMemberType] instead of inline.

"""Get schema for KQL."""
indexes = indexes or ()
converted = flatten_multi_fields(get_schema(version, name='ecs_flat'))
indexes = indexes or []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious as to why this was a tuple

@Mikaayenson
Copy link
Contributor

Mikaayenson commented Jun 17, 2025

  • Any reason why the build didn't run? Waiting for status to be reported
  • Note, I think we need to run the lint tests locally and add to this PR (since the workflow won't run until the action is on main)
  • We'll also want to open a maintenance window and test the backporting logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport: auto ci/cd Hunting maintenance Internal changes minor python Internal python for the repository schema
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants