-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(flags): Store options changes in the audit log #78622
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
cmanallen
merged 11 commits into
master
from
cmanallen/flags-capture-audit-log-of-options-changes
Oct 7, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b2d50da
Add audit log presenter class
cmanallen 726af10
Add internal flag pole provider
cmanallen 4856391
Fix type errors
cmanallen d409a55
Fix more type errors
cmanallen ba18fc1
Register audit log presenter
cmanallen 9e6b95f
Use -1 as sentinel value
cmanallen d01d4a7
Use none
cmanallen 9a5146e
Remove reference to urls
cmanallen 07eda36
Rip out flag pole API interfaces
cmanallen 957d05a
Merge branch 'master' into cmanallen/flags-capture-audit-log-of-optio…
cmanallen 8ca4309
Move import
cmanallen 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
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,85 @@ | ||
import datetime | ||
from typing import Any, TypedDict | ||
|
||
from sentry.flags.models import ACTION_MAP, CREATED_BY_TYPE_MAP, FlagAuditLogModel | ||
|
||
|
||
def write(rows: list["FlagAuditLogRow"]) -> None: | ||
FlagAuditLogModel.objects.bulk_create(FlagAuditLogModel(**row) for row in rows) | ||
|
||
|
||
"""Provider definitions. | ||
|
||
Provider definitions are pure functions. They accept data and return data. Providers do not | ||
initiate any IO operations. Instead they return commands in the form of the return type or | ||
an exception. These commands inform the caller (the endpoint defintion) what IO must be | ||
emitted to satisfy the request. This is done primarily to improve testability and test | ||
performance but secondarily to allow easy extension of the endpoint without knowledge of | ||
the underlying systems. | ||
""" | ||
|
||
|
||
class FlagAuditLogRow(TypedDict): | ||
"""A complete flag audit log row instance.""" | ||
|
||
action: int | ||
created_at: datetime.datetime | ||
created_by: str | ||
created_by_type: int | ||
flag: str | ||
organization_id: int | ||
tags: dict[str, Any] | ||
|
||
|
||
class DeserializationError(Exception): | ||
"""The request body could not be deserialized.""" | ||
|
||
def __init__(self, errors): | ||
self.errors = errors | ||
|
||
|
||
class InvalidProvider(Exception): | ||
"""An unsupported provider type was specified.""" | ||
|
||
... | ||
|
||
|
||
def handle_provider_event( | ||
provider: str, | ||
request_data: dict[str, Any], | ||
organization_id: int, | ||
) -> list[FlagAuditLogRow]: | ||
raise InvalidProvider(provider) | ||
|
||
|
||
"""Internal flag-pole provider. | ||
|
||
Allows us to skip the HTTP endpoint. | ||
""" | ||
|
||
|
||
class FlagAuditLogItem(TypedDict): | ||
"""A simplified type which is easier to work with than the row definition.""" | ||
|
||
action: str | ||
flag: str | ||
created_at: datetime.datetime | ||
created_by: str | ||
tags: dict[str, str] | ||
|
||
|
||
def handle_flag_pole_event_internal(items: list[FlagAuditLogItem], organization_id: int) -> None: | ||
write( | ||
[ | ||
{ | ||
"action": ACTION_MAP[item["action"]], | ||
"created_at": item["created_at"], | ||
"created_by": item["created_by"], | ||
"created_by_type": CREATED_BY_TYPE_MAP["name"], | ||
"flag": item["flag"], | ||
"organization_id": organization_id, | ||
"tags": item["tags"], | ||
} | ||
for item in items | ||
] | ||
) |
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
45 changes: 45 additions & 0 deletions
45
src/sentry/runner/commands/presenters/audit_log_presenter.py
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,45 @@ | ||
import itertools | ||
import logging | ||
from datetime import datetime, timezone | ||
|
||
from sentry import options | ||
from sentry.flags.providers import FlagAuditLogItem, handle_flag_pole_event_internal | ||
from sentry.runner.commands.presenters.webhookpresenter import WebhookPresenter | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
class AuditLogPresenter(WebhookPresenter): | ||
@staticmethod | ||
def is_webhook_enabled() -> bool: | ||
return ( | ||
options.get("flags:options-audit-log-is-enabled") is True | ||
and options.get("flags:options-audit-log-organization-id") is not None | ||
) | ||
|
||
def flush(self) -> None: | ||
if not self.is_webhook_enabled(): | ||
logger.warning("Options audit log webhook is disabled.") | ||
return None | ||
|
||
items = self._create_audit_log_items() | ||
handle_flag_pole_event_internal( | ||
items, organization_id=options.get("flags:options-audit-log-organization-id") | ||
) | ||
|
||
def _create_audit_log_items(self) -> list[FlagAuditLogItem]: | ||
return [ | ||
{ | ||
"action": action, | ||
"created_at": datetime.now(tz=timezone.utc), | ||
"created_by": "internal", | ||
"flag": flag, | ||
"tags": tags, | ||
} | ||
for flag, action, tags in itertools.chain( | ||
((flag, "created", {"value": v}) for flag, v in self.set_options), | ||
((flag, "deleted", {}) for flag in self.unset_options), | ||
((flag, "updated", {"value": v}) for flag, _, v in self.updated_options), | ||
((flag, "updated", {}) for flag, _ in self.drifted_options), | ||
cmanallen 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.