Skip to content

Commit

Permalink
[Bug] Version Comparison Bug in Related Integrations Field at Build T…
Browse files Browse the repository at this point in the history
…ime (#2331)

* addresses version comparison bug for related_integrations field during build

* Update detection_rules/misc.py

Co-authored-by: Mika Ayenson <[email protected]>

* Update detection_rules/misc.py

Co-authored-by: Mika Ayenson <[email protected]>

* Update detection_rules/integrations.py

Co-authored-by: Mika Ayenson <[email protected]>

* addressed package version loading bug

* addressed flake errors

* adjusted find_least_compatible_version function to address sorting and semantic version comparison

* adjusted major version comparison in compare_versions sub function

* removed compare_versions sub function and included logic in iteration

* Update detection_rules/integrations.py

Co-authored-by: Mika Ayenson <[email protected]>

* Update detection_rules/integrations.py

Co-authored-by: Mika Ayenson <[email protected]>

* added OrderedDict to version and manifest iteration to enforce sorted dict object

Co-authored-by: Mika Ayenson <[email protected]>

(cherry picked from commit 4abd3b8)
  • Loading branch information
terrancedejesus authored and github-actions[bot] committed Sep 29, 2022
1 parent 6d35e44 commit bb19d46
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions detection_rules/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import json
import os
import re
from collections import OrderedDict
from pathlib import Path
from typing import Union

import requests
from marshmallow import EXCLUDE, Schema, fields, post_load
Expand Down Expand Up @@ -63,32 +63,28 @@ def build_integrations_manifest(overwrite: bool, rule_integrations: list) -> Non


def find_least_compatible_version(package: str, integration: str,
current_stack_version: str, packages_manifest: dict) -> Union[str, None]:
current_stack_version: str, packages_manifest: dict) -> str:
"""Finds least compatible version for specified integration based on stack version supplied."""
integration_manifests = {k: v for k, v in sorted(packages_manifest[package].items(), key=Version)}
integration_manifests = {k: v for k, v in sorted(packages_manifest[package].items(),
key=lambda x: Version(str(x[0])))}

# trim integration_manifests to only the latest major entries
# filter integration_manifests to only the latest major entries
max_major, *_ = max([Version(manifest_version) for manifest_version in integration_manifests])
latest_major_integration_manifests = \
{k: v for k, v in integration_manifests.items() if Version(k)[0] == max_major}

def compare_versions(int_ver: str, pkg_ver: str) -> bool:
"""Compares integration and package version"""
pkg_major, pkg_minor = Version(pkg_ver)
integration_major, integration_minor = Version(int_ver)[:2]

if int(integration_major) < int(pkg_major) or int(pkg_major) > int(integration_major):
return False

compatible = Version(int_ver) <= Version(pkg_ver)
return compatible

for version, manifest in latest_major_integration_manifests.items():
for kibana_compat_vers in re.sub(r"\>|\<|\=|\^", "", manifest["conditions"]["kibana"]["version"]).split(" || "):
if compare_versions(kibana_compat_vers, current_stack_version):
return f"^{version}"
print(f"no compatible version for integration {package}:{integration}")
return None
# iterates through ascending integration manifests
# returns latest major version that is least compatible
for version, manifest in OrderedDict(sorted(latest_major_integration_manifests.items(),
key=lambda x: Version(str(x[0])))).items():
compatible_versions = re.sub(r"\>|\<|\=|\^", "", manifest["conditions"]["kibana"]["version"]).split(" || ")
for kibana_ver in compatible_versions:
# check versions have the same major
if int(kibana_ver[0]) == int(current_stack_version[0]):
if Version(kibana_ver) <= Version(current_stack_version + ".0"):
return f"^{version}"

raise ValueError(f"no compatible version for integration {package}:{integration}")


def get_integration_manifests(integration: str) -> list:
Expand Down

0 comments on commit bb19d46

Please sign in to comment.