-
Notifications
You must be signed in to change notification settings - Fork 401
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
fix: audit log integrations for versioned environments #4876
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7db9052
Ensure audit log events are sent to slack for new environment feature…
matthewelwell 7eb3c52
Add TODOs
matthewelwell 3db04b3
Add test and settings
matthewelwell f367f29
Add test for grafana integration with EnvironmentFeatureVersion and a…
matthewelwell ce1ae4c
Merge branch 'refs/heads/main' into fix/slack-integration-feature-ver…
matthewelwell ff27f89
Add logic to handle EnvironmentFeatureVersion in Dynatrace integration
matthewelwell 1734cf5
Fix dynatrace tests
matthewelwell 68a05de
Fix remaining tests
matthewelwell 0c3f1c1
Add pragma no cover
matthewelwell b4cce4d
Merge branch 'refs/heads/main' into fix/slack-integration-feature-ver…
matthewelwell 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 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 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 |
---|---|---|
|
@@ -18,3 +18,5 @@ | |
RETRY_WEBHOOKS = True | ||
|
||
INFLUXDB_BUCKET = "test_bucket" | ||
|
||
ENABLE_POSTPONE_DECORATOR = False |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import json | ||
|
||
import responses | ||
from pytest_django.fixtures import SettingsWrapper | ||
from pytest_mock import MockerFixture | ||
|
||
|
@@ -9,13 +12,19 @@ | |
send_audit_log_event_to_grafana, | ||
) | ||
from environments.models import Environment | ||
from features.models import Feature | ||
from features.versioning.models import EnvironmentFeatureVersion | ||
from integrations.dynatrace.dynatrace import EVENTS_API_URI | ||
from integrations.dynatrace.models import DynatraceConfiguration | ||
from integrations.grafana.grafana import ROUTE_API_ANNOTATIONS | ||
from integrations.grafana.models import ( | ||
GrafanaOrganisationConfiguration, | ||
GrafanaProjectConfiguration, | ||
) | ||
from organisations.models import Organisation, OrganisationWebhook | ||
from projects.models import Project | ||
from projects.tags.models import Tag | ||
from users.models import FFAdminUser | ||
from webhooks.webhooks import WebhookEventType | ||
|
||
|
||
|
@@ -99,15 +108,16 @@ def test_send_audit_log_event_to_grafana__project_grafana_config__calls_expected | |
project: Project, | ||
) -> None: | ||
# Given | ||
grafana_config = GrafanaProjectConfiguration(base_url="test.com", api_key="test") | ||
project.grafana_config = grafana_config | ||
audit_log_record = AuditLog.objects.create( | ||
project=project, | ||
related_object_type=RelatedObjectType.FEATURE.name, | ||
) | ||
grafana_wrapper_mock = mocker.patch("audit.signals.GrafanaWrapper", autospec=True) | ||
grafana_wrapper_instance_mock = grafana_wrapper_mock.return_value | ||
|
||
grafana_config = GrafanaProjectConfiguration(base_url="test.com", api_key="test") | ||
project.grafana_config = grafana_config | ||
Comment on lines
+118
to
+119
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. Having added the logic to prevent the postpone decorator from spawning unmanaged threads we need to move this here to prevent the generation of the audit log record itself from triggering an external request to Grafana (or Dynatrace as the case may be below). |
||
|
||
# When | ||
send_audit_log_event_to_grafana(AuditLog, audit_log_record) | ||
|
||
|
@@ -130,17 +140,18 @@ def test_send_audit_log_event_to_grafana__organisation_grafana_config__calls_exp | |
project: Project, | ||
) -> None: | ||
# Given | ||
grafana_config = GrafanaOrganisationConfiguration( | ||
base_url="test.com", api_key="test" | ||
) | ||
organisation.grafana_config = grafana_config | ||
audit_log_record = AuditLog.objects.create( | ||
project=project, | ||
related_object_type=RelatedObjectType.FEATURE.name, | ||
) | ||
grafana_wrapper_mock = mocker.patch("audit.signals.GrafanaWrapper", autospec=True) | ||
grafana_wrapper_instance_mock = grafana_wrapper_mock.return_value | ||
|
||
grafana_config = GrafanaOrganisationConfiguration( | ||
base_url="test.com", api_key="test" | ||
) | ||
organisation.grafana_config = grafana_config | ||
|
||
# When | ||
send_audit_log_event_to_grafana(AuditLog, audit_log_record) | ||
|
||
|
@@ -157,15 +168,68 @@ def test_send_audit_log_event_to_grafana__organisation_grafana_config__calls_exp | |
) | ||
|
||
|
||
@responses.activate | ||
def test_send_environment_feature_version_audit_log_event_to_grafana( | ||
tagged_feature: Feature, | ||
tag_one: Tag, | ||
tag_two: Tag, | ||
environment_v2_versioning: Environment, | ||
project: Project, | ||
organisation: Organisation, | ||
admin_user: FFAdminUser, | ||
) -> None: | ||
# Given | ||
_, audit_log_record = _create_and_publish_environment_feature_version( | ||
environment=environment_v2_versioning, | ||
feature=tagged_feature, | ||
user=admin_user, | ||
) | ||
|
||
base_url = "https://test.com" | ||
GrafanaOrganisationConfiguration.objects.create( | ||
base_url=base_url, api_key="test", organisation=organisation | ||
) | ||
|
||
responses.add( | ||
method=responses.POST, | ||
url=f"{base_url}{ROUTE_API_ANNOTATIONS}", | ||
status=200, | ||
json={ | ||
"message": "Annotation added", | ||
"id": 1, | ||
}, | ||
) | ||
|
||
# When | ||
send_audit_log_event_to_grafana(AuditLog, audit_log_record) | ||
|
||
# Then | ||
expected_time = int(audit_log_record.created_date.timestamp() * 1000) | ||
|
||
assert len(responses.calls) == 1 | ||
assert responses.calls[0].request.body == json.dumps( | ||
{ | ||
"tags": [ | ||
"flagsmith", | ||
f"project:{project.name}", | ||
f"environment:{environment_v2_versioning.name}", | ||
f"by:{admin_user.email}", | ||
f"feature:{tagged_feature.name}", | ||
tag_one.label, | ||
tag_two.label, | ||
], | ||
"text": audit_log_record.log, | ||
"time": expected_time, | ||
"timeEnd": expected_time, | ||
} | ||
) | ||
|
||
|
||
def test_send_audit_log_event_to_dynatrace__environment_dynatrace_config__calls_expected( | ||
mocker: MockerFixture, | ||
environment: Environment, | ||
) -> None: | ||
# Given | ||
dynatrace_config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
environment.refresh_from_db() | ||
audit_log_record = AuditLog.objects.create( | ||
environment=environment, | ||
related_object_type=RelatedObjectType.FEATURE.name, | ||
|
@@ -175,6 +239,10 @@ def test_send_audit_log_event_to_dynatrace__environment_dynatrace_config__calls_ | |
) | ||
dynatrace_wrapper_instance_mock = dynatrace_wrapper_mock.return_value | ||
|
||
dynatrace_config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
|
||
# When | ||
send_audit_log_event_to_dynatrace(AuditLog, audit_log_record) | ||
|
||
|
@@ -190,3 +258,71 @@ def test_send_audit_log_event_to_dynatrace__environment_dynatrace_config__calls_ | |
dynatrace_wrapper_instance_mock.track_event_async.assert_called_once_with( | ||
event=dynatrace_wrapper_instance_mock.generate_event_data.return_value | ||
) | ||
|
||
|
||
@responses.activate | ||
def test_send_environment_feature_version_audit_log_event_to_dynatrace( | ||
feature: Feature, | ||
environment_v2_versioning: Environment, | ||
project: Project, | ||
organisation: Organisation, | ||
admin_user: FFAdminUser, | ||
) -> None: | ||
# Given | ||
_, audit_log_record = _create_and_publish_environment_feature_version( | ||
environment=environment_v2_versioning, feature=feature, user=admin_user | ||
) | ||
|
||
base_url = "https://dynatrace.test.com" | ||
api_key = "api_123" | ||
DynatraceConfiguration.objects.create( | ||
base_url=base_url, api_key=api_key, environment=environment_v2_versioning | ||
) | ||
|
||
responses.add( | ||
method=responses.POST, | ||
url=f"{base_url}{EVENTS_API_URI}?api-token={api_key}", | ||
status=201, | ||
json={ | ||
"reportCount": 1, | ||
"eventIngestResults": [{"correlationId": "foobar123456", "status": "OK"}], | ||
}, | ||
) | ||
|
||
# When | ||
send_audit_log_event_to_dynatrace(AuditLog, audit_log_record) | ||
|
||
# Then | ||
assert len(responses.calls) == 1 | ||
assert json.loads(responses.calls[0].request.body) == { | ||
"title": "Flagsmith flag change.", | ||
"eventType": "CUSTOM_DEPLOYMENT", | ||
"properties": { | ||
"event": f"{audit_log_record.log} by user {admin_user.email}", | ||
"environment": environment_v2_versioning.name, | ||
"dt.event.deployment.name": f"Flagsmith Deployment - Flag Changed: {feature.name}", | ||
}, | ||
"entitySelector": "", | ||
} | ||
|
||
|
||
def _create_and_publish_environment_feature_version( | ||
environment: Environment, | ||
feature: Feature, | ||
user: FFAdminUser, | ||
) -> (EnvironmentFeatureVersion, AuditLog): | ||
version = EnvironmentFeatureVersion( | ||
environment=environment, | ||
feature=feature, | ||
) | ||
version.publish(user) | ||
|
||
audit_log_record = ( | ||
AuditLog.objects.filter( | ||
related_object_uuid=version.uuid, | ||
related_object_type=RelatedObjectType.EF_VERSION.name, | ||
) | ||
.order_by("-created_date") | ||
.first() | ||
) | ||
return version, audit_log_record |
This file contains 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.
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.
This is essentially the only thing that needed to change to fix the issue. The rest of the changes are related to the tests, improving the integration for EF versions, or refactoring.