-
Notifications
You must be signed in to change notification settings - Fork 0
Adding GitHub Audit Log Extractor #16
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 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f66f52d
Adding Audit Log Extractor
0b06d76
Merge branch 'master' into adding-audit-log
rreddy2 625f3f6
updating events -> actions
d7a098d
Merge branch 'adding-audit-log' of github.com:nodestream-proj/nodestr…
9bdf109
formatting changes
2a8d38f
formatting changes
8d10651
updating tests
62d9015
adding cov to makefile and splitting url to 2 lines
d69f765
adding support for lookback_period and changing timestamp key
f26ac7a
updating code to resolve tests
33250f2
updating formatting
6e9a808
format
5721f9b
more formatting
e91cd54
updating pipeline
08b1850
making changes based on comments
671cc1d
Merge branch 'master' into adding-audit-log
rreddy2 02b147d
keeping everything consistent
a187da3
Merge branch 'master' into adding-audit-log
rreddy2 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 |
|---|---|---|
|
|
@@ -28,3 +28,6 @@ lint: fmt | |
| test: | ||
| poetry run pytest | ||
|
|
||
| .PHONY: coverage | ||
| coverage: | ||
| poetry run coverage html | ||
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,47 @@ | ||
| """ | ||
| Nodestream Extractor that extracts audit logs from the GitHub REST API. | ||
|
|
||
| Developed using Enterprise Server 3.12 | ||
| https://docs.github.com/en/enterprise-server@3.12/rest?apiVersion=2022-11-28 | ||
| """ | ||
|
|
||
| from collections.abc import AsyncGenerator | ||
|
|
||
| from nodestream.pipeline import Extractor | ||
|
|
||
| from .client import GithubRestApiClient | ||
| from .logging import get_plugin_logger | ||
| from .types import GithubAuditLog | ||
|
|
||
| logger = get_plugin_logger(__name__) | ||
|
|
||
|
|
||
| class GithubAuditLogExtractor(Extractor): | ||
| """ | ||
| Extracts audit logs from the GitHub REST API. | ||
| You can pass the enterprise_name, actions and lookback_period to the extractor | ||
| along with the regular GitHub parameters. | ||
|
|
||
| lookback_period can contain keys for days, months, and/or years as ints | ||
| actions can be found in the GitHub documentation | ||
| https://docs.github.com/en/enterprise-cloud@latest/admin/monitoring-activity-in-your-enterprise/reviewing-audit-logs-for-your-enterprise/searching-the-audit-log-for-your-enterprise#search-based-on-the-action-performed | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| enterprise_name: str, | ||
| actions: list[str] | None = None, | ||
| lookback_period: dict[str, int] | None = None, | ||
| **github_client_kwargs: any, | ||
| ): | ||
| self.enterprise_name = enterprise_name | ||
| self.client = GithubRestApiClient(**github_client_kwargs) | ||
| self.lookback_period = lookback_period | ||
| self.actions = actions | ||
|
|
||
| async def extract_records(self) -> AsyncGenerator[GithubAuditLog]: | ||
| async for audit in self.client.fetch_enterprise_audit_log( | ||
| self.enterprise_name, self.actions, self.lookback_period | ||
| ): | ||
| audit["timestamp"] = audit.pop("@timestamp") | ||
| yield audit | ||
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,26 @@ | ||
| - implementation: nodestream_github:GithubAuditLogExtractor | ||
| arguments: | ||
| github_hostname: !config 'github_hostname' | ||
| auth_token: !config 'auth_token' | ||
| user_agent: !config 'user_agent' | ||
| enterprise_name: 'test-enterprise' | ||
| actions: | ||
| - protected_branch.create | ||
| - repo.download_zip | ||
| lookback_period: | ||
| days: 1 | ||
|
|
||
| - implementation: nodestream.interpreting:Interpreter | ||
| arguments: | ||
| interpretations: | ||
| - type: source_node | ||
| node_type: BranchProtectionPolicyChange | ||
| key: | ||
| timestamp: !jmespath 'timestamp' | ||
| actor: !jmespath 'actor' | ||
| action: !jmespath 'action' | ||
| - type: properties | ||
| properties: | ||
| org: !jmespath 'org' | ||
| repo: !jmespath 'repo' | ||
| created_at: !jmespath 'created_at' |
rreddy2 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,71 @@ | ||
| from nodestream_github.types import GithubAuditLog | ||
|
|
||
|
|
||
| def audit() -> GithubAuditLog: | ||
| return [ | ||
| { | ||
| "@timestamp": 1606929874512, | ||
| "action": "team.add_member", | ||
| "actor": "octocat", | ||
| "created_at": 1606929874512, | ||
| "_document_id": "xJJFlFOhQ6b-5vaAFy9Rjw", | ||
| "org": "octo-corp", | ||
| "team": "octo-corp/example-team", | ||
| "user": "monalisa", | ||
| }, | ||
| { | ||
| "@timestamp": 1606507117008, | ||
| "action": "org.create", | ||
| "actor": "octocat", | ||
| "created_at": 1606507117008, | ||
| "_document_id": "Vqvg6kZ4MYqwWRKFDzlMoQ", | ||
| "org": "octocat-test-org", | ||
| }, | ||
| { | ||
| "@timestamp": 1605719148837, | ||
| "action": "repo.destroy", | ||
| "actor": "monalisa", | ||
| "created_at": 1605719148837, | ||
| "_document_id": "LwW2vpJZCDS-WUmo9Z-ifw", | ||
| "org": "mona-org", | ||
| "repo": "mona-org/mona-test-repo", | ||
| "visibility": "private", | ||
| }, | ||
| ] | ||
rreddy2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def audit_expected_output() -> GithubAuditLog: | ||
| return [ | ||
| { | ||
| "timestamp": 1606929874512, | ||
| "action": "team.add_member", | ||
| "actor": "octocat", | ||
| "created_at": 1606929874512, | ||
| "_document_id": "xJJFlFOhQ6b-5vaAFy9Rjw", | ||
| "org": "octo-corp", | ||
| "team": "octo-corp/example-team", | ||
| "user": "monalisa", | ||
| }, | ||
| { | ||
| "timestamp": 1606507117008, | ||
| "action": "org.create", | ||
| "actor": "octocat", | ||
| "created_at": 1606507117008, | ||
| "_document_id": "Vqvg6kZ4MYqwWRKFDzlMoQ", | ||
| "org": "octocat-test-org", | ||
| }, | ||
| { | ||
| "timestamp": 1605719148837, | ||
| "action": "repo.destroy", | ||
| "actor": "monalisa", | ||
| "created_at": 1605719148837, | ||
| "_document_id": "LwW2vpJZCDS-WUmo9Z-ifw", | ||
| "org": "mona-org", | ||
| "repo": "mona-org/mona-test-repo", | ||
| "visibility": "private", | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| GITHUB_AUDIT = audit() | ||
| GITHUB_EXPECTED_OUTPUT = audit_expected_output() | ||
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,32 @@ | ||
| import pytest | ||
|
|
||
| from nodestream_github import GithubAuditLogExtractor | ||
| from tests.data.audit import GITHUB_AUDIT, GITHUB_EXPECTED_OUTPUT | ||
| from tests.mocks.githubrest import ( | ||
| DEFAULT_HOSTNAME, | ||
| DEFAULT_PER_PAGE, | ||
| GithubHttpxMock, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def audit_client() -> GithubAuditLogExtractor: | ||
rreddy2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return GithubAuditLogExtractor( | ||
| auth_token="test-token", | ||
| github_hostname=DEFAULT_HOSTNAME, | ||
| user_agent="test-agent", | ||
| max_retries=0, | ||
| per_page=DEFAULT_PER_PAGE, | ||
| enterprise_name="test-enterprise", | ||
| actions=["protected_branch.create"], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_audit( | ||
| audit_client: GithubAuditLogExtractor, gh_rest_mock: GithubHttpxMock | ||
| ): | ||
| gh_rest_mock.get_enterprise_audit_logs(status_code=200, json=GITHUB_AUDIT) | ||
|
|
||
| all_records = [record async for record in audit_client.extract_records()] | ||
| assert all_records == GITHUB_EXPECTED_OUTPUT | ||
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.
Uh oh!
There was an error while loading. Please reload this page.