-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/audit strategy log #356
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
7 commits
Select commit
Hold shift + click to select a range
c94fc68
chore: add to table SummitAttendeeTicket fild summit id to improve pe…
smarcet 8762fef
Merge branch 'main' of github.com:OpenStackweb/summit-api
andrestejerina97 7ad4b4c
feat: add log audit strategy for replace audit in BBDD
andrestejerina97 5b430dd
feat: Add unit test for Audit Otlp Strategy
andrestejerina97 de4d063
fix: add null checks to EventListener, fix in getAuditStrategy
andrestejerina97 9c4bf52
fix: change env function by config function
andrestejerina97 2072284
fix: add improvements in the use of constants, remove code unnecessary
andrestejerina97 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
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,200 @@ | ||
| <?php | ||
|
|
||
| namespace App\Audit; | ||
|
|
||
| use App\Audit\Interfaces\IAuditStrategy; | ||
| use Doctrine\ORM\PersistentCollection; | ||
| use Illuminate\Support\Facades\App; | ||
| use models\summit\PresentationAction; | ||
| use models\summit\PresentationExtraQuestionAnswer; | ||
| use models\summit\SummitAttendeeBadgePrint; | ||
| use models\summit\SummitEvent; | ||
| 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 Keepsuit\LaravelOpenTelemetry\Facades\Logger; | ||
|
|
||
| /** | ||
| * OpenTelemetry Logs Audit Strategy | ||
| */ | ||
| class AuditLogOtlpStrategy implements IAuditStrategy | ||
| { | ||
| public const EVENT_COLLECTION_UPDATE = 'event_collection_update'; | ||
| public const EVENT_ENTITY_CREATION = 'event_entity_creation'; | ||
| public const EVENT_ENTITY_DELETION = 'event_entity_deletion'; | ||
| public const EVENT_ENTITY_UPDATE = 'event_entity_update'; | ||
|
|
||
| public const ACTION_CREATE = 'create'; | ||
| public const ACTION_UPDATE = 'update'; | ||
| public const ACTION_DELETE = 'delete'; | ||
| public const ACTION_COLLECTION_UPDATE = 'collection_update'; | ||
| public const ACTION_UNKNOWN = 'unknown'; | ||
|
|
||
| public const LOG_MESSAGE_CREATED = 'audit.entity.created'; | ||
| public const LOG_MESSAGE_UPDATED = 'audit.entity.updated'; | ||
| public const LOG_MESSAGE_DELETED = 'audit.entity.deleted'; | ||
| public const LOG_MESSAGE_COLLECTION_UPDATED = 'audit.collection.updated'; | ||
| public const LOG_MESSAGE_CHANGED = 'audit.entity.changed'; | ||
|
|
||
| private bool $enabled; | ||
| private string $elasticIndex; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->enabled = config('opentelemetry.enabled', false); | ||
| $this->elasticIndex = config('opentelemetry.logs.elasticsearch_index', 'logs-audit'); | ||
| } | ||
|
|
||
| public function audit($subject, array $change_set, string $event_type): void | ||
| { | ||
| if (!$this->enabled) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $entity = $this->resolveAuditableEntity($subject); | ||
| if (is_null($entity)) { | ||
| return; | ||
| } | ||
|
|
||
| $resource_server_ctx = App::make(\models\oauth2\IResourceServerContext::class); | ||
| $user_id = $resource_server_ctx->getCurrentUserId(); | ||
| $user_email = $resource_server_ctx->getCurrentUserEmail(); | ||
|
|
||
| $formatter = null; | ||
| switch ($event_type) { | ||
| case self::EVENT_COLLECTION_UPDATE: | ||
| $child_entity = null; | ||
| if (count($subject) > 0) { | ||
| $child_entity = $subject[0]; | ||
| } | ||
| if (is_null($child_entity) && count($subject->getSnapshot()) > 0) { | ||
| $child_entity = $subject->getSnapshot()[0]; | ||
| } | ||
| $child_entity_formatter = $child_entity != null ? ChildEntityFormatterFactory::build($child_entity) : null; | ||
| $formatter = new EntityCollectionUpdateAuditLogFormatter($child_entity_formatter); | ||
| break; | ||
| case self::EVENT_ENTITY_CREATION: | ||
| $formatter = new EntityCreationAuditLogFormatter(); | ||
| break; | ||
| case self::EVENT_ENTITY_DELETION: | ||
| $child_entity_formatter = ChildEntityFormatterFactory::build($subject); | ||
| $formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter); | ||
| break; | ||
| case self::EVENT_ENTITY_UPDATE: | ||
| $child_entity_formatter = ChildEntityFormatterFactory::build($subject); | ||
| $formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter); | ||
| break; | ||
| } | ||
|
|
||
| $description = null; | ||
| if ($formatter) { | ||
| $description = $formatter->format($subject, $change_set); | ||
| } | ||
|
|
||
| $auditData = $this->buildAuditLogData($entity, $subject, $change_set, $event_type, $user_id, $user_email); | ||
| if (!empty($description)) { | ||
| $auditData['audit.description'] = $description; | ||
| } | ||
| Logger::info($this->getLogMessage($event_type), $auditData); | ||
|
|
||
| } catch (\Exception $ex) { | ||
| Logger::warning('OTEL audit logging error: ' . $ex->getMessage(), [ | ||
| 'exception' => $ex, | ||
| 'subject_class' => get_class($subject), | ||
| 'event_type' => $event_type, | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| private function resolveAuditableEntity($subject) | ||
| { | ||
| if ($subject instanceof SummitEvent) return $subject; | ||
|
|
||
| if ($subject instanceof PersistentCollection && $subject->getOwner() instanceof SummitEvent) { | ||
| return $subject->getOwner(); | ||
| } | ||
|
|
||
| if ($subject instanceof PresentationAction || $subject instanceof PresentationExtraQuestionAnswer) { | ||
| return $subject->getPresentation(); | ||
| } | ||
|
|
||
| if ($subject instanceof SummitAttendeeBadgePrint) { | ||
| return $subject->getBadge(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function buildAuditLogData($entity, $subject, array $change_set, string $event_type, ?string $user_id, ?string $user_email): array | ||
| { | ||
| $auditData = [ | ||
| 'audit.action' => $this->mapEventTypeToAction($event_type), | ||
| 'audit.entity' => class_basename($entity), | ||
| 'audit.entity_id' => (string) (method_exists($entity, 'getId') ? $entity->getId() : 'unknown'), | ||
| 'audit.entity_class' => get_class($entity), | ||
| 'audit.timestamp' => now()->toISOString(), | ||
| 'audit.event_type' => $event_type, | ||
| 'auth.user.id' => $user_id, | ||
| 'auth.user.email' => $user_email, | ||
| 'elasticsearch.index' => $this->elasticIndex, | ||
| ]; | ||
|
|
||
| switch ($event_type) { | ||
| case self::EVENT_COLLECTION_UPDATE: | ||
| if ($subject instanceof PersistentCollection) { | ||
| $auditData['audit.collection_type'] = $this->getCollectionType($subject); | ||
| $auditData['audit.collection_count'] = count($subject); | ||
| $auditData['audit.collection_changes'] = $this->getCollectionChanges($subject, $change_set); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| return $auditData; | ||
| } | ||
|
|
||
| private function getCollectionType(PersistentCollection $collection): string | ||
| { | ||
| if (empty($collection) && empty($collection->getSnapshot())) { | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| $item = !empty($collection) ? $collection->first() : $collection->getSnapshot()[0]; | ||
| return class_basename($item); | ||
| } | ||
|
|
||
| private function getCollectionChanges(PersistentCollection $collection, array $change_set): array | ||
| { | ||
| return [ | ||
| 'current_count' => count($collection), | ||
| 'snapshot_count' => count($collection->getSnapshot()), | ||
| 'is_dirty' => $collection->isDirty(), | ||
| ]; | ||
| } | ||
|
|
||
| private function mapEventTypeToAction(string $event_type): string | ||
| { | ||
| return match($event_type) { | ||
| self::EVENT_ENTITY_CREATION => self::ACTION_CREATE, | ||
| self::EVENT_ENTITY_UPDATE => self::ACTION_UPDATE, | ||
| self::EVENT_ENTITY_DELETION => self::ACTION_DELETE, | ||
| self::EVENT_COLLECTION_UPDATE => self::ACTION_COLLECTION_UPDATE, | ||
| default => self::ACTION_UNKNOWN | ||
| }; | ||
| } | ||
|
|
||
| private function getLogMessage(string $event_type): string | ||
| { | ||
| return match($event_type) { | ||
| self::EVENT_ENTITY_CREATION => self::LOG_MESSAGE_CREATED, | ||
| self::EVENT_ENTITY_UPDATE => self::LOG_MESSAGE_UPDATED, | ||
| self::EVENT_ENTITY_DELETION => self::LOG_MESSAGE_DELETED, | ||
| self::EVENT_COLLECTION_UPDATE => self::LOG_MESSAGE_COLLECTION_UPDATED, | ||
| default => self::LOG_MESSAGE_CHANGED | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| } | ||
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,21 @@ | ||
| <?php | ||
|
|
||
| namespace App\Audit\Interfaces; | ||
|
|
||
| /** | ||
| * Audit Strategy Interface | ||
| * Defines contract for different audit implementations (DB, OTLP, etc.) | ||
| */ | ||
| interface IAuditStrategy | ||
| { | ||
| /** | ||
| * Audit an entity change | ||
| * | ||
| * @param mixed $subject The entity or collection being audited | ||
| * @param array $change_set Array of changes (field => [old, new]) | ||
| * @param string $event_type Type of audit event (create, update, delete, collection_update) | ||
| * @return void | ||
| */ | ||
| public function audit($subject, array $change_set, string $event_type): void; | ||
|
|
||
| } |
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
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.