-
Notifications
You must be signed in to change notification settings - Fork 2
chore: revamp OTEL audit log #454
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php namespace App\Audit; | ||
| /** | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| class AuditContext | ||
| { | ||
| public function __construct( | ||
| public ?int $userId = null, | ||
| public ?string $userEmail = null, | ||
| public ?string $userFirstName = null, | ||
| public ?string $userLastName = null, | ||
| public ?string $uiApp = null, | ||
| public ?string $uiFlow = null, | ||
| public ?string $route = null, | ||
| public ?string $httpMethod = null, | ||
| public ?string $clientIp = null, | ||
| public ?string $userAgent = null, | ||
| ) {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| <?php | ||
|
|
||
| namespace App\Audit; | ||
|
|
||
| <?php namespace App\Audit; | ||
| /** | ||
| * Copyright 2022 OpenStack Foundation | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
|
|
@@ -15,7 +12,7 @@ | |
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use App\Audit\AuditLogOtlpStrategy; | ||
| use App\Audit\Interfaces\IAuditStrategy; | ||
| use Doctrine\ORM\Event\OnFlushEventArgs; | ||
| use Illuminate\Support\Facades\App; | ||
| use Illuminate\Support\Facades\Log; | ||
|
|
@@ -29,30 +26,34 @@ class AuditEventListener | |
|
|
||
| public function onFlush(OnFlushEventArgs $eventArgs): void | ||
| { | ||
| if (app()->environment('testing')){ | ||
| return; | ||
| } | ||
| $em = $eventArgs->getObjectManager(); | ||
| $uow = $em->getUnitOfWork(); | ||
| // Strategy selection based on environment configuration | ||
| $strategy = $this->getAuditStrategy($em); | ||
|
|
||
| if (!$strategy) { | ||
| return; // No audit strategy enabled | ||
| } | ||
|
|
||
| $ctx = $this->buildAuditContext(); | ||
|
|
||
| try { | ||
| foreach ($uow->getScheduledEntityInsertions() as $entity) { | ||
| $strategy->audit($entity, [], AuditLogOtlpStrategy::EVENT_ENTITY_CREATION); | ||
| $strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_CREATION, $ctx); | ||
| } | ||
|
|
||
| foreach ($uow->getScheduledEntityUpdates() as $entity) { | ||
| $strategy->audit($entity, $uow->getEntityChangeSet($entity), AuditLogOtlpStrategy::EVENT_ENTITY_UPDATE); | ||
| $strategy->audit($entity, $uow->getEntityChangeSet($entity), IAuditStrategy::EVENT_ENTITY_UPDATE, $ctx); | ||
| } | ||
|
|
||
| foreach ($uow->getScheduledEntityDeletions() as $entity) { | ||
| $strategy->audit($entity, [], AuditLogOtlpStrategy::EVENT_ENTITY_DELETION); | ||
| $strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_DELETION, $ctx); | ||
| } | ||
|
|
||
| foreach ($uow->getScheduledCollectionUpdates() as $col) { | ||
| $strategy->audit($col, [], AuditLogOtlpStrategy::EVENT_COLLECTION_UPDATE); | ||
| $strategy->audit($col, [], IAuditStrategy::EVENT_COLLECTION_UPDATE, $ctx); | ||
| } | ||
| } catch (\Exception $e) { | ||
| Log::error('Audit event listener failed', [ | ||
|
|
@@ -78,9 +79,36 @@ private function getAuditStrategy($em) | |
| ]); | ||
| } | ||
| } | ||
|
|
||
| // Use database strategy (either as default or fallback) | ||
| return new AuditLogStrategy($em); | ||
| } | ||
|
|
||
| private function buildAuditContext(): AuditContext | ||
| { | ||
| $resourceCtx = app(\models\oauth2\IResourceServerContext::class); | ||
| $userExternalId = $resourceCtx->getCurrentUserId(); | ||
| $member = null; | ||
| if ($userExternalId) { | ||
| $memberRepo = app(\models\main\IMemberRepository::class); | ||
| $member = $memberRepo->findOneBy(["user_external_id" => $userExternalId]); | ||
| } | ||
|
|
||
| //$ui = app()->bound('ui.context') ? app('ui.context') : []; | ||
|
|
||
| $req = request(); | ||
|
|
||
| return new AuditContext( | ||
| userId: $member?->getId(), | ||
| userEmail: $member?->getEmail(), | ||
| userFirstName: $member?->getFirstName(), | ||
| userLastName: $member?->getLastName(), | ||
| uiApp: $ui['app'] ?? null, | ||
| uiFlow: $ui['flow'] ?? null, | ||
|
Comment on lines
+106
to
+107
|
||
| route: $req?->path(), | ||
| httpMethod: $req?->method(), | ||
| clientIp: $req?->ip(), | ||
| userAgent: $req?->userAgent(), | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||
| <?php namespace App\Audit; | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Copyright 2025 OpenStack Foundation | ||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||
| **/ | ||||||||||||||||||
| use App\Audit\ConcreteFormatters\ChildEntityFormatters\ChildEntityFormatterFactory; | ||||||||||||||||||
| use App\Audit\ConcreteFormatters\EntityCollectionUpdateAuditLogFormatter; | ||||||||||||||||||
| use App\Audit\ConcreteFormatters\EntityCreationAuditLogFormatter; | ||||||||||||||||||
| use App\Audit\ConcreteFormatters\EntityDeletionAuditLogFormatter; | ||||||||||||||||||
| use App\Audit\ConcreteFormatters\EntityUpdateAuditLogFormatter; | ||||||||||||||||||
| use App\Audit\Interfaces\IAuditStrategy; | ||||||||||||||||||
|
|
||||||||||||||||||
| class AuditLogFormatterFactory implements IAuditLogFormatterFactory | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
||||||||||||||||||
| private array $config; | ||||||||||||||||||
|
|
||||||||||||||||||
| public function __construct() | ||||||||||||||||||
| { | ||||||||||||||||||
| // cache the config so we don't hit config() repeatedly | ||||||||||||||||||
| $this->config = config('audit_log', []); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function getStrategyClass(object $subject, string $event_type): ?IAuditLogFormatter | ||||||||||||||||||
| { | ||||||||||||||||||
| $class = get_class($subject); | ||||||||||||||||||
| $cls = $this->config['entities'][$class]['strategy'] ?? null; | ||||||||||||||||||
| return !is_null($cls) ? new $cls($event_type):null; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function make($subject, $eventType): ?IAuditLogFormatter | ||||||||||||||||||
| { | ||||||||||||||||||
| $formatter = null; | ||||||||||||||||||
| switch ($eventType) { | ||||||||||||||||||
| case IAuditStrategy::EVENT_COLLECTION_UPDATE: | ||||||||||||||||||
| $child_entity = null; | ||||||||||||||||||
| if (count($subject) > 0) { | ||||||||||||||||||
| $child_entity = $subject[0]; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (is_null($child_entity) && count($subject->getSnapshot()) > 0) { | ||||||||||||||||||
|
Comment on lines
+45
to
+48
|
||||||||||||||||||
| if (count($subject) > 0) { | |
| $child_entity = $subject[0]; | |
| } | |
| if (is_null($child_entity) && count($subject->getSnapshot()) > 0) { | |
| if (is_countable($subject) && count($subject) > 0) { | |
| $child_entity = $subject[0]; | |
| } | |
| if (is_null($child_entity) && is_countable($subject->getSnapshot()) && count($subject->getSnapshot()) > 0) { |
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.
[nitpick] The copyright year was updated from 2022 to 2025, but the original 2022 copyright should be preserved. Copyright notices typically use a range format like "Copyright 2022-2025" to reflect the original creation date and subsequent modifications.