-
Notifications
You must be signed in to change notification settings - Fork 4
fix: Tranform PyPi package version to BHE-friendly SemVer in API user agent - BED-9332 #68
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3191fb
fix: Convert pypi package version reporting to SemVer for BHE
definitelynotagoblin 496a130
fix: Add support for -dev0 local builds
definitelynotagoblin 838ee96
fix: Pull latest tag in CI for version tests
definitelynotagoblin 5b9d9ab
chore: Allow optional '.' for rc suffix
definitelynotagoblin be448eb
fix: Scrub -dev suffix from output
definitelynotagoblin 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 |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ jobs: | |
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to get a version tag, otherwise this resolves to version |
||
|
|
||
| - name: Set up python | ||
| uses: actions/setup-python@v5 | ||
|
|
@@ -56,6 +58,10 @@ jobs: | |
| run: | | ||
| .venv/bin/pytest tests/test_extensions_format.py -v | ||
|
|
||
| - name: Run BHE version boundary tests | ||
| run: | | ||
| .venv/bin/pytest tests/test_bhe_version.py -v | ||
|
|
||
| - name: Run BHE job scheduling test | ||
| run: | | ||
| .venv/bin/pytest tests/test_bhe_job_scheduling.py -v | ||
|
|
||
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,31 @@ | ||
| import re | ||
|
|
||
| _BHE_PACKAGE_VERSION = re.compile( | ||
| r"^(?P<major>0|[1-9]\d*)" | ||
| r"\.(?P<minor>0|[1-9]\d*)" | ||
| r"\.(?P<patch>0|[1-9]\d*)" | ||
| r"(?:\.?rc(?P<rc>0|[1-9]\d*))?" | ||
| r"(?:\.?dev(?P<dev>0|[1-9]\d*))?$" | ||
| ) | ||
|
|
||
|
|
||
| class UnsupportedBHEVersion(ValueError): | ||
| """Raised when the package version cannot be represented safely for BHE.""" | ||
|
|
||
|
|
||
| def render_bhe_version(package_version: str) -> str: | ||
| """Convert a supported Python package version to BHE wire format.""" | ||
| match = _BHE_PACKAGE_VERSION.fullmatch(package_version) | ||
| if match is None: | ||
| raise UnsupportedBHEVersion( | ||
| "OpenHound package version " | ||
| f"{package_version!r} cannot be reported to BloodHound Enterprise; " | ||
| "supported forms are MAJOR.MINOR.PATCH[rcN]" | ||
| ) | ||
|
|
||
| release = f"v{match.group('major')}.{match.group('minor')}.{match.group('patch')}" | ||
| prerelease = [] | ||
| if (rc := match.group("rc")) is not None: | ||
| prerelease.append(f"rc{rc}") | ||
|
|
||
| return f"{release}-{'.'.join(prerelease)}" if prerelease else release |
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
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,86 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from openhound.core.clients import bloodhound | ||
| from openhound.core.clients.bhe_version import ( | ||
| UnsupportedBHEVersion, | ||
| render_bhe_version, | ||
| ) | ||
| from openhound.core.clients.bloodhound import BloodHound, BloodHoundJWT | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("package_version", "reported_version"), | ||
| [ | ||
| ("0.3.0", "v0.3.0"), | ||
| ("0.3.0rc1", "v0.3.0-rc1"), | ||
| ("0.3.0.rc1", "v0.3.0-rc1"), | ||
| ("0.3.0dev1", "v0.3.0"), | ||
| ("0.3.0.dev0", "v0.3.0"), | ||
| ("0.3.0rc2.dev3", "v0.3.0-rc2"), | ||
| ("12.34.56rc10", "v12.34.56-rc10"), | ||
| ], | ||
| ) | ||
| def test_render_bhe_version(package_version, reported_version): | ||
| assert render_bhe_version(package_version) == reported_version | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "package_version", | ||
| [ | ||
| "unknown", | ||
| "", | ||
| "0.3", | ||
| "v0.3.0", | ||
| "0.3.0-rc1", | ||
| "0.3.0a1", | ||
| "0.3.0b1", | ||
| "0.3.0.post1", | ||
| "0.3.0+container.1", | ||
| "1!0.3.0", | ||
| "01.3.0", | ||
| ], | ||
| ) | ||
| def test_render_bhe_version_rejects_unsupported_versions(package_version): | ||
| with pytest.raises( | ||
| UnsupportedBHEVersion, | ||
| match="supported forms are MAJOR.MINOR.PATCH", | ||
| ): | ||
| render_bhe_version(package_version) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("client_factory", "authorization"), | ||
| [ | ||
| ( | ||
| lambda: BloodHound(token_key="key", token_id="id"), | ||
| "bhesignature id", | ||
| ), | ||
| ( | ||
| lambda: BloodHoundJWT(token="jwt"), | ||
| "Bearer jwt", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_bhe_clients_use_reported_version(monkeypatch, client_factory, authorization): | ||
| monkeypatch.setattr(bloodhound.openhound, "__version__", "0.3.0rc1") | ||
| request = Mock(return_value=Mock(status_code=200)) | ||
| monkeypatch.setattr(bloodhound.requests, "request", request) | ||
|
|
||
| client_factory().request("GET", "/test") | ||
|
|
||
| headers = request.call_args.kwargs["headers"] | ||
| assert headers["User-Agent"] == "openhound/v0.3.0-rc1" | ||
| assert headers["Authorization"] == authorization | ||
|
|
||
|
|
||
| def test_bhe_client_rejects_unsupported_version_before_request(monkeypatch): | ||
| monkeypatch.setattr(bloodhound.openhound, "__version__", "unknown") | ||
| request = Mock() | ||
| monkeypatch.setattr(bloodhound.requests, "request", request) | ||
|
|
||
| with pytest.raises(UnsupportedBHEVersion, match="'unknown'"): | ||
| BloodHound(token_key="key", token_id="id") | ||
|
|
||
| request.assert_not_called() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: SpecterOps/OpenHound
Length of output: 4145
🌐 Web query:
GitHub actions/checkout v4 persist-credentials default local git config official README💡 Result:
In the official GitHub actions/checkout v4 documentation, the persist-credentials input has a default value of true [1][2]. When persist-credentials is set to true (the default), the action configures the provided authentication token or SSH key within the local Git configuration of the repository [3][4]. This allows subsequent steps in your workflow job to execute authenticated Git commands using those credentials [3][4]. The credentials are automatically removed during the post-job cleanup process [3][4]. To opt out of this behavior, you can explicitly set persist-credentials to false [3][5]. This is often done to prevent the action from configuring these credentials or if you intend to manage Git authentication manually in later steps [4].
Citations:
🏁 Script executed:
Repository: SpecterOps/OpenHound
Length of output: 23275
🏁 Script executed:
Repository: SpecterOps/OpenHound
Length of output: 2567
Disable persisted checkout credentials.
This workflow runs pull-request code after checkout. That code can read the local Git configuration and access
GITHUB_TOKEN. Setpersist-credentials: falseunless authenticated Git operations are required.🧰 Tools
🪛 zizmor (1.29.0)
[warning] 25-28: credential persistence through GitHub Actions artifacts (artipacked): does not set persist-credentials: false
(artipacked)
🤖 Prompt for AI Agents
Source: Linters/SAST tools